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: TouchkeyControlMappingFactory.cpp: factory for the TouchKeys control andrewm@0: mapping, which converts an arbitrary touch parameter into a MIDI or andrewm@0: OSC control message. andrewm@0: */ andrewm@0: andrewm@0: #include "TouchkeyControlMappingFactory.h" andrewm@0: #include "TouchkeyControlMappingShortEditor.h" andrewm@0: andrewm@0: const int TouchkeyControlMappingFactory::kDefaultController = 1; andrewm@0: const float TouchkeyControlMappingFactory::kDefaultOutputRangeMin = 0.0; andrewm@0: const float TouchkeyControlMappingFactory::kDefaultOutputRangeMax = 127.0; andrewm@0: const float TouchkeyControlMappingFactory::kDefaultOutputDefault = 0.0; andrewm@0: andrewm@0: TouchkeyControlMappingFactory::TouchkeyControlMappingFactory(PianoKeyboard &keyboard, MidiKeyboardSegment& segment) : andrewm@0: TouchkeyBaseMappingFactory(keyboard, segment), andrewm@0: inputParameter_(TouchkeyControlMapping::kInputParameterYPosition), andrewm@0: inputType_(TouchkeyControlMapping::kTypeAbsolute), andrewm@0: outputRangeMin_(kDefaultOutputRangeMin), outputRangeMax_(kDefaultOutputRangeMax), andrewm@0: outputDefault_(kDefaultOutputDefault), threshold_(0.0), andrewm@0: ignoresTwoFingers_(TouchkeyControlMapping::kDefaultIgnoresTwoFingers), andrewm@0: ignoresThreeFingers_(TouchkeyControlMapping::kDefaultIgnoresThreeFingers), andrewm@0: direction_(TouchkeyControlMapping::kDefaultDirection) andrewm@0: { andrewm@0: setController(kDefaultController); andrewm@0: } andrewm@0: andrewm@0: // ***** Destructor ***** andrewm@0: andrewm@0: TouchkeyControlMappingFactory::~TouchkeyControlMappingFactory() { andrewm@0: andrewm@0: } andrewm@0: andrewm@0: // ***** Accessors / Modifiers ***** andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setInputParameter(int inputParameter) { andrewm@0: inputParameter_ = inputParameter; andrewm@0: } andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setInputType(int inputType) { andrewm@0: inputType_ = inputType; andrewm@0: } andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setController(int controller) { andrewm@0: // Before changing the controller, check if we were going to or from the pitch wheel. andrewm@0: // If so, we should scale the value to or from a 14-bit value andrewm@0: if(midiControllerNumber_ == MidiKeyboardSegment::kControlPitchWheel && andrewm@0: controller != MidiKeyboardSegment::kControlPitchWheel) { andrewm@0: outputRangeMax_ = outputRangeMax_ / 128.0; andrewm@0: if(outputRangeMax_ > 127.0) andrewm@0: outputRangeMax_ = 127.0; andrewm@0: outputRangeMin_ = outputRangeMin_ / 128.0; andrewm@0: if(outputRangeMin_ > 127.0) andrewm@0: outputRangeMin_ = 127.0; andrewm@0: outputDefault_ = outputDefault_ / 128.0; andrewm@0: if(outputDefault_ > 127.0) andrewm@0: outputDefault_ = 127.0; andrewm@0: } andrewm@0: else if(midiControllerNumber_ != MidiKeyboardSegment::kControlPitchWheel && andrewm@0: controller == MidiKeyboardSegment::kControlPitchWheel) { andrewm@0: if(outputRangeMax_ == 127.0) andrewm@0: outputRangeMax_ = 16383.0; andrewm@0: else andrewm@0: outputRangeMax_ = outputRangeMax_ * 128.0; andrewm@0: if(outputRangeMin_ == 127.0) andrewm@0: outputRangeMin_ = 16383.0; andrewm@0: else andrewm@0: outputRangeMin_ = outputRangeMin_ * 128.0; andrewm@0: if(outputDefault_ == 127.0) andrewm@0: outputDefault_ = 16383.0; andrewm@0: else andrewm@0: outputDefault_ = outputDefault_ * 128.0; andrewm@0: } andrewm@0: andrewm@0: setMidiParameters(controller, inputRangeMin_, inputRangeMax_, inputRangeCenter_, andrewm@0: outputDefault_, outputRangeMin_, outputRangeMax_); andrewm@0: andrewm@0: // Listen to incoming controls from the keyboard too, if this is enabled andrewm@0: // in MidiKeyboardSegment andrewm@0: if(midiConverter_ != 0) { andrewm@0: midiConverter_->listenToIncomingControl(midiControllerNumber_); andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: /*void TouchkeyControlMappingFactory::setRange(float inputMin, float inputMax, float inputCenter, float outputMin, float outputMax, float outputDefault) { andrewm@0: // All possible input parameters are in the range [-1, 1], andrewm@0: // and actually in the range [0, 1] if absolute values are used andrewm@0: // (though we don't check this here) andrewm@0: if(inputMin < -1.0) andrewm@0: inputRangeMin_ = -1.0; andrewm@0: else if(inputMin > 1.0) andrewm@0: inputRangeMin_ = 1.0; andrewm@0: else andrewm@0: inputRangeMin_ = inputMin; andrewm@0: if(inputMax < -1.0) andrewm@0: inputRangeMax_ = -1.0; andrewm@0: else if(inputMax > 1.0) andrewm@0: inputRangeMax_ = 1.0; andrewm@0: else andrewm@0: inputRangeMax_ = inputMax; andrewm@0: if(inputCenter < -1.0) andrewm@0: inputRangeCenter_ = -1.0; andrewm@0: else if(inputCenter > 1.0) andrewm@0: inputRangeCenter_ = 1.0; andrewm@0: else andrewm@0: inputRangeCenter_ = inputCenter; andrewm@0: outputRangeMin_ = outputMin; andrewm@0: outputRangeMax_ = outputMax; andrewm@0: outputDefault_ = outputDefault; andrewm@0: andrewm@0: // Update control andrewm@0: if(midiConverter_ == 0) andrewm@0: return; andrewm@0: midiConverter_->removeAllControls(); andrewm@0: midiConverter_->addControl(controlName_.c_str(), 1, inputRangeMin_, inputRangeMax_, inputRangeCenter_, OscMidiConverter::kOutOfRangeClip); andrewm@0: }*/ andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setRangeInputMin(float inputMin) { andrewm@0: if(inputMin < -1.0) andrewm@0: inputRangeMin_ = -1.0; andrewm@0: else if(inputMin > 1.0) andrewm@0: inputRangeMin_ = 1.0; andrewm@0: else andrewm@0: inputRangeMin_ = inputMin; andrewm@0: andrewm@0: // Update control andrewm@0: //if(midiConverter_ == 0) andrewm@0: // return; andrewm@0: //midiConverter_->setControlMinValue(controlName_.c_str(), inputRangeMin_); andrewm@0: setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_, andrewm@0: outputDefault_, outputRangeMin_, outputRangeMax_); andrewm@0: } andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setRangeInputMax(float inputMax) { andrewm@0: if(inputMax < -1.0) andrewm@0: inputRangeMax_ = -1.0; andrewm@0: else if(inputMax > 1.0) andrewm@0: inputRangeMax_ = 1.0; andrewm@0: else andrewm@0: inputRangeMax_ = inputMax; andrewm@0: andrewm@0: // Update control andrewm@0: //if(midiConverter_ == 0) andrewm@0: // return; andrewm@0: //midiConverter_->setControlMaxValue(controlName_.c_str(), inputRangeMax_); andrewm@0: setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_, andrewm@0: outputDefault_, outputRangeMin_, outputRangeMax_); andrewm@0: } andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setRangeInputCenter(float inputCenter) { andrewm@0: if(inputCenter < -1.0) andrewm@0: inputRangeCenter_ = -1.0; andrewm@0: else if(inputCenter > 1.0) andrewm@0: inputRangeCenter_ = 1.0; andrewm@0: else andrewm@0: inputRangeCenter_ = inputCenter; andrewm@0: andrewm@0: // Update control andrewm@0: //if(midiConverter_ == 0) andrewm@0: // return; andrewm@0: //midiConverter_->setControlCenterValue(controlName_.c_str(), inputRangeCenter_); andrewm@0: setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_, andrewm@0: outputDefault_, outputRangeMin_, outputRangeMax_); andrewm@0: } andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setRangeOutputMin(float outputMin) { andrewm@0: outputRangeMin_ = outputMin; andrewm@0: andrewm@0: setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_, andrewm@0: outputDefault_, outputRangeMin_, outputRangeMax_); andrewm@0: } andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setRangeOutputMax(float outputMax) { andrewm@0: outputRangeMax_ = outputMax; andrewm@0: andrewm@0: setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_, andrewm@0: outputDefault_, outputRangeMin_, outputRangeMax_); andrewm@0: } andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setRangeOutputDefault(float outputDefault) { andrewm@0: outputDefault_ = outputDefault; andrewm@0: andrewm@0: setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_, andrewm@0: outputDefault_, outputRangeMin_, outputRangeMax_); andrewm@0: } andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setThreshold(float threshold) { andrewm@0: threshold_ = threshold; andrewm@0: } andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setIgnoresTwoFingers(bool ignoresTwo) { andrewm@0: ignoresTwoFingers_ = ignoresTwo; andrewm@0: } andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setIgnoresThreeFingers(bool ignoresThree) { andrewm@0: ignoresThreeFingers_ = ignoresThree; andrewm@0: } andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::setDirection(int direction) { andrewm@0: direction_ = direction; andrewm@0: } andrewm@0: andrewm@0: // ***** GUI Support ***** andrewm@0: MappingEditorComponent* TouchkeyControlMappingFactory::createBasicEditor() { andrewm@0: return new TouchkeyControlMappingShortEditor(*this); andrewm@0: } andrewm@0: andrewm@33: // ****** Preset Save/Load ****** andrewm@33: XmlElement* TouchkeyControlMappingFactory::getPreset() { andrewm@33: XmlElement* preset = new XmlElement("MappingFactory"); andrewm@33: preset->setAttribute("type", "Control"); andrewm@33: return preset; andrewm@33: } andrewm@33: andrewm@33: bool TouchkeyControlMappingFactory::loadPreset(XmlElement const* preset) { andrewm@33: return true; andrewm@33: } andrewm@33: andrewm@0: // ***** Private Methods ***** andrewm@0: andrewm@0: void TouchkeyControlMappingFactory::initializeMappingParameters(int noteNumber, TouchkeyControlMapping *mapping) { andrewm@0: // Set parameters andrewm@0: mapping->setInputParameter(inputParameter_, inputType_); andrewm@0: mapping->setRange(inputRangeMin_, inputRangeMax_, outputRangeMin_, outputRangeMax_, outputDefault_); andrewm@0: mapping->setThreshold(threshold_); andrewm@0: mapping->setIgnoresMultipleFingers(ignoresTwoFingers_, ignoresThreeFingers_); andrewm@0: mapping->setDirection(direction_); andrewm@0: }