How can i get my stored midi notes to play back without sounding sustained like in this video : https://youtu.be/zc1zbwheyE0?t=2m30s. Here is some of my code:

// Callback function to for recording note played is called upon reception of a NoteOn.
void MyHandleNoteOn(byte channel, byte pitch, byte velocity) { 

if ((currentState != STATE_R) || (noteIndex >= MAX_NOTE))

  // Record the note
  noteList[noteIndex].channel = channel;
  noteList[noteIndex].pitch = pitch;
  noteList[noteIndex].velocity = velocity;
  noteList[noteIndex].deltaT = millis() - lastNoteTime;

  lastNoteTime = millis();


 void StateMachine(void)
  static uint16_t  nextNote;

MIDI.read();   // keep this going as fast as possible and empty buffer all the time

  switch (currentState)
    case STATE_I2P:    // Set up for playback

      nextNote = 0;
      lastNoteTime = millis();

    case STATE_P:      // Play the notes back
  lcd.print("Playing Recorded Song");
  if (nextNote >= noteIndex) SwitchState(STATE_P2I);  // we are done

       if ((millis() - lastNoteTime) >= noteList[nextNote].deltaT)
         MIDI.sendNoteOn(noteList[nextNote].pitch, noteList[nextNote].velocity, 1); //noteList[nextNote].channel);  // Send the note
         lastNoteTime = millis();

You need to store and send out the 'note off' messages in addition to the 'note on' messages.

  • how do i add stored midi for note off to what i already have ? am not sure how i would implement that. if the note is on then off wouldn't the note not sound?
    – Hjhk90
    Apr 14 '17 at 9:15

A MIDI stream is far more than just a list of notes to play. It's a set of instructions to the musical instrument on exactly how to play the music.

Take the typical MIDI keyboard, for example. When you press a note a "Note On" message is generated. This contains the note number (pitch) of the note to play along with the velocity (volume) of the note. It's called velocity because it's derived from how fast the key travels between two points - the faster it travels the harder you must have hit it and so the louder it should sound.

Then when you release the key a "Note Off" message is generated. This tells the target to stop playing the note.

Normally if you get two "Note On" messages for the same note without a "Note Off" for that note in between the new "Note On" replaces the old one, and a subsequent "Note Off" will stop all sound for that note, not just the most recent "Note On".

As well as those there are other messages as well detailing various other aspects of the music - patch and bank (instrument) selection, pitch bend, sustain, after-touch, etc. All of these are important to the overall sound of the music. And then there are "SysEx" messages (System Exclusive) which are (to a large extent) exclusive to the system that is generating them (hence the name). To faithfully recreate the music all of these should be stored as well.

The bare minimum though would be to store both the "Note On" and "Note Off" and maybe the velocity. How you store them is up to you, but both of them are required.

In your existing code you would need to expand the structure of your array to include what the instruction is. Not just assume it's all "Note On", but store whether the instruction is a "Note On" or a "Note Off", so when you play the MIDI back you can send the correct sequence of On and Off for the notes.

  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Nick Gammon
    Apr 15 '17 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.