problems with usbMIDI.sendPitchBend

CBec

New member
Hi,
first of all I'm completely new to Teensy and Arduino and programming in general. I hope the post (length and detail) are OK...
I have adapted the TeensyWI project of Johan Berglund (https://github.com/Trasselfrisyr/MiniWI/tree/master/T.WI - many thanks for the great work and inspiration:)) to make a small MIDI wind controller for travelling.
I use a Teensy LC with Arduino 1.8.19 on xubuntu Linux 20.04 LTS with the latest Teensyduino and Teensy Loader 1.56. To check MIDI-Data I use MIDIWrench on iPad.
Everything worked out fine except the pitch bend function with the joystick.
After some strange up and down the pitch went to lower minimum and stayed there.
I changed hardware (Joystick, Teensy, even used a Teensy 4,0) with no effect. So I think I can exclude a specific hardware problem.
Then I wrote a small test for PitchBend (some code fragments are left from origina T.Wi.ino):

Code:
int pitchBend;
int oldpb=8192;
int PB_sens=4096;
int PB_max=0;
int PB_min=3000;
int i_corr = 0;

#define modsLo_Thr 1600 // Low threshold for mod stick center
#define modsHi_Thr 2480 // High threshold for mod stick center
#define modsMin 560     // PSP joystick min value
#define modsMax 3400    // PSP joystick max value

void setup() {
  // put your setup code here, to run once:
 // initialize serial communication at 9600 bits per second:
  analogReadResolution(12);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

  usbMIDI.sendNoteOn(60,90,1); 

  for (int i = 0;i >= -100; i=i-10) {
    usbMIDI.sendPitchBend(i,1);
    delay(30);
  }
  delay(1000);
  for (int i = -100; i <= 100; i=i+10){
    usbMIDI.sendPitchBend(i, 1);
    delay(50);
  }
  delay(1000);

  for (int i = 100; i>=0; i = i-10){
    usbMIDI.sendPitchBend(i,1);
    delay(30);
  }
delay(1000);
usbMIDI.sendNoteOff(60,90,1);

The MIDI result was:

17:49:19.083 [Note On] ch=1 note=60 velocity=90
17:49:19.083 [Pitch Bend] ch=1 pitch=64 [MP]
17:49:19.113 [Pitch Bend] ch=1 pitch=15167
17:49:19.143 [Pitch Bend] ch=1 pitch=13887
17:49:19.173 [Pitch Bend] ch=1 pitch=12607
17:49:19.203 [Pitch Bend] ch=1 pitch=11327
17:49:19.233 [Pitch Bend] ch=1 pitch=10047
17:49:19.263 [Pitch Bend] ch=1 pitch=8767
17:49:19.293 [Pitch Bend] ch=1 pitch=7487
17:49:19.323 [Pitch Bend] ch=1 pitch=6207
17:49:19.353 [Pitch Bend] ch=1 pitch=4927
17:49:19.383 [Pitch Bend] ch=1 pitch=3647
17:49:20.413 [Pitch Bend] ch=1 pitch=3647
17:49:20.463 [Pitch Bend] ch=1 pitch=4927
17:49:20.513 [Pitch Bend] ch=1 pitch=6207
17:49:20.563 [Pitch Bend] ch=1 pitch=7487
17:49:20.613 [Pitch Bend] ch=1 pitch=8767
17:49:20.663 [Pitch Bend] ch=1 pitch=10047
17:49:20.713 [Pitch Bend] ch=1 pitch=11327
17:49:20.763 [Pitch Bend] ch=1 pitch=12607
17:49:20.813 [Pitch Bend] ch=1 pitch=13887
17:49:20.863 [Pitch Bend] ch=1 pitch=15167
17:49:20.913 [Pitch Bend] ch=1 pitch=64
17:49:20.963 [Pitch Bend] ch=1 pitch=1344
17:49:21.013 [Pitch Bend] ch=1 pitch=2624
17:49:21.063 [Pitch Bend] ch=1 pitch=3904
17:49:21.113 [Pitch Bend] ch=1 pitch=5184
17:49:21.163 [Pitch Bend] ch=1 pitch=6464
17:49:21.213 [Pitch Bend] ch=1 pitch=7744
17:49:21.263 [Pitch Bend] ch=1 pitch=9024
17:49:21.313 [Pitch Bend] ch=1 pitch=10304
17:49:21.363 [Pitch Bend] ch=1 pitch=11584
17:49:21.413 [Pitch Bend] ch=1 pitch=12864
17:49:22.463 [Pitch Bend] ch=1 pitch=12864
17:49:22.493 [Pitch Bend] ch=1 pitch=11584
17:49:22.523 [Pitch Bend] ch=1 pitch=10304
17:49:22.553 [Pitch Bend] ch=1 pitch=9024
17:49:22.583 [Pitch Bend] ch=1 pitch=7744
17:49:22.613 [Pitch Bend] ch=1 pitch=6464
17:49:22.643 [Pitch Bend] ch=1 pitch=5184
17:49:22.673 [Pitch Bend] ch=1 pitch=3904
17:49:22.703 [Pitch Bend] ch=1 pitch=2624
17:49:22.733 [Pitch Bend] ch=1 pitch=1344
17:49:22.763 [Pitch Bend] ch=1 pitch=64
17:49:23.793 [Note Off] ch=1 note=60 velocity=90


After some investigations I found out that MSB and LSB of the PB Message seem to be flipped.
So I tried to find out where I could flip them back and found and changed .../arduino-1.8.19/hardware/teensy/avr/cores/teensy3/usb_midi.h to test this:
Code:
void sendPitchBend(int value, uint8_t channel, uint8_t cable=0) __attribute__((always_inline)) {
        if (value < -8192) {
            value = -8192;
        } else if (value > 8191) {
            value = 8191;
        }
        value += 8192;
        //send(0xE0, value, value >> 7, channel, cable);
        send(0xE0, value >> 7, value , channel, cable); //flip MSB and LSB

after that the MIDI log looked like expected:

17:54:56.186 [Note On] ch=1 note=60 velocity=90
17:54:56.186 [Pitch Bend] ch=1 pitch=8192 [MP]
17:54:56.215 [Pitch Bend] ch=1 pitch=8182
17:54:56.245 [Pitch Bend] ch=1 pitch=8172
17:54:56.275 [Pitch Bend] ch=1 pitch=8162
17:54:56.305 [Pitch Bend] ch=1 pitch=8152
17:54:56.335 [Pitch Bend] ch=1 pitch=8142
17:54:56.365 [Pitch Bend] ch=1 pitch=8132
17:54:56.395 [Pitch Bend] ch=1 pitch=8122
17:54:56.425 [Pitch Bend] ch=1 pitch=8112
17:54:56.455 [Pitch Bend] ch=1 pitch=8102
17:54:56.485 [Pitch Bend] ch=1 pitch=8092
17:54:57.515 [Pitch Bend] ch=1 pitch=8092
17:54:57.565 [Pitch Bend] ch=1 pitch=8102
17:54:57.615 [Pitch Bend] ch=1 pitch=8112
17:54:57.665 [Pitch Bend] ch=1 pitch=8122
17:54:57.715 [Pitch Bend] ch=1 pitch=8132
17:54:57.765 [Pitch Bend] ch=1 pitch=8142
17:54:57.815 [Pitch Bend] ch=1 pitch=8152
17:54:57.865 [Pitch Bend] ch=1 pitch=8162
17:54:57.915 [Pitch Bend] ch=1 pitch=8172
17:54:57.965 [Pitch Bend] ch=1 pitch=8182
17:54:58.015 [Pitch Bend] ch=1 pitch=8192
17:54:58.065 [Pitch Bend] ch=1 pitch=8202
17:54:58.115 [Pitch Bend] ch=1 pitch=8212
17:54:58.165 [Pitch Bend] ch=1 pitch=8222
17:54:58.215 [Pitch Bend] ch=1 pitch=8232
17:54:58.265 [Pitch Bend] ch=1 pitch=8242
17:54:58.315 [Pitch Bend] ch=1 pitch=8252
17:54:58.365 [Pitch Bend] ch=1 pitch=8262
17:54:58.415 [Pitch Bend] ch=1 pitch=8272
17:54:58.465 [Pitch Bend] ch=1 pitch=8282
17:54:58.515 [Pitch Bend] ch=1 pitch=8292
17:54:59.565 [Pitch Bend] ch=1 pitch=8292
17:54:59.595 [Pitch Bend] ch=1 pitch=8282
17:54:59.625 [Pitch Bend] ch=1 pitch=8272
17:54:59.655 [Pitch Bend] ch=1 pitch=8262
17:54:59.685 [Pitch Bend] ch=1 pitch=8252
17:54:59.715 [Pitch Bend] ch=1 pitch=8242
17:54:59.745 [Pitch Bend] ch=1 pitch=8232
17:54:59.775 [Pitch Bend] ch=1 pitch=8222
17:54:59.805 [Pitch Bend] ch=1 pitch=8212
17:54:59.835 [Pitch Bend] ch=1 pitch=8202
17:54:59.865 [Pitch Bend] ch=1 pitch=8192
17:55:00.895 [Note Off] ch=1 note=60 velocity=90


So far so good.
But when I connect the Teensy to a SW Synth (Synthmaster 2 on iPad), the Pitchbend behavior is still the weird one (after some ups and downs going to the minimum and staying there).

And now I have absolutely no clue what causes this strange behavior - flipped bytes (type mismatch?) as well as different behavior between synth and MIDIWrench :)confused:).
So any advise is very much appreciated.
 
Back
Top