Whitelist and blacklist chords by inversion type

Add checkboxes to support various chord positions.
This commit is contained in:
William Carroll 2020-04-12 19:20:00 +01:00
parent 24692ab465
commit 1298263629
2 changed files with 84 additions and 10 deletions

View file

@ -24,6 +24,7 @@ type alias Model =
{ enable : Bool { enable : Bool
, inspectChord : Bool , inspectChord : Bool
} }
, whitelistedInversions : List Theory.ChordInversion
} }
@ -36,6 +37,7 @@ type Msg
| DecreaseTempo | DecreaseTempo
| SetTempo String | SetTempo String
| ToggleInspectChord | ToggleInspectChord
| ToggleInversion Theory.ChordInversion
tempoStep : Int tempoStep : Int
@ -71,7 +73,8 @@ init =
( firstNote, lastNote ) = ( firstNote, lastNote ) =
( Theory.C3, Theory.C5 ) ( Theory.C3, Theory.C5 )
in in
{ whitelistedChords = Theory.allChords firstNote lastNote { whitelistedChords = Theory.allChords firstNote lastNote Theory.allInversions
, whitelistedInversions = Theory.allInversions
, selectedChord = cmajor , selectedChord = cmajor
, isPaused = True , isPaused = True
, tempo = 60 , tempo = 60
@ -147,6 +150,22 @@ update msg model =
, Cmd.none , Cmd.none
) )
ToggleInversion inversion ->
let
inversions =
if List.member inversion model.whitelistedInversions then
List.filter ((/=) inversion) model.whitelistedInversions
else
inversion :: model.whitelistedInversions
in
( { model
| whitelistedInversions = inversions
, whitelistedChords = Theory.allChords model.firstNote model.lastNote inversions
}
, Cmd.none
)
SetTempo tempo -> SetTempo tempo ->
( { model ( { model
| tempo = | tempo =
@ -178,6 +197,26 @@ debugger =
] ]
inversionCheckboxes : List Theory.ChordInversion -> Html Msg
inversionCheckboxes inversions =
ul []
(Theory.allInversions
|> List.map
(\inversion ->
li []
[ label [] [ text (Theory.inversionName inversion) ]
, input
[ type_
"checkbox"
, onClick (ToggleInversion inversion)
, checked (List.member inversion inversions)
]
[]
]
)
)
view : Model -> Html Msg view : Model -> Html Msg
view model = view model =
case Theory.notesForChord model.selectedChord of case Theory.notesForChord model.selectedChord of
@ -198,6 +237,7 @@ view model =
, handleDecrease = DecreaseTempo , handleDecrease = DecreaseTempo
, handleInput = SetTempo , handleInput = SetTempo
} }
, inversionCheckboxes model.whitelistedInversions
, playPause model , playPause model
, if model.debug.enable then , if model.debug.enable then
debugger debugger

View file

@ -269,6 +269,21 @@ noteInCentralOctave noteClass =
B4 B4
{-| Return the human-readable version of a chord inversion.
-}
inversionName : ChordInversion -> String
inversionName inversion =
case inversion of
Root ->
"Root"
First ->
"First"
Second ->
"Second"
{-| Return the note that is one half step away from `note` in the direction, {-| Return the note that is one half step away from `note` in the direction,
`dir`. `dir`.
In the case of stepping up or down from the end of the piano, this returns a In the case of stepping up or down from the end of the piano, this returns a
@ -750,24 +765,43 @@ notesFromRange start end =
|> List.Extra.takeWhile ((/=) end) |> List.Extra.takeWhile ((/=) end)
{-| Return a list of all of the chord inversions about which we know.
-}
allInversions : List ChordInversion
allInversions =
[ Root, First, Second ]
{-| Return a list of all of the chord types about which we know.
-}
allChordTypes : List ChordType
allChordTypes =
[ Major
, Major7
, MajorDominant7
, Minor
, MinorMajor7
, MinorDominant7
, Augmented
, AugmentedDominant7
, Diminished
, DiminishedDominant7
, DiminishedMajor7
]
{-| Return a list of all of the chords that we know about. {-| Return a list of all of the chords that we know about.
Only create chords from the range of notes delimited by the range `start` and Only create chords from the range of notes delimited by the range `start` and
`end`. `end`.
-} -}
allChords : Note -> Note -> List Chord allChords : Note -> Note -> List ChordInversion -> List Chord
allChords start end = allChords start end chordInversions =
let let
notes = notes =
notesFromRange start end notesFromRange start end
chordTypes = chordTypes =
[ Major allChordTypes
]
chordInversions =
[ Root
, First
]
in in
notes notes
|> List.Extra.andThen |> List.Extra.andThen