Skip to content

Delay in noteOn() can be arbitrarily offset #168

@espadrine

Description

@espadrine

Even for something as simple as the basic example which plays a single note, we can notice the following by playing around in the JS console:

var wac = MIDI.getContext()
wac.currentTime  // 26.485333333333333
wac.currentTime  // 27.68

The WebAudioContext's currentTime is perpetually increasing, starting from a value of zero. So far so good.

However, the following line of code, present since the very beginning, will negatively impact the delay parameter of the noteOn() method:

/// convert relative delay to absolute delay
if (delay < ctx.currentTime) {
  delay += ctx.currentTime;
}

Consider the case where we play two notes, one with a delay of 2, the other with a delay of 4, with two calls to noteOn() at the same moment, when currentTime is 3. The first note will be played in 5 seconds (because of delay += ctx.currentTime), while the second will be played in 4 seconds (before the first)!

What is the point of modifying the delay? It is unclear to me what the difference between the relative delay and the absolute delay is, but the end result is that the following code gives results that are highly different from what I would assume it to do:

MIDI.noteOn(channel, note1, velocity, 2);
MIDI.noteOff(channel, note1, velocity, 4);
MIDI.noteOn(channel, note2, velocity, 4);
MIDI.noteOff(channel, note2, velocity, 6);

Could you help me understand and hopefully fix this issue in MIDI.js, be it a matter of documentation or a bug in the code?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions