-
-
Notifications
You must be signed in to change notification settings - Fork 12
MIDI Class
This is the module responsible for parsing MIDI files into objects readable by Sequencer
class.
import {MIDI} from "./spessasynth_lib/midi_parser/midi_loader.js";
const parsedMIDI = new MIDI(arrayBuffer);
- arrayBuffer - an
arrayBuffer
anstance of the midi file.
The amount of tracks in the file.
console.log(`this file has ${parsedMIDI.tracksAmount}`);
The time division of the midi file. Used by the Sequencer
class
console.log(`this sequence's time division is ${parsedMIDI.timeDivision}`);
The sequence's name. The first track's Track Name
's event text.
console.log(`This sequence is named "${parsedMIDI.midiName}"`);
The decoded copyright and description of the file. Also includes the Sound Canvas display messages.
console.log(`Midi file description: ${parsedMIDI.copyright}`);
Ordered from last to first, all the tempo changes in the file. Will always contain at least 1 tempo (the default 120BPM).
[
{
tempo: /* tempo in BPM */,
ticks: /* absolute amount of MIDI Ticks from the start */
},
/*...*/
{
tempo: 120,
ticks: 0
}
];
The points of the loop detected in the MIDI file in ticks. If there's nothing detected, the loop will start from the first NoteOn event and end will be the tick length of the file. Current looping detection is: CC 2/4, 116/117 and "start", "loopStart" and "loopEnd" markers.
console.log(parsedMIDI.loop); // {start: 1294, end: 49573}
The tick number of the first noteOn event in the sequence. Can be used to skip the initial silence.
console.log(parsedMIDI.firstNoteOn); // 1294
The sequence's duration in seconds.
console.log(parsedMIDI.duration); // 125.64;
The detected midi ports for each track. Each port represents a batch of 16 channels.
console.log(parsedMIDI.midiPorts); // [0, 0, 0, 1, 1, 2, ...]
The actual MIDI sequence data. Described below.
The file is stored as an array of tracks, accesible via parsedMIDI.tracks
.
Each track is an array of events.
Each event is a MidiMessage
class, which is defined as follows;
class MidiMessage
{
constructor(ticks, byte, data) {
this.ticks = ticks;
this.messageStatusByte = byte;
this.messageData = data;
}
}
- ticks - absolute amount of MIDI Ticks from the start of the track.
- messageStatusByte - the status byte of the message as a number from 0 to 255. Learn more here and here.
Important
Note that for Meta Events, the status byte is the SECOND status byte, not the 0xFF!
- messageData - a
ShiftableByteArray
instance of the event's data.
Renders the sequence as a .mid file.
writeMIDIFile(midi);
- midi - the
MIDI
instance to export.
The returned value is an Uint8Array
- a binary representation of the .mid file.
Applies a SynthesizerSnapshot to the sequence in place. This means changing the programs and controllers if they are locked.
applySnapshotToMIDI(midi, snapshot);
- midi - the
MIDI
instance to modify. - snapshot - the
SynthesizerSnapshot
to use.
For example if channel 1 has locked preset on Drawbar Organ
,
this will remove all program changes for channel 1 and add one at the start to change the program to Drawbar organ
.
Tip
If you encounter any errors in this documentation, please open an issue!
Warning
Make sure you always update worklet_processor.min.js
along with the npm package!