annotate Source/TouchKeys/OscMidiConverter.cpp @ 0:3580ffe87dc8

First commit of TouchKeys public pre-release.
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Mon, 11 Nov 2013 18:19:35 +0000
parents
children 353276611036
rev   line source
andrewm@0 1 /*
andrewm@0 2 TouchKeys: multi-touch musical keyboard control software
andrewm@0 3 Copyright (c) 2013 Andrew McPherson
andrewm@0 4
andrewm@0 5 This program is free software: you can redistribute it and/or modify
andrewm@0 6 it under the terms of the GNU General Public License as published by
andrewm@0 7 the Free Software Foundation, either version 3 of the License, or
andrewm@0 8 (at your option) any later version.
andrewm@0 9
andrewm@0 10 This program is distributed in the hope that it will be useful,
andrewm@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
andrewm@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
andrewm@0 13 GNU General Public License for more details.
andrewm@0 14
andrewm@0 15 You should have received a copy of the GNU General Public License
andrewm@0 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
andrewm@0 17
andrewm@0 18 =====================================================================
andrewm@0 19
andrewm@0 20 OscMidiConverter.cpp: converts incoming OSC messages to outgoing MIDI
andrewm@0 21 messages with adjustable ranges and parameters.
andrewm@0 22 */
andrewm@0 23 #include "OscMidiConverter.h"
andrewm@0 24 #include "MidiKeyboardSegment.h"
andrewm@0 25
andrewm@0 26 #undef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 27
andrewm@0 28 // Main constructor: set up OSC reception from the keyboard
andrewm@0 29 OscMidiConverter::OscMidiConverter(PianoKeyboard& keyboard, MidiKeyboardSegment& segment, int controllerId) :
andrewm@0 30 keyboard_(keyboard), keyboardSegment_(segment), midiOutputController_(0),
andrewm@0 31 controller_(controllerId), lastUniqueId_(0),
andrewm@0 32 pitchWheelRange_(2.0), incomingController_(MidiKeyboardSegment::kControlDisabled)
andrewm@0 33 {
andrewm@0 34 setOscController(&keyboard_);
andrewm@0 35
andrewm@0 36 for(int i = 0; i < 16; i++) {
andrewm@0 37 currentValue_[i] = 0;
andrewm@0 38 lastOutputValue_[i] = -1;
andrewm@0 39 }
andrewm@0 40 }
andrewm@0 41
andrewm@0 42 // Set the type of MIDI message (CC, Pitch Wheel, Aftertouch)
andrewm@0 43 // that this particular object is in charge of controlling.
andrewm@0 44 // Optionally specify the default, range, and whether to use a
andrewm@0 45 // 14 bit controller.
andrewm@0 46 void OscMidiConverter::setMidiMessageType(int defaultValue, int minValue,
andrewm@0 47 int maxValue, int centerValue, bool use14BitControl) {
andrewm@0 48 /*if(controller < 0 || controller >= MidiKeyboardSegment::kControlMax) {
andrewm@0 49 controller_ = MidiKeyboardSegment::kControlDisabled;
andrewm@0 50 return;
andrewm@0 51 }
andrewm@0 52
andrewm@0 53 keyboardSegment_ = segment;*/
andrewm@0 54
andrewm@0 55 // Otherwise, send defaults on the current controller to all channels and update to the new values
andrewm@0 56 //for(int i = 0; i < 16; i++)
andrewm@0 57 // sendDefaultValue(i);
andrewm@0 58
andrewm@0 59 // Clear any existing active inputs, but not the mappings themselves
andrewm@0 60 lastValues_.clear();
andrewm@0 61
andrewm@0 62 //controller_ = controller;
andrewm@0 63 if(defaultValue >= 0)
andrewm@0 64 controlDefaultValue_ = defaultValue;
andrewm@0 65 else {
andrewm@0 66 // Default value for MIDI CCs and aftertouch is 0
andrewm@0 67 // Default value for MIDI pitch wheel is 8192
andrewm@0 68 if(controller_ == MidiKeyboardSegment::kControlPitchWheel)
andrewm@0 69 controlDefaultValue_ = 8192;
andrewm@0 70 else
andrewm@0 71 controlDefaultValue_ = 0;
andrewm@0 72 }
andrewm@0 73
andrewm@0 74 if(centerValue >= 0)
andrewm@0 75 controlCenterValue_ = centerValue;
andrewm@0 76 else
andrewm@0 77 controlCenterValue_ = controlDefaultValue_;
andrewm@0 78
andrewm@0 79 // Pitch wheel is always 14 bit. Aftertouch is always 7 bit.
andrewm@0 80 // Other CCs are selectable (though according to MIDI spec not every
andrewm@0 81 // controller has an LSB defined). And above 96 using "control+32"
andrewm@0 82 // for fine adjust no longer makes sense
andrewm@0 83 if(controller_ < 96)
andrewm@0 84 controllerIs14Bit_ = use14BitControl;
andrewm@0 85 else if(controller_ == MidiKeyboardSegment::kControlPitchWheel)
andrewm@0 86 controllerIs14Bit_ = true;
andrewm@0 87 else
andrewm@0 88 controllerIs14Bit_ = false;
andrewm@0 89
andrewm@0 90 // Handle the outer ranges of the control, providing defaults if unspecified
andrewm@0 91 if(controllerIs14Bit_) {
andrewm@0 92 if(maxValue < 0 || maxValue > 16383)
andrewm@0 93 controlMaxValue_ = 16383;
andrewm@0 94 else
andrewm@0 95 controlMaxValue_ = maxValue;
andrewm@0 96 if(minValue < 0 || minValue > 16383)
andrewm@0 97 controlMinValue_ = 0;
andrewm@0 98 else
andrewm@0 99 controlMinValue_ = minValue;
andrewm@0 100 }
andrewm@0 101 else {
andrewm@0 102 if(maxValue < 0 || maxValue > 127)
andrewm@0 103 controlMaxValue_ = 127;
andrewm@0 104 else
andrewm@0 105 controlMaxValue_ = maxValue;
andrewm@0 106 if(minValue < 0 || minValue > 127)
andrewm@0 107 controlMinValue_ = 0;
andrewm@0 108 else
andrewm@0 109 controlMinValue_ = minValue;
andrewm@0 110 }
andrewm@0 111 }
andrewm@0 112
andrewm@0 113 void OscMidiConverter::listenToIncomingControl(int controller, int centerValue, bool use14BitControl) {
andrewm@0 114 if(controller < 0 || controller >= MidiKeyboardSegment::kControlMax) {
andrewm@0 115 incomingController_ = MidiKeyboardSegment::kControlDisabled;
andrewm@0 116 return;
andrewm@0 117 }
andrewm@0 118
andrewm@0 119 incomingController_ = controller;
andrewm@0 120
andrewm@0 121 // Assign the center value that will be subtacted from the incoming controller
andrewm@0 122 if(centerValue >= 0)
andrewm@0 123 incomingControllerCenterValue_ = centerValue;
andrewm@0 124 else if(controller == MidiKeyboardSegment::kControlPitchWheel)
andrewm@0 125 incomingControllerCenterValue_ = 8192; // Pitch wheel is centered at 8192
andrewm@0 126 else
andrewm@0 127 incomingControllerCenterValue_ = 0;
andrewm@0 128
andrewm@0 129 // Pitch wheel is always 14 bit. Aftertouch is always 7 bit.
andrewm@0 130 // Other CCs are selectable (though according to MIDI spec not every
andrewm@0 131 // controller has an LSB defined). And above 96 using "control+32"
andrewm@0 132 // for fine adjust no longer makes sense
andrewm@0 133 if(controller < 96)
andrewm@0 134 incomingControllerIs14Bit_ = use14BitControl;
andrewm@0 135 else if(controller == MidiKeyboardSegment::kControlPitchWheel)
andrewm@0 136 incomingControllerIs14Bit_ = true;
andrewm@0 137 else
andrewm@0 138 incomingControllerIs14Bit_ = false;
andrewm@0 139 }
andrewm@0 140
andrewm@0 141 // Resend the most recent value
andrewm@0 142 void OscMidiConverter::resend(int channel) {
andrewm@0 143 sendCurrentValue(keyboardSegment_.outputPort(), channel, -1, true);
andrewm@0 144 }
andrewm@0 145
andrewm@0 146 // Send the default value on the specified channel.
andrewm@0 147 void OscMidiConverter::sendDefaultValue(int channel) {
andrewm@0 148 if(midiOutputController_ == 0 || controller_ == MidiKeyboardSegment::kControlDisabled)
andrewm@0 149 return;
andrewm@0 150
andrewm@0 151 // Modulate the default value by the value of the incoming controller,
andrewm@0 152 // if one is enabled.
andrewm@0 153 int defaultValue = controlDefaultValue_;
andrewm@0 154 if(incomingController_ != MidiKeyboardSegment::kControlDisabled)
andrewm@0 155 defaultValue += keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_;
andrewm@0 156
andrewm@0 157 if(controller_ == MidiKeyboardSegment::kControlPitchWheel) {
andrewm@0 158 sendPitchWheelRange(keyboardSegment_.outputPort(), channel);
andrewm@0 159 midiOutputController_->sendPitchWheel(keyboardSegment_.outputPort(), channel, defaultValue);
andrewm@0 160 }
andrewm@0 161 else if(controller_ == MidiKeyboardSegment::kControlChannelAftertouch) {
andrewm@0 162 midiOutputController_->sendAftertouchChannel(keyboardSegment_.outputPort(), channel, defaultValue);
andrewm@0 163 }
andrewm@0 164 else if(controller_ == MidiKeyboardSegment::kControlPolyphonicAftertouch) {
andrewm@0 165 // TODO
andrewm@0 166 }
andrewm@0 167 else if(controllerIs14Bit_) {
andrewm@0 168 midiOutputController_->sendControlChange(keyboardSegment_.outputPort(), channel, controller_, defaultValue >> 7);
andrewm@0 169 midiOutputController_->sendControlChange(keyboardSegment_.outputPort(), channel, controller_ + 32, defaultValue & 0x7F);
andrewm@0 170 }
andrewm@0 171 else
andrewm@0 172 midiOutputController_->sendControlChange(keyboardSegment_.outputPort(), channel, controller_, defaultValue);
andrewm@0 173 }
andrewm@0 174
andrewm@0 175 int OscMidiConverter::currentControllerValue(int channel) {
andrewm@0 176 float controlValue = (float)controlCenterValue_ + (float)controlMinValue_
andrewm@0 177 + currentValue_[channel]*(float)(controlMaxValue_ - controlMinValue_);
andrewm@0 178
andrewm@0 179 if(incomingController_ != MidiKeyboardSegment::kControlDisabled)
andrewm@0 180 controlValue += keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_;
andrewm@0 181
andrewm@0 182 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 183 std::cout << "current value " << currentValue_[channel] << " corresponds to " << controlValue << std::endl;
andrewm@0 184 #endif
andrewm@0 185 int roundedControlValue = (int)roundf(controlValue);
andrewm@0 186 if(roundedControlValue > controlMaxValue_)
andrewm@0 187 roundedControlValue = controlMaxValue_;
andrewm@0 188 if(roundedControlValue < controlMinValue_)
andrewm@0 189 roundedControlValue = controlMinValue_;
andrewm@0 190
andrewm@0 191 return roundedControlValue;
andrewm@0 192 }
andrewm@0 193
andrewm@0 194 // Add a new OSC input to this MIDI control
andrewm@0 195 void OscMidiConverter::addControl(const string& oscPath, int oscParamNumber, float oscMinValue,
andrewm@0 196 float oscMaxValue, float oscCenterValue, int outOfRangeBehavior) {
andrewm@0 197 // First remove any existing mapping with these exact parameters
andrewm@0 198 removeControl(oscPath);
andrewm@0 199
andrewm@0 200 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 201 std::cout << "OscMidiConverter: adding path " << oscPath << std::endl;
andrewm@0 202 #endif
andrewm@0 203
andrewm@0 204 // Insert the mapping
andrewm@0 205 OscInput input;
andrewm@0 206
andrewm@0 207 input.oscParamNumber = oscParamNumber;
andrewm@0 208 input.oscMinValue = oscMinValue;
andrewm@0 209 input.oscMaxValue = oscMaxValue;
andrewm@0 210
andrewm@0 211 // Calculate the normalized center value which will be subtracted
andrewm@0 212 // from the scaled input. Do this once now to save computation.
andrewm@0 213 if(oscMinValue == oscMaxValue)
andrewm@0 214 input.oscScaledCenterValue = 0;
andrewm@0 215 else {
andrewm@0 216 input.oscScaledCenterValue = (oscCenterValue - oscMinValue) / (oscMaxValue - oscMinValue);
andrewm@0 217 if(input.oscScaledCenterValue < 0)
andrewm@0 218 input.oscScaledCenterValue = 0;
andrewm@0 219 if(input.oscScaledCenterValue > 1.0)
andrewm@0 220 input.oscScaledCenterValue = 1.0;
andrewm@0 221 }
andrewm@0 222 input.outOfRangeBehavior = outOfRangeBehavior;
andrewm@0 223 input.uniqueId = lastUniqueId_++;
andrewm@0 224 inputs_[oscPath] = input;
andrewm@0 225
andrewm@0 226 // Register for the relevant OSC message
andrewm@0 227 addOscListener(oscPath);
andrewm@0 228 }
andrewm@0 229
andrewm@0 230 // Remove an existing OSC input
andrewm@0 231 void OscMidiConverter::removeControl(const string& oscPath) {
andrewm@0 232 // Find the affected control and its ID
andrewm@0 233 if(inputs_.count(oscPath) == 0)
andrewm@0 234 return;
andrewm@0 235 int controlId = inputs_[oscPath].uniqueId;
andrewm@0 236
andrewm@0 237 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 238 std::cout << "OscMidiConverter: removing path " << oscPath << std::endl;
andrewm@0 239 #endif
andrewm@0 240
andrewm@0 241 // Look for any active inputs on this channel
andrewm@0 242 for(int i = 0; i < 16; i++) {
andrewm@0 243 int channelModulatedId = idWithChannel(i, controlId);
andrewm@0 244 if(lastValues_.count(channelModulatedId) == 0)
andrewm@0 245 continue;
andrewm@0 246
andrewm@0 247 // Found a last value. Subtract it off and get new value
andrewm@0 248 float lastValueThisChannel = lastValues_[channelModulatedId];
andrewm@0 249 currentValue_[i] -= lastValueThisChannel;
andrewm@0 250
andrewm@0 251 // Remove this value from the set of active inputs
andrewm@0 252 lastValues_.erase(channelModulatedId);
andrewm@0 253
andrewm@0 254 // Send the new value after removing this one
andrewm@0 255 sendCurrentValue(keyboardSegment_.outputPort(), i, -1, true);
andrewm@0 256 }
andrewm@0 257
andrewm@0 258 // Having removed any active inputs, now remove the control itself
andrewm@0 259 // TODO: mutex protection
andrewm@0 260 inputs_.erase(oscPath);
andrewm@0 261
andrewm@0 262 removeOscListener(oscPath);
andrewm@0 263 }
andrewm@0 264
andrewm@0 265 void OscMidiConverter::removeAllControls() {
andrewm@0 266 // Clear all active inputs and send default values to all channels
andrewm@0 267 for(int i = 0; i < 16; i++)
andrewm@0 268 sendDefaultValue(i);
andrewm@0 269 lastValues_.clear();
andrewm@0 270 inputs_.clear();
andrewm@0 271 lastUniqueId_ = 0;
andrewm@0 272 removeAllOscListeners();
andrewm@0 273 }
andrewm@0 274
andrewm@0 275 // Update the minimum input value of an existing path
andrewm@0 276 void OscMidiConverter::setControlMinValue(const string& oscPath, float newValue) {
andrewm@0 277 if(inputs_.count(oscPath) == 0)
andrewm@0 278 return;
andrewm@0 279 inputs_[oscPath].oscMinValue = newValue;
andrewm@0 280 }
andrewm@0 281
andrewm@0 282 // Update the maximum input value of an existing path
andrewm@0 283 void OscMidiConverter::setControlMaxValue(const string& oscPath, float newValue) {
andrewm@0 284 if(inputs_.count(oscPath) == 0)
andrewm@0 285 return;
andrewm@0 286 inputs_[oscPath].oscMaxValue = newValue;
andrewm@0 287 }
andrewm@0 288
andrewm@0 289 // Update the center input value of an existing path
andrewm@0 290 void OscMidiConverter::setControlCenterValue(const string& oscPath, float newValue) {
andrewm@0 291 if(inputs_.count(oscPath) == 0)
andrewm@0 292 return;
andrewm@0 293 float minValue, maxValue, scaledCenterValue;
andrewm@0 294 minValue = inputs_[oscPath].oscMinValue;
andrewm@0 295 maxValue = inputs_[oscPath].oscMaxValue;
andrewm@0 296
andrewm@0 297 scaledCenterValue = (newValue - minValue) / (maxValue - minValue);
andrewm@0 298 if(scaledCenterValue < 0)
andrewm@0 299 scaledCenterValue = 0;
andrewm@0 300 if(scaledCenterValue > 1.0)
andrewm@0 301 scaledCenterValue = 1.0;
andrewm@0 302
andrewm@0 303 inputs_[oscPath].oscScaledCenterValue = scaledCenterValue;
andrewm@0 304 }
andrewm@0 305
andrewm@0 306 // Update the out of range behavior for an existing path
andrewm@0 307 void OscMidiConverter::setControlOutOfRangeBehavior(const string& oscPath, int newBehavior) {
andrewm@0 308 if(inputs_.count(oscPath) == 0)
andrewm@0 309 return;
andrewm@0 310 inputs_[oscPath].outOfRangeBehavior = newBehavior;
andrewm@0 311 }
andrewm@0 312
andrewm@0 313 // Reset any active previous values on the given channel
andrewm@0 314 // 'send' indicated whether to send the value when finished
andrewm@0 315 // even if it hasn't changed
andrewm@0 316 void OscMidiConverter::clearLastValues(int channel, bool send) {
andrewm@0 317 std::map<int, float>::iterator it = lastValues_.begin();
andrewm@0 318
andrewm@0 319 bool erased = false;
andrewm@0 320
andrewm@0 321 while(it != lastValues_.end()) {
andrewm@0 322 // Look for any last values matching this channel
andrewm@0 323 if((it->first & 0x0F) == channel) {
andrewm@0 324 lastValues_.erase(it++);
andrewm@0 325 erased = true;
andrewm@0 326 }
andrewm@0 327 else
andrewm@0 328 it++;
andrewm@0 329 }
andrewm@0 330
andrewm@0 331 currentValue_[channel] = 0;
andrewm@0 332 lastOutputValue_[channel] = -1;
andrewm@0 333
andrewm@0 334 // If any last values were erased and send is enabled,
andrewm@0 335 // resend the current values
andrewm@0 336 if(erased || send)
andrewm@0 337 sendDefaultValue(channel);
andrewm@0 338 }
andrewm@0 339
andrewm@0 340 // OSC Handler, called by the data source (PianoKeyboard in this case). Check path against stored
andrewm@0 341 // inputs and map to MIDI accordingly
andrewm@0 342
andrewm@0 343 bool OscMidiConverter::oscHandlerMethod(const char *path, const char *types, int numValues, lo_arg **values, void *data) {
andrewm@0 344 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 345 std::cout << "OscMidiConverter: received path " << path << std::endl;
andrewm@0 346 #endif
andrewm@0 347 if(midiOutputController_ == 0 || controller_ == MidiKeyboardSegment::kControlDisabled)
andrewm@0 348 return false;
andrewm@0 349
andrewm@0 350 //double before = Time::getMillisecondCounterHiRes(); // DEBUG
andrewm@0 351
andrewm@0 352 // First value should always be MIDI note number (integer type) so we can retrieve
andrewm@0 353 // information from the PianoKeyboard class
andrewm@0 354 if(numValues < 1)
andrewm@0 355 return false;
andrewm@0 356 if(types[0] != 'i')
andrewm@0 357 return false;
andrewm@0 358 int midiNoteNumber = values[0]->i;
andrewm@0 359
andrewm@0 360 // Get the MIDI retransmission channel from the note number
andrewm@0 361 if(keyboard_.key(midiNoteNumber) == 0)
andrewm@0 362 return false;
andrewm@0 363 int midiChannel = keyboard_.key(midiNoteNumber)->midiChannel();
andrewm@0 364 if(midiChannel < 0 || midiChannel > 15) {
andrewm@0 365 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 366 std::cout << "OscMidiConverter: no retransmission channel on note " << midiNoteNumber << std::endl;
andrewm@0 367 #endif
andrewm@0 368 return false;
andrewm@0 369 }
andrewm@0 370
andrewm@0 371 // Find the relevant input and make sure this OSC message has enough parameters
andrewm@0 372 if(inputs_.count(path) == 0)
andrewm@0 373 return false;
andrewm@0 374 OscInput const& input = inputs_[path];
andrewm@0 375 if(input.oscParamNumber >= numValues)
andrewm@0 376 return false;
andrewm@0 377
andrewm@0 378 // Find the relevant input value from this OSC message
andrewm@0 379 float oscParamValue;
andrewm@0 380 if(types[input.oscParamNumber] == 'f')
andrewm@0 381 oscParamValue = values[input.oscParamNumber]->f;
andrewm@0 382 else if(types[input.oscParamNumber] == 'i')
andrewm@0 383 oscParamValue = (float)values[input.oscParamNumber]->i;
andrewm@0 384 else
andrewm@0 385 return false;
andrewm@0 386
andrewm@0 387 // Scale input to a 0-1 range, the to the output range
andrewm@0 388 float scaledValue = (oscParamValue - input.oscMinValue) / (input.oscMaxValue - input.oscMinValue);
andrewm@0 389
andrewm@0 390 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 391 std::cout << "port " << keyboardSegment_.outputPort() << " received input " << oscParamValue << " which scales to " << scaledValue << std::endl;
andrewm@0 392 #endif
andrewm@0 393
andrewm@0 394 // Figure out what to do with an out of range value...
andrewm@0 395 if(scaledValue < 0.0 || scaledValue > 1.0) {
andrewm@0 396 if(input.outOfRangeBehavior == kOutOfRangeClip) {
andrewm@0 397 if(scaledValue < 0.0)
andrewm@0 398 scaledValue = 0.0;
andrewm@0 399 if(scaledValue > 1.0)
andrewm@0 400 scaledValue = 1.0;
andrewm@0 401 }
andrewm@0 402 else if(input.outOfRangeBehavior == kOutOfRangeExtrapolate) {
andrewm@0 403 // Do nothing
andrewm@0 404 }
andrewm@0 405 else // Ignore or unknown behavior
andrewm@0 406 return false;
andrewm@0 407 }
andrewm@0 408
andrewm@0 409 //double midpoint = Time::getMillisecondCounterHiRes(); // DEBUG
andrewm@0 410
andrewm@0 411 // Now subtract the normalized center value, which may put the range outside 0-1
andrewm@0 412 // but this is expected. For example, in a pitch wheel situation, we might move
andrewm@0 413 // to a range of -0.5 to 0.5.
andrewm@0 414 scaledValue -= input.oscScaledCenterValue;
andrewm@0 415
andrewm@0 416 // Look for previous input with this path and channel and remove it
andrewm@0 417 int channelModulatedId = idWithChannel(midiChannel, input.uniqueId);
andrewm@0 418 if(lastValues_.count(channelModulatedId) > 0) {
andrewm@0 419 // Found a last value. Subtract it off and replace with our current value
andrewm@0 420 float lastValueThisChannel = lastValues_[channelModulatedId];
andrewm@0 421 currentValue_[midiChannel] -= lastValueThisChannel;
andrewm@0 422 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 423 std::cout << "found and removed " << lastValueThisChannel << ", now have " << currentValue_[midiChannel] << std::endl;
andrewm@0 424 #endif
andrewm@0 425 }
andrewm@0 426
andrewm@0 427 lastValues_[channelModulatedId] = scaledValue;
andrewm@0 428 currentValue_[midiChannel] += scaledValue;
andrewm@0 429
andrewm@0 430 // Send the total current value as a MIDI controller
andrewm@0 431 sendCurrentValue(keyboardSegment_.outputPort(), midiChannel, midiNoteNumber, false);
andrewm@0 432
andrewm@0 433 //double after = Time::getMillisecondCounterHiRes(); // DEBUG
andrewm@0 434
andrewm@0 435 //std::cout << "OscMidiConverter: total " << after - before << "ms, of which " << after - midpoint << " in 2nd half\n";
andrewm@0 436
andrewm@0 437 return true;
andrewm@0 438 }
andrewm@0 439
andrewm@0 440 // Send a MIDI RPN message to set the pitch wheel range
andrewm@0 441 void OscMidiConverter::sendPitchWheelRange(int port, int channel) {
andrewm@0 442 if(midiOutputController_ == 0)
andrewm@0 443 return;
andrewm@0 444
andrewm@0 445 // KLUDGE: fix by using MidiKeyboardSegment version
andrewm@0 446
andrewm@0 447 // Find number of semitones and cents
andrewm@0 448 int majorRange = (int)floorf(pitchWheelRange_);
andrewm@0 449 int minorRange = (int)(100.0 * (pitchWheelRange_ - floorf(pitchWheelRange_)));
andrewm@0 450
andrewm@0 451 // Set RPN controller = 0
andrewm@0 452 midiOutputController_->sendControlChange(port, channel, 101, 0);
andrewm@0 453 midiOutputController_->sendControlChange(port, channel, 100, 0);
andrewm@0 454 // Set data value MSB/LSB for bend range in semitones
andrewm@0 455 midiOutputController_->sendControlChange(port, channel, 6, majorRange);
andrewm@0 456 midiOutputController_->sendControlChange(port, channel, 38, minorRange);
andrewm@0 457 // Set RPN controller back to 16383
andrewm@0 458 midiOutputController_->sendControlChange(port, channel, 101, 127);
andrewm@0 459 midiOutputController_->sendControlChange(port, channel, 100, 127);
andrewm@0 460 }
andrewm@0 461
andrewm@0 462 // Send the current sum value of all OSC inputs as a MIDI message
andrewm@0 463 void OscMidiConverter::sendCurrentValue(int port, int channel, int note, bool force) {
andrewm@0 464 if(midiOutputController_ == 0 || channel < 0 || channel > 15)
andrewm@0 465 return;
andrewm@0 466
andrewm@0 467 // TODO: what about values that are centered by default e.g. pitch?
andrewm@0 468 /*float controlValue = (float)controlCenterValue_ + (float)controlMinValue_
andrewm@0 469 + currentValue_[channel]*(float)(controlMaxValue_ - controlMinValue_);
andrewm@0 470
andrewm@0 471 if(incomingController_ != MidiKeyboardSegment::kControlDisabled)
andrewm@0 472 controlValue += keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_;
andrewm@0 473
andrewm@0 474 std::cout << "sending value " << currentValue_[channel] << " as " << controlValue << std::endl;
andrewm@0 475 int roundedControlValue = (int)roundf(controlValue);
andrewm@0 476 if(roundedControlValue > controlMaxValue_)
andrewm@0 477 roundedControlValue = controlMaxValue_;
andrewm@0 478 if(roundedControlValue < controlMinValue_)
andrewm@0 479 roundedControlValue = controlMinValue_;*/
andrewm@0 480
andrewm@0 481 int roundedControlValue = currentControllerValue(channel);
andrewm@0 482
andrewm@0 483 // If this is the same ultimate CC value as before, don't resend unless forced
andrewm@0 484 if(roundedControlValue == lastOutputValue_[channel] && !force)
andrewm@0 485 return;
andrewm@0 486 lastOutputValue_[channel] = roundedControlValue;
andrewm@0 487
andrewm@0 488 // Four cases: Pitch Wheel messages, aftertouch, 14-bit controls (major and minor controllers), ordinary 7-bit controls
andrewm@0 489 if(controller_ == MidiKeyboardSegment::kControlPitchWheel) {
andrewm@0 490 midiOutputController_->sendPitchWheel(port, channel, roundedControlValue);
andrewm@0 491 }
andrewm@0 492 else if(controller_ == MidiKeyboardSegment::kControlChannelAftertouch) {
andrewm@0 493 midiOutputController_->sendAftertouchChannel(port, channel, roundedControlValue);
andrewm@0 494 }
andrewm@0 495 else if(controller_ == MidiKeyboardSegment::kControlPolyphonicAftertouch && note >= 0) {
andrewm@0 496 midiOutputController_->sendAftertouchPoly(port, channel, note, roundedControlValue);
andrewm@0 497 }
andrewm@0 498 else if(controllerIs14Bit_) {
andrewm@0 499 // LSB for controllers 0-31 are found on controllers 32-63
andrewm@0 500 int lsb = roundedControlValue & 0x007F;
andrewm@0 501 int msb = (roundedControlValue >> 7) & 0x007F;
andrewm@0 502
andrewm@0 503 midiOutputController_->sendControlChange(port, channel, controller_, msb);
andrewm@0 504 midiOutputController_->sendControlChange(port, channel, controller_ + 32, lsb);
andrewm@0 505 }
andrewm@0 506 else { // 7 bit
andrewm@0 507 midiOutputController_->sendControlChange(port, channel, controller_, roundedControlValue);
andrewm@0 508 }
andrewm@0 509 }
andrewm@0 510