grimmdude / MidiPlayerJS

♬ MIDI parser & player engine for browser or Node. As a parser converts MIDI events into JSON. Works well with single or multitrack MIDI files.
https://grimmdude.com/MidiPlayerJS/
MIT License
357 stars 52 forks source link

Need help with NOTE_OFF #82

Closed oneandonlyonebutyou closed 2 years ago

oneandonlyonebutyou commented 2 years ago

I am trying to use the demo , but I cant find what I should do to the the Note is Off



  let playingNotes = {};
  const playNote = ({ instrument, noteName, noteKey }) => {
    if (!(noteKey in playingNotes)) {
      playingNotes[noteKey] = [];
    }
    playingNotes[noteKey].push(instrument.play(noteName));
  };
  const stopNote = ({ instrument, noteName, noteKey }) => {
   DONT KNOW WHAT TO DO .......

  };

  midiPlayer.on("midiEvent", midiEvent => {
    const action = midiEventsToNoteActions(midiEvent);
    if (action === null) return;
    const { payload, type } = action;
    const {
      instrumentName,
      noteName,
      key,
      channel
    } = payload;
    const instrument= getInstrumentByName(instruments, instrumentName)
    switch (type) {
      case "NOTE_ON": {
        console.log(`NOTE_ON ${noteName}`);
        playNote({
          instrument: instrument,
          noteName,
          noteKey: key, 
        })
      }
      case "NOTE_OFF": {
        console.log(`NOTE_OFF ${noteName}`);
        stopNote({
          instrument: instrument,
          noteName,
          noteKey: key
        })
      }
      default: {
        return;
      }
    }
  })
oneandonlyonebutyou commented 2 years ago

@grimmdude hey bro could you please help me on this

grimmdude commented 2 years ago

Hey @joseph-vedadi, usually for the Note off event you would release/stop the note that began playing from the corresponding Note on event.

For instruments that don't have a long sustain you can get away with not taking any action on the Note off event if you're happy letting the notes fully decay on their own (which is the case for the demo). It all kinda depends on your implementation.

Garrett

oneandonlyonebutyou commented 2 years ago

Thank you so much…