-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmidi.js
383 lines (355 loc) · 13.7 KB
/
midi.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// --------------------------------------------------------------------------
// -- midi.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
// --------------------------------------------------------------------------
//Send Midi Data to music synthesizer:
function sendMidiData (info, player, note){
// add2Log(note)
//add2Log('--------------------------------------------------------------------------')
console.log('note', note, 'velocity: ', info.velocity, 'channel', player.channel - 1, 'session', player.session - 1)
e.outputs[player.session - 1].send('noteon', {
note: note,
velocity: info.velocity,
channel: player.channel - 1,
});
setTimeout(() => {
e.outputs[player.session - 1].send('noteoff', {
note: note,
velocity: info.velocity,
channel: player.channel - 1,
});
}, info.noteDuration * 1000)
}
//Convert velocity values from 0-1 to midi 0-127:
function convertVelocityToMidiValues (inputVelocity){
if (inputVelocity > 1){
return inputVelocity
}
else {
return inputVelocity * 127
}
}
//This function is modified to set action as sequenceSize instead of superDirt
function setupMidiRhythm (env, sequenceName, rhythmPatternName = 'default') {
env.players[sequenceName] = new Player(sequenceName);
env.players[sequenceName].maskMap = 'default'
//env.players[playerName].samplePattern = playerName;
env.players[sequenceName].action = 'midiSequencedRhythm';
env.players[sequenceName].rhythmMap = rhythmPatternName
return sequenceName
}
//This function is modified to set action as sendPlaybackMessage instead of superDirt
function setupPlaybackPlayer (env, sequenceName, rhythmPatternName = 'default') {
env.players[sequenceName] = new Player(sequenceName);
env.players[sequenceName].maskMap = 'default'
//env.players[playerName].samplePattern = playerName;
env.players[sequenceName].action = 'sendPlaybackMessage';
env.players[sequenceName].rhythmMap = rhythmPatternName
return sequenceName
}
//Action function:
function musicSynthesizerCaller (p,b) {if ((mask(p, e.maskMaps[e.players[p].maskMap] ,(e.currentBeat()),1)) != true) {callMusicSynthesizerRhythm(e, b, p);}}
e.actions.midiSequencedRhythm = musicSynthesizerCaller
//Action function:
function sendPlaybackMessage (p,b) {if ((mask(p, e.maskMaps[e.players[p].maskMap] ,(e.currentBeat()),1)) != true) {callMusicSynthesizerRhythm(e, b, p);}}
e.actions.sendPlaybackMessage = sendPlaybackMessage
function filterMode (note, e, b, player){
let mode = e.modeFilters[player.modeFilter]
if (mode === undefined){
return note
}
else if (e.notesInputMode === 'relativeSemitone'){
console.log('filtering mode map for', player.modeFilter)
return mode.floorWrapLookup(note)
}
else{
return mode[note]
}
}
function filterPolyphany (e, b, player, info){
let playerPolyphany = e.maxPolyphonyMaps[player.polyphonyMap]
if (playerPolyphany === undefined){
return info
}
let maxNoteSpanLength = playerPolyphany.wrapLookup(b)
if (info.noteDuration.length > maxNoteSpanLength){
// for (let i = 0; i < maxNoteSpanLength - info.noteDuration.length; i++) {
Array.from({length: maxNoteSpanLength - info.noteDuration.length}, () => {
info.noteDuration = A.safeSplice(info.noteDuration, 1, randomRange(0, info.noteDuration.length - 1))
})
}
return info
}
function convertRomanNumeralsToMidi (info){
let stringOctaves = info.octaves
if (typeof info.octaves[0] === 'string'){
info.octaves = info.octaves.map(x => {
return Note.midi(x)
})
}
else {
stringOctaves = info.octaves.map(x => {
return Note.fromMidi(x)
})
}
// console.log('testing for roman numerals', info.noteValues[0])
if (typeof info.noteValues[0] === 'string'){
console.log('Yo octave stuff', info.letters[0] + info.octaves[0])
info.finalValues = Progression.fromRomanNumerals(info.letters[0] + info.octaves[0], info.noteValues)
info.finalValues = info.finalValues.map(x => {
return Midi.toMidi(x)
})
console.log('converted', info.finalValues)
return info
}
// info.noteValues = info.noteValues.map((x, i) => {
// console.log(stringOctaves[i], x)
// return Progression.fromRomanNumerals(stringOctaves[i], x)
// })
// info.noteValues = Progression.fromRomanNumerals(stringOctaves[0], info.noteValues)
return info
}
//Roman numeral conversions to midi with tonal helped by chatgpt
function calculateFinalNoteValue (info){
if (info.finalValues === undefined){
// console.log('finalNote not detected', info.finalNote)
info.finalValues = info.noteValues.map(x => {
return Note.midi(info.letters[0] + info.octaves[0]) + x
})
}
return info
}
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
//Function from: https://stackoverflow.com/a/5778071
function checkIfStringIncluesNumber (inputString){
let splitArray = inputString.split('')
return !splitArray.every(x => {
if (isNumeric(x) === true){
return false
}
else {return true}
})
}
function convertLettersToMidi (info){
if (info.finalValues !== undefined){
console.log('finalNote already defined')
return info
}
if (typeof info.octaves[0] === 'string'){
info.octaves = info.octaves.map(x => {
if (checkIfStringIncluesNumber(x) === false){
return Note.midi(x + '-1')
}
else {
return Note.midi(x)
}
})
}
if (typeof info.noteValues[0] === 'string'){
console.log('beforee', info.noteValues)
info.noteValues = info.noteValues.map(x => {
if (checkIfStringIncluesNumber(x) === false){
return Note.midi(x + '-1')
}
else {
console.log('wrong')
return Note.midi(x)
}
})
console.log('Converting note values NOW', info.noteValues)
}
return info
}
//Helped by chatgpt
function convertNoteValuesToMidi (info, e, b, player){
if (info.finalValues !== undefined){
console.log('finalNote already detected 2')
return info
}
if (e.notesInputMode === 'relativeScaleDegree'){
let mode = e.modeFilters[player.modeFilter]
info.noteValues = info.noteValues.map(x => {
return mode.nearestWrapLookup(x)
})
console.log('mode', mode)
console.log('this feature works HAHA', info.noteValues)
}
else{
info.noteValues = info.noteValues.map(x => {
return filterMode(x, e, b, player)
})
}
return info
}
function getRelativeMode (modeName){
return Mode.get(modeName).intervals.map(x => Interval.semitones(x))
}
function checkIfChangeFilteredMode (e, b, player){
let currentModeMap = e.modeMaps[player.modeMap]
if (currentModeMap === undefined){
return false
}
let currentMode = currentModeMap.wrapLookup(b)
let correctMode = getRelativeMode(currentMode)
console.log('testing modeFilter', e.modeFilters[player.modeFilter], correctMode)
try{
if (JSON.stringify(e.modeFilters[player.modeFilter].keys) === JSON.stringify(correctMode)){
return false
}
else {
console.log('changing mode NOW')
e.modeFilters[player.modeFilter] = new QuantizedMap(correctMode[correctMode.length - 1], correctMode, correctMode)
return true
}
}
catch{
console.log('changing mode NOW')
e.modeFilters[player.modeFilter] = new QuantizedMap(correctMode[correctMode.length - 1], correctMode, correctMode)
return true
}
}
//Gather and sort information and prepare to send through midi:
function callMusicSynthesizerRhythm (e, b, session){
let player = e.players[session]
let info = getNoteInfoToSend(player, b, session)
info = filterPolyphany(e, b, player, info)
let octaveFloor = new QuantizedMap(8, [3, 4, 5, 6, 7], [3, 4, 5, 6, 7])
/*
console.log('first step look up', e.noteDurationMaps[player.noteDurationMapName].wrapLookup(b))
console.log('b', b)
console.log('noteValues', info.noteValues)
console.log('music notes', midiToMusicNotes(info.noteValues))
*/
//info.noteValues = handleDissonance(b, info)
info = convertRomanNumeralsToMidi(info)
info = convertLettersToMidi(info)
visualizeVolume(info)
checkIfChangeChordProgression(e, b, player)
checkIfSendMidiControlChange(e, b, player)
checkIfChangeFilteredMode(e, b, player)
info = convertNoteValuesToMidi(info, e, b, player)
info = calculateFinalNoteValue(info)
// console.log('FINAL playing finaValues', info.finalValues)
info.finalValues.forEach((x, i) => {
//console.log('info;', x,octaveFloor.floorLookup(info.octaves[i]))
// console.log('note', x + (octaveFloor.floorLookup(info.octaves[i]) * 12))
// console.log(session + ': ' ,x + (octaveFloor.floorLookup(info.octaves[i]) * 12))
// sendMidiData(info, player, x + (octaveFloor.floorLookup(info.octaves[i]) * 12))
sendMidiData(info, player, x)
})
return true
}
//Collect sound information to play:
function getNoteInfoToSend (player, b, session){
return {
noteDuration: e.rhythmMaps[player.rhythmMap].values[0].wrapLookup(b),
velocity: convertVelocityToMidiValues(e.velocityMaps[player.velocityMap].wrapLookup(b)),
// noteValues: e.noteMaps[player.noteMap].wrapLookup(e.noteDurationMaps[player.noteDurationMapName].wrapLookup(b)),
noteValues: e.noteMaps[player.noteMap].wrapLookup(e.noteDurationMaps[player.noteDurationMapName].wrapLookup(b)),
// noteValues: (e.octaveMaps[player.noteMap].wrapLookup(e.noteDurationMaps[player.noteDurationMapName].wrapLookup(b)) * 12) + e.rootNoteMaps[player.noteMap].wrapLookup(e.noteDurationMaps[player.noteDurationMapName].wrapLookup(b)),
octaves: e.octaveMaps[player.octaveMap].wrapLookup(b),
letters: e.rootMaps[player.rootMap].wrapLookup(b),
}
}
//Update Midi outputs:
function updateMidiOutputList (e){
let easymidiOutputs = easymidi.getOutputs()
if (process.platform === 'linux'){
easymidiOutputs.shift()
}
if (e.outputs !== undefined){
e.outputs.forEach(x => {
x.close()
})
}
e.outputs = easymidiOutputs.map(x => {
return new easymidi.Output(x)
})
}
//Yiler function:
function getMidiKeys(scaleOrChordNotesArray) {
let outputArray = scaleOrChordNotesArray.map(e => Note.midi(e))
return outputArray
}
//Get the root note of a midi Values:
function getRootMidiValues (note){
note.forEach((x, i) =>{
note[i] = x + 0
})
let midiValues = note.map ( x =>{
return Midi.toMidi(x)
})
return midiValues.map (x =>{
return x - 12
})
}
//Converts midi values to musical notes:
function midiToMusicNotes (array){
return array.map(x =>{
return Midi.midiToNoteName(x)
})
}
function checkIfSendMidiControlChange (e, b, player){
// console.log(e.controlChangeMaps, player.controlChangeMaps)
if (e.controlChangeMaps[player.controlChangeMap] === undefined){
console.log('CC unknown')
return true
}
let correctCC = e.controlChangeMaps[player.controlChangeMap].wrapLookup(b)
console.log('correctCC', correctCC)
if (player.currentControlChange !== correctCC){
player.currentControlChange = correctCC
e.outputs[player.session - 1].send('cc', correctCC)
console.log('CC Data sent')
}
}
function updateMidiInputList (e){
e.inputs = easymidi.getInputs().map(x => {
return {inputName: x, recordMessage: false}
})
}
function ignoreMessagesFromInput (e, inputIndex){
let currentInput = e.inputs[inputIndex]
currentInput.outputPort.off('message', currentInput.inputFunc)
}
function receiveMessagesFromInput (e, inputIndex, outputIndex, recordMessages){
let currentInput = e.inputs[inputIndex]
if (currentInput.recordedMessages === undefined){
currentInput.recordedMessages = new QuantizedMap(0, [], [])
}
if (recordMessages !== undefined){
currentInput.recordMessages = recordMessages
}
currentInput.outputIndex = outputIndex
currentInput.inputFunc = (deltaTime, message) => {
let currentInput = e.inputs[inputIndex]
if (currentInput.outputIndex !== undefined){
e.outputs[currentInput.outputIndex].send(deltaTime._type, deltaTime)
}
if (currentInput.recordMessages === true){
// currentInput.recordedMessages.keys.push(e.currentBeat())
currentInput.recordedMessages.keys.push(Math.floor(new Date().getTime() / 1000))
//Time from: https://stackoverflow.com/a/25250596
currentInput.recordedMessages.values.push(deltaTime)
currentInput.recordedMessages.keyspan = e.currentBeat() + 2
}
}
currentInput.outputPort = new easymidi.Input(currentInput.inputName)
currentInput.outputPort.on('message', currentInput.inputFunc)
}
function addInputMessageToRecordedMessages (inputIndex, recordedMessagesName){
e.recordedMessages[recordedMessagesName] = e.inputs[inputIndex].recordedMessages
let messages = e.recordedMessages[recordedMessages]
let relativeKeys = [0]
messages.keys.forEach((x, i) => {
if (i < messages.length - 1){
relativeKeys.push(messages[i] - x)
}
})
messages.keys = relativeKeys
}