Advertisement
Guest User

Untitled

a guest
Apr 30th, 2024
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function processMidiJson(json: any) {
  2.     console.log('Processing MIDI JSON:', json);
  3.     const jsonArray = convertArraysToObjectArrays(Object.values(json));
  4.     let notesArray: [string, number][] = [];
  5.     const tempo = json.tracks[0][1].setTempo.microsecondsPerQuarter; // Tempo in microseconds per quarter note
  6.     const division = json.division; // Division value from MIDI file
  7.     const ticksPerQuarterNote = tempo / division; // Ticks per quarter note
  8.  
  9.     for (let i = 0; i < jsonArray[2][1].length; i++) {
  10.         const currentObject = jsonArray[2][1][i];
  11.  
  12.         if (currentObject.noteOn) {
  13.             const durationTicks = calculateNoteDuration(jsonArray[2][1], i); // Calculate note duration in ticks
  14.             const durationMicroseconds = durationTicks * ticksPerQuarterNote; // Convert duration to microseconds
  15.             const durationMilliseconds = durationMicroseconds / 1000; // Convert duration to milliseconds
  16.             notesArray.push([String(currentObject.noteOn.noteNumber - 96), Math.floor(durationMilliseconds)]);
  17.         }
  18.     }
  19.  
  20.     console.log(notesArray);
  21.     return notesArray;
  22. }
  23.  
  24. // Function to calculate the duration of a note in ticks
  25. function calculateNoteDuration(track: any[], currentIndex: number): number {
  26.     let durationTicks = 0;
  27.     let i = currentIndex;
  28.  
  29.     // Start at the current note event and iterate until a noteOff event is found
  30.     while (i < track.length && !track[i].noteOff) {
  31.         durationTicks += track[i].delta; // Accumulate delta times
  32.         i++;
  33.     }
  34.  
  35.     durationTicks += track[i].delta; // Include the delta time of the noteOff event
  36.     return durationTicks;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement