Midi icon indicating copy to clipboard operation
Midi copied to clipboard

Downloading midi file without Node

Open dassoop opened this issue 3 years ago • 2 comments

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 avatar Jan 12 '23 00:01 dassoop

@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);

allandiego avatar Feb 23 '23 15:02 allandiego

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)?~

TrebledJ avatar May 06 '23 10:05 TrebledJ