annotate Source/TouchKeys/OscMidiConverter.cpp @ 16:61e3c9df4674

Fix bug where TouchKeys standalone mode turns off when mode is changed.
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Mon, 25 Nov 2013 21:36:02 +0000
parents 353276611036
children dfff66c07936
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@7 32 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@7 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@7 179 if(incomingController_ != MidiKeyboardSegment::kControlDisabled) {
andrewm@0 180 controlValue += keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_;
andrewm@7 181 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@7 182 std::cout << "current value " << currentValue_[channel] << " corresponds to " << controlValue;
andrewm@7 183 std::cout << " including incoming value " << (keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_) << std::endl;
andrewm@7 184 #endif
andrewm@7 185 }
andrewm@7 186 else {
andrewm@7 187 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@7 188 std::cout << "current value " << currentValue_[channel] << " corresponds to " << controlValue << std::endl;
andrewm@7 189 #endif
andrewm@7 190 }
andrewm@0 191
andrewm@0 192 int roundedControlValue = (int)roundf(controlValue);
andrewm@0 193 if(roundedControlValue > controlMaxValue_)
andrewm@0 194 roundedControlValue = controlMaxValue_;
andrewm@0 195 if(roundedControlValue < controlMinValue_)
andrewm@0 196 roundedControlValue = controlMinValue_;
andrewm@0 197
andrewm@0 198 return roundedControlValue;
andrewm@0 199 }
andrewm@0 200
andrewm@0 201 // Add a new OSC input to this MIDI control
andrewm@0 202 void OscMidiConverter::addControl(const string& oscPath, int oscParamNumber, float oscMinValue,
andrewm@0 203 float oscMaxValue, float oscCenterValue, int outOfRangeBehavior) {
andrewm@0 204 // First remove any existing mapping with these exact parameters
andrewm@0 205 removeControl(oscPath);
andrewm@0 206
andrewm@0 207 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 208 std::cout << "OscMidiConverter: adding path " << oscPath << std::endl;
andrewm@0 209 #endif
andrewm@0 210
andrewm@0 211 // Insert the mapping
andrewm@0 212 OscInput input;
andrewm@0 213
andrewm@0 214 input.oscParamNumber = oscParamNumber;
andrewm@0 215 input.oscMinValue = oscMinValue;
andrewm@0 216 input.oscMaxValue = oscMaxValue;
andrewm@0 217
andrewm@0 218 // Calculate the normalized center value which will be subtracted
andrewm@0 219 // from the scaled input. Do this once now to save computation.
andrewm@0 220 if(oscMinValue == oscMaxValue)
andrewm@7 221 input.oscScaledCenterValue = 0.5;
andrewm@0 222 else {
andrewm@0 223 input.oscScaledCenterValue = (oscCenterValue - oscMinValue) / (oscMaxValue - oscMinValue);
andrewm@0 224 if(input.oscScaledCenterValue < 0)
andrewm@0 225 input.oscScaledCenterValue = 0;
andrewm@0 226 if(input.oscScaledCenterValue > 1.0)
andrewm@0 227 input.oscScaledCenterValue = 1.0;
andrewm@0 228 }
andrewm@0 229 input.outOfRangeBehavior = outOfRangeBehavior;
andrewm@0 230 input.uniqueId = lastUniqueId_++;
andrewm@0 231 inputs_[oscPath] = input;
andrewm@0 232
andrewm@0 233 // Register for the relevant OSC message
andrewm@0 234 addOscListener(oscPath);
andrewm@0 235 }
andrewm@0 236
andrewm@0 237 // Remove an existing OSC input
andrewm@0 238 void OscMidiConverter::removeControl(const string& oscPath) {
andrewm@0 239 // Find the affected control and its ID
andrewm@0 240 if(inputs_.count(oscPath) == 0)
andrewm@0 241 return;
andrewm@0 242 int controlId = inputs_[oscPath].uniqueId;
andrewm@0 243
andrewm@0 244 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 245 std::cout << "OscMidiConverter: removing path " << oscPath << std::endl;
andrewm@0 246 #endif
andrewm@0 247
andrewm@0 248 // Look for any active inputs on this channel
andrewm@0 249 for(int i = 0; i < 16; i++) {
andrewm@0 250 int channelModulatedId = idWithChannel(i, controlId);
andrewm@0 251 if(lastValues_.count(channelModulatedId) == 0)
andrewm@0 252 continue;
andrewm@0 253
andrewm@0 254 // Found a last value. Subtract it off and get new value
andrewm@0 255 float lastValueThisChannel = lastValues_[channelModulatedId];
andrewm@0 256 currentValue_[i] -= lastValueThisChannel;
andrewm@0 257
andrewm@0 258 // Remove this value from the set of active inputs
andrewm@0 259 lastValues_.erase(channelModulatedId);
andrewm@0 260
andrewm@0 261 // Send the new value after removing this one
andrewm@0 262 sendCurrentValue(keyboardSegment_.outputPort(), i, -1, true);
andrewm@0 263 }
andrewm@0 264
andrewm@0 265 // Having removed any active inputs, now remove the control itself
andrewm@0 266 // TODO: mutex protection
andrewm@0 267 inputs_.erase(oscPath);
andrewm@0 268
andrewm@0 269 removeOscListener(oscPath);
andrewm@0 270 }
andrewm@0 271
andrewm@0 272 void OscMidiConverter::removeAllControls() {
andrewm@0 273 // Clear all active inputs and send default values to all channels
andrewm@0 274 for(int i = 0; i < 16; i++)
andrewm@0 275 sendDefaultValue(i);
andrewm@0 276 lastValues_.clear();
andrewm@0 277 inputs_.clear();
andrewm@0 278 lastUniqueId_ = 0;
andrewm@0 279 removeAllOscListeners();
andrewm@0 280 }
andrewm@0 281
andrewm@0 282 // Update the minimum input value of an existing path
andrewm@0 283 void OscMidiConverter::setControlMinValue(const string& oscPath, float newValue) {
andrewm@0 284 if(inputs_.count(oscPath) == 0)
andrewm@0 285 return;
andrewm@0 286 inputs_[oscPath].oscMinValue = newValue;
andrewm@0 287 }
andrewm@0 288
andrewm@0 289 // Update the maximum input value of an existing path
andrewm@0 290 void OscMidiConverter::setControlMaxValue(const string& oscPath, float newValue) {
andrewm@0 291 if(inputs_.count(oscPath) == 0)
andrewm@0 292 return;
andrewm@0 293 inputs_[oscPath].oscMaxValue = newValue;
andrewm@0 294 }
andrewm@0 295
andrewm@0 296 // Update the center input value of an existing path
andrewm@0 297 void OscMidiConverter::setControlCenterValue(const string& oscPath, float newValue) {
andrewm@0 298 if(inputs_.count(oscPath) == 0)
andrewm@0 299 return;
andrewm@0 300 float minValue, maxValue, scaledCenterValue;
andrewm@0 301 minValue = inputs_[oscPath].oscMinValue;
andrewm@0 302 maxValue = inputs_[oscPath].oscMaxValue;
andrewm@0 303
andrewm@7 304 if(minValue == maxValue)
andrewm@7 305 scaledCenterValue = 0.0;
andrewm@7 306 else
andrewm@7 307 scaledCenterValue = (newValue - minValue) / (maxValue - minValue);
andrewm@0 308 if(scaledCenterValue < 0)
andrewm@0 309 scaledCenterValue = 0;
andrewm@0 310 if(scaledCenterValue > 1.0)
andrewm@0 311 scaledCenterValue = 1.0;
andrewm@0 312
andrewm@0 313 inputs_[oscPath].oscScaledCenterValue = scaledCenterValue;
andrewm@0 314 }
andrewm@0 315
andrewm@0 316 // Update the out of range behavior for an existing path
andrewm@0 317 void OscMidiConverter::setControlOutOfRangeBehavior(const string& oscPath, int newBehavior) {
andrewm@0 318 if(inputs_.count(oscPath) == 0)
andrewm@0 319 return;
andrewm@0 320 inputs_[oscPath].outOfRangeBehavior = newBehavior;
andrewm@0 321 }
andrewm@0 322
andrewm@0 323 // Reset any active previous values on the given channel
andrewm@0 324 // 'send' indicated whether to send the value when finished
andrewm@0 325 // even if it hasn't changed
andrewm@0 326 void OscMidiConverter::clearLastValues(int channel, bool send) {
andrewm@0 327 std::map<int, float>::iterator it = lastValues_.begin();
andrewm@0 328
andrewm@0 329 bool erased = false;
andrewm@0 330
andrewm@0 331 while(it != lastValues_.end()) {
andrewm@0 332 // Look for any last values matching this channel
andrewm@0 333 if((it->first & 0x0F) == channel) {
andrewm@0 334 lastValues_.erase(it++);
andrewm@0 335 erased = true;
andrewm@0 336 }
andrewm@0 337 else
andrewm@0 338 it++;
andrewm@0 339 }
andrewm@0 340
andrewm@0 341 currentValue_[channel] = 0;
andrewm@0 342 lastOutputValue_[channel] = -1;
andrewm@0 343
andrewm@0 344 // If any last values were erased and send is enabled,
andrewm@0 345 // resend the current values
andrewm@0 346 if(erased || send)
andrewm@0 347 sendDefaultValue(channel);
andrewm@0 348 }
andrewm@0 349
andrewm@0 350 // OSC Handler, called by the data source (PianoKeyboard in this case). Check path against stored
andrewm@0 351 // inputs and map to MIDI accordingly
andrewm@0 352
andrewm@0 353 bool OscMidiConverter::oscHandlerMethod(const char *path, const char *types, int numValues, lo_arg **values, void *data) {
andrewm@0 354 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 355 std::cout << "OscMidiConverter: received path " << path << std::endl;
andrewm@0 356 #endif
andrewm@0 357 if(midiOutputController_ == 0 || controller_ == MidiKeyboardSegment::kControlDisabled)
andrewm@0 358 return false;
andrewm@0 359
andrewm@0 360 //double before = Time::getMillisecondCounterHiRes(); // DEBUG
andrewm@0 361
andrewm@0 362 // First value should always be MIDI note number (integer type) so we can retrieve
andrewm@0 363 // information from the PianoKeyboard class
andrewm@0 364 if(numValues < 1)
andrewm@0 365 return false;
andrewm@0 366 if(types[0] != 'i')
andrewm@0 367 return false;
andrewm@0 368 int midiNoteNumber = values[0]->i;
andrewm@0 369
andrewm@0 370 // Get the MIDI retransmission channel from the note number
andrewm@0 371 if(keyboard_.key(midiNoteNumber) == 0)
andrewm@0 372 return false;
andrewm@0 373 int midiChannel = keyboard_.key(midiNoteNumber)->midiChannel();
andrewm@0 374 if(midiChannel < 0 || midiChannel > 15) {
andrewm@0 375 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 376 std::cout << "OscMidiConverter: no retransmission channel on note " << midiNoteNumber << std::endl;
andrewm@0 377 #endif
andrewm@0 378 return false;
andrewm@0 379 }
andrewm@0 380
andrewm@0 381 // Find the relevant input and make sure this OSC message has enough parameters
andrewm@0 382 if(inputs_.count(path) == 0)
andrewm@0 383 return false;
andrewm@0 384 OscInput const& input = inputs_[path];
andrewm@0 385 if(input.oscParamNumber >= numValues)
andrewm@0 386 return false;
andrewm@0 387
andrewm@0 388 // Find the relevant input value from this OSC message
andrewm@0 389 float oscParamValue;
andrewm@0 390 if(types[input.oscParamNumber] == 'f')
andrewm@0 391 oscParamValue = values[input.oscParamNumber]->f;
andrewm@0 392 else if(types[input.oscParamNumber] == 'i')
andrewm@0 393 oscParamValue = (float)values[input.oscParamNumber]->i;
andrewm@0 394 else
andrewm@0 395 return false;
andrewm@0 396
andrewm@7 397 // Scale input to a 0-1 range, then to the output range. There's a special case for MIDI pitch wheel,
andrewm@7 398 // where if the range is set to 0, it means to use the segment-wide pitch wheel range. This is done so
andrewm@7 399 // we don't have to cache multiple copies of the pitch wheel range in every OSC-MIDI converter.
andrewm@7 400 float scaledValue;
andrewm@7 401 if(controller_ == MidiKeyboardSegment::kControlPitchWheel &&
andrewm@7 402 input.oscMaxValue == 0 && input.oscMinValue == 0) {
andrewm@7 403 float pitchWheelRange = keyboardSegment_.midiPitchWheelRange();
andrewm@7 404 scaledValue = (oscParamValue + pitchWheelRange) / (2.0 * pitchWheelRange);
andrewm@7 405 }
andrewm@7 406 else
andrewm@7 407 scaledValue = (oscParamValue - input.oscMinValue) / (input.oscMaxValue - input.oscMinValue);
andrewm@0 408
andrewm@0 409 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 410 std::cout << "port " << keyboardSegment_.outputPort() << " received input " << oscParamValue << " which scales to " << scaledValue << std::endl;
andrewm@0 411 #endif
andrewm@0 412
andrewm@0 413 // Figure out what to do with an out of range value...
andrewm@0 414 if(scaledValue < 0.0 || scaledValue > 1.0) {
andrewm@0 415 if(input.outOfRangeBehavior == kOutOfRangeClip) {
andrewm@0 416 if(scaledValue < 0.0)
andrewm@0 417 scaledValue = 0.0;
andrewm@0 418 if(scaledValue > 1.0)
andrewm@0 419 scaledValue = 1.0;
andrewm@0 420 }
andrewm@0 421 else if(input.outOfRangeBehavior == kOutOfRangeExtrapolate) {
andrewm@0 422 // Do nothing
andrewm@0 423 }
andrewm@0 424 else // Ignore or unknown behavior
andrewm@0 425 return false;
andrewm@0 426 }
andrewm@0 427
andrewm@0 428 //double midpoint = Time::getMillisecondCounterHiRes(); // DEBUG
andrewm@0 429
andrewm@0 430 // Now subtract the normalized center value, which may put the range outside 0-1
andrewm@0 431 // but this is expected. For example, in a pitch wheel situation, we might move
andrewm@0 432 // to a range of -0.5 to 0.5.
andrewm@0 433 scaledValue -= input.oscScaledCenterValue;
andrewm@0 434
andrewm@0 435 // Look for previous input with this path and channel and remove it
andrewm@0 436 int channelModulatedId = idWithChannel(midiChannel, input.uniqueId);
andrewm@0 437 if(lastValues_.count(channelModulatedId) > 0) {
andrewm@0 438 // Found a last value. Subtract it off and replace with our current value
andrewm@0 439 float lastValueThisChannel = lastValues_[channelModulatedId];
andrewm@0 440 currentValue_[midiChannel] -= lastValueThisChannel;
andrewm@0 441 #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0 442 std::cout << "found and removed " << lastValueThisChannel << ", now have " << currentValue_[midiChannel] << std::endl;
andrewm@0 443 #endif
andrewm@0 444 }
andrewm@0 445
andrewm@0 446 lastValues_[channelModulatedId] = scaledValue;
andrewm@0 447 currentValue_[midiChannel] += scaledValue;
andrewm@0 448
andrewm@0 449 // Send the total current value as a MIDI controller
andrewm@0 450 sendCurrentValue(keyboardSegment_.outputPort(), midiChannel, midiNoteNumber, false);
andrewm@0 451
andrewm@0 452 //double after = Time::getMillisecondCounterHiRes(); // DEBUG
andrewm@0 453
andrewm@0 454 //std::cout << "OscMidiConverter: total " << after - before << "ms, of which " << after - midpoint << " in 2nd half\n";
andrewm@0 455
andrewm@0 456 return true;
andrewm@0 457 }
andrewm@0 458
andrewm@0 459 // Send the current sum value of all OSC inputs as a MIDI message
andrewm@0 460 void OscMidiConverter::sendCurrentValue(int port, int channel, int note, bool force) {
andrewm@0 461 if(midiOutputController_ == 0 || channel < 0 || channel > 15)
andrewm@0 462 return;
andrewm@0 463
andrewm@0 464 // TODO: what about values that are centered by default e.g. pitch?
andrewm@0 465 /*float controlValue = (float)controlCenterValue_ + (float)controlMinValue_
andrewm@0 466 + currentValue_[channel]*(float)(controlMaxValue_ - controlMinValue_);
andrewm@0 467
andrewm@0 468 if(incomingController_ != MidiKeyboardSegment::kControlDisabled)
andrewm@0 469 controlValue += keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_;
andrewm@0 470
andrewm@0 471 std::cout << "sending value " << currentValue_[channel] << " as " << controlValue << std::endl;
andrewm@0 472 int roundedControlValue = (int)roundf(controlValue);
andrewm@0 473 if(roundedControlValue > controlMaxValue_)
andrewm@0 474 roundedControlValue = controlMaxValue_;
andrewm@0 475 if(roundedControlValue < controlMinValue_)
andrewm@0 476 roundedControlValue = controlMinValue_;*/
andrewm@0 477
andrewm@0 478 int roundedControlValue = currentControllerValue(channel);
andrewm@0 479
andrewm@0 480 // If this is the same ultimate CC value as before, don't resend unless forced
andrewm@0 481 if(roundedControlValue == lastOutputValue_[channel] && !force)
andrewm@0 482 return;
andrewm@0 483 lastOutputValue_[channel] = roundedControlValue;
andrewm@0 484
andrewm@0 485 // Four cases: Pitch Wheel messages, aftertouch, 14-bit controls (major and minor controllers), ordinary 7-bit controls
andrewm@0 486 if(controller_ == MidiKeyboardSegment::kControlPitchWheel) {
andrewm@0 487 midiOutputController_->sendPitchWheel(port, channel, roundedControlValue);
andrewm@0 488 }
andrewm@0 489 else if(controller_ == MidiKeyboardSegment::kControlChannelAftertouch) {
andrewm@0 490 midiOutputController_->sendAftertouchChannel(port, channel, roundedControlValue);
andrewm@0 491 }
andrewm@0 492 else if(controller_ == MidiKeyboardSegment::kControlPolyphonicAftertouch && note >= 0) {
andrewm@0 493 midiOutputController_->sendAftertouchPoly(port, channel, note, roundedControlValue);
andrewm@0 494 }
andrewm@0 495 else if(controllerIs14Bit_) {
andrewm@0 496 // LSB for controllers 0-31 are found on controllers 32-63
andrewm@0 497 int lsb = roundedControlValue & 0x007F;
andrewm@0 498 int msb = (roundedControlValue >> 7) & 0x007F;
andrewm@0 499
andrewm@0 500 midiOutputController_->sendControlChange(port, channel, controller_, msb);
andrewm@0 501 midiOutputController_->sendControlChange(port, channel, controller_ + 32, lsb);
andrewm@0 502 }
andrewm@0 503 else { // 7 bit
andrewm@0 504 midiOutputController_->sendControlChange(port, channel, controller_, roundedControlValue);
andrewm@0 505 }
andrewm@0 506 }
andrewm@0 507