I'm loading up a file from the browser and I can see its content starts with MThd
followed by plenty of garbbled unreadable characters.
Now, this is a MIDI .mid
file and I'd like to parse it with the Tonejs / midi library.
I thus coded the following service method:
import * as ToneMidi from '@tonejs/midi';
public parseRawMidi(rawMidiData: ArrayBuffer): Soundtrack {
const midi = new ToneMidi.Midi(rawMidiData);
}
But it gives me the error: ypeError: _tonejs_midi__WEBPACK_IMPORTED_MODULE_2__.Midi is not a constructor
I reckon this is because the rawMidiData
argument is not of the correct expected type by the constructor.
Indeed the Midi.d.ts
file shows:
constructor(midiArray?: (ArrayLike<number> | ArrayBuffer));
Here is my file uploader:
public onUpload(fileList: FileList): void {
const file = fileList[0];
const fileReader: FileReader = new FileReader();
fileReader.onloadend = (event: Event) => {
this.rawMidi = fileReader.result as ArrayBuffer;
// var array = new Uint8Array(new ArrayBuffer(rawLength));
const soundtrack: Soundtrack = this.midiService.parseRawMidi(this.rawMidi);
};
fileReader.readAsText(file);
}
UPDATE: A little experiement. I added the following source code:
const buffer = new ArrayBuffer(16);
console.log('Before the call to the constructor');
const midi = new ToneMidi.Midi(buffer);
console.log('and after');
The first logger was displayed, but not the second one, and the same error showed up.
UPDATE: I was using the wrong reader readAsText
and when switching to readAsArrayBuffer
as in fileReader.readAsArrayBuffer(file);
I could get an array buffer passed to the service method. But I still have the same issue of not resolving the constructor call: Midi is not a constructor
UPDATE: After the GitHub issue was resolved, I could upgrade to @tonejs/midi@2.0.5
with the npm install @tonejs/midi
command and the constructor then worked fine.