Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MIDI.js note duration does not change

As the documentation for mudcube MIDI.js seems to be lacking in this regard, I'm hoping there is someone who might be able to point me in the right direction. To put it plainly, using midi.js, how do you adjust the duration and ending of midi notes?

For some background, I've created a music staff widget with draggable and droppable notes (jQuery-ui), that can be added or removed, to form sequences which can be called upon to playback midi. Everything works thus far as it should, however, as I'm trying to expand the functionality of this widget with more intricate timing, I've run into some issues.

It seems that I can only play the notes at a set duration and, for all my efforts, haven't been able to implement changes that could vary this duration to say, half notes, quarter notes, eight notes, etc. Also, and this may be one of the causes, MIDI.noteOff has no perceivable effect on the output. One would think that a given note would play indefinitely until MIDI.noteOff is called, however, said note will play and then trail off regardless of whether noteOff is called.

Let me say here that I have done extensive research on SO and the web in general to get to the bottom of this. I have found people with similar problems and applied their fixes but their answers do not work for me. See how do I play arbitrary notes in Midi with javascript and MIDI.noteOff does not seem to work reliably

I have a sequence of notes called noteSequence where each note is an object with noteValue (i.e. 'C4') and noteDuration properties that are converted into a midiSequence in the function convertToMidiSequence. Everything works well with melodic sequences and chords, I just want to have the duration functionality added. I should also say that adjusting the 'delay' variable or the noteDuration property only adds to the time before the sequence is played. Then after the initial delay, the sequence plays with the same timing as before.

Thank you for taking some time to look this over and hopefully help out. It would be very much appreciated.

Here is the applicable code that runs my MIDI:

$(document).ready(function() {
  MIDI.loadPlugin({
    soundfontUrl: GLOBALS.SOUNDFONT_URL,
    instrument: 'acoustic_grand_piano',
    callback: function() {
      var channel = 0,
          instrument = 0,
          velocity = 127,
          delay = 1;
      MIDI.programChange(0, instrument);
    }
  });
});

function playCurrentSequence() {
  playSequence(noteSequence);
}

function playSequence(sequence) {
  var midiSequence = convertToMidiSequence(sequence);
  for (var i = 0; i < midiSequence.length; i++) {
    if (!(midiSequence[i] instanceof Array)) {
      playNote(midiSequence[i], i);
    }
    else {
      playChord(midiSequence[i], i);
    }
  }
}

function playNote(midiNote, notePositionInMeasure) {
  var channel = 0,
      velocity = 127,
      delay = 1;
  MIDI.noteOn(channel, midiNote, velocity, delay + notePositionInMeasure + 1);
  MIDI.noteOff(channel, midiNote,
    delay + midiNote.noteDuration + notePositionInMeasure + 1);
  MIDI.Player.stop();
}

function playChord(midiChord, notePositionInMeasure) {
  var channel = 0,
      velocity = 127,
      delay = 1;
  for (var i = 0; i < midiChord.length; i++) {
    MIDI.noteOn(channel, midiChord[i], velocity, delay + notePositionInMeasure + 1);
    MIDI.noteOff(channel, midiChord[i],
      delay + midiChord[i].noteDuration + notePositionInMeasure + 1);
    MIDI.Player.stop();
  }
}
like image 296
catch22 Avatar asked Nov 11 '22 14:11

catch22


1 Answers

It's worth pointing out what a MIDI NoteOff actually means - typically it is for the note that has previously received a NoteOn to enter the release state of its ADSR envelope, and eventually tail away to nothing. It doesn't release the channel until some time later.

Now in the case of most percussion voices, there isn't a concept of NoteOff per-se - they just decay to nothing after the initial attack in the sustain phase (a bit of misnomer). Note-off on a piano applies the damper, but the sound could already have died away to nothing at this point.

Other voices may be straight sample triggers with no concept of NoteOff.

The behaviour is therefore not consistent and depends hugely on the voice you have selected. Have you verified that NoteOffs are actually meaningful for it?

like image 103
marko Avatar answered Nov 14 '22 23:11

marko