comparison Source/Mappings/Control/TouchkeyControlMappingFactory.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 e8965409903e
comparison
equal deleted inserted replaced
-1:000000000000 0:3580ffe87dc8
1 /*
2 TouchKeys: multi-touch musical keyboard control software
3 Copyright (c) 2013 Andrew McPherson
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 =====================================================================
19
20 TouchkeyControlMappingFactory.cpp: factory for the TouchKeys control
21 mapping, which converts an arbitrary touch parameter into a MIDI or
22 OSC control message.
23 */
24
25 #include "TouchkeyControlMappingFactory.h"
26 #include "TouchkeyControlMappingShortEditor.h"
27
28 const int TouchkeyControlMappingFactory::kDefaultController = 1;
29 const float TouchkeyControlMappingFactory::kDefaultOutputRangeMin = 0.0;
30 const float TouchkeyControlMappingFactory::kDefaultOutputRangeMax = 127.0;
31 const float TouchkeyControlMappingFactory::kDefaultOutputDefault = 0.0;
32
33 TouchkeyControlMappingFactory::TouchkeyControlMappingFactory(PianoKeyboard &keyboard, MidiKeyboardSegment& segment) :
34 TouchkeyBaseMappingFactory<TouchkeyControlMapping>(keyboard, segment),
35 inputParameter_(TouchkeyControlMapping::kInputParameterYPosition),
36 inputType_(TouchkeyControlMapping::kTypeAbsolute),
37 outputRangeMin_(kDefaultOutputRangeMin), outputRangeMax_(kDefaultOutputRangeMax),
38 outputDefault_(kDefaultOutputDefault), threshold_(0.0),
39 ignoresTwoFingers_(TouchkeyControlMapping::kDefaultIgnoresTwoFingers),
40 ignoresThreeFingers_(TouchkeyControlMapping::kDefaultIgnoresThreeFingers),
41 direction_(TouchkeyControlMapping::kDefaultDirection)
42 {
43 setController(kDefaultController);
44 }
45
46 // ***** Destructor *****
47
48 TouchkeyControlMappingFactory::~TouchkeyControlMappingFactory() {
49
50 }
51
52 // ***** Accessors / Modifiers *****
53
54 void TouchkeyControlMappingFactory::setInputParameter(int inputParameter) {
55 inputParameter_ = inputParameter;
56 }
57
58 void TouchkeyControlMappingFactory::setInputType(int inputType) {
59 inputType_ = inputType;
60 }
61
62 void TouchkeyControlMappingFactory::setController(int controller) {
63 // Before changing the controller, check if we were going to or from the pitch wheel.
64 // If so, we should scale the value to or from a 14-bit value
65 if(midiControllerNumber_ == MidiKeyboardSegment::kControlPitchWheel &&
66 controller != MidiKeyboardSegment::kControlPitchWheel) {
67 outputRangeMax_ = outputRangeMax_ / 128.0;
68 if(outputRangeMax_ > 127.0)
69 outputRangeMax_ = 127.0;
70 outputRangeMin_ = outputRangeMin_ / 128.0;
71 if(outputRangeMin_ > 127.0)
72 outputRangeMin_ = 127.0;
73 outputDefault_ = outputDefault_ / 128.0;
74 if(outputDefault_ > 127.0)
75 outputDefault_ = 127.0;
76 }
77 else if(midiControllerNumber_ != MidiKeyboardSegment::kControlPitchWheel &&
78 controller == MidiKeyboardSegment::kControlPitchWheel) {
79 if(outputRangeMax_ == 127.0)
80 outputRangeMax_ = 16383.0;
81 else
82 outputRangeMax_ = outputRangeMax_ * 128.0;
83 if(outputRangeMin_ == 127.0)
84 outputRangeMin_ = 16383.0;
85 else
86 outputRangeMin_ = outputRangeMin_ * 128.0;
87 if(outputDefault_ == 127.0)
88 outputDefault_ = 16383.0;
89 else
90 outputDefault_ = outputDefault_ * 128.0;
91 }
92
93 setMidiParameters(controller, inputRangeMin_, inputRangeMax_, inputRangeCenter_,
94 outputDefault_, outputRangeMin_, outputRangeMax_);
95
96 // Listen to incoming controls from the keyboard too, if this is enabled
97 // in MidiKeyboardSegment
98 if(midiConverter_ != 0) {
99 midiConverter_->listenToIncomingControl(midiControllerNumber_);
100 }
101 }
102
103 /*void TouchkeyControlMappingFactory::setRange(float inputMin, float inputMax, float inputCenter, float outputMin, float outputMax, float outputDefault) {
104 // All possible input parameters are in the range [-1, 1],
105 // and actually in the range [0, 1] if absolute values are used
106 // (though we don't check this here)
107 if(inputMin < -1.0)
108 inputRangeMin_ = -1.0;
109 else if(inputMin > 1.0)
110 inputRangeMin_ = 1.0;
111 else
112 inputRangeMin_ = inputMin;
113 if(inputMax < -1.0)
114 inputRangeMax_ = -1.0;
115 else if(inputMax > 1.0)
116 inputRangeMax_ = 1.0;
117 else
118 inputRangeMax_ = inputMax;
119 if(inputCenter < -1.0)
120 inputRangeCenter_ = -1.0;
121 else if(inputCenter > 1.0)
122 inputRangeCenter_ = 1.0;
123 else
124 inputRangeCenter_ = inputCenter;
125 outputRangeMin_ = outputMin;
126 outputRangeMax_ = outputMax;
127 outputDefault_ = outputDefault;
128
129 // Update control
130 if(midiConverter_ == 0)
131 return;
132 midiConverter_->removeAllControls();
133 midiConverter_->addControl(controlName_.c_str(), 1, inputRangeMin_, inputRangeMax_, inputRangeCenter_, OscMidiConverter::kOutOfRangeClip);
134 }*/
135
136 void TouchkeyControlMappingFactory::setRangeInputMin(float inputMin) {
137 if(inputMin < -1.0)
138 inputRangeMin_ = -1.0;
139 else if(inputMin > 1.0)
140 inputRangeMin_ = 1.0;
141 else
142 inputRangeMin_ = inputMin;
143
144 // Update control
145 //if(midiConverter_ == 0)
146 // return;
147 //midiConverter_->setControlMinValue(controlName_.c_str(), inputRangeMin_);
148 setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_,
149 outputDefault_, outputRangeMin_, outputRangeMax_);
150 }
151
152 void TouchkeyControlMappingFactory::setRangeInputMax(float inputMax) {
153 if(inputMax < -1.0)
154 inputRangeMax_ = -1.0;
155 else if(inputMax > 1.0)
156 inputRangeMax_ = 1.0;
157 else
158 inputRangeMax_ = inputMax;
159
160 // Update control
161 //if(midiConverter_ == 0)
162 // return;
163 //midiConverter_->setControlMaxValue(controlName_.c_str(), inputRangeMax_);
164 setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_,
165 outputDefault_, outputRangeMin_, outputRangeMax_);
166 }
167
168 void TouchkeyControlMappingFactory::setRangeInputCenter(float inputCenter) {
169 if(inputCenter < -1.0)
170 inputRangeCenter_ = -1.0;
171 else if(inputCenter > 1.0)
172 inputRangeCenter_ = 1.0;
173 else
174 inputRangeCenter_ = inputCenter;
175
176 // Update control
177 //if(midiConverter_ == 0)
178 // return;
179 //midiConverter_->setControlCenterValue(controlName_.c_str(), inputRangeCenter_);
180 setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_,
181 outputDefault_, outputRangeMin_, outputRangeMax_);
182 }
183
184 void TouchkeyControlMappingFactory::setRangeOutputMin(float outputMin) {
185 outputRangeMin_ = outputMin;
186
187 setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_,
188 outputDefault_, outputRangeMin_, outputRangeMax_);
189 }
190
191 void TouchkeyControlMappingFactory::setRangeOutputMax(float outputMax) {
192 outputRangeMax_ = outputMax;
193
194 setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_,
195 outputDefault_, outputRangeMin_, outputRangeMax_);
196 }
197
198 void TouchkeyControlMappingFactory::setRangeOutputDefault(float outputDefault) {
199 outputDefault_ = outputDefault;
200
201 setMidiParameters(midiControllerNumber_, inputRangeMin_, inputRangeMax_, inputRangeCenter_,
202 outputDefault_, outputRangeMin_, outputRangeMax_);
203 }
204
205 void TouchkeyControlMappingFactory::setThreshold(float threshold) {
206 threshold_ = threshold;
207 }
208
209 void TouchkeyControlMappingFactory::setIgnoresTwoFingers(bool ignoresTwo) {
210 ignoresTwoFingers_ = ignoresTwo;
211 }
212
213 void TouchkeyControlMappingFactory::setIgnoresThreeFingers(bool ignoresThree) {
214 ignoresThreeFingers_ = ignoresThree;
215 }
216
217 void TouchkeyControlMappingFactory::setDirection(int direction) {
218 direction_ = direction;
219 }
220
221 // ***** GUI Support *****
222 MappingEditorComponent* TouchkeyControlMappingFactory::createBasicEditor() {
223 return new TouchkeyControlMappingShortEditor(*this);
224 }
225
226 // ***** Private Methods *****
227
228 void TouchkeyControlMappingFactory::initializeMappingParameters(int noteNumber, TouchkeyControlMapping *mapping) {
229 // Set parameters
230 mapping->setInputParameter(inputParameter_, inputType_);
231 mapping->setRange(inputRangeMin_, inputRangeMax_, outputRangeMin_, outputRangeMax_, outputDefault_);
232 mapping->setThreshold(threshold_);
233 mapping->setIgnoresMultipleFingers(ignoresTwoFingers_, ignoresThreeFingers_);
234 mapping->setDirection(direction_);
235 }