-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchords.js
369 lines (343 loc) · 14.6 KB
/
chords.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
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
// --------------------------------------------------------------------------
// -- chords.js
// -- initial author: Renick Bell ([email protected])
// -- initial creation date: Wed Jun 28 10:08:48 AM CST 2023
// -- contributors: Yiler Huang ([email protected]); Steve Wang ([email protected])
// -- license: GPL 3.0
// --------------------------------------------------------------------------
//Yiler Function:
function setChordsKey(root, octave, template) {
const notes = ["C", "D", "E", "F", "G", "A", "B"];
root = notes.indexOf(root.toUpperCase())
if (template === undefined){
template = Object.keys(ChordTemplates)[randomRange(0, Object.keys(ChordTemplates).length - 1)]
}
let outputChord = [];
// for (let i = 0; i < 12; i++) {
Array.from({length: 12}).forEach((x, i) =>{
if (i == 4 || i == 5) {
outputChord.push(new Chord({
root: root + 5,
octave: octave,
template: ChordTemplates[template]
}))
}else if (i == 8 || i == 9){
outputChord.push(new Chord({
root: root + 7,
octave: octave,
template: ChordTemplates[template]
}))
}else {
outputChord.push(new Chord({
root: root,
octave: octave,
template: ChordTemplates[template]
}))
}
})
return outputChord
}
//Yiler Function
function generateChords(root, octave, voicing, majorOrMinor) {
let keyScale = Scale.get(notes[root] + " " + majorOrMinor)
let letterChords = [];
// for (let i = 0; i < 12; i++) {
Array.from({length: 12}).forEach((x, i) =>{
if (i == 4 || i == 5) {
letterChords.push(Chord.get(keyScale.notes[3] + voicing).notes.map(e => e + "" + octave))
} else if (i == 8 || i == 9) {
letterChords.push(Chord.get(keyScale.notes[4] + voicing).notes.map(e => e + "" + octave))
} else {
letterChords.push(Chord.get(keyScale.notes[0] + voicing).notes.map(e => e + "" + octave))
}
})
let midiChords = letterChords.map(x => {
return getMidiKeys(x)
})
return midiChords.map(x => {
return x.map(c => {
return {note: c % 12, octave: roundTo(c / 12)}
})
})
}
function generateChordsV2 (root, octave, progression) {
let letterChords = Progression.fromRomanNumerals(root, progression);
let noteChords = letterChords.map(chord => Chord.get(chord).notes);
let midiChords = noteChords.map(c => c.map(n => Note.midi(n + "" + octave)));
return midiChords
}
//Create noteDuration values for noteValueData
function createNoteSpans (noteValueData, e){
noteValueData.noteDurations = A.resizeArray(noteValueData.noteValues.length, noteValueData.noteDurations)
// noteValueData.bools = A.resizeArray(noteValueData.noteValues.length, noteValueData.bools)
}
//Creating notespan values from noteValueData:
function createNoteSpanValues (noteValueData, name){
if (noteValueData.noteDurationValues !== undefined && noteValueData.noteDurationKeyspan !== undefined){
e.noteDurationMaps[name] = new QuantizedMap(noteValueData.noteDurationKeyspan, noteValueData.noteDurations, noteValueData.noteDurationValues)
}
else if (noteValueData.noteDurationValues !== undefined && noteValueData.noteDurationKeyspan === undefined){
console.log('noteDurationValues defined')
e.noteDurationMaps[name] = new QuantizedMap(noteValueData.noteDurations.length, noteValueData.noteDurations, noteValueData.noteDurationValues)
}
else{
e.noteDurationMaps[name] = new QuantizedMap(noteValueData.noteDurations.length, A.buildArray(noteValueData.noteDurations.length, x => {return x}), noteValueData.noteDurations)
}
}
//Create quantizedMaps that have to do with notes and other things related to note:
function createNoteRelatedMaps (noteValueData, name, e){
createNoteSpanValues(noteValueData, name)
console.log(name, e)
if (noteValueData.noteValuesKeys === undefined){
noteValueData.noteValuesKeys = A.buildArray(noteValueData.noteValues.length, x => {return x})
}
e.noteMaps[name] = new QuantizedMap(noteValueData.noteValues.length, noteValueData.noteValuesKeys, noteValueData.noteValues)
}
//Create octave related things:
function createOctaveMaps (noteValueData, name, e){
if (noteValueData.octaveMapKeys === undefined){
e.octaveMaps[name] = new QuantizedMap(noteValueData.octave.length, A.buildArray(noteValueData.octave.length, x => {return x}), noteValueData.octave)
}
else {
e.octaveMaps[name] = new QuantizedMap(noteValueData.octaveMapKeyspan, noteValueData.octaveMapKeys, noteValueData.octave)
}
}
function createRootMap (noteValueData, name, e){
if (noteValueData.rootMapKeys === undefined){
e.rootMaps[name] = new QuantizedMap(noteValueData.rootMap.length, A.buildArray(noteValueData.rootMap.length, x => {return x}), noteValueData.rootMap)
}
else {
e.rootMaps[name] = new QuantizedMap(noteValueData.rootMapKeyspan, noteValueData.rootMapKeys, noteValueData.root)
}
}
// function scaleWithNote (noteLetter, octave, keyArray){
// const notes = ["C", "D", "E", "F", "G", "A", "B"];
// let note = notes.indexOf(noteLetter)
// if (note === -1){
// return 'Invalid note letter'
// }
// note += (octave * 12)
// let vals = []
// for (let i = 0; i < keyArray.length; i++) {
// vals.push(note + (12 * i))
// }
// return new QuantizedMap(Math.max(...keyArray) + 1, keyArray, vals)
// }
//scaleWithNote('C', randomRange(1, 8), aeolianMode)
// function makeNote (note, octave, add){
// const notes = ["C", "D", "E", "F", "G", "A", "B"];
// let modifiedNote = note
// if (add !== 0 && add !== undefined){
// modifiedNote = notes[notes.indexOf(note.toUpperCase()) + (add - 1)]
// }
// return Note.get(modifiedNote + octave)
// }
//Generates that chord progression that are placed in the chordProgressions variable in the musical environment:
function generateChordProgressions (){
let twelveBarsProgression = generateRandomMelody('C', 'bluesScale', 18, 6, 10)
return {
twelveBars: new QuantizedMap(18, A.buildArray(12, x => {return 4}), twelveBarsProgression.map(x => {x.velocity = 100; return {data: [x], bool: true}})),
lsystem: generateRandomLsystemChordProgression(),
scarboroughFair: new QuantizedMap(48, A.buildArray(12, x => {return 4}), generateScarboroughFairValues())
}
}
//Generate the note related part of the configuration object to change chord progressions:
function noteRelatedNecessaryConfigurations (chosenProgression, configurationObj){
configurationObj.rootNote = retreiveDataFromChosenProgressionValuesData('note', chosenProgression)
configurationObj.noteValues = chosenProgression.values.map(x => {
return x.data.map(b => {
return b.note
})
})
return configurationObj
}
//Generate the necessary configurations of the configuration object to change chord progressions:
function necessaryConfigurations (chosenProgression){
let configurationObj = {bools: chosenProgression.values.map(x => {return x.bool})}
configurationObj = noteRelatedNecessaryConfigurations(chosenProgression, configurationObj)
configurationObj.total = chosenProgression.values.length
configurationObj.octave = retreiveDataFromChosenProgressionValuesData('octave', chosenProgression)
configurationObj.noteDurationKeyspan = chosenProgression.keyspan
let beatCounter = 0
configurationObj.rhythmMapKeys = chosenProgression.keys
return configurationObj
}
//Retreive data from chord progression variable in musical environment:
function retreiveDataFromChosenProgressionValuesData (dataToRetreive, chosenProgression){
return chosenProgression.values.map(x => {
return x.data.map(n => {
return n[dataToRetreive]
})
})
}
//Generate the conditional configurations of the configuration object to change chord progressions:
function conditionalNoteConfigurations (chosenProgression, configurationObj){
if (chosenProgression.values[0].data[0].noteDuration === undefined){
let beatCounter = 0
configurationObj.noteDurations = []
chosenProgression.keys.forEach(x => {
configurationObj.noteDurations.push(beatCounter)
beatCounter += x
})
}
else {
configurationObj.noteDurations = retreiveDataFromChosenProgressionValuesData('noteDuration', chosenProgression)
}
if (chosenProgression.values[0].data[0].noteDurationValues === undefined){
configurationObj.noteDurationValues = chosenProgression.keys
}
else {
configurationObj.noteDurationValues = retreiveDataFromChosenProgressionValuesData('noteDurationValues', chosenProgression)
}
if (chosenProgression.values[0].data[0].keySpan === undefined){
configurationObj.rhythmMapValues = A.buildArray(chosenProgression.keys.length, x => {
return 1})
}
else {
configurationObj.rhythmMapValues = retreiveDataFromChosenProgressionValuesData('keySpan', chosenProgression)
}
return configurationObj
}
// //Generate configuration obj from chord progression:
// function assignChordProgressionToPlayer (playerName, chosenChordProgression){
// let chosenProgression = e.chordProgressions[chosenChordProgression]
// let configurationObj = necessaryConfigurations(chosenProgression)
// configurationObj = conditionalNoteConfigurations (chosenProgression, configurationObj)
// if (chosenProgression.values[0].data[0].velocity === undefined){
// configurationObj.velocity = [100, 100]
// }
// else {
// configurationObj.velocity = chosenProgression.values.map(x => {
// if (x.data[0].velocity <= 127){
// return x.data[0].velocity
// }
// else {
// return 127
// }
// })
// }
// if (chosenProgression.values[0].data[0].polyphony === undefined){
// configurationObj.polyphonyMap = [10000000000000000000000, 10000000000000000000000]
// }
// else {
// configurationObj.polyphonyMap = retreiveDataFromChosenProgressionValuesData('polyphony', chosenProgression)
// }
// return configurationObj
// }
function assignChordProgressionToPlayer (chosenChordProgression){
let chosenProgression = e.chordProgressions[chosenChordProgression]
let configurationObj = sortIntoConfigurationObj(chosenProgression)
configurationObj = inputOtherNecessaryConfigurationVariables(configurationObj)
return configurationObj
}
function sortIntoConfigurationObj (chosenProgression){
let configurationObj = {}
configurationObj.octaves = chosenProgression.values.map(x => {return x.octaves})
configurationObj.rootNote = chosenProgression.values.map(x => {return x.note})
configurationObj.rhythmMap = chosenProgression.keys
configurationObj.total = chosenProgression.keyspan
configurationObj.notespan = chosenProgression.keys
return configurationObj
}
function inputOtherNecessaryConfigurationVariables (chosenProgression){
configurationObj.velocity = [100]
configurationObj.bools = [true, true]
configurationObj.polyphanyMap = [50, 50]
configurationObj.modeMap = [0, 1, 2, 3, 4 ,5 , 6, 7, 8, 9, 10, 11, 12]
}
//Check current if it is time to change chord progressions
function checkIfChangeChordProgression (e, b, player){
// console.log('song', player.song)
if (player.song === undefined){
return true
}
let correctCurrentChordProgression = e.songs[player.song].wrapLookup(b)
console.log('correct', correctCurrentChordProgression)
if (player.currentChordProgression === correctCurrentChordProgression){
return true
}
else {
let defaultName = A.findMostFrequentItem(Object.values(player))
console.log('step1')
recordConfigurationDataIntoMusicalEnvironment(assignChordProgressionToPlayer(correctCurrentChordProgression), defaultName, e)
console.log('step2')
assignPlayerForMusicSynthesizerSession(e, 3, {rhythmMapName: 'straight'}, defaultName)
console.log('step3')
player.currentChordProgression = correctCurrentChordProgression
console.log('step4')
}
}
//Yiler function or Yiler may be able to explain this better:
function scaleWithNote (noteLetter, octave, mode){
let note = Mode.notes(mode, noteLetter)
note.forEach((x, i) =>{
note[i] = x + octave
})
let midiValues = note.map ( x =>{
return Midi.toMidi(x)
})
let rootNoteValues = getRootMidiValues(Mode.notes(mode, noteLetter))
return new QuantizedMap(Math.max(...rootNoteValues) + 1, rootNoteValues, midiValues)
}
//generated by chatgpt:
function separateOctaveAndRoot(midiNotes) {
let octaveNotes = [];
let rootNotes = [];
midiNotes.forEach(x => {
let octave = Math.floor(x / 12); // Divide by 12 to get the octave
let rootNote = x % 12; // The remainder gives the root note within the octave
octaveNotes.push(octave);
rootNotes.push(rootNote);
})
return {
octaveNotes,
rootNotes
};
}
function reformatIoisToRelative (iois){
if (A.isArrayAscending(iois) === false){
return iois
}
return iois.map((x, i) =>{
if (iois[i + 1] !== undefined){
return iois[i + 1] - x
}
}).slice(0, iois.length - 1)
}
function convertMusicalLettersToMidi (letterArray){
if (typeof letterArray[0] !== 'string'){
return letterArray
}
return letterArray.map(x => {
return Note.midi(x)
})
}
//Helped by chatgpt
function makeChordProgression (name, total, iois, notes, octaves){
if (iois === undefined){
let splitNotes = separateOctaveAndRoot(iois.map(x => {
return x[1]
}))
notes = splitNotes.rootNotes
octaves = splitNotes.octaveNotes
iois = iois.map(x => {
return x[0]
})
}
if (iois.length > notes.length){
notes = A.resizeArray(iois.length, notes)
}
else if (iois.length < notes.length){
iois = A.resizeArray(notes.length, iois)
}
if (octaves.length !== notes.length){
octaves = A.resizeArray(notes.length, octaves)
}
if (typeof notes[0] === 'object'){
e.chordProgressions[name] = new QuantizedMap(total, reformatIoisToRelative(iois), notes.map((x, i) => { return x.map((d, n) => {return {note: d, octave: octaves[i][n]}})}))
}
else {
e.chordProgressions[name] = new QuantizedMap(total, reformatIoisToRelative(iois), notes.map((x, i) => { return {note: x, octave: octaves[i]}}))
}
}
// makeChordProgression('yo', 10, [4, 8, 12, 16, 20], [10, 10, 10, 10, 20], [5, 5, 5])