-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.go
162 lines (138 loc) · 3.98 KB
/
music.go
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
package main
import (
"fmt"
"sort"
)
// https://gist.github.com/inky/3188870
var Notes = []string{"A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"}
// Scales as steps from the previous note
var Scales = map[string][]int{
"Major (Ionian)": {2, 2, 1, 2, 2, 2, 1},
"Dorian Mode": {2, 1, 2, 2, 2, 1, 2},
"Phrygian Mode": {1, 2, 2, 2, 1, 2, 2},
"Lydian Mode": {2, 2, 2, 1, 2, 2, 1},
"Mixolydian Mode": {2, 2, 1, 2, 2, 1, 2},
"Natural Minor (Aeolian)": {2, 1, 2, 2, 1, 2, 2},
"Locrian Mode": {1, 2, 2, 1, 2, 2, 2},
"Harmonic Minor": {2, 1, 2, 2, 1, 3, 1},
"Locrian nat6": {1, 2, 2, 1, 3, 1, 2},
"Ionian #5": {2, 2, 1, 3, 1, 2, 1},
"Ukranian minor": {2, 1, 3, 1, 2, 1, 2},
"Phrygian dominant": {1, 3, 1, 2, 1, 2, 2},
"Lydian #2": {3, 1, 2, 1, 2, 2, 1},
"Super Locrian diminished": {1, 2, 1, 2, 2, 1, 3},
"Diminished": {2, 1, 2, 1, 2, 1, 2, 1},
"Dominant Diminished": {1, 2, 1, 2, 1, 2, 1, 2},
"Pentatonic Major": {2, 2, 3, 2, 3},
"Pentatonic Minor": {3, 2, 2, 3, 2},
"Metallica": {1, 1, 1, 2, 1, 1, 1, 2, 2},
}
// Chords as the distance from the root note
var Chords = map[string][]int{
"Major": {0, 4, 7},
"Minor": {0, 3, 7},
"Augmented": {0, 4, 8},
"Diminished": {0, 4, 6},
"sus2": {0, 2, 7},
"sus4": {0, 5, 7},
"Power": {0, 7},
"7": {0, 4, 7, 10},
"m7": {0, 3, 7, 10},
"maj7": {0, 4, 7, 11},
"dom7": {0, 4, 7, 10},
"dim7": {0, 3, 6, 9},
"dom7f5": {0, 4, 6, 10},
"halfdim7": {0, 3, 6, 10},
"majdim7": {0, 3, 6, 11},
"minmaj7": {0, 3, 7, 11},
"augmaj7": {0, 4, 8, 11},
"aug7": {0, 4, 8, 10},
"7sus2": {0, 5, 7, 10},
"9": {0, 4, 7, 10, 14},
"m9": {0, 3, 7, 10, 14},
"maj9": {0, 4, 7, 11, 14},
"11": {0, 4, 7, 10, 14, 17},
"m11": {0, 3, 7, 10, 14, 17},
}
func NotePosition(note string) (int, error) {
for i := range Notes {
if Notes[i] == note {
return i, nil
}
}
return -1, fmt.Errorf("note '%s' doesn't exist", note)
}
func GetNote(note string, steps int) (string, error) {
pos, err := NotePosition(note)
if err != nil {
return "", err
}
return Notes[(pos+steps)%len(Notes)], nil
}
func GetScale(note, scale string) ([]string, error) {
pos, err := NotePosition(note)
if err != nil {
return nil, err
}
if _, ok := Scales[scale]; !ok {
return nil, fmt.Errorf("scale '%s' doesn't exist", scale)
}
ret := make([]string, 0, len(Scales[scale]))
for _, steps := range Scales[scale] {
ret = append(ret, Notes[pos])
pos = (pos + steps) % len(Notes)
}
return ret, nil
}
func GetChord(note, chord string) ([]string, error) {
pos, err := NotePosition(note)
if err != nil {
return nil, err
}
if _, ok := Chords[chord]; !ok {
return nil, fmt.Errorf("chord '%s' doesn't exist", chord)
}
ret := make([]string, 0, len(Chords[chord]))
for _, distance := range Chords[chord] {
ret = append(ret, Notes[(pos+distance)%len(Notes)])
}
return ret, nil
}
func IsChordInScale(chordNotes, scaleNotes []string) bool {
for i := range chordNotes {
found := false
for j := range scaleNotes {
if chordNotes[i] == scaleNotes[j] {
found = true
break
}
}
if !found {
return false
}
}
return true
}
type ChordMap map[string][]string
// Get the chords that can be played with the notes in the given scale
func GetChordsInScale(root, scale string) (ChordMap, error) {
scalenotes, err := GetScale(root, scale)
if err != nil {
return nil, err
}
scalechords := ChordMap{}
// Get the chords in a map with the Note name as the key
for i := range Notes {
for j := range Chords {
notes, err := GetChord(Notes[i], j)
if err != nil {
return nil, err
}
if IsChordInScale(notes, scalenotes) {
scalechords[Notes[i]] = append(scalechords[Notes[i]], j)
}
}
sort.Strings(scalechords[Notes[i]])
}
return scalechords, err
}