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

NoteEvent chord duration? #55

Closed leegee closed 4 years ago

leegee commented 5 years ago

I find that setting NoteEvent's duration parameter to Tn, where n > 1, results in only some of the notes of the chord being sustained.

Am I doing something wrong?


const track = new MidiWriter.Track();

track.addTrackName('Colours ' + this._inputFilename);
track.setTimeSignature(1, 1);

track.addEvent(
    new MidiWriter.NoteEvent({
        velocity: 1,
        duration: 'T3', // 'T15', // 'T1584'
        pitch: ['D4', 'F4', 'A4'],
        startTick: 0
    })
);

new MidiWriter.Writer(track)
    .saveMIDI('temp');
leegee commented 5 years ago

Digging into the build folder (not checkout yet), I found the following code in NoteEvent.buildData:

    this.pitch.forEach(function (p, i) {
        console.log('p', p);
        if (i == 0) {
            //noteOff = new NoteOffEvent({data: Utils.numberToVariableLength(tickDuration).concat(this.getNoteOffStatus(), Utils.getPitch(p), Utils.convertVelocity(this.velocity))});
            var noteOffNew = new NoteOffEvent({
            channel: _this.channel,
            duration: _this.duration,
            velocity: _this.velocity,
            pitch: p,
            noteOnTick: _this.startTick
            });
        } else {
            // Running status (can ommit the note off status)
            //noteOff = new NoteOffEvent({data: [0, Utils.getPitch(p), Utils.convertVelocity(this.velocity)]});
            var noteOffNew = new NoteOffEvent({
            channel: _this.channel,
            duration: _this.duration, // 0,
            velocity: _this.velocity,
            pitch: p,
            noteOnTick: _this.startTick
            });
        }

The line duration: _this.duration, // 0, is where I made a change that fixes the issue -- but I have on idea what other issues is may introduce. If I have time, I'll checkout the code and try adding a test.

leegee commented 5 years ago

Can't seem to get any of the npm scripts running at first try....

grimmdude commented 5 years ago

Hi @leegee,

Thanks for your message, I see what you mean. I will take a look at this as soon as I get a chance.

-Garrett

grimmdude commented 4 years ago

This should be fixed with 1.7.4 👍