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@41: #include "TouchkeyControlMappingExtendedEditor.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@41: int TouchkeyControlMappingFactory::getDirection() {
andrewm@41: // Get the direction of motion. This is always positive for
andrewm@41: if(inputType_ == TouchkeyControlMapping::kTypeAbsolute)
andrewm@41: return TouchkeyControlMapping::kDirectionPositive;
andrewm@41: return direction_;
andrewm@41: }
andrewm@41:
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@41: outputDefault_, outputRangeMin_, outputRangeMax_,
andrewm@41: -1, use14BitControl_, outOfRangeBehavior_);
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::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@41: outputDefault_, outputRangeMin_, outputRangeMax_,
andrewm@41: -1, use14BitControl_, outOfRangeBehavior_);
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@41: outputDefault_, outputRangeMin_, outputRangeMax_,
andrewm@41: -1, use14BitControl_, outOfRangeBehavior_);
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@41: outputDefault_, outputRangeMin_, outputRangeMax_,
andrewm@41: -1, use14BitControl_, outOfRangeBehavior_);
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@41: outputDefault_, outputRangeMin_, outputRangeMax_,
andrewm@41: -1, use14BitControl_, outOfRangeBehavior_);
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@41: outputDefault_, outputRangeMin_, outputRangeMax_,
andrewm@41: -1, use14BitControl_, outOfRangeBehavior_);
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@41: outputDefault_, outputRangeMin_, outputRangeMax_,
andrewm@41: -1, use14BitControl_, outOfRangeBehavior_);
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@41: void TouchkeyControlMappingFactory::setOutOfRangeBehavior(int behavior) {
andrewm@41: outOfRangeBehavior_ = behavior;
andrewm@41:
andrewm@41: setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_,
andrewm@41: outputDefault_, outputRangeMin_, outputRangeMax_,
andrewm@41: -1, use14BitControl_, outOfRangeBehavior_);
andrewm@41: }
andrewm@41:
andrewm@41: void TouchkeyControlMappingFactory::setUses14BitControl(bool use) {
andrewm@41: use14BitControl_ = use;
andrewm@41:
andrewm@41: setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_,
andrewm@41: outputDefault_, outputRangeMin_, outputRangeMax_,
andrewm@41: -1, use14BitControl_, outOfRangeBehavior_);
andrewm@41: }
andrewm@41:
andrewm@0: // ***** GUI Support *****
andrewm@0: MappingEditorComponent* TouchkeyControlMappingFactory::createBasicEditor() {
andrewm@0: return new TouchkeyControlMappingShortEditor(*this);
andrewm@0: }
andrewm@0:
andrewm@41: MappingEditorComponent* TouchkeyControlMappingFactory::createExtendedEditor() {
andrewm@41: return new TouchkeyControlMappingExtendedEditor(*this);
andrewm@41: }
andrewm@41:
andrewm@41:
andrewm@33: // ****** Preset Save/Load ******
andrewm@33: XmlElement* TouchkeyControlMappingFactory::getPreset() {
andrewm@34: PropertySet properties;
andrewm@34:
andrewm@34: storeCommonProperties(properties);
andrewm@34: properties.setValue("inputParameter", inputParameter_);
andrewm@34: properties.setValue("inputType", inputType_);
andrewm@34: properties.setValue("outputRangeMin", outputRangeMin_);
andrewm@34: properties.setValue("outputRangeMax", outputRangeMax_);
andrewm@34: properties.setValue("outputDefault", outputDefault_);
andrewm@34: properties.setValue("threshold", threshold_);
andrewm@34: properties.setValue("ignoresTwoFingers", ignoresTwoFingers_);
andrewm@34: properties.setValue("ignoresThreeFingers", ignoresThreeFingers_);
andrewm@34: properties.setValue("direction", direction_);
andrewm@34:
andrewm@34: XmlElement* preset = properties.createXml("MappingFactory");
andrewm@33: preset->setAttribute("type", "Control");
andrewm@34:
andrewm@33: return preset;
andrewm@33: }
andrewm@33:
andrewm@33: bool TouchkeyControlMappingFactory::loadPreset(XmlElement const* preset) {
andrewm@34: if(preset == 0)
andrewm@34: return false;
andrewm@34:
andrewm@34: PropertySet properties;
andrewm@34: properties.restoreFromXml(*preset);
andrewm@34:
andrewm@34: if(!loadCommonProperties(properties))
andrewm@34: return false;
andrewm@34: if(!properties.containsKey("inputParameter") ||
andrewm@34: !properties.containsKey("inputType") ||
andrewm@34: !properties.containsKey("outputRangeMin") ||
andrewm@34: !properties.containsKey("outputRangeMax") ||
andrewm@34: !properties.containsKey("outputDefault") ||
andrewm@34: !properties.containsKey("threshold") ||
andrewm@34: !properties.containsKey("ignoresTwoFingers") ||
andrewm@34: !properties.containsKey("ignoresThreeFingers") ||
andrewm@34: !properties.containsKey("direction"))
andrewm@34: return false;
andrewm@34:
andrewm@34: inputParameter_ = properties.getIntValue("inputParameter");
andrewm@34: inputType_ = properties.getIntValue("inputType");
andrewm@35: outputRangeMin_ = properties.getDoubleValue("outputRangeMin");
andrewm@35: outputRangeMax_ = properties.getDoubleValue("outputRangeMax");
andrewm@35: outputDefault_ = properties.getDoubleValue("outputDefault");
andrewm@35: threshold_ = properties.getDoubleValue("threshold");
andrewm@35: ignoresTwoFingers_ = properties.getBoolValue("ignoresTwoFingers");
andrewm@35: ignoresThreeFingers_ = properties.getBoolValue("ignoresThreeFingers");
andrewm@34: direction_ = properties.getIntValue("direction");
andrewm@34:
andrewm@34: // Update MIDI information; this doesn't actually change the controller
andrewm@34: // (which is already set) but it adds a listener and updates the ranges
andrewm@34: setController(midiControllerNumber_);
andrewm@34:
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: }