about summary refs log tree commit diff
path: root/website/sandbox/chord-drill-sergeant/src/Main.elm
blob: 8a606767bb69a83bc9e144163304bd4943c8b1ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
module Main exposing (main)

import Browser
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Random
import Random.List
import Time exposing (..)

import Piano
import Theory
import Tempo

type alias Model =
  { whitelistedChords : List Theory.Chord
  , selectedChord : Theory.Chord
  , isPaused : Bool
  , tempo : Int
  }

type Msg = NextChord
         | NewChord Theory.Chord
         | Play
         | Pause
         | IncreaseTempo
         | DecreaseTempo
         | SetTempo String

tempoStep : Int
tempoStep = 5

{-| Return the number of milliseconds that elapse during an interval in a
`target` bpm.
-}
bpmToMilliseconds : Int -> Int
bpmToMilliseconds target =
  let msPerMinute = 1000 * 60
  in round (toFloat msPerMinute / toFloat target)

viewChord : Theory.Chord -> String
viewChord {note, chordType, chordPosition} =
  viewNote note ++ " " ++
  (case chordType of
     Theory.Major -> "major"
     Theory.Major7 -> "major 7th"
     Theory.MajorDominant7 -> "major dominant 7th"
     Theory.Minor -> "minor"
     Theory.Minor7 -> "minor 7th"
     Theory.MinorDominant7 -> "minor dominant 7th"
     Theory.Augmented -> "augmented"
     Theory.Augmented7 -> "augmented 7th"
     Theory.Diminished -> "diminished"
     Theory.Diminished7 -> "diminished 7th") ++ " " ++
  (case chordPosition of
     Theory.First -> "root position"
     Theory.Second -> "2nd position"
     Theory.Third -> "3rd position"
     Theory.Fourth -> "4th position")

{-| Serialize a human-readable format of `note` -}
viewNote : Theory.Note -> String
viewNote note =
  case note of
    Theory.C -> "C"
    Theory.C_sharp -> "C♯/D♭"
    Theory.D -> "D"
    Theory.D_sharp -> "D♯/E♭"
    Theory.E -> "E"
    Theory.F -> "F"
    Theory.F_sharp -> "F♯/G♭"
    Theory.G -> "G"
    Theory.G_sharp -> "G♯/A♭"
    Theory.A -> "A"
    Theory.A_sharp -> "A♯/B♭"
    Theory.B -> "B"

cmajor : Theory.Chord
cmajor =
  { note = Theory.C
  , chordType = Theory.Major
  , chordPosition = Theory.First
  }

{-| The initial state for the application. -}
init : Model
init =
  { whitelistedChords = Theory.allChords
  , selectedChord = cmajor
  , isPaused = True
  , tempo = 60
  }

subscriptions : Model -> Sub Msg
subscriptions {isPaused, tempo} =
  if isPaused then
    Sub.none
  else
    Time.every (tempo |> bpmToMilliseconds |> toFloat) (\_ -> NextChord)

{-| Now that we have state, we need a function to change the state. -}
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    NewChord chord -> ( { model | selectedChord = chord }
                      , Cmd.none
                      )
    NextChord -> ( model
                 , Random.generate (\x ->
                                      case x of
                                        (Just chord, _) -> NewChord chord
                                        (Nothing, _)    -> NewChord cmajor)
                   (Random.List.choose model.whitelistedChords)
                 )
    Play -> ( { model | isPaused = False }
            , Cmd.none
            )
    Pause -> ( { model | isPaused = True }
             , Cmd.none
             )
    IncreaseTempo -> ( { model | tempo = model.tempo + tempoStep }
                     , Cmd.none
                     )
    DecreaseTempo -> ( { model | tempo = model.tempo - tempoStep }
                     , Cmd.none
                     )
    SetTempo tempo -> ( { model |
                          tempo = case String.toInt tempo of
                                    Just x  -> x
                                    Nothing -> model.tempo
                         }
                      , Cmd.none
                      )

playPause : Model -> Html Msg
playPause {isPaused} =
  if isPaused then
    button [ onClick Play ] [ text "Play" ]
  else
    button [ onClick Pause ] [ text "Pause" ]

view : Model -> Html Msg
view model =
  div [] [ Tempo.render { tempo = model.tempo
                        , handleIncrease = IncreaseTempo
                        , handleDecrease = DecreaseTempo
                        , handleInput    = SetTempo
                        }
         , playPause model
         , p [] [ text (viewChord model.selectedChord) ]
         , Piano.render { highlight = Theory.notesForChord model.selectedChord }
         ]

{-| For now, I'm just dumping things onto the page to sketch ideas. -}
main =
  Browser.element { init = \() -> (init, Cmd.none)
                  , subscriptions = subscriptions
                  , update = update
                  , view = view
                  }