grimmdude / MidiWriterJS

♬ A JavaScript library which provides an API for programmatically generating and creating expressive multi-track MIDI files and JSON.
MIT License
547 stars 58 forks source link

How to create a midi file #115

Closed jepcolombia closed 9 months ago

jepcolombia commented 1 year ago

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

lplume commented 1 year ago

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]

dirkk0 commented 1 year ago

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.

jepcolombia commented 1 year ago

Thank you very much . If you can provide full example I would appreciate it very very much Best regards Jairo

On Mon, 8 May 2023 at 11:53 AM Dirk Krause @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/grimmdude/MidiWriterJS/issues/115#issuecomment-1538719462, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF7FUHYZZPGQMX4YQKRFED3XFEQITANCNFSM6AAAAAAX2CRI4E . You are receiving this because you were mentioned.Message ID: @.***>

jepcolombia commented 1 year ago

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

dirkk0 commented 1 year ago

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.

grimmdude commented 9 months ago

Thanks for your help @lplume & @dirkk0 🙏