Help with code for Midi legato playing and pitchbend

Hello to everyone, I am currently trying to program an arduino to send pitchbend messaged on particular notes and its actually working untill i try to play legato. For example as you can see in the midi monitor i play D3 then play D#3 while holding D3 and because there is no noteoff from D3 detected the pitch bend does not return to zero and the pitchbend has also affected D#3, I tried setting pitch bend for D#3 on zero but then if i go from D#3 to D pitchbend is not sent. Is there anybody that can help me please?

#include <MIDI.h>  // Din Midi Library

MIDI_CREATE_DEFAULT_INSTANCE();

 void setup() {

 MIDI.begin(MIDI_CHANNEL_OMNI);

 MIDI.setHandleNoteOn(MyHandleNoteOn); // This command
  // tells the Midi Library which function you want to call when a NOTE ON command
  // is received. In this case it's "MyHandleNoteOn".
 MIDI.setHandleNoteOff(MyHandleNoteOff); // This command tells the Midi Library 
  // to call "MyHandleNoteOff" when a NOTE OFF command is received.
  
  
}


void loop() {
    
    MIDI.read();
    
   
}     // end loop function


// MyHandleNoteON is the function that will be called by the Midi Library
// when a MIDI NOTE ON message is received.
// It will be passed bytes for Channel, Pitch, and Velocity

void MyHandleNoteOn(byte channel, byte pitch, byte velocity) { 
  
  if(pitch == 62)
  MIDI.sendPitchBend(-2949,channel);
  
}

// MyHandleNoteOFF is the function that will be called by the Midi Library
// when a MIDI NOTE OFF message is received.
// * A NOTE ON message with Velocity = 0 will be treated as a NOTE OFF message *
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOff(byte channel, byte pitch, byte velocity) { 
 
  if(pitch == 62)
  MIDI.sendPitchBend(0,channel);
  
  
}

You can only pitch bend an entire channel, you can't do it for individual notes.

PieterP: You can only pitch bend an entire channel, you can't do it for individual notes.

I know that , this is for 1 note playing at a time (mono) but that's why i'm thinking there may be a way i can detect whether the note has been turned off or is still pressed after another note off has been sent, for example like i said i play d3 and it sends pitchbend and then i play d#3 after with the d3 still held down and i tell it to pitchbend 0 amount on d3 so everything is fine untill here, its playing only D#3 with pitchbend zero, then when i release D#3 D2 will sound again but a note on is not sent because it was already pressed from before so no pitchbend is sent, im i making sense, so i guess i need something that tells the program that after the release of D#3 go to previous state with pitch bend -2949

pempi78: I know that , this is for 1 note playing at a time (mono) but that's why i'm thinking there may be a way i can detect whether the note has been turned off or is still pressed after another note off has been sent, for example like i said i play d3 and it sends pitchbend and then i play d#3 after with the d3 still held down and i tell it to pitchbend 0 amount on d3 so everything is fine untill here, its playing only D#3 with pitchbend zero

That is not what the picture of the log shows. It shows you send the pitchbend -2949 just before the D3 ON passes through, then you press and release D#3, and press and release C3, then send pitchbend 0 just before the D3 off passes through.

Does your output device allow Aftertouch (polyphonic key pressure) to be used for pitch control? That might allow you to pitch bend individual notes). Your sketch could add aftertouch for specific notes.

johnwasser:
That is not what the picture of the log shows. It shows you send the pitchbend -2949 just before the D3 ON passes through, then you press and release D#3, and press and release C3, then send pitchbend 0 just before the D3 off passes through.

Does your output device allow Aftertouch (polyphonic key pressure) to be used for pitch control? That might allow you to pitch bend individual notes). Your sketch could add aftertouch for specific notes.

ok well i’m just testing the code out so i added some more notes so its easier to understand, so as u can see from the code i just posted i only want pitchbend to send on the D3 note out of all the notes in a scale but now if i press C3(60) pitchbend is not sent, while holding C3(60) if press D3(62) pitchbend is sent which is what i want, now though, when i release D3(60) C3 Continues playing and has pitchbend amount from note D3 which i do not want, i have treid many different combinations of noteon and noteoff commands but a different problem arises when playing different notes in a different sequence. How does afterthouch work with pitchbend?

#include <MIDI.h>  // Din Midi Library

MIDI_CREATE_DEFAULT_INSTANCE();

 void setup() {

 MIDI.begin(MIDI_CHANNEL_OMNI);

 MIDI.setHandleNoteOn(MyHandleNoteOn); // This command
  // tells the Midi Library which function you want to call when a NOTE ON command
  // is received. In this case it's "MyHandleNoteOn".
 MIDI.setHandleNoteOff(MyHandleNoteOff); // This command tells the Midi Library 
  // to call "MyHandleNoteOff" when a NOTE OFF command is received.
  
  
}


void loop() {
    
    MIDI.read();
    
   
}     // end loop function


// MyHandleNoteON is the function that will be called by the Midi Library
// when a MIDI NOTE ON message is received.
// It will be passed bytes for Channel, Pitch, and Velocity

void MyHandleNoteOn(byte channel, byte pitch, byte velocity) { 
  
  if(pitch == 60)
  MIDI.sendPitchBend(0,channel);
  if(pitch == 62)
  MIDI.sendPitchBend(-2949,channel);
  if(pitch == 63)
  MIDI.sendPitchBend(0,channel);
  if(pitch == 65)
  MIDI.sendPitchBend(0,channel);
  if(pitch == 67)
  MIDI.sendPitchBend(0,channel);
  if(pitch == 69)
  MIDI.sendPitchBend(0,channel);
}

