Your pitch bend message is incorrect. Instead of this:
UInt32 noteCommand = kMIDIMessage_PitchBend << 4 | 0;
OSStatus result = MusicDeviceMIDIEvent(self.samplerUnit, noteCommand, noteNum, bendMSB, bendLSB);
do this:
UInt32 bendCommand = kMIDIMessage_PitchBend << 4 | 0;
OSStatus result = MusicDeviceMIDIEvent(self.samplerUnit, bendCommand, bendLSB, bendMSB, 0);
The note value isn't part of the pitch bend command. (Also, I changed the name of the variable noteCommand
to bendCommand
to more accurately reflect its purpose.)
In LoadPresetDemo I added a property to MainViewController.m
:
@property (readwrite) NSInteger bendValue;
and this code:
- (void)sendBendValue:(NSInteger)bendValue {
//bendValue in the range [-8192, 8191]
const UInt32 bendCommand = kMIDIMessage_PitchBend << 4 | 0;
bendValue += 8192;
UInt32 bendMSB = (bendValue >> 7) & 0x7F;
UInt32 bendLSB = bendValue & 0x7F;
NSLog(@"MSB=%d, LSB=%d", (unsigned int)bendMSB, (unsigned int)bendLSB);
OSStatus result = MusicDeviceMIDIEvent(self.samplerUnit, bendCommand, bendLSB, bendMSB, 0);
NSAssert (result == noErr, @"Unable to send pitch bend message. Error code: %d '%.4s'", (int) result, (const char *)&result);
}
- (IBAction)bendDown:(id)sender {
self.bendValue = MAX(-8192, self.bendValue - 0x20);
[self sendBendValue:self.bendValue];
}
- (IBAction)bendCenter:(id)sender {
self.bendValue = 0;
[self setBendRange:50 cents:0];
[self sendBendValue:self.bendValue];
}
- (IBAction)bendUp:(id)sender {
self.bendValue = MIN(8191, self.bendValue + 0x20);
[self sendBendValue:self.bendValue];
}
-(void)setBendRange:(UInt32)semitones cents:(UInt32)cents {
MusicDeviceMIDIEvent(self.samplerUnit, 0xB0, 0x64, 0, 0);
MusicDeviceMIDIEvent(self.samplerUnit, 0xB0, 0x65, 0, 0);
MusicDeviceMIDIEvent(self.samplerUnit, 0xB0, 0x06, semitones, 0);
MusicDeviceMIDIEvent(self.samplerUnit, 0xB0, 0x26, cents, 0);
//The following two lines are not really necessary. They only matter if additional controller 0x06 or 0x26 messages are sent
//MusicDeviceMIDIEvent(self.samplerUnit, 0xB0, 0x64, 0x7F, 0);
//MusicDeviceMIDIEvent(self.samplerUnit, 0xB0, 0x65, 0x7F, 0);
}
I created three buttons and assigned them to bendDown:
, bendCenter:
, and bendUp:
.
Run the program and press the bendCenter
button. Then, with the trombone sound selected, press and hold the "Mid Note" button. While holding that down, press the bendUp
or bendDown
buttons. I can hear changes in pitch when the LSB changes and the MSB stays the same.