Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: MIDI NoteOn remains pressed

  1. #1
    Member
    Join Date
    Apr 2020
    Location
    France
    Posts
    21

    MIDI NoteOn remains pressed

    Hi, I'm making good progress on my HANPAN project but I'm stuck on this. When I change scale it works:

    NoteOn ,ch: 1 , Note: 57, Velocity: 90
    NoteOff ,ch: 1 , Note: 57, Velocity: 0

    NoteOn ,ch: 1 , Note: 55, Velocity: 70
    NoteOff ,ch: 1 , Note: 55, Velocity: 0

    the noteOff is well stored and then it's released. Perfect !

    But when I change octave I have this :

    NoteOn ,ch: 1 , Note: 69, Velocity: 45
    NoteOff ,ch: 1 , Note: 57, Velocity: 0
    NoteOn ,ch: 1 , Note: 81, Velocity: 45
    NoteOff ,ch: 1 , Note: 57, Velocity: 0

    NoteOn remains pressed and she plays continuously, NoteOff does not update

    Thanks for your help

    Code:
    ///////PAD1/////////////////
    void peakDetect1(int voltage) {
     
      // "static" variables keep their numbers between each run of this function
      static int state;  // 0=idle, 1=looking for peak, 2=ignore aftershocks
      static int peak;   // remember the highest reading
      static elapsedMillis msec; // timer to end states 1 and 2
    
      switch (state) {
         
       case 0:
          if (voltage > thresholdMin) {
            Serial.print("begin peak track ");
            Serial.println(voltage);
            peak = voltage;
            msec = 0;
            state = 1;
           
          }
          return;
    
        // Peak Tracking state: capture largest reading
        
        case 1:
          if (voltage > peak) {
            peak = voltage;       
          }
          if (msec >= peakTrackMillis) {
            Serial.print("peak = ");
            Serial.println(peak);
            int velocity = map(peak, thresholdMin, 1023, 1, 127);
    
            if(octave_index == 1)
           usbMIDI.sendNoteOn(scale[scale_index][0]+12, velocity, channel); 
           else if(octave_index == -1)
           usbMIDI.sendNoteOn(scale[scale_index][0]-12, velocity, channel);
           else if(octave_index == 2)
           usbMIDI.sendNoteOn(scale[scale_index][0]+24, velocity, channel);
           else if(octave_index == -2)
           usbMIDI.sendNoteOn(scale[scale_index][0]-24, velocity, channel);
           else 
           usbMIDI.sendNoteOn(scale[scale_index][0], velocity, channel);
           msec = 0;
           state = 2;
           
           leds.setColorRGB(0, 255, 0, 0); // Led Color ON RED
           etat1 = 1;
           startTime1 = millis();
         }
    
          return;
    
        // Ignore Aftershock state: wait for things to be quiet again.
        default:
          if (voltage > thresholdMin) {
            msec = 0; // keep resetting timer if above threshold
          } else if (msec > aftershockMillis) {
            usbMIDI.sendNoteOff(scale[scale_index][0], 0, channel);
            state = 0; // go back to idle when
    
          }
      }
    }

  2. #2
    Member
    Join Date
    Apr 2020
    Location
    France
    Posts
    21
    I guess my problem is from here ?

    Code:
    void buttonCheck()
    {
    
      ////// Change Octave //////
      
      octaveUp.update();
      octaveDown.update();
    
      //if Button 3 is pressed, increment octave 
      if (octaveUp.fallingEdge())
      { 
        octave_index++;
        
        if(octave_index > 2)
        octave_index = 2;
      }
    
      //if Button 4 is pressed, decrement octave 
      if (octaveDown.fallingEdge())
      { 
        octave_index--;
        
    
        if(octave_index < -2)
        octave_index = -2;
      }
    
      ////// Chage Scale //////
    
      button0.update();
      button1.update();
    
      //if button 0 is pressed, increment the scale being used
      if (button0.fallingEdge())
      { 
        scale_index++;
        //check for overflow
        if(scale_index > numOfScales)  //numOfScales variable found in the scales.h file
        scale_index = 0;
      }
    
      //if button 1 is pressed, decrement the scale being used
      if (button1.fallingEdge())
      { 
        scale_index--;
        //check for negative numbers
        if(scale_index < 0)
        scale_index = numOfScales;  //numOfScales variable found in the scales.h file
      }
    
    }

  3. #3
    Senior Member
    Join Date
    Jul 2020
    Posts
    964
    Your noteOff code doesn't take account of octave_index - it needs to.

    If you want noteOn/noteOff to work across a change of octave you'll need to use a datastructure to keep
    a record of the notes currently in-flight so you can generate the right noteOff's.

  4. #4
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,402
    Quote Originally Posted by MarkT View Post
    Your noteOff code doesn't take account of octave_index - it needs to.

    If you want noteOn/noteOff to work across a change of octave you'll need to use a datastructure to keep
    a record of the notes currently in-flight so you can generate the right noteOff's.
    +1

    It's a bit too much guesswork about your scale and octave system with code fragments (the rule is to post complete code) but I would expect it better to do the octave shift on a variable that is then used in a single note-on and the same variable in the note-off.

    If the octave can change while a note is active you may have problems with stuck notes and you may need to resort to tracking active notes as Mark suggests.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •