about summary refs log tree commit diff
path: root/website/sandbox/chord-drill-sergeant/src/Theory.elm
blob: 33137d9ffba5d673306b6590333f2765e2129c18 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
module Theory exposing (..)

import List.Extra
import Maybe.Extra
import Misc


{-| Notes are the individuals sounds that we use to create music. Think: "do re
mi fa so la ti do".

Note: Technically a "C-sharp" is also a "D-flat", but I will model accidentals
(i.e. sharps and flats) as sharps and represent the ambiguity when I render the
underlying state of the application.

Note: There are "notes" like A, B, D-flat, and then there are notes like "middle
C", also denoted in scientific pitch notation as C4. I'm unsure of what to call
each of these, and my application does not model scientific pitch notation yet,
so these non-scientific pitch denote values are "notes" for now.

-}
type Note
    = C1
    | C_sharp1
    | D1
    | D_sharp1
    | E1
    | F1
    | F_sharp1
    | G1
    | G_sharp1
    | A1
    | A_sharp1
    | B1
    | C2
    | C_sharp2
    | D2
    | D_sharp2
    | E2
    | F2
    | F_sharp2
    | G2
    | G_sharp2
    | A2
    | A_sharp2
    | B2
    | C3
    | C_sharp3
    | D3
    | D_sharp3
    | E3
    | F3
    | F_sharp3
    | G3
    | G_sharp3
    | A3
    | A_sharp3
    | B3
    | C4
    | C_sharp4
    | D4
    | D_sharp4
    | E4
    | F4
    | F_sharp4
    | G4
    | G_sharp4
    | A4
    | A_sharp4
    | B4
    | C5
    | C_sharp5
    | D5
    | D_sharp5
    | E5
    | F5
    | F_sharp5
    | G5
    | G_sharp5
    | A5
    | A_sharp5
    | B5
    | C6
    | C_sharp6
    | D6
    | D_sharp6
    | E6
    | F6
    | F_sharp6
    | G6
    | G_sharp6
    | A6
    | A_sharp6
    | B6
    | C7
    | C_sharp7
    | D7
    | D_sharp7
    | E7
    | F7
    | F_sharp7
    | G7
    | G_sharp7
    | A7
    | A_sharp7
    | B7
    | C8


{-| I alluded to this concept in the Note type's documentation. These are the
letters of notes. For instance C2, C3, C4 are all instances of C.
-}
type NoteClass
    = C
    | C_sharp
    | D
    | D_sharp
    | E
    | F
    | F_sharp
    | G
    | G_sharp
    | A
    | A_sharp
    | B


{-| Encode whether you are traversing "up" or "down" intervals
-}
type StepDirection
    = Up
    | Down


{-| One can measure the difference between between notes using intervals.
-}
type Interval
    = Half
    | NHalves Int
    | Whole
    | MajorThird
    | MinorThird
    | PerfectFifth
    | AugmentedFifth
    | DiminishedFifth
    | MajorSeventh
    | DominantSeventh


{-| Add direction to a distance on the piano.
-}
type alias IntervalVector =
    { interval : Interval
    , direction : StepDirection
    }


{-| A bundle of notes which are usually, but not necessarily harmonious.
-}
type alias Chord =
    { note : Note
    , chordType : ChordType
    , chordInversion : ChordInversion
    }


{-| Many possible chords exist. This type encodes the possibilities. I am
tempted to model these in a more "DRY" way, but I worry that this abstraction
may cause more problems than it solves.
-}
type ChordType
    = Major
    | Sus2
    | Sus4
    | Major7
    | MajorDominant7
    | Minor
    | MinorMajor7
    | MinorDominant7
    | Augmented
    | AugmentedDominant7
    | Diminished
    | DiminishedDominant7
    | DiminishedMajor7


{-| On a piano, a triad can be played three ways. As a rule-of-thumb, The number
of ways a pianist can play a chord is equal to the number of notes in the chord
itself.
-}
type ChordInversion
    = Root
    | First
    | Second


