Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloading midi file without Node #170

Open
dassoop opened this issue Jan 12, 2023 · 2 comments
Open

Downloading midi file without Node #170

dassoop opened this issue Jan 12, 2023 · 2 comments

Comments

@dassoop
Copy link

dassoop commented Jan 12, 2023

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?

@allandiego
Copy link

allandiego commented Feb 23, 2023

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

@TrebledJ
Copy link

TrebledJ commented May 6, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants