grimmdude / MidiWriterJS

♬ A JavaScript library which provides an API for programmatically generating and creating expressive multi-track MIDI files and JSON.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to create a midi file

jepcolombia opened this issue · comments

I would appreciate an example on how to write a midi file or do we have to use an extra library?

commented

Hello @jepcolombia,

this feature was removed in version 2.0.0. Depending on the context it could be achieved in different ways. To answer your question: yes, it is beyond the scope of this library.

Feel free to give more context and you could get a few hints on how to do that.

thanks

Release 2.0.0: you might already find something in previous releases

[edit] better english hopefully[/edit]

It is pretty straightforward to write out the file, thankfully.

You basically do this:

const buffer = new Buffer.from(w.buildFile());
fs.writeFileSync('song.mid', buffer, function (err) {
    if (err) throw err;
});

with w being a new MidiWriter( ... ).

I can provide a full example if needed.

Trying to use the example provided bu a syntax error appears Undefined ¨Buffer ¨. do I need to add something else?
Thanks in advance

Apologies, here's a full example.

const MidiWriter = require('midi-writer-js')
const fs = require('fs');

const track = new MidiWriter.Track();
track.addEvent([
    new MidiWriter.NoteEvent({ pitch: ['E4', 'D4'], duration: '4' }),
    new MidiWriter.NoteEvent({ pitch: ['C4'], duration: '2' }),
    new MidiWriter.NoteEvent({ pitch: ['E4', 'D4'], duration: '4' }),
    new MidiWriter.NoteEvent({ pitch: ['C4'], duration: '2' }),
    new MidiWriter.NoteEvent({ pitch: ['C4', 'C4', 'C4', 'C4', 'D4', 'D4', 'D4', 'D4'], duration: '8' }),
    new MidiWriter.NoteEvent({ pitch: ['E4', 'D4'], duration: '4' }),
    new MidiWriter.NoteEvent({ pitch: ['C4'], duration: '2' })
], function (event, index) {
    return { sequential: true };
}
);

const write = new MidiWriter.Writer(track);
console.log(write.dataUri());

const buffer = new Buffer.from(write.buildFile());
fs.writeFileSync('song.mid', buffer, function (err) {
    if (err) throw err;
});

This is NodeJS, so you have to install it first with npm install midi-writer-js.

Thanks for your help @lplume & @dirkk0 🙏