{-| Whether a given note is a white key or a black key.
-}
type KeyClass
    = Natural
    | Accidental


{-| Songs are written in one or more keys, which define the notes and therefore
chords that harmonize with one another.
-}
type alias Key =
    { noteClass : NoteClass
    , mode : Mode
    }


{-| We create "scales" by enumerating the notes of a given key. These keys are
defined by the "tonic" note and the "mode". I thought about including Ionian,
Dorian, Phrygian, etc., but in the I would like to avoid over-abstracting this
early on, so I'm going to err on the side of overly concrete until I have a
better idea of the extent of this project.
-}
type Mode
    = BluesMode
    | MajorMode
    | MinorMode


scaleDegree : Int -> Key -> NoteClass
scaleDegree which { noteClass } =
    case noteClass of
        _ ->
            C


{-| Returns the Note in the cental octave of the piano for a given
NoteClass. For example, C4 -- or "middle C" -- for C.
-}
noteInCentralOctave : NoteClass -> Note
noteInCentralOctave noteClass =
    case noteClass of
        C ->
            C4

        C_sharp ->
            C_sharp4

        D ->
            D4

        D_sharp ->
            D_sharp4

        E ->
            E4

        F ->
            F4

        F_sharp ->
            F_sharp4

        G ->
            G4

        G_sharp ->
            G_sharp4

        A ->
            A4

        A_sharp ->
            A_sharp4

        B ->
            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 human-readable version of a chord type.
-}
chordTypeName : ChordType -> String
chordTypeName chordType =
    case chordType of
        Major ->
            "major"

        Sus2 ->
            "suspended 2"

        Sus4 ->
            "suspended 4"

        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,
`dir`.
In the case of stepping up or down from the end of the piano, this returns a
Maybe.
-}
halfStep : StepDirection -> Note -> Maybe Note
halfStep dir note =
    let
        everyNote =
            notesFromRange C2 C8
    in
    case dir of
        Up ->
            Misc.comesAfter note everyNote

        Down ->
            Misc.comesBefore note everyNote


{-| Return a list of steps to take away from the root note to return back to the
root note for a given mode.
-}
intervalsForMode : Mode -> List IntervalVector
intervalsForMode mode =
    let
        up x =
            { direction = Up, interval = x }

        down x =
            { direction = Down, interval = x }
    in
    case mode of
        MajorMode ->
            List.map up [ Whole, Whole, Half, Whole, Whole, Whole, Half ]

        MinorMode ->
            List.map up [ Whole, Half, Whole, Whole, Half, Whole, Whole ]

        BluesMode ->
            List.map up [ MinorThird, Whole, Half, Half, MinorThird ]


{-| Return a list of the intervals the comprise a chord. Each interval measures
the distance away from the root-note of the chord.
-}
intervalsForChordType : ChordType -> ChordInversion -> List IntervalVector
intervalsForChordType chordType chordInversion =
    let
        up x =
            { direction = Up, interval = x }

        down x =
            { direction = Down, interval = x }
    in
    case ( chordType, chordInversion ) of
        -- Major
        ( Major, Root ) ->
            [ up MajorThird, up PerfectFifth ]

        ( Major, First ) ->
            [ down (NHalves 5), down (NHalves 8) ]

        ( Major, Second ) ->
            [ down (NHalves 5), up MajorThird ]

        -- Sus2
        ( Sus2, Root ) ->
            [ up Whole, up PerfectFifth ]

        ( Sus2, First ) ->
            [ down (NHalves 10), down (NHalves 5) ]

        ( Sus2, Second ) ->
            [ down (NHalves 5), up Whole ]

        -- Sus4
        ( Sus4, Root ) ->
            [ up (NHalves 5), up PerfectFifth ]

        ( Sus4, First ) ->
            [ down (NHalves 7), down (NHalves 5) ]

        ( Sus4, Second ) ->
            [ down (NHalves 5), up (NHalves 5) ]

        -- Major7
        ( Major7, Root ) ->
            [ up MajorThird, up PerfectFifth, up MajorSeventh ]

        ( Major7, First ) ->
            down Half :: intervalsForChordType Major chordInversion

        ( Major7, Second ) ->
            down Half :: intervalsForChordType Major chordInversion

        -- MajorDominant7
        ( MajorDominant7, Root ) ->
            up DominantSeventh :: intervalsForChordType Major chordInversion

        ( MajorDominant7, First ) ->
            down Whole :: intervalsForChordType Major chordInversion

        ( MajorDominant7, Second ) ->
            down Whole :: intervalsForChordType Major chordInversion

        -- Minor
        ( Minor, Root ) ->
            [ up MinorThird, up PerfectFifth ]

        ( Minor, First ) ->
            [ down (NHalves 5), down (NHalves 9) ]

        ( Minor, Second ) ->
            [ down (NHalves 5), up MinorThird ]

        -- MinorMajor7
        ( MinorMajor7, Root ) ->
            up MajorSeventh :: intervalsForChordType Minor chordInversion

        ( MinorMajor7, First ) ->
            down Half :: intervalsForChordType Minor chordInversion

        ( MinorMajor7, Second ) ->
            down Half :: intervalsForChordType Minor chordInversion

        -- MinorDominant7
        ( MinorDominant7, Root ) ->
            up DominantSeventh :: intervalsForChordType Minor chordInversion

        ( MinorDominant7, First ) ->
            down Whole :: intervalsForChordType Minor chordInversion

        ( MinorDominant7, Second ) ->
            down Whole :: intervalsForChordType Minor chordInversion

        -- Augmented
        ( Augmented, Root ) ->
            [ up MajorThird, up AugmentedFifth ]

        ( Augmented, First ) ->
            [ down (NHalves 8), down (NHalves 4) ]

        ( Augmented, Second ) ->
            [ down (NHalves 4), up MajorThird ]

        -- AugmentedDominant7
        ( AugmentedDominant7, Root ) ->
            up DominantSeventh :: intervalsForChordType Augmented chordInversion

        ( AugmentedDominant7, First ) ->
            down Whole :: intervalsForChordType Augmented chordInversion

        ( AugmentedDominant7, Second ) ->
            down Whole :: intervalsForChordType Augmented chordInversion

        -- Diminished
        ( Diminished, Root ) ->
            [ up MinorThird, up DiminishedFifth ]

        ( Diminished, First ) ->
            [ down (NHalves 6), down (NHalves 9) ]

        ( Diminished, Second ) ->
            [ down (NHalves 6), up MinorThird ]

        -- DiminishedDominant7
        ( DiminishedDominant7, Root ) ->
            up DominantSeventh :: intervalsForChordType Diminished chordInversion

        ( DiminishedDominant7, First ) ->
            down Whole :: intervalsForChordType Diminished chordInversion

        ( DiminishedDominant7, Second ) ->
            down Whole :: intervalsForChordType Diminished chordInversion

        -- DiminishedMajor7
        ( DiminishedMajor7, Root ) ->
            up MajorSeventh :: intervalsForChordType Diminished chordInversion

        ( DiminishedMajor7, First ) ->
            down Half :: intervalsForChordType Diminished chordInversion

        ( DiminishedMajor7, Second ) ->
            down Half :: intervalsForChordType Diminished chordInversion


{-| Return the note in the direction, `dir`, away from `note` `s` intervals
-}
step : IntervalVector -> Note -> Maybe Note
step { direction, interval } note =
    let
        doStep int =
            step { direction = direction, interval = int }
    in
    case interval of
        Half ->
            halfStep direction note

        NHalves n ->
            List.repeat n
                { direction = direction
                , interval = Half
                }
                |> (\x -> applySteps x note)
                |> Maybe.andThen (List.reverse >> List.head)

        Whole ->
            note
                |> doStep Half
                |> Maybe.andThen (doStep Half)

        MinorThird ->
            note
                |> doStep Whole
                |> Maybe.andThen (doStep Half)

        MajorThird ->
            note
                |> doStep Whole
                |> Maybe.andThen (doStep Whole)

        PerfectFifth ->
            note
                |> doStep MajorThird
                |> Maybe.andThen (doStep MinorThird)

        AugmentedFifth ->
            note
                |> doStep PerfectFifth
                |> Maybe.andThen (doStep Half)

        DiminishedFifth ->
            note
                |> doStep MajorThird
                |> Maybe.andThen (doStep Whole)

        MajorSeventh ->
            note
                |> doStep PerfectFifth
                |> Maybe.andThen (doStep MajorThird)

        DominantSeventh ->
            note
                |> doStep PerfectFifth
                |> Maybe.andThen (doStep MinorThird)


{-| Returns a list of all of the notes away from a give `note`.

  - The 0th element is applied to `note`.
  - The 1st element is applied to the result of the previous operation.
  - The 2nd element is applied to the result of the previous operation.
  - and so on...until all of the `steps` are exhausted.

In the case where applying any of the steps would result in running off of
either edge of the piano, this function returns a Nothing.

-}
applySteps : List IntervalVector -> Note -> Maybe (List Note)
applySteps steps note =
    doApplySteps steps note [] |> Maybe.map List.reverse


doApplySteps : List IntervalVector -> Note -> List Note -> Maybe (List Note)
doApplySteps steps note result =
    case steps of
        [] ->
            Just (note :: result)

        s :: rest ->
            case step s note of
                Just x ->
                    doApplySteps rest x (note :: result)

                Nothing ->
                    Nothing


{-| Return the KeyClass for a given `note`.
-}
keyClass : Note -> KeyClass
keyClass note =
    if isNatural note then
        Natural

    else
        Accidental


{-| Return the NoteClass for a given note.
-}
classifyNote : Note -> NoteClass
classifyNote note =
    if List.member note [ C1, C2, C3, C4, C5, C6, C7, C8 ] then
        C

    else if List.member note [ C_sharp1, C_sharp2, C_sharp3, C_sharp4, C_sharp5, C_sharp6, C_sharp7 ] then
        C_sharp

    else if List.member note [ D1, D2, D3, D4, D5, D6, D7 ] then
        D

    else if List.member note [ D_sharp1, D_sharp2, D_sharp3, D_sharp4, D_sharp5, D_sharp6, D_sharp7 ] then
        D_sharp

    else if List.member note [ E1, E2, E3, E4, E5, E6, E7 ] then
        E

    else if List.member note [ F1, F2, F3, F4, F5, F6, F7 ] then
        F

    else if List.member note [ F_sharp1, F_sharp2, F_sharp3, F_sharp4, F_sharp5, F_sharp6, F_sharp7 ] then
        F_sharp

    else if List.member note [ G1, G2, G3, G4, G5, G6, G7 ] then
        G

    else if List.member note [ G_sharp1, G_sharp2, G_sharp3, G_sharp4, G_sharp5, G_sharp6, G_sharp7 ] then
        G_sharp

    else if List.member note [ A1, A2, A3, A4, A5, A6, A7 ] then
        A

    else if List.member note [ A_sharp1, A_sharp2, A_sharp3, A_sharp4, A_sharp5, A_sharp6, A_sharp7 ] then
        A_sharp

    else
        B


{-| Return a list of the notes that comprise a `chord`
-}
notesForChord : Chord -> Maybe (List Note)
notesForChord { note, chordType, chordInversion } =
    intervalsForChordType chordType chordInversion
        |> List.map (\interval -> step interval note)
        |> Maybe.Extra.combine
        |> Maybe.map (\notes -> note :: notes)


{-| Return the scale for a given `key`
-}
notesForKey : Key -> List Note
notesForKey { noteClass, mode } =
    let
        origin =
            noteInCentralOctave noteClass
    in
    case applySteps (intervalsForMode mode) origin of
        -- We should never hit the Nothing case here.
        Nothing ->
            []

        Just scale ->
            scale


{-| Return true if `note` is a black key.
-}
isAccidental : Note -> Bool
isAccidental note =
    case classifyNote note of
        C ->
            False

        C_sharp ->
            True

        D ->
            False

        D_sharp ->
            True

        E ->
            False

        F ->
            False

        F_sharp ->
            True

        G ->
            False

        G_sharp ->
            True

        A ->
            False

        A_sharp ->
            True

        B ->
            False


{-| Return true if `note` is a white key.
-}
isNatural : Note -> Bool
isNatural note =
    note |> isAccidental |> not


{-| Return a list of all of the notes that we know about.
Only return the notes within the range `start` and `end`.
-}
notesFromRange : Note -> Note -> List Note
notesFromRange start end =
    [ C1
    , C_sharp1
    , D1
    , D_sharp1
    , E1
    , F1
    , F_sharp1
    , G1
    , G_sharp1
    , A1
    , A_sharp1
    , B1
    , C2
    , C_sharp2
    , D2
    , D_sharp2
    , E2
    , F2
    , F_sharp2
    , G2
    , G_sharp2
    , A2
    , A_sharp2
    , B2
    , C3
    , C_sharp3
    , D3
    , D_sharp3
    , E3
    , F3
    , F_sharp3
    , G3
    , G_sharp3
    , A3
    , A_sharp3
    , B3
    , C4
    , C_sharp4
    , D4
    , D_sharp4
    , E4
    , F4
    , F_sharp4
    , G4
    , G_sharp4
    , A4
    , A_sharp4
    , B4
    , C5
    , C_sharp5
    , D5
    , D_sharp5
    , E5
    , F5
    , F_sharp5
    , G5
    , G_sharp5
    , A5
    , A_sharp5
    , B5
    , C6
    , C_sharp6
    , D6
    , D_sharp6
    , E6
    , F6
    , F_sharp6
    , G6
    , G_sharp6
    , A6
    , A_sharp6
    , B6
    , C7
    , C_sharp7
    , D7
    , D_sharp7
    , E7
    , F7
    , F_sharp7
    , G7
    , G_sharp7
    , A7
    , A_sharp7
    , B7
    , C8
    ]
        |> List.Extra.dropWhile ((/=) start)
        |> 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
    , Sus2
    , Sus4
    , Major7
    , MajorDominant7
    , Minor
    , MinorMajor7
    , MinorDominant7
    , Augmented
    , AugmentedDominant7
    , Diminished
    , DiminishedDominant7
    , DiminishedMajor7
    ]


{-| 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
`end`.
-}
allChords :
    { start : Note
    , end : Note
    , inversions : List ChordInversion
    , chordTypes : List ChordType
    }
    -> List Chord
allChords { start, end, inversions, chordTypes } =
    let
        notes =
            notesFromRange start end
    in
    notes
        |> List.Extra.andThen
            (\note ->
                chordTypes
                    |> List.Extra.andThen
                        (\chordType ->
                            inversions
                                |> List.Extra.andThen
                                    (\inversion ->
                                        [ { note = note
                                          , chordType = chordType
                                          , chordInversion = inversion
                                          }
                                        ]
                                    )
                        )
            )


{-| Serialize a human-readable format of `note`.
-}
viewNote : Note -> String
viewNote note =
    case note of
        C1 ->
            "C1"

        C_sharp1 ->
            "C♯/D♭1"

        D1 ->
            "D1"

        D_sharp1 ->
            "D♯/E♭1"

        E1 ->
            "E1"

        F1 ->
            "F1"

        F_sharp1 ->
            "F♯/G♭1"

        G1 ->
            "G1"

        G_sharp1 ->
            "G♯/A♭1"

        A1 ->
            "A1"

        A_sharp1 ->
            "A♯/B♭1"

        B1 ->
            "B1"

        C2 ->
            "C2"

        C_sharp2 ->
            "C♯/D♭2"

        D2 ->
            "D2"

        D_sharp2 ->
            "D♯/E♭2"

        E2 ->
            "E2"

        F2 ->
            "F2"

        F_sharp2 ->
            "F♯/G♭2"

        G2 ->
            "G2"

        G_sharp2 ->
            "G♯/A♭2"

        A2 ->
            "A2"

        A_sharp2 ->
            "A♯/B♭2"

        B2 ->
            "B2"

        C3 ->
            "C3"

        C_sharp3 ->
            "C♯/D♭3"

        D3 ->
            "D3"

        D_sharp3 ->
            "D♯/E♭3"

        E3 ->
            "E3"

        F3 ->
            "F3"

        F_sharp3 ->
            "F♯/G♭3"

        G3 ->
            "G3"

        G_sharp3 ->
            "G♯/A♭3"

        A3 ->
            "A3"

        A_sharp3 ->
            "A♯/B♭3"

        B3 ->
            "B3"

        C4 ->
            "C4"

        C_sharp4 ->
            "C♯/D♭4"

        D4 ->
            "D4"

        D_sharp4 ->
            "D♯/E♭4"

        E4 ->
            "E4"

        F4 ->
            "F4"

        F_sharp4 ->
            "F♯/G♭4"

        G4 ->
            "G4"

        G_sharp4 ->
            "G♯/A♭4"

        A4 ->
            "A4"

        A_sharp4 ->
            "A♯/B♭4"

        B4 ->
            "B4"

        C5 ->
            "C5"

        C_sharp5 ->
            "C♯/D♭5"

        D5 ->
            "D5"

        D_sharp5 ->
            "D♯/E♭5"

        E5 ->
            "E5"

        F5 ->
            "F5"

        F_sharp5 ->
            "F♯/G♭5"

        G5 ->
            "G5"

        G_sharp5 ->
            "G♯/A♭5"

        A5 ->
            "A5"

        A_sharp5 ->
            "A♯/B♭5"

        B5 ->
            "B5"

        C6 ->
            "C6"

        C_sharp6 ->
            "C♯/D♭6"

        D6 ->
            "D6"

        D_sharp6 ->
            "D♯/E♭6"

        E6 ->
            "E6"

        F6 ->
            "F6"

        F_sharp6 ->
            "F♯/G♭6"

        G6 ->
            "G6"

        G_sharp6 ->
            "G♯/A♭6"

        A6 ->
            "A6"

        A_sharp6 ->
            "A♯/B♭6"

        B6 ->
            "B6"

        C7 ->
            "C7"

        C_sharp7 ->
            "C♯/D♭7"

        D7 ->
            "D7"

        D_sharp7 ->
            "D♯/E♭7"

        E7 ->
            "E7"

        F7 ->
            "F7"

        F_sharp7 ->
            "F♯/G♭7"

        G7 ->
            "G7"

        G_sharp7 ->
            "G♯/A♭7"

        A7 ->
            "A7"

        A_sharp7 ->
            "A♯/B♭7"

        B7 ->
            "B7"

        C8 ->
            "C8"


inspectChord : Chord -> String
inspectChord { note, chordType, chordInversion } =
    viewNote note ++ " " ++ chordTypeName chordType ++ " " ++ inversionName chordInversion ++ " position"


viewChord : Chord -> String
viewChord { note, chordType, chordInversion } =
    viewNoteClass (classifyNote note) ++ " " ++ chordTypeName chordType ++ " " ++ inversionName chordInversion ++ " position"


{-| Serialize a human-readable format of `noteClass`.
-}
viewNoteClass : NoteClass -> String
viewNoteClass noteClass =
    case noteClass of
        C ->
            "C"

        C_sharp ->
            "C♯/D♭"

        D ->
            "D"

        D_sharp ->
            "D♯/E♭"

        E ->
            "E"

        F ->
            "F"

        F_sharp ->
            "F♯/G♭"

        G ->
            "G"

        G_sharp ->
            "G♯/A♭"

        A ->
            "A"

        A_sharp ->
            "A♯/B♭"

        B ->
            "B"