Midi
Midi copied to clipboard
Downloading midi file without Node
I am using Javascript in a browser where I can not import javascript.fs
Is there another way I can re-download the midi file without using fs, or is there a way to use fs in the browser without Node?
@dassoop
install browserify version of buffer
npm install buffer
function exportMidiToBlob(midi: Midi) {
const midiBuffer = Buffer.from(midi.toArray());
const fileData = new Blob([midiBuffer], {
type: 'audio/mid'
});
return fileData;
}
function downloadFile(fileName: string, fileContent: Blob) {
const url = window.URL.createObjectURL(fileContent);
const a = document.createElement('a');
a.href = url;
a.download = `${fileName}`;
a.click();
window.URL.revokeObjectURL(url);
}
const midi = new Midi();
const midiBlob = exportMidiToBlob(midi);
downloadFile(`my-midi-file.mid`, midiBlob);
Update: Nevermind, I got it to work without any Buffer buffoonery:
const buf = midi.toArray();
const blob = new Blob([buf], {
type: "audio/mid",
});
~@allandiego Hi! Any idea of a way to do this without using browserify and buffer? I'm looking for a simple way to convert Midi to Blob and it's a bit annoying having to jump through so many hoops (with browserify and npm). Are there any simple web APIs substitutes (ArrayBuffer or something)?~