-
Notifications
You must be signed in to change notification settings - Fork 0
/
Music.js
232 lines (196 loc) · 5.46 KB
/
Music.js
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
/* Built-in music utilities */
const SEMITONES_PER_OCTAVE = 12
const A = Math.pow(2, 1/SEMITONES_PER_OCTAVE)
const BASE_FREQ = 440 // A4 = 440Hz
const LOWEST_SEMITONE_DIFF_FROM_BASE = -57 // C0 = -57 semitones from A4
const OCTAVES = [0, 1, 2, 3, 4, 5, 6, 7, 8]
const PITCHES = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
const ACCIDENTALS = {
'bb': -2,
'♭♭': -2,
'𝄫': -2,
'b': -1,
'♭': -1,
'': 0,
'♮': 0,
'♯': 1,
'#': 1,
'♯♯': 2,
'##': 2,
'𝄪': 2,
}
export {SEMITONES_PER_OCTAVE, A, BASE_FREQ, LOWEST_SEMITONE_DIFF_FROM_BASE, OCTAVES, PITCHES, ACCIDENTALS}
export function semitoneToFrequency(semitonesFromBaseTone) {
return BASE_FREQ * Math.pow(A, semitonesFromBaseTone)
}
const noteNameToSemitone = {}
const noteNameToFrequency = {}
for (let octave of OCTAVES) {
for (let pitch of Object.keys(PITCHES)) {
for (let accidental of Object.keys(ACCIDENTALS)) {
let noteName = `${pitch}${accidental}${octave}`
let absoluteSemitone = LOWEST_SEMITONE_DIFF_FROM_BASE
+ (octave * SEMITONES_PER_OCTAVE)
+ PITCHES[pitch]
+ ACCIDENTALS[accidental]
noteNameToSemitone[noteName] = absoluteSemitone
noteNameToFrequency[noteName] = semitoneToFrequency(absoluteSemitone)
}
}
}
export {noteNameToSemitone, noteNameToFrequency}
export function Tempo(bpb, bv, bpm) {
let beatLength = 60/bpm
let barLength = bpb * beatLength
function timeInBar(t) {
return t % barLength
}
function currentBar(t) {
return 1 + Math.floor(t / barLength)
}
function noteLength(noteValue = bv) {
return beatLength * bv/noteValue
}
function timeInNote(noteValue, t) {
if (typeof t === 'undefined') {
t = noteValue
noteValue = bv
}
return t % noteLength(noteValue)
}
function currentNoteInBar(noteValue, t) {
if (typeof t === 'undefined') {
t = noteValue
noteValue = bv
}
return 1 + Math.floor(timeInBar(t) / noteLength(noteValue))
}
function isInBar(which, noteValue, t) {
return which.indexOf(currentNoteInBar(noteValue, t)) !== -1
}
return {
beatLength: beatLength,
barLength: barLength,
timeInBar: timeInBar,
currentBar: currentBar,
noteLength: noteLength,
timeInNote: timeInNote,
currentNoteInBar: currentNoteInBar,
isInBar: isInBar,
}
}
const interval = {
'unison': 0,
'1': 0,
'P1': 0,
'm2': 1,
'2': 2,
'M2': 2,
'm3': 3,
'3': 4,
'M3': 4,
'4': 5,
'P4': 5,
'#4': 6,
'♯4': 6,
'A4': 6,
'TT': 6,
'b5': 6,
'♭5': 6,
'd5': 6,
'5': 7,
'P5': 7,
'A5': 8,
'm6': 8,
'6': 9,
'M6': 9,
'd7': 9,
'm7': 10,
'7': 11,
'M7': 11,
'octave': 12,
'8': 12,
'P8': 12,
'm9': 13,
'b9': 13,
'♭9': 13,
'9': 14,
'#9': 15,
'♯9': 15,
'11': 17,
'#11': 18,
'♯11': 18,
'13': 21,
}
const chord = {}
// Triads
chord[''] = ['1', '3', '5']
chord['m'] = ['1', 'm3', '5']
chord['+'] = ['1', '3', 'A5']
chord['dim'] = ['1', 'm3', 'd5']
chord['sus2'] = ['1', '2', '5']
chord['sus4'] = ['1', '4', '5']
// 7th tetrads
chord['maj7'] = ['1', '3', '5', '7']
chord['7'] = ['1', '3', '5', 'm7']
chord['+7'] = ['1', '3', 'A5', 'm7']
chord['m7'] = ['1', 'm3', '5', 'm7']
chord['mM7'] = ['1', 'm3', '5', '7']
chord['m7b5'] = ['1', 'm3', 'd5', 'm7']
chord['dim7'] = ['1', 'm3', 'd5', 'd7']
// Extended
chord['9'] = ['1', 'm3', 'd5', 'd7', '9']
chord['13'] = ['1', 'm3', 'd5', 'd7', '9', '13']
// Other
chord['5'] = ['1', '5', '8']
for (let name of Object.keys(chord)) {
chord[name] = chord[name].map(i => interval[i])
}
// Aliases
chord['M'] = chord['maj'] = chord['Maj'] = chord['Δ'] = chord['']
chord['-'] = chord['min'] = chord['m']
chord['aug'] = chord['+']
chord['o'] = chord['º'] = chord['°'] = chord['dim']
chord['2'] = chord['sus2']
chord['4'] = chord['sus4']
chord['M7'] = chord['Maj7'] = chord['Δ7'] = chord['maj7']
chord['dom'] = chord['dom7'] = chord['7']
chord['aug7'] = chord['+7']
chord['min7'] = chord['-7'] = chord['m7']
chord['mMaj7'] = chord['mΔ7'] = chord['minΔ7'] = chord['minMaj7'] = chord['mM7']
chord['ø'] = chord['Ø'] = chord['m7b5']
chord['o7'] = chord['º7'] = chord['°7'] = chord['dim7']
const scale = {}
// Heptatonic
scale.major = ['1', '2', '3', '4', '5', '6', '7']
scale.minor = ['1', '2', 'm3', '4', '5', 'm6', 'm7']
scale.harmonic = ['1', '2', 'm3', '4', '5', 'm6', '7']
scale.melodic = ['1', '2', 'm3', '4', '5', '6', '7']
// Major modes
scale.lydian = ['1', '2', '3', 'A4', '5', '6', '7']
// scale.ionian = scale.major
scale.mixolydian = ['1', '2', '3', '4', '5', '6', 'm7']
scale.dorian = ['1', '2', 'm3', '4', '5', '6', 'm7']
// scale.aeolian = scale.minor
scale.phrygian = ['1', 'm2', 'm3', '4', '5', 'm6', 'm7']
scale.locrian = ['1', 'm2', 'm3', '4', 'd5', 'm6', 'm7']
for (let name of Object.keys(scale)) {
scale[name] = scale[name].map(i => interval[i])
}
// Pentatonic
scale.pentatonic = {}
scale.pentatonic.major = ['1', '2', '3', '5', '6']
scale.pentatonic.minor = ['1', 'm3', '4', '5', 'm7']
for (let name of Object.keys(scale.pentatonic)) {
scale.pentatonic[name] = scale.pentatonic[name].map(i => interval[i])
}
// Aliases
scale.ionian = scale.major
scale.aeolian = scale.minor
export {interval, chord, scale}
export function extend(fundamental, type) {
let semitones = Array.isArray(type)
? type
: chordSemitones(type)
return semitones.map(d => noteNameToSemitone[fundamental] + d)
}