// MyHandleNoteOFF is the function that will be called by the Midi Library
// when a MIDI NOTE OFF message is received.
// * A NOTE ON message with Velocity = 0 will be treated as a NOTE OFF message *
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOff(byte channel, byte pitch, byte velocity) { 
 
}

I need to keep track of the order of notes played so note-off and pitchbend commands are treated correctly. I guess i need a note-off to retrigger the pitchbend for previously played notes if they have not been released yet. could this be done with an array listing active notes, last played notes? so the program knows when and not to send the pitchbend?

I don't think I understand what you mean.

If any note other than 62 is playing, PB = 0, if note 62 is playing, and all other notes are off PB = -2949. Is that a correct interpretation of your problem?

PieterP: I don't think I understand what you mean.

If any note other than 62 is playing, PB = 0, if note 62 is playing, and all other notes are off PB = -2949. Is that a correct interpretation of your problem?

Well that is a correct interpretation of what i would like to happen, but the actual problem is :

noteOn = 62 PB = -2949 noteOn = 63 PB = 0

so at this point i have pressed note 63 but have not released 62

note 63 is playing now with PB = 0

So now this is the problem:

noteOff = 63 so note 62 continues playing because noteOff was never sent because i am still holding it down so now it is playing note 62 with PB = 0 when it should be PB = -2949 and the reason is it started playing again without a noteOn being sent so i cannot resend PB = -2949 On NoteOn

Something like this maybe?

uint8_t otherNotesPlaying = 0;
bool note62playing = false;

void MyHandleNoteOn(byte channel, byte pitch, byte velocity) { 
  if(pitch == 62)
    note62playing = true;
  else
    otherNotesPlaying++;
  if (note62playing && otherNotesPlaying == 0)
    MIDI.sendPitchBend(-2949,channel);
  else 
    MIDI.sendPitchBend(0,channel);
}

void MyHandleNoteOff(byte channel, byte pitch, byte velocity) { 
 if(pitch == 62)
    note62playing = false;
  else
    otherNotesPlaying--;
  if (note62playing && otherNotesPlaying == 0)
    MIDI.sendPitchBend(-2949,channel);
  else 
    MIDI.sendPitchBend(0,channel);
}

PieterP: Something like this maybe?

uint8_t otherNotesPlaying = 0;
bool note62playing = false;

void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {   if(pitch == 62)     note62playing = true;   else     otherNotesPlaying++;   if (note62playing && otherNotesPlaying == 0)     MIDI.sendPitchBend(-2949,channel);   else     MIDI.sendPitchBend(0,channel); }

void MyHandleNoteOff(byte channel, byte pitch, byte velocity) { if(pitch == 62)     note62playing = false;   else     otherNotesPlaying--;   if (note62playing && otherNotesPlaying == 0)     MIDI.sendPitchBend(-2949,channel);   else     MIDI.sendPitchBend(0,channel); }

Peter i think that's half way there, that works when the starting note is 62 but now when i start say on note 63 or any other note for that matter and go down to note 62 without releasing 63 its not sending PB, thanks alot for your help by the way!

i added a line in the code and its working properly now i think, is there any other way of implementing this in your code?

void MyHandleNoteOn(byte channel, byte pitch, byte velocity) { 
  
  if(pitch == 62)
    note62playing = true;
  else
    otherNotesPlaying++;
  if (note62playing && otherNotesPlaying == 0)
    MIDI.sendPitchBend(-2949,channel);
  else 
    MIDI.sendPitchBend(0,channel);
  if (pitch == 62)
  MIDI.sendPitchBend(-2949,channel);
}

pempi78: Peter i think that's half way there, that works when the starting note is 62 but now when i start say on note 63 or any other note for that matter and go down to note 62 without releasing 63 its not sending PB, thanks alot for your help by the way!

What do you expect to happen then? Please specify all possible cases.

Well PB wasn't being sent when i was going to note 62 from any other note while the other note was being held down as well

PieterP: What do you expect to happen then? Please specify all possible cases.

If i use your code in an array will it allow for multiple notes to send PB?

PieterP: What do you expect to happen then? Please specify all possible cases.

so i modified the code a little to try with 2 notes and its playing perfectly except from one to the other, in the following code if i press note 69, hold down, press 62 and then release 62, 69 is played without PB or the other way around, All these variables are coming up now as the program is progressing, i really appreciate your help on this

uint8_t otherNotesPlaying = 0;
bool noteplaying = false;


void MyHandleNoteOn(byte channel, byte pitch, byte velocity) { 
  
  if(pitch == 62 || pitch == 69)
    noteplaying = true;
  else
    otherNotesPlaying++;
  if (noteplaying && otherNotesPlaying == 0)
    MIDI.sendPitchBend(-2949,channel);
  else 
    MIDI.sendPitchBend(0,channel);
  if (pitch == 62)
    MIDI.sendPitchBend(-2949,channel);
  if (pitch == 69)
    MIDI.sendPitchBend(-2949,channel);

  
}
  
void MyHandleNoteOff(byte channel, byte pitch, byte velocity) { 

  if(pitch == 62 || pitch == 69)
    noteplaying = false;
  else
    otherNotesPlaying--;
  if (noteplaying && otherNotesPlaying == 0)
    MIDI.sendPitchBend(-2949,channel);
  else 
    MIDI.sendPitchBend(0,channel);

}

Hi iam working on this project ,there still a problem when playing more than 2 notes at the same time, did u solve it ,can u help please.

Hi iam working on this project ,there still a problem when playing more than 2 notes at the same time, did u solve it ,can u help please.