midiweb-midi

How to get the note and its velocity from MIDI event?


I can parse a MIDI file using the midi-parser-js library

and create a soundtrack object containing all the tracks and their events:

export class Soundtrack {
  name: string;
  duration: number;
  tracks: Array<Track>;
}
export class Track {
  events: Array<MidiEvent>;
}
export class MidiEvent {
  data: any;
  deltaTime: number;
  metaType: number;
  type: number;
}

I also have a working synth service to which I'd like to feed these MIDI notes:

synth.triggerAttack(note, null, velocity);
synth.triggerRelease(note);

How to filter the MIDI events that are notes ?

How to get the actual note and its velocity from such a MIDI event ?


Solution

  • This is how the notes are read from the file:

    ....
    MIDI.track[t-1].event[e-1].type = parseInt(statusByte[0], 16);// first byte is EVENT TYPE ID
    MIDI.track[t-1].event[e-1].channel = parseInt(statusByte[1], 16);// second byte is channel
    ...
    switch(MIDI.track[t-1].event[e-1].type){
    ...
    case 0x8:                                               // Note off
    case 0x9:                                               // Note On
        MIDI.track[t-1].event[e-1].data = [];
        MIDI.track[t-1].event[e-1].data[0] = file.readInt(1);
        MIDI.track[t-1].event[e-1].data[1] = file.readInt(1);
    

    First data byte is the note number, second byte is the velocity.