Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function processMidiJson(json: any) {
- console.log('Processing MIDI JSON:', json);
- const jsonArray = convertArraysToObjectArrays(Object.values(json));
- let notesArray: [string, number][] = [];
- const tempo = json.tracks[0][1].setTempo.microsecondsPerQuarter; // Tempo in microseconds per quarter note
- const division = json.division; // Division value from MIDI file
- const ticksPerQuarterNote = tempo / division; // Ticks per quarter note
- for (let i = 0; i < jsonArray[2][1].length; i++) {
- const currentObject = jsonArray[2][1][i];
- if (currentObject.noteOn) {
- const durationTicks = calculateNoteDuration(jsonArray[2][1], i); // Calculate note duration in ticks
- const durationMicroseconds = durationTicks * ticksPerQuarterNote; // Convert duration to microseconds
- const durationMilliseconds = durationMicroseconds / 1000; // Convert duration to milliseconds
- notesArray.push([String(currentObject.noteOn.noteNumber - 96), Math.floor(durationMilliseconds)]);
- }
- }
- console.log(notesArray);
- return notesArray;
- }
- // Function to calculate the duration of a note in ticks
- function calculateNoteDuration(track: any[], currentIndex: number): number {
- let durationTicks = 0;
- let i = currentIndex;
- // Start at the current note event and iterate until a noteOff event is found
- while (i < track.length && !track[i].noteOff) {
- durationTicks += track[i].delta; // Accumulate delta times
- i++;
- }
- durationTicks += track[i].delta; // Include the delta time of the noteOff event
- return durationTicks;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement