-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathmidi.js
358 lines (314 loc) · 14.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
// Copyright (c) 2023-24 Mubashirshariq
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the The GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// You should have received a copy of the GNU Affero General Public
// License along with this library; if not, write to the Free Software
// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
// Midi Importer
/* exported transcribeMidi*/
const MAX_NOTEBLOCKS = 100;
const defaultTempo = 90;
const standardDurations = [
{ value: "1/1", duration: 1 },
{ value: "1/2", duration: 0.5 },
{ value: "1/4", duration: 0.25 },
{ value: "1/8", duration: 0.125 },
{ value: "1/16", duration: 0.0625 },
{ value: "1/32", duration: 0.03125 },
{ value: "1/64", duration: 0.015625 },
{ value: "1/128", duration: 0.0078125 }
];
const getClosestStandardNoteValue = (duration) => {
let closest = standardDurations[0];
let minDiff = Math.abs(duration - closest.duration);
for (let i = 1; i < standardDurations.length; i++) {
let diff = Math.abs(duration - standardDurations[i].duration);
if (diff < minDiff) {
closest = standardDurations[i];
minDiff = diff;
}
}
return closest.value.split('/').map(Number);
}
const transcribeMidi = async (midi) => {
const currentMidi = midi;
const drumMidi = getReverseDrumMidi();
const isPercussion = [];
let jsONON = [];
let actionBlockCounter = 0; // Counter for action blocks
let actionBlockNames = []; // Array to store action block names
let totalnoteblockCount = 0; // Initialize noteblock counter
let noteblockCount = 0;
let shortestNoteDenominator = 0;
let offset = 100;
let stopProcessing = false;
let trackCount = 0;
let actionBlockPerTrack = [];
let instruments = [];
let currentMidiTempoBpm = currentMidi.header.tempos;
if (currentMidiTempoBpm && currentMidiTempoBpm.length > 0) {
currentMidiTempoBpm = Math.round(currentMidiTempoBpm[0].bpm);
} else {
currentMidiTempoBpm = defaultTempo;
}
let defaultTimeSignature = [4, 4];
let currentMidiTimeSignature = currentMidi.header.timeSignatures;
if (currentMidiTimeSignature && currentMidiTimeSignature.length > 0) {
currentMidiTimeSignature = currentMidiTimeSignature[0].timeSignature;
} else {
currentMidiTimeSignature = defaultTimeSignature;
}
let precurssionFlag = false;
// console.log("tempoBpm is: ", currentMidiTempoBpm);
// console.log("tempo is : ",currentMidi.header.tempos);
// console.log("time signatures are: ", currentMidi.header.timeSignatures);
currentMidi.tracks.forEach((track, trackIndex) => {
let k = 0;
if (stopProcessing) return; // Exit if flag is set
if (!track.notes.length) return;
let r = jsONON.length;
let instrument = "electronic synth";
if (track.instrument.name && !track.instrument.percussion) {
for (let voices of VOICENAMES) {
if (track.instrument.name.indexOf(voices[1]) > -1) {
instrument = voices[0];
}
}
} else if (track.instrument.percussion) {
precurssionFlag = true;
}
actionBlockPerTrack[trackCount] = 0;
instruments[trackCount] = instrument;
let actionBlockName = `track${trackCount}chunk${actionBlockCounter}`;
jsONON.push(
[r, ["action", { collapsed: false }], 150, 100, [null, r + 1, r + 2, null]],
[r + 1, ["text", { value: actionBlockName }], 0, 0, [r]]
);
let sched = [];
isPercussion.push(track.instrument.percussion && (track.channel === 9 || track.channel === 10));
track.notes.forEach((note, index) => {
let name = note.name;
let start = Math.round(note.time * 100) / 100;
let end = Math.round((note.time + note.duration) * 100) / 100;
if (note.duration === 0) return;
let lastNote = sched[sched.length - 1];
if (index === 0 && start > 0) {
sched.push({ start: 0, end: start, notes: ["R"] });
}
if (lastNote && lastNote.start === start && lastNote.end === end) {
lastNote.notes.push(name);
return;
}
if (lastNote && lastNote.start <= start && lastNote.end > start) {
let prevNotes = [...lastNote.notes];
let oldEnd = lastNote.end;
lastNote.end = start;
sched.push({ start: start, end: end, notes: [...prevNotes, name] });
if (oldEnd > end) {
sched.push({ start: end, end: oldEnd, notes: prevNotes });
}
return;
}
if (lastNote && lastNote.end < start) {
sched.push({ start: lastNote.end, end: start, notes: ["R"] });
}
sched.push({ start: start, end: end, notes: [name] });
});
let noteSum = 0;
let currentActionBlock = [];
const addNewActionBlock = (isLastBlock = false) => {
let r = jsONON.length;
let actionBlockName = `track${trackCount}chunk${actionBlockCounter}`;
actionBlockNames.push(actionBlockName);
actionBlockPerTrack[trackCount]++;
if (k == 0) {
jsONON.push(
...currentActionBlock
);
k = 1;
} else {
let settimbreIndex = r;
// Adjust the first note block's top connection to settimbre
currentActionBlock[0][4][0] = settimbreIndex;
jsONON.push(
[r, ["action", { collapsed: false }], 100 + offset, 100 + offset, [null, r + 1, settimbreIndex + 2, null]],
[r + 1, ["text", { value: actionBlockName }], 0, 0, [r]],
...currentActionBlock
);
}
if (isLastBlock) {
let lastIndex = jsONON.length - 1;
// Set the last hidden block's second value to null
jsONON[lastIndex][4][1] = null;
}
currentActionBlock = [];
actionBlockCounter++;
offset += 100;
};
//Using for loop for finding the shortest note value
for (let j in sched) {
let dur = sched[j].end - sched[j].start;;
let temp = getClosestStandardNoteValue(dur * 3 / 8);
shortestNoteDenominator = Math.max(shortestNoteDenominator, temp[1]);
}
for (let i in sched) {
if (stopProcessing) break; // Exit inner loop if flag is set
let { notes, start, end } = sched[i];
let duration = end - start;
noteSum += duration;
let isLastNoteInBlock = (noteSum >= 16) || (noteblockCount > 0 && noteblockCount % 24 === 0);
if (isLastNoteInBlock) {
totalnoteblockCount += noteblockCount;
noteblockCount = 0;
noteSum = 0;
}
let isLastNoteInSched = (i == sched.length - 1);
let last = isLastNoteInBlock || isLastNoteInSched;
let first = (i == 0);
let val = jsONON.length + currentActionBlock.length;
const getPitch = (x, notes, prev) => {
let ar = [];
if (notes[0] == "R") {
ar.push(
[x, "rest2", 0, 0, [prev, null]]
);
} else if (precurssionFlag) {
let drumname = drumMidi[track.notes[0].midi][0] || "kick drum";
ar.push(
[x, "playdrum", 0, 0, [first ? prev : x - 1, x + 1, null]],
[x + 1, ["drumname", { "value": drumname }], 0, 0, [x]],
);
x += 2;
} else {
for (let na in notes) {
let name = notes[na];
let first = na == 0;
let last = na == notes.length - 1;
ar.push(
[x, "pitch", 0, 0, [first ? prev : x - 3, x + 1, x + 2, last ? null : x + 3]],
[x + 1, ["notename", { "value": name.substring(0, name.length - 1) }], 0, 0, [x]],
[x + 2, ["number", { "value": parseInt(name[name.length - 1]) }], 0, 0, [x]]
);
x += 3;
}
}
return ar;
};
let obj = getClosestStandardNoteValue(duration * 3 / 8);
// let scalingFactor=1;
// if(shortestNoteDenominator>32)
// scalingFactor=shortestNoteDenominator/32;
// if(obj[1]>=scalingFactor)
// obj[1]=obj[1]/scalingFactor;
// else
// obj[0]=obj[0]*scalingFactor;
// To get the reduced fraction for 4/2 to 2/1
obj = getClosestStandardNoteValue(obj[0] / obj[1]);
// Since we are going to add action block in the front later
if (k != 0) val = val + 2;
let pitches = getPitch(val + 5, notes, val);
currentActionBlock.push(
[val, ["newnote", { "collapsed": true }], 0, 0, [first ? val - 2 : val - 1, val + 1, val + 4, val + pitches.length + 5]],
[val + 1, "divide", 0, 0, [val, val + 2, val + 3]],
[val + 2, ["number", { value: obj[0] }], 0, 0, [val + 1]],
[val + 3, ["number", { value: obj[1] }], 0, 0, [val + 1]],
[val + 4, "vspace", 0, 0, [val, val + 5]],
);
noteblockCount++;
pitches[0][4][0] = val + 4;
currentActionBlock = currentActionBlock.concat(pitches);
let newLen = jsONON.length + currentActionBlock.length;
if (k != 0) newLen = newLen + 2;
currentActionBlock.push(
[newLen, "hidden", 0, 0, [val, last ? null : newLen + 1]]
);
if (isLastNoteInBlock || isLastNoteInSched) {
addNewActionBlock(isLastNoteInSched);
}
if (totalnoteblockCount >= MAX_NOTEBLOCKS) {
activity.textMsg("MIDI file is too large.. Generating only 100 noteblocks");
stopProcessing = true;
break;
}
}
if (currentActionBlock.length > 0) {
addNewActionBlock(true);
}
trackCount++;
// console.log("current action block: ", currentActionBlock);
// console.log("current json: ", jsONON);
// console.log("noteblockCount: ", noteblockCount);
// console.debug('finished when you see: "block loading finished "');
document.body.style.cursor = "wait";
});
let len = jsONON.length;
let m = 0;
let actionIndex = 0;
for (let i = 0; i < trackCount; i++) {
let vspaceIndex = len + m + 6;
let startIndex = len + m;
let flag = true;
if (isPercussion[i]) {
jsONON.push(
[len + m, ["start", { collapsed: false }], 300 + offset, 100, [null, len + m + 14 + actionBlockPerTrack[i], null]],
[len + m + 1, "meter", 0, 0, [len + m + 14 + actionBlockPerTrack[i], len + m + 2, len + m + 3, len + m + 6]],
[len + m + 2, ["number", { value: currentMidiTimeSignature[0] }], 0, 0, [len + m + 1]],
[len + m + 3, "divide", 0, 0, [len + m + 1, len + m + 4, len + m + 5]],
[len + m + 4, ["number", { value: 1 }], 0, 0, [len + m + 3]],
[len + m + 5, ["number", { value: currentMidiTimeSignature[1] }], 0, 0, [len + m + 3]],
[len + m + 6, "vspace", 0, 0, [len + m + 1, len + m + 8 + actionBlockPerTrack[i]]],
[len + m + 7, "hidden", 0, 0, [len + m + 13 + actionBlockPerTrack[i], len + m + 8]],
);
flag = false;
m += 8;
} else {
jsONON.push(
[len + m, ["start", { collapsed: false }], 300 + offset, 100, [null, len + m + 16 + actionBlockPerTrack[i], null]],
[len + m + 1, "meter", 0, 0, [len + m + 16 + actionBlockPerTrack[i], len + m + 2, len + m + 3, len + m + 6]],
[len + m + 2, ["number", { value: currentMidiTimeSignature[0] }], 0, 0, [len + m + 1]],
[len + m + 3, "divide", 0, 0, [len + m + 1, len + m + 4, len + m + 5]],
[len + m + 4, ["number", { value: 1 }], 0, 0, [len + m + 3]],
[len + m + 5, ["number", { value: currentMidiTimeSignature[1] }], 0, 0, [len + m + 3]],
[len + m + 6, "vspace", 0, 0, [len + m + 1, len + m + 10 + actionBlockPerTrack[i]]],
[len + m + 7, "settimbre", 0, 0, [len + m + 15 + actionBlockPerTrack[i], len + m + 8, len + m + 10, len + m + 9]],
[len + m + 8, ["voicename", { value: instruments[i] }], 0, 0, [len + m + 7]],
[len + m + 9, "hidden", 0, 0, [len + m + 7, null]]
);
m += 10;
}
for (let j = 0; j < actionBlockPerTrack[i]; j++) {
jsONON.push(
[len + m, ["nameddo", { value: actionBlockNames[actionIndex] }], 0, 0, [flag ? len + m - 3 : len + m - 1, len + m + 1]]
);
m++;
flag = false;
actionIndex++;
}
jsONON[len + m - 1][4][1] = null;
let setBpmIndex = jsONON.length;
jsONON.push(
[setBpmIndex, ["setbpm3"], 0, 0, [vspaceIndex, setBpmIndex + 1, setBpmIndex + 2, setBpmIndex + 5]],
[setBpmIndex + 1, ["number", { value: currentMidiTempoBpm }], 0, 0, [setBpmIndex]],
[setBpmIndex + 2, "divide", 0, 0, [setBpmIndex, setBpmIndex + 3, setBpmIndex + 4]],
[setBpmIndex + 3, ["number", { value: 1 }], 0, 0, [setBpmIndex + 2]],
[setBpmIndex + 4, ["number", { value: currentMidiTimeSignature[1] }], 0, 0, [setBpmIndex + 2]],
[setBpmIndex + 5, "vspace", 0, 0, [setBpmIndex, vspaceIndex + 1]],
);
m += 6;
jsONON.push(
[len + m, "setturtlename2", 0, 0, [startIndex, len + m + 1, startIndex + 1]],
[len + m + 1, ["text", { value: `track${i}` }], 0, 0, [len + m]]
);
m += 2;
}
activity.blocks.loadNewBlocks(jsONON);
// this.textMsg("MIDI import is not currently precise. Consider changing the speed with the Beats Per Minute block or modifying note value with the Multiply Note Value block");
return null;
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = { getClosestStandardNoteValue, transcribeMidi };
}