Whitelist and blacklist chordTypes

Allow and disallow chords by the type of chords.
This commit is contained in:
William Carroll 2020-04-12 19:32:54 +01:00
parent 1298263629
commit bf460fe5ac
2 changed files with 112 additions and 105 deletions

View file

@ -15,6 +15,8 @@ import Time exposing (..)
type alias Model = type alias Model =
{ whitelistedChords : List Theory.Chord { whitelistedChords : List Theory.Chord
, whitelistedChordTypes : List Theory.ChordType
, whitelistedInversions : List Theory.ChordInversion
, selectedChord : Theory.Chord , selectedChord : Theory.Chord
, isPaused : Bool , isPaused : Bool
, tempo : Int , tempo : Int
@ -24,7 +26,6 @@ type alias Model =
{ enable : Bool { enable : Bool
, inspectChord : Bool , inspectChord : Bool
} }
, whitelistedInversions : List Theory.ChordInversion
} }
@ -38,6 +39,7 @@ type Msg
| SetTempo String | SetTempo String
| ToggleInspectChord | ToggleInspectChord
| ToggleInversion Theory.ChordInversion | ToggleInversion Theory.ChordInversion
| ToggleChordType Theory.ChordType
tempoStep : Int tempoStep : Int
@ -73,7 +75,14 @@ init =
( firstNote, lastNote ) = ( firstNote, lastNote ) =
( Theory.C3, Theory.C5 ) ( Theory.C3, Theory.C5 )
in in
{ whitelistedChords = Theory.allChords firstNote lastNote Theory.allInversions { whitelistedChords =
Theory.allChords
{ start = firstNote
, end = lastNote
, inversions = Theory.allInversions
, chordTypes = Theory.allChordTypes
}
, whitelistedChordTypes = Theory.allChordTypes
, whitelistedInversions = Theory.allInversions , whitelistedInversions = Theory.allInversions
, selectedChord = cmajor , selectedChord = cmajor
, isPaused = True , isPaused = True
@ -150,6 +159,28 @@ update msg model =
, Cmd.none , Cmd.none
) )
ToggleChordType chordType ->
let
chordTypes =
if List.member chordType model.whitelistedChordTypes then
List.filter ((/=) chordType) model.whitelistedChordTypes
else
chordType :: model.whitelistedChordTypes
in
( { model
| whitelistedChordTypes = chordTypes
, whitelistedChords =
Theory.allChords
{ start = model.firstNote
, end = model.lastNote
, inversions = model.whitelistedInversions
, chordTypes = chordTypes
}
}
, Cmd.none
)
ToggleInversion inversion -> ToggleInversion inversion ->
let let
inversions = inversions =
@ -161,7 +192,13 @@ update msg model =
in in
( { model ( { model
| whitelistedInversions = inversions | whitelistedInversions = inversions
, whitelistedChords = Theory.allChords model.firstNote model.lastNote inversions , whitelistedChords =
Theory.allChords
{ start = model.firstNote
, end = model.lastNote
, inversions = inversions
, chordTypes = model.whitelistedChordTypes
}
} }
, Cmd.none , Cmd.none
) )
@ -197,6 +234,25 @@ debugger =
] ]
chordTypeCheckboxes : List Theory.ChordType -> Html Msg
chordTypeCheckboxes chordTypes =
ul []
(Theory.allChordTypes
|> List.map
(\chordType ->
li []
[ label [] [ text (Theory.chordTypeName chordType) ]
, input
[ type_ "checkbox"
, onClick (ToggleChordType chordType)
, checked (List.member chordType chordTypes)
]
[]
]
)
)
inversionCheckboxes : List Theory.ChordInversion -> Html Msg inversionCheckboxes : List Theory.ChordInversion -> Html Msg
inversionCheckboxes inversions = inversionCheckboxes inversions =
ul [] ul []
@ -238,6 +294,7 @@ view model =
, handleInput = SetTempo , handleInput = SetTempo
} }
, inversionCheckboxes model.whitelistedInversions , inversionCheckboxes model.whitelistedInversions
, chordTypeCheckboxes model.whitelistedChordTypes
, playPause model , playPause model
, if model.debug.enable then , if model.debug.enable then
debugger debugger

