andrewm@0: /*
andrewm@0: TouchKeys: multi-touch musical keyboard control software
andrewm@0: Copyright (c) 2013 Andrew McPherson
andrewm@0:
andrewm@0: This program is free software: you can redistribute it and/or modify
andrewm@0: it under the terms of the GNU General Public License as published by
andrewm@0: the Free Software Foundation, either version 3 of the License, or
andrewm@0: (at your option) any later version.
andrewm@0:
andrewm@0: This program is distributed in the hope that it will be useful,
andrewm@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
andrewm@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
andrewm@0: GNU General Public License for more details.
andrewm@0:
andrewm@0: You should have received a copy of the GNU General Public License
andrewm@0: along with this program. If not, see .
andrewm@0:
andrewm@0: =====================================================================
andrewm@0:
andrewm@0: OscMidiConverter.cpp: converts incoming OSC messages to outgoing MIDI
andrewm@0: messages with adjustable ranges and parameters.
andrewm@0: */
andrewm@0: #include "OscMidiConverter.h"
andrewm@0: #include "MidiKeyboardSegment.h"
andrewm@0:
andrewm@44: #undef DEBUG_OSC_MIDI_CONVERTER
andrewm@0:
andrewm@0: // Main constructor: set up OSC reception from the keyboard
andrewm@0: OscMidiConverter::OscMidiConverter(PianoKeyboard& keyboard, MidiKeyboardSegment& segment, int controllerId) :
andrewm@0: keyboard_(keyboard), keyboardSegment_(segment), midiOutputController_(0),
andrewm@0: controller_(controllerId), lastUniqueId_(0),
andrewm@7: incomingController_(MidiKeyboardSegment::kControlDisabled)
andrewm@0: {
andrewm@0: setOscController(&keyboard_);
andrewm@0:
andrewm@0: for(int i = 0; i < 16; i++) {
andrewm@0: currentValue_[i] = 0;
andrewm@0: lastOutputValue_[i] = -1;
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: // Set the type of MIDI message (CC, Pitch Wheel, Aftertouch)
andrewm@0: // that this particular object is in charge of controlling.
andrewm@0: // Optionally specify the default, range, and whether to use a
andrewm@0: // 14 bit controller.
andrewm@0: void OscMidiConverter::setMidiMessageType(int defaultValue, int minValue,
andrewm@0: int maxValue, int centerValue, bool use14BitControl) {
andrewm@0: /*if(controller < 0 || controller >= MidiKeyboardSegment::kControlMax) {
andrewm@0: controller_ = MidiKeyboardSegment::kControlDisabled;
andrewm@0: return;
andrewm@0: }
andrewm@0:
andrewm@0: keyboardSegment_ = segment;*/
andrewm@0:
andrewm@0: // Otherwise, send defaults on the current controller to all channels and update to the new values
andrewm@0: //for(int i = 0; i < 16; i++)
andrewm@0: // sendDefaultValue(i);
andrewm@0:
andrewm@0: // Clear any existing active inputs, but not the mappings themselves
andrewm@0: lastValues_.clear();
andrewm@0:
andrewm@0: //controller_ = controller;
andrewm@0: if(defaultValue >= 0)
andrewm@0: controlDefaultValue_ = defaultValue;
andrewm@0: else {
andrewm@0: // Default value for MIDI CCs and aftertouch is 0
andrewm@0: // Default value for MIDI pitch wheel is 8192
andrewm@0: if(controller_ == MidiKeyboardSegment::kControlPitchWheel)
andrewm@0: controlDefaultValue_ = 8192;
andrewm@0: else
andrewm@0: controlDefaultValue_ = 0;
andrewm@0: }
andrewm@0:
andrewm@0: if(centerValue >= 0)
andrewm@0: controlCenterValue_ = centerValue;
andrewm@43: else {
andrewm@43: // Set center value with same procedure as default value
andrewm@43: if(controller_ == MidiKeyboardSegment::kControlPitchWheel)
andrewm@43: controlCenterValue_ = 8192;
andrewm@43: else
andrewm@43: controlCenterValue_ = 0;
andrewm@43: }
andrewm@0:
andrewm@0: // Pitch wheel is always 14 bit. Aftertouch is always 7 bit.
andrewm@0: // Other CCs are selectable (though according to MIDI spec not every
andrewm@0: // controller has an LSB defined). And above 96 using "control+32"
andrewm@0: // for fine adjust no longer makes sense
andrewm@0: if(controller_ < 96)
andrewm@0: controllerIs14Bit_ = use14BitControl;
andrewm@0: else if(controller_ == MidiKeyboardSegment::kControlPitchWheel)
andrewm@0: controllerIs14Bit_ = true;
andrewm@0: else
andrewm@0: controllerIs14Bit_ = false;
andrewm@0:
andrewm@0: // Handle the outer ranges of the control, providing defaults if unspecified
andrewm@0: if(controllerIs14Bit_) {
andrewm@0: if(maxValue < 0 || maxValue > 16383)
andrewm@0: controlMaxValue_ = 16383;
andrewm@0: else
andrewm@0: controlMaxValue_ = maxValue;
andrewm@0: if(minValue < 0 || minValue > 16383)
andrewm@0: controlMinValue_ = 0;
andrewm@0: else
andrewm@0: controlMinValue_ = minValue;
andrewm@0: }
andrewm@0: else {
andrewm@0: if(maxValue < 0 || maxValue > 127)
andrewm@0: controlMaxValue_ = 127;
andrewm@0: else
andrewm@0: controlMaxValue_ = maxValue;
andrewm@0: if(minValue < 0 || minValue > 127)
andrewm@0: controlMinValue_ = 0;
andrewm@0: else
andrewm@0: controlMinValue_ = minValue;
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: void OscMidiConverter::listenToIncomingControl(int controller, int centerValue, bool use14BitControl) {
andrewm@0: if(controller < 0 || controller >= MidiKeyboardSegment::kControlMax) {
andrewm@0: incomingController_ = MidiKeyboardSegment::kControlDisabled;
andrewm@0: return;
andrewm@0: }
andrewm@0:
andrewm@0: incomingController_ = controller;
andrewm@0:
andrewm@0: // Assign the center value that will be subtacted from the incoming controller
andrewm@0: if(centerValue >= 0)
andrewm@0: incomingControllerCenterValue_ = centerValue;
andrewm@0: else if(controller == MidiKeyboardSegment::kControlPitchWheel)
andrewm@0: incomingControllerCenterValue_ = 8192; // Pitch wheel is centered at 8192
andrewm@0: else
andrewm@0: incomingControllerCenterValue_ = 0;
andrewm@0:
andrewm@0: // Pitch wheel is always 14 bit. Aftertouch is always 7 bit.
andrewm@0: // Other CCs are selectable (though according to MIDI spec not every
andrewm@0: // controller has an LSB defined). And above 96 using "control+32"
andrewm@0: // for fine adjust no longer makes sense
andrewm@0: if(controller < 96)
andrewm@0: incomingControllerIs14Bit_ = use14BitControl;
andrewm@0: else if(controller == MidiKeyboardSegment::kControlPitchWheel)
andrewm@0: incomingControllerIs14Bit_ = true;
andrewm@0: else
andrewm@0: incomingControllerIs14Bit_ = false;
andrewm@0: }
andrewm@0:
andrewm@0: // Resend the most recent value
andrewm@0: void OscMidiConverter::resend(int channel) {
andrewm@0: sendCurrentValue(keyboardSegment_.outputPort(), channel, -1, true);
andrewm@0: }
andrewm@0:
andrewm@0: // Send the default value on the specified channel.
andrewm@0: void OscMidiConverter::sendDefaultValue(int channel) {
andrewm@0: if(midiOutputController_ == 0 || controller_ == MidiKeyboardSegment::kControlDisabled)
andrewm@0: return;
andrewm@0:
andrewm@0: // Modulate the default value by the value of the incoming controller,
andrewm@0: // if one is enabled.
andrewm@0: int defaultValue = controlDefaultValue_;
andrewm@0: if(incomingController_ != MidiKeyboardSegment::kControlDisabled)
andrewm@0: defaultValue += keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_;
andrewm@0:
andrewm@0: if(controller_ == MidiKeyboardSegment::kControlPitchWheel) {
andrewm@7: //sendPitchWheelRange(keyboardSegment_.outputPort(), channel);
andrewm@0: midiOutputController_->sendPitchWheel(keyboardSegment_.outputPort(), channel, defaultValue);
andrewm@0: }
andrewm@0: else if(controller_ == MidiKeyboardSegment::kControlChannelAftertouch) {
andrewm@0: midiOutputController_->sendAftertouchChannel(keyboardSegment_.outputPort(), channel, defaultValue);
andrewm@0: }
andrewm@0: else if(controller_ == MidiKeyboardSegment::kControlPolyphonicAftertouch) {
andrewm@0: // TODO
andrewm@0: }
andrewm@0: else if(controllerIs14Bit_) {
andrewm@0: midiOutputController_->sendControlChange(keyboardSegment_.outputPort(), channel, controller_, defaultValue >> 7);
andrewm@0: midiOutputController_->sendControlChange(keyboardSegment_.outputPort(), channel, controller_ + 32, defaultValue & 0x7F);
andrewm@0: }
andrewm@0: else
andrewm@0: midiOutputController_->sendControlChange(keyboardSegment_.outputPort(), channel, controller_, defaultValue);
andrewm@0: }
andrewm@0:
andrewm@0: int OscMidiConverter::currentControllerValue(int channel) {
andrewm@0: float controlValue = (float)controlCenterValue_ + (float)controlMinValue_
andrewm@0: + currentValue_[channel]*(float)(controlMaxValue_ - controlMinValue_);
andrewm@0:
andrewm@7: if(incomingController_ != MidiKeyboardSegment::kControlDisabled) {
andrewm@0: controlValue += keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_;
andrewm@7: #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@7: std::cout << "current value " << currentValue_[channel] << " corresponds to " << controlValue;
andrewm@7: std::cout << " including incoming value " << (keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_) << std::endl;
andrewm@7: #endif
andrewm@7: }
andrewm@7: else {
andrewm@7: #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@7: std::cout << "current value " << currentValue_[channel] << " corresponds to " << controlValue << std::endl;
andrewm@7: #endif
andrewm@7: }
andrewm@0:
andrewm@41: // For 14-bit CC messages, multiply by 128 first to keep the same apparent range as
andrewm@41: // the 7-bit version but adding extra resolution on the second CC number. This should
andrewm@41: // not be done for pitch wheel where the values are already normalised to 14 bits.
andrewm@41: if(controllerIs14Bit_ && controller_ != MidiKeyboardSegment::kControlPitchWheel)
andrewm@41: controlValue *= 128.0;
andrewm@41:
andrewm@20: int roundedControlValue = (int)floorf(controlValue + 0.5f);
andrewm@41: int maxValue = controllerIs14Bit_ ? 16383 : 127;
andrewm@41: if(roundedControlValue > maxValue)
andrewm@41: roundedControlValue = maxValue;
andrewm@41: if(roundedControlValue < 0)
andrewm@41: roundedControlValue = 0;
andrewm@0:
andrewm@0: return roundedControlValue;
andrewm@0: }
andrewm@0:
andrewm@0: // Add a new OSC input to this MIDI control
andrewm@0: void OscMidiConverter::addControl(const string& oscPath, int oscParamNumber, float oscMinValue,
andrewm@0: float oscMaxValue, float oscCenterValue, int outOfRangeBehavior) {
andrewm@0: // First remove any existing mapping with these exact parameters
andrewm@0: removeControl(oscPath);
andrewm@0:
andrewm@0: #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0: std::cout << "OscMidiConverter: adding path " << oscPath << std::endl;
andrewm@0: #endif
andrewm@0:
andrewm@0: // Insert the mapping
andrewm@0: OscInput input;
andrewm@0:
andrewm@0: input.oscParamNumber = oscParamNumber;
andrewm@0: input.oscMinValue = oscMinValue;
andrewm@0: input.oscMaxValue = oscMaxValue;
andrewm@0:
andrewm@0: // Calculate the normalized center value which will be subtracted
andrewm@0: // from the scaled input. Do this once now to save computation.
andrewm@0: if(oscMinValue == oscMaxValue)
andrewm@7: input.oscScaledCenterValue = 0.5;
andrewm@0: else {
andrewm@0: input.oscScaledCenterValue = (oscCenterValue - oscMinValue) / (oscMaxValue - oscMinValue);
andrewm@0: if(input.oscScaledCenterValue < 0)
andrewm@0: input.oscScaledCenterValue = 0;
andrewm@0: if(input.oscScaledCenterValue > 1.0)
andrewm@0: input.oscScaledCenterValue = 1.0;
andrewm@0: }
andrewm@0: input.outOfRangeBehavior = outOfRangeBehavior;
andrewm@0: input.uniqueId = lastUniqueId_++;
andrewm@0: inputs_[oscPath] = input;
andrewm@0:
andrewm@0: // Register for the relevant OSC message
andrewm@0: addOscListener(oscPath);
andrewm@0: }
andrewm@0:
andrewm@0: // Remove an existing OSC input
andrewm@0: void OscMidiConverter::removeControl(const string& oscPath) {
andrewm@0: // Find the affected control and its ID
andrewm@0: if(inputs_.count(oscPath) == 0)
andrewm@0: return;
andrewm@0: int controlId = inputs_[oscPath].uniqueId;
andrewm@0:
andrewm@0: #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0: std::cout << "OscMidiConverter: removing path " << oscPath << std::endl;
andrewm@0: #endif
andrewm@0:
andrewm@0: // Look for any active inputs on this channel
andrewm@0: for(int i = 0; i < 16; i++) {
andrewm@0: int channelModulatedId = idWithChannel(i, controlId);
andrewm@0: if(lastValues_.count(channelModulatedId) == 0)
andrewm@0: continue;
andrewm@0:
andrewm@0: // Found a last value. Subtract it off and get new value
andrewm@0: float lastValueThisChannel = lastValues_[channelModulatedId];
andrewm@0: currentValue_[i] -= lastValueThisChannel;
andrewm@0:
andrewm@0: // Remove this value from the set of active inputs
andrewm@0: lastValues_.erase(channelModulatedId);
andrewm@0:
andrewm@0: // Send the new value after removing this one
andrewm@0: sendCurrentValue(keyboardSegment_.outputPort(), i, -1, true);
andrewm@0: }
andrewm@0:
andrewm@0: // Having removed any active inputs, now remove the control itself
andrewm@0: // TODO: mutex protection
andrewm@0: inputs_.erase(oscPath);
andrewm@0:
andrewm@0: removeOscListener(oscPath);
andrewm@0: }
andrewm@0:
andrewm@0: void OscMidiConverter::removeAllControls() {
andrewm@0: // Clear all active inputs and send default values to all channels
andrewm@0: for(int i = 0; i < 16; i++)
andrewm@0: sendDefaultValue(i);
andrewm@0: lastValues_.clear();
andrewm@0: inputs_.clear();
andrewm@0: lastUniqueId_ = 0;
andrewm@0: removeAllOscListeners();
andrewm@0: }
andrewm@0:
andrewm@0: // Update the minimum input value of an existing path
andrewm@0: void OscMidiConverter::setControlMinValue(const string& oscPath, float newValue) {
andrewm@0: if(inputs_.count(oscPath) == 0)
andrewm@0: return;
andrewm@0: inputs_[oscPath].oscMinValue = newValue;
andrewm@0: }
andrewm@0:
andrewm@0: // Update the maximum input value of an existing path
andrewm@0: void OscMidiConverter::setControlMaxValue(const string& oscPath, float newValue) {
andrewm@0: if(inputs_.count(oscPath) == 0)
andrewm@0: return;
andrewm@0: inputs_[oscPath].oscMaxValue = newValue;
andrewm@0: }
andrewm@0:
andrewm@0: // Update the center input value of an existing path
andrewm@0: void OscMidiConverter::setControlCenterValue(const string& oscPath, float newValue) {
andrewm@0: if(inputs_.count(oscPath) == 0)
andrewm@0: return;
andrewm@0: float minValue, maxValue, scaledCenterValue;
andrewm@0: minValue = inputs_[oscPath].oscMinValue;
andrewm@0: maxValue = inputs_[oscPath].oscMaxValue;
andrewm@0:
andrewm@7: if(minValue == maxValue)
andrewm@7: scaledCenterValue = 0.0;
andrewm@7: else
andrewm@7: scaledCenterValue = (newValue - minValue) / (maxValue - minValue);
andrewm@0: if(scaledCenterValue < 0)
andrewm@0: scaledCenterValue = 0;
andrewm@0: if(scaledCenterValue > 1.0)
andrewm@0: scaledCenterValue = 1.0;
andrewm@0:
andrewm@0: inputs_[oscPath].oscScaledCenterValue = scaledCenterValue;
andrewm@0: }
andrewm@0:
andrewm@0: // Update the out of range behavior for an existing path
andrewm@0: void OscMidiConverter::setControlOutOfRangeBehavior(const string& oscPath, int newBehavior) {
andrewm@0: if(inputs_.count(oscPath) == 0)
andrewm@0: return;
andrewm@0: inputs_[oscPath].outOfRangeBehavior = newBehavior;
andrewm@0: }
andrewm@0:
andrewm@0: // Reset any active previous values on the given channel
andrewm@0: // 'send' indicated whether to send the value when finished
andrewm@0: // even if it hasn't changed
andrewm@0: void OscMidiConverter::clearLastValues(int channel, bool send) {
andrewm@0: std::map::iterator it = lastValues_.begin();
andrewm@0:
andrewm@0: bool erased = false;
andrewm@0:
andrewm@0: while(it != lastValues_.end()) {
andrewm@0: // Look for any last values matching this channel
andrewm@0: if((it->first & 0x0F) == channel) {
andrewm@0: lastValues_.erase(it++);
andrewm@0: erased = true;
andrewm@0: }
andrewm@0: else
andrewm@0: it++;
andrewm@0: }
andrewm@0:
andrewm@0: currentValue_[channel] = 0;
andrewm@0: lastOutputValue_[channel] = -1;
andrewm@0:
andrewm@0: // If any last values were erased and send is enabled,
andrewm@0: // resend the current values
andrewm@0: if(erased || send)
andrewm@0: sendDefaultValue(channel);
andrewm@0: }
andrewm@0:
andrewm@0: // OSC Handler, called by the data source (PianoKeyboard in this case). Check path against stored
andrewm@0: // inputs and map to MIDI accordingly
andrewm@0:
andrewm@0: bool OscMidiConverter::oscHandlerMethod(const char *path, const char *types, int numValues, lo_arg **values, void *data) {
andrewm@0: #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0: std::cout << "OscMidiConverter: received path " << path << std::endl;
andrewm@0: #endif
andrewm@0: if(midiOutputController_ == 0 || controller_ == MidiKeyboardSegment::kControlDisabled)
andrewm@0: return false;
andrewm@0:
andrewm@0: //double before = Time::getMillisecondCounterHiRes(); // DEBUG
andrewm@0:
andrewm@0: // First value should always be MIDI note number (integer type) so we can retrieve
andrewm@0: // information from the PianoKeyboard class
andrewm@0: if(numValues < 1)
andrewm@0: return false;
andrewm@0: if(types[0] != 'i')
andrewm@0: return false;
andrewm@0: int midiNoteNumber = values[0]->i;
andrewm@0:
andrewm@0: // Get the MIDI retransmission channel from the note number
andrewm@0: if(keyboard_.key(midiNoteNumber) == 0)
andrewm@0: return false;
andrewm@0: int midiChannel = keyboard_.key(midiNoteNumber)->midiChannel();
andrewm@0: if(midiChannel < 0 || midiChannel > 15) {
andrewm@0: #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0: std::cout << "OscMidiConverter: no retransmission channel on note " << midiNoteNumber << std::endl;
andrewm@0: #endif
andrewm@0: return false;
andrewm@0: }
andrewm@0:
andrewm@0: // Find the relevant input and make sure this OSC message has enough parameters
andrewm@0: if(inputs_.count(path) == 0)
andrewm@0: return false;
andrewm@0: OscInput const& input = inputs_[path];
andrewm@0: if(input.oscParamNumber >= numValues)
andrewm@0: return false;
andrewm@0:
andrewm@0: // Find the relevant input value from this OSC message
andrewm@0: float oscParamValue;
andrewm@0: if(types[input.oscParamNumber] == 'f')
andrewm@0: oscParamValue = values[input.oscParamNumber]->f;
andrewm@0: else if(types[input.oscParamNumber] == 'i')
andrewm@0: oscParamValue = (float)values[input.oscParamNumber]->i;
andrewm@0: else
andrewm@0: return false;
andrewm@0:
andrewm@7: // Scale input to a 0-1 range, then to the output range. There's a special case for MIDI pitch wheel,
andrewm@7: // where if the range is set to 0, it means to use the segment-wide pitch wheel range. This is done so
andrewm@7: // we don't have to cache multiple copies of the pitch wheel range in every OSC-MIDI converter.
andrewm@7: float scaledValue;
andrewm@7: if(controller_ == MidiKeyboardSegment::kControlPitchWheel &&
andrewm@7: input.oscMaxValue == 0 && input.oscMinValue == 0) {
andrewm@7: float pitchWheelRange = keyboardSegment_.midiPitchWheelRange();
andrewm@7: scaledValue = (oscParamValue + pitchWheelRange) / (2.0 * pitchWheelRange);
andrewm@7: }
andrewm@7: else
andrewm@7: scaledValue = (oscParamValue - input.oscMinValue) / (input.oscMaxValue - input.oscMinValue);
andrewm@0:
andrewm@0: #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0: std::cout << "port " << keyboardSegment_.outputPort() << " received input " << oscParamValue << " which scales to " << scaledValue << std::endl;
andrewm@0: #endif
andrewm@0:
andrewm@0: // Figure out what to do with an out of range value...
andrewm@0: if(scaledValue < 0.0 || scaledValue > 1.0) {
andrewm@0: if(input.outOfRangeBehavior == kOutOfRangeClip) {
andrewm@0: if(scaledValue < 0.0)
andrewm@0: scaledValue = 0.0;
andrewm@0: if(scaledValue > 1.0)
andrewm@0: scaledValue = 1.0;
andrewm@0: }
andrewm@0: else if(input.outOfRangeBehavior == kOutOfRangeExtrapolate) {
andrewm@0: // Do nothing
andrewm@0: }
andrewm@0: else // Ignore or unknown behavior
andrewm@0: return false;
andrewm@0: }
andrewm@0:
andrewm@0: //double midpoint = Time::getMillisecondCounterHiRes(); // DEBUG
andrewm@0:
andrewm@0: // Now subtract the normalized center value, which may put the range outside 0-1
andrewm@0: // but this is expected. For example, in a pitch wheel situation, we might move
andrewm@0: // to a range of -0.5 to 0.5.
andrewm@0: scaledValue -= input.oscScaledCenterValue;
andrewm@0:
andrewm@0: // Look for previous input with this path and channel and remove it
andrewm@0: int channelModulatedId = idWithChannel(midiChannel, input.uniqueId);
andrewm@0: if(lastValues_.count(channelModulatedId) > 0) {
andrewm@0: // Found a last value. Subtract it off and replace with our current value
andrewm@0: float lastValueThisChannel = lastValues_[channelModulatedId];
andrewm@0: currentValue_[midiChannel] -= lastValueThisChannel;
andrewm@0: #ifdef DEBUG_OSC_MIDI_CONVERTER
andrewm@0: std::cout << "found and removed " << lastValueThisChannel << ", now have " << currentValue_[midiChannel] << std::endl;
andrewm@0: #endif
andrewm@0: }
andrewm@0:
andrewm@0: lastValues_[channelModulatedId] = scaledValue;
andrewm@0: currentValue_[midiChannel] += scaledValue;
andrewm@0:
andrewm@0: // Send the total current value as a MIDI controller
andrewm@0: sendCurrentValue(keyboardSegment_.outputPort(), midiChannel, midiNoteNumber, false);
andrewm@0:
andrewm@0: //double after = Time::getMillisecondCounterHiRes(); // DEBUG
andrewm@0:
andrewm@0: //std::cout << "OscMidiConverter: total " << after - before << "ms, of which " << after - midpoint << " in 2nd half\n";
andrewm@0:
andrewm@0: return true;
andrewm@0: }
andrewm@0:
andrewm@0: // Send the current sum value of all OSC inputs as a MIDI message
andrewm@0: void OscMidiConverter::sendCurrentValue(int port, int channel, int note, bool force) {
andrewm@0: if(midiOutputController_ == 0 || channel < 0 || channel > 15)
andrewm@0: return;
andrewm@0:
andrewm@0: // TODO: what about values that are centered by default e.g. pitch?
andrewm@0: /*float controlValue = (float)controlCenterValue_ + (float)controlMinValue_
andrewm@0: + currentValue_[channel]*(float)(controlMaxValue_ - controlMinValue_);
andrewm@0:
andrewm@0: if(incomingController_ != MidiKeyboardSegment::kControlDisabled)
andrewm@0: controlValue += keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_;
andrewm@0:
andrewm@0: std::cout << "sending value " << currentValue_[channel] << " as " << controlValue << std::endl;
andrewm@0: int roundedControlValue = (int)roundf(controlValue);
andrewm@0: if(roundedControlValue > controlMaxValue_)
andrewm@0: roundedControlValue = controlMaxValue_;
andrewm@0: if(roundedControlValue < controlMinValue_)
andrewm@0: roundedControlValue = controlMinValue_;*/
andrewm@0:
andrewm@0: int roundedControlValue = currentControllerValue(channel);
andrewm@0:
andrewm@0: // If this is the same ultimate CC value as before, don't resend unless forced
andrewm@0: if(roundedControlValue == lastOutputValue_[channel] && !force)
andrewm@0: return;
andrewm@0: lastOutputValue_[channel] = roundedControlValue;
andrewm@0:
andrewm@0: // Four cases: Pitch Wheel messages, aftertouch, 14-bit controls (major and minor controllers), ordinary 7-bit controls
andrewm@0: if(controller_ == MidiKeyboardSegment::kControlPitchWheel) {
andrewm@0: midiOutputController_->sendPitchWheel(port, channel, roundedControlValue);
andrewm@0: }
andrewm@0: else if(controller_ == MidiKeyboardSegment::kControlChannelAftertouch) {
andrewm@0: midiOutputController_->sendAftertouchChannel(port, channel, roundedControlValue);
andrewm@0: }
andrewm@0: else if(controller_ == MidiKeyboardSegment::kControlPolyphonicAftertouch && note >= 0) {
andrewm@28: midiOutputController_->sendAftertouchPoly(port, channel, note + keyboardSegment_.outputTransposition(),
andrewm@28: roundedControlValue);
andrewm@0: }
andrewm@0: else if(controllerIs14Bit_) {
andrewm@0: // LSB for controllers 0-31 are found on controllers 32-63
andrewm@0: int lsb = roundedControlValue & 0x007F;
andrewm@0: int msb = (roundedControlValue >> 7) & 0x007F;
andrewm@0:
andrewm@0: midiOutputController_->sendControlChange(port, channel, controller_, msb);
andrewm@0: midiOutputController_->sendControlChange(port, channel, controller_ + 32, lsb);
andrewm@0: }
andrewm@0: else { // 7 bit
andrewm@0: midiOutputController_->sendControlChange(port, channel, controller_, roundedControlValue);
andrewm@0: }
andrewm@0: }
andrewm@0: