♬ MidiWriterJS
+ +MidiWriterJS is a JavaScript library providing an API for generating expressive multi-track MIDI files.
Note that the master
branch is in active development so if you're looking for a tried and true stable version please use the latest release.
VexFlow Integration
diff --git a/docs/meta-events_controller-change-event.js.html b/docs/meta-events_controller-change-event.js.html index e9b5d18..22a9c87 100644 --- a/docs/meta-events_controller-change-event.js.html +++ b/docs/meta-events_controller-change-event.js.html @@ -24,7 +24,7 @@
@@ -68,7 +68,7 @@
diff --git a/docs/meta-events_copyright-event.js.html b/docs/meta-events_copyright-event.js.html index 1cc1839..d0b8e4b 100644 --- a/docs/meta-events_copyright-event.js.html +++ b/docs/meta-events_copyright-event.js.html @@ -24,7 +24,7 @@
meta-events/controller-change-event.js
diff --git a/docs/meta-events_copyright-event.js.html b/docs/meta-events_copyright-event.js.html index 1cc1839..d0b8e4b 100644 --- a/docs/meta-events_copyright-event.js.html +++ b/docs/meta-events_copyright-event.js.html @@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@
diff --git a/docs/meta-events_cue-point-event.js.html b/docs/meta-events_cue-point-event.js.html index e489fd9..7583104 100644 --- a/docs/meta-events_cue-point-event.js.html +++ b/docs/meta-events_cue-point-event.js.html @@ -24,7 +24,7 @@
meta-events/copyright-event.js
diff --git a/docs/meta-events_cue-point-event.js.html b/docs/meta-events_cue-point-event.js.html index e489fd9..7583104 100644 --- a/docs/meta-events_cue-point-event.js.html +++ b/docs/meta-events_cue-point-event.js.html @@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@
diff --git a/docs/meta-events_end-track-event.js.html b/docs/meta-events_end-track-event.js.html index 72ca246..22b2a1a 100644 --- a/docs/meta-events_end-track-event.js.html +++ b/docs/meta-events_end-track-event.js.html @@ -24,7 +24,7 @@
meta-events/cue-point-event.js
diff --git a/docs/meta-events_end-track-event.js.html b/docs/meta-events_end-track-event.js.html index 72ca246..22b2a1a 100644 --- a/docs/meta-events_end-track-event.js.html +++ b/docs/meta-events_end-track-event.js.html @@ -24,7 +24,7 @@
@@ -71,7 +71,7 @@
diff --git a/docs/meta-events_instrument-name-event.js.html b/docs/meta-events_instrument-name-event.js.html index 654a92e..99abb6e 100644 --- a/docs/meta-events_instrument-name-event.js.html +++ b/docs/meta-events_instrument-name-event.js.html @@ -24,7 +24,7 @@
meta-events/end-track-event.js
diff --git a/docs/meta-events_instrument-name-event.js.html b/docs/meta-events_instrument-name-event.js.html index 654a92e..99abb6e 100644 --- a/docs/meta-events_instrument-name-event.js.html +++ b/docs/meta-events_instrument-name-event.js.html @@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@
diff --git a/docs/meta-events_key-signature-event.js.html b/docs/meta-events_key-signature-event.js.html index b9bec5d..517297d 100644 --- a/docs/meta-events_key-signature-event.js.html +++ b/docs/meta-events_key-signature-event.js.html @@ -24,7 +24,7 @@
meta-events/instrument-name-event.js
diff --git a/docs/meta-events_key-signature-event.js.html b/docs/meta-events_key-signature-event.js.html index b9bec5d..517297d 100644 --- a/docs/meta-events_key-signature-event.js.html +++ b/docs/meta-events_key-signature-event.js.html @@ -24,7 +24,7 @@
@@ -117,7 +117,7 @@
diff --git a/docs/meta-events_lyric-event.js.html b/docs/meta-events_lyric-event.js.html index 5a01aa2..982afed 100644 --- a/docs/meta-events_lyric-event.js.html +++ b/docs/meta-events_lyric-event.js.html @@ -24,7 +24,7 @@
meta-events/key-signature-event.js
diff --git a/docs/meta-events_lyric-event.js.html b/docs/meta-events_lyric-event.js.html index 5a01aa2..982afed 100644 --- a/docs/meta-events_lyric-event.js.html +++ b/docs/meta-events_lyric-event.js.html @@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@
diff --git a/docs/meta-events_marker-event.js.html b/docs/meta-events_marker-event.js.html index 9546a49..da9f1bc 100644 --- a/docs/meta-events_marker-event.js.html +++ b/docs/meta-events_marker-event.js.html @@ -24,7 +24,7 @@
meta-events/lyric-event.js
diff --git a/docs/meta-events_marker-event.js.html b/docs/meta-events_marker-event.js.html index 9546a49..da9f1bc 100644 --- a/docs/meta-events_marker-event.js.html +++ b/docs/meta-events_marker-event.js.html @@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@
diff --git a/docs/meta-events_pitch-bend-event.js.html b/docs/meta-events_pitch-bend-event.js.html index c32f691..7d00744 100644 --- a/docs/meta-events_pitch-bend-event.js.html +++ b/docs/meta-events_pitch-bend-event.js.html @@ -24,7 +24,7 @@
meta-events/marker-event.js
diff --git a/docs/meta-events_pitch-bend-event.js.html b/docs/meta-events_pitch-bend-event.js.html index c32f691..7d00744 100644 --- a/docs/meta-events_pitch-bend-event.js.html +++ b/docs/meta-events_pitch-bend-event.js.html @@ -24,7 +24,7 @@
@@ -82,7 +82,7 @@
diff --git a/docs/meta-events_program-change-event.js.html b/docs/meta-events_program-change-event.js.html index 5867e79..317f4a2 100644 --- a/docs/meta-events_program-change-event.js.html +++ b/docs/meta-events_program-change-event.js.html @@ -24,7 +24,7 @@
meta-events/pitch-bend-event.js
diff --git a/docs/meta-events_program-change-event.js.html b/docs/meta-events_program-change-event.js.html index 5867e79..317f4a2 100644 --- a/docs/meta-events_program-change-event.js.html +++ b/docs/meta-events_program-change-event.js.html @@ -24,7 +24,7 @@
@@ -68,7 +68,7 @@
diff --git a/docs/meta-events_tempo-event.js.html b/docs/meta-events_tempo-event.js.html index 39418f3..11b2d77 100644 --- a/docs/meta-events_tempo-event.js.html +++ b/docs/meta-events_tempo-event.js.html @@ -24,7 +24,7 @@
meta-events/program-change-event.js
diff --git a/docs/meta-events_tempo-event.js.html b/docs/meta-events_tempo-event.js.html index 39418f3..11b2d77 100644 --- a/docs/meta-events_tempo-event.js.html +++ b/docs/meta-events_tempo-event.js.html @@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@
diff --git a/docs/meta-events_text-event.js.html b/docs/meta-events_text-event.js.html index 20b1963..05b28da 100644 --- a/docs/meta-events_text-event.js.html +++ b/docs/meta-events_text-event.js.html @@ -24,7 +24,7 @@
meta-events/tempo-event.js
diff --git a/docs/meta-events_text-event.js.html b/docs/meta-events_text-event.js.html index 20b1963..05b28da 100644 --- a/docs/meta-events_text-event.js.html +++ b/docs/meta-events_text-event.js.html @@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@
diff --git a/docs/meta-events_time-signature-event.js.html b/docs/meta-events_time-signature-event.js.html index 8c4c3a5..aacdcfa 100644 --- a/docs/meta-events_time-signature-event.js.html +++ b/docs/meta-events_time-signature-event.js.html @@ -24,7 +24,7 @@
meta-events/text-event.js
diff --git a/docs/meta-events_time-signature-event.js.html b/docs/meta-events_time-signature-event.js.html index 8c4c3a5..aacdcfa 100644 --- a/docs/meta-events_time-signature-event.js.html +++ b/docs/meta-events_time-signature-event.js.html @@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@
diff --git a/docs/meta-events_track-name-event.js.html b/docs/meta-events_track-name-event.js.html index 4163dbf..ef1411b 100644 --- a/docs/meta-events_track-name-event.js.html +++ b/docs/meta-events_track-name-event.js.html @@ -24,7 +24,7 @@
meta-events/time-signature-event.js
diff --git a/docs/meta-events_track-name-event.js.html b/docs/meta-events_track-name-event.js.html index 4163dbf..ef1411b 100644 --- a/docs/meta-events_track-name-event.js.html +++ b/docs/meta-events_track-name-event.js.html @@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@
diff --git a/docs/note-events_note-event.js.html b/docs/note-events_note-event.js.html index 0be7dde..6a83159 100644 --- a/docs/note-events_note-event.js.html +++ b/docs/note-events_note-event.js.html @@ -24,7 +24,7 @@
meta-events/track-name-event.js
diff --git a/docs/note-events_note-event.js.html b/docs/note-events_note-event.js.html index 0be7dde..6a83159 100644 --- a/docs/note-events_note-event.js.html +++ b/docs/note-events_note-event.js.html @@ -24,7 +24,7 @@
@@ -211,7 +211,7 @@
diff --git a/docs/note-events_note-off-event.js.html b/docs/note-events_note-off-event.js.html index b33396d..d8169c1 100644 --- a/docs/note-events_note-off-event.js.html +++ b/docs/note-events_note-off-event.js.html @@ -24,7 +24,7 @@
note-events/note-event.js
diff --git a/docs/note-events_note-off-event.js.html b/docs/note-events_note-off-event.js.html index b33396d..d8169c1 100644 --- a/docs/note-events_note-off-event.js.html +++ b/docs/note-events_note-off-event.js.html @@ -24,7 +24,7 @@
@@ -111,7 +111,7 @@
diff --git a/docs/note-events_note-on-event.js.html b/docs/note-events_note-on-event.js.html index e3ddfab..649a21d 100644 --- a/docs/note-events_note-on-event.js.html +++ b/docs/note-events_note-on-event.js.html @@ -24,7 +24,7 @@
note-events/note-off-event.js
diff --git a/docs/note-events_note-on-event.js.html b/docs/note-events_note-on-event.js.html index e3ddfab..649a21d 100644 --- a/docs/note-events_note-on-event.js.html +++ b/docs/note-events_note-on-event.js.html @@ -24,7 +24,7 @@
@@ -125,7 +125,7 @@
diff --git a/docs/track.js.html b/docs/track.js.html index ab97650..c1aa21c 100644 --- a/docs/track.js.html +++ b/docs/track.js.html @@ -24,7 +24,7 @@
note-events/note-on-event.js
diff --git a/docs/track.js.html b/docs/track.js.html index ab97650..c1aa21c 100644 --- a/docs/track.js.html +++ b/docs/track.js.html @@ -24,7 +24,7 @@
@@ -383,7 +383,7 @@
diff --git a/docs/utils.js.html b/docs/utils.js.html index 2154551..6aa4a9e 100644 --- a/docs/utils.js.html +++ b/docs/utils.js.html @@ -24,7 +24,7 @@
track.js
diff --git a/docs/utils.js.html b/docs/utils.js.html index 2154551..6aa4a9e 100644 --- a/docs/utils.js.html +++ b/docs/utils.js.html @@ -24,7 +24,7 @@
@@ -303,7 +303,7 @@
diff --git a/docs/vexflow.js.html b/docs/vexflow.js.html index c31fb66..8431c73 100644 --- a/docs/vexflow.js.html +++ b/docs/vexflow.js.html @@ -24,7 +24,7 @@
utils.js
diff --git a/docs/vexflow.js.html b/docs/vexflow.js.html index c31fb66..8431c73 100644 --- a/docs/vexflow.js.html +++ b/docs/vexflow.js.html @@ -24,7 +24,7 @@
@@ -48,14 +48,14 @@
diff --git a/docs/writer.js.html b/docs/writer.js.html index c3d659e..c6bb5ee 100644 --- a/docs/writer.js.html +++ b/docs/writer.js.html @@ -24,7 +24,7 @@
vexflow.js
* Support for converting VexFlow voice into MidiWriterJS track * @return MidiWriter.Track object */ - trackFromVoice(voice) { + trackFromVoice(voice, options={addRenderedAccidentals: false}) { const track = new Track(); let wait = []; voice.tickables.forEach(tickable => { if (tickable.noteType === 'n') { track.addEvent(new NoteEvent({ - pitch: tickable.keys.map(this.convertPitch), + pitch: tickable.keys.map((pitch, index) => this.convertPitch(pitch, index, tickable, options.addRenderedAccidentals)), duration: this.convertDuration(tickable), wait })); @@ -81,9 +81,31 @@vexflow.js
/** * Converts VexFlow pitch syntax to MidiWriterJS syntax * @param pitch string + * @param index pitch index + * @param note struct from Vexflow + * @param addRenderedAccidentals adds Vexflow rendered accidentals */ - convertPitch(pitch) { - return pitch.replace('/', ''); + convertPitch(pitch, index, note, addRenderedAccidentals=false) { + // Splits note name from octave + const pitchParts = pitch.split('/'); + + // Retrieves accidentals from pitch + // Removes natural accidentals since they are not accepted in Tonal Midi + let accidentals = pitchParts[0].substring(1).replace('n', ''); + + if (addRenderedAccidentals) { + note.getAccidentals()?.forEach(accidental => { + if (accidental.index === index) { + if (accidental.type === 'n') { + accidentals = ''; + } else { + accidentals += accidental.type; + } + } + }); + } + + return pitchParts[0][0] + accidentals + pitchParts[1]; } /** @@ -91,18 +113,25 @@vexflow.js
* @param note struct from VexFlow */ convertDuration(note) { - switch (note.duration) { + return 'd'.repeat(note.dots) + this.convertBaseDuration(note.duration) + (note.tuplet ? 't' + note.tuplet.num_notes : ''); + } + + /** + * Converts VexFlow base duration syntax to MidiWriterJS syntax + * @param duration Vexflow duration + * @returns MidiWriterJS duration + */ + convertBaseDuration(duration) { + switch (duration) { case 'w': return '1'; case 'h': - return note.isDotted() ? 'd2' : '2'; + return '2'; case 'q': - return note.isDotted() ? 'd4' : '4'; - case '8': - return note.isDotted() ? 'd8' : '8'; + return '4'; + default: + return duration; } - - return note.duration; } } @@ -119,7 +148,7 @@vexflow.js
diff --git a/docs/writer.js.html b/docs/writer.js.html index c3d659e..c6bb5ee 100644 --- a/docs/writer.js.html +++ b/docs/writer.js.html @@ -24,7 +24,7 @@
@@ -98,18 +98,6 @@