View file

@ -284,6 +284,45 @@ inversionName inversion =
"Second" "Second"
{-| Return the human-readable version of a chord type.
-}
chordTypeName : ChordType -> String
chordTypeName chordType =
case chordType of
Major ->
"major"
Major7 ->
"major 7th"
MajorDominant7 ->
"major dominant 7th"
Minor ->
"minor"
MinorMajor7 ->
"minor major 7th"
MinorDominant7 ->
"minor dominant 7th"
Augmented ->
"augmented"
AugmentedDominant7 ->
"augmented dominant 7th"
Diminished ->
"diminished"
DiminishedDominant7 ->
"diminished dominant 7th"
DiminishedMajor7 ->
"diminished major 7th"
{-| 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
@ -794,14 +833,17 @@ allChordTypes =
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 ChordInversion -> List Chord allChords :
allChords start end chordInversions = { start : Note
, end : Note
, inversions : List ChordInversion
, chordTypes : List ChordType
}
-> List Chord
allChords { start, end, inversions, chordTypes } =
let let
notes = notes =
notesFromRange start end notesFromRange start end
chordTypes =
allChordTypes
in in
notes notes
|> List.Extra.andThen |> List.Extra.andThen
@ -809,12 +851,12 @@ allChords start end chordInversions =
chordTypes chordTypes
|> List.Extra.andThen |> List.Extra.andThen
(\chordType -> (\chordType ->
chordInversions inversions
|> List.Extra.andThen |> List.Extra.andThen
(\chordInversion -> (\inversion ->
[ { note = note [ { note = note
, chordType = chordType , chordType = chordType
, chordInversion = chordInversion , chordInversion = inversion
} }
] ]
) )
@ -1085,104 +1127,12 @@ viewNote note =
inspectChord : Chord -> String inspectChord : Chord -> String
inspectChord { note, chordType, chordInversion } = inspectChord { note, chordType, chordInversion } =
viewNote note viewNote note ++ " " ++ chordTypeName chordType ++ " " ++ inversionName chordInversion ++ " position"
++ " "
++ (case chordType of
Major ->
"major"
Major7 ->
"major 7th"
MajorDominant7 ->
"major dominant 7th"
Minor ->
"minor"
MinorMajor7 ->
"minor major 7th"
MinorDominant7 ->
"minor dominant 7th"
Augmented ->
"augmented"
AugmentedDominant7 ->
"augmented dominant 7th"
Diminished ->
"diminished"
DiminishedDominant7 ->
"diminished dominant 7th"
DiminishedMajor7 ->
"diminished major 7th"
)
++ " "
++ (case chordInversion of
Root ->
"root position"
First ->
"1st inversion"
Second ->
"2nd inversion"
)
viewChord : Chord -> String viewChord : Chord -> String
viewChord { note, chordType, chordInversion } = viewChord { note, chordType, chordInversion } =
viewNoteClass (classifyNote note) viewNoteClass (classifyNote note) ++ " " ++ chordTypeName chordType ++ " " ++ inversionName chordInversion ++ " position"
++ " "
++ (case chordType of
Major ->
"major"
Major7 ->
"major 7th"
MajorDominant7 ->
"major dominant 7th"
Minor ->
"minor"
MinorMajor7 ->
"minor 7th"
MinorDominant7 ->
"minor dominant 7th"
Augmented ->
"augmented"
AugmentedDominant7 ->
"augmented 7th"
Diminished ->
"diminished"
DiminishedDominant7 ->
"diminished 7th"
DiminishedMajor7 ->
"diminished major 7th"
)
++ " "
++ (case chordInversion of
Root ->
"root position"
First ->
"1st inversion"
Second ->
"2nd inversion"
)
{-| Serialize a human-readable format of `noteClass`. {-| Serialize a human-readable format of `noteClass`.