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@44
|
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@43
|
76 else {
|
andrewm@43
|
77 // Set center value with same procedure as default value
|
andrewm@43
|
78 if(controller_ == MidiKeyboardSegment::kControlPitchWheel)
|
andrewm@43
|
79 controlCenterValue_ = 8192;
|
andrewm@43
|
80 else
|
andrewm@43
|
81 controlCenterValue_ = 0;
|
andrewm@43
|
82 }
|
andrewm@0
|
83
|
andrewm@0
|
84 // Pitch wheel is always 14 bit. Aftertouch is always 7 bit.
|
andrewm@0
|
85 // Other CCs are selectable (though according to MIDI spec not every
|
andrewm@0
|
86 // controller has an LSB defined). And above 96 using "control+32"
|
andrewm@0
|
87 // for fine adjust no longer makes sense
|
andrewm@0
|
88 if(controller_ < 96)
|
andrewm@0
|
89 controllerIs14Bit_ = use14BitControl;
|
andrewm@0
|
90 else if(controller_ == MidiKeyboardSegment::kControlPitchWheel)
|
andrewm@0
|
91 controllerIs14Bit_ = true;
|
andrewm@0
|
92 else
|
andrewm@0
|
93 controllerIs14Bit_ = false;
|
andrewm@0
|
94
|
andrewm@0
|
95 // Handle the outer ranges of the control, providing defaults if unspecified
|
andrewm@0
|
96 if(controllerIs14Bit_) {
|
andrewm@0
|
97 if(maxValue < 0 || maxValue > 16383)
|
andrewm@0
|
98 controlMaxValue_ = 16383;
|
andrewm@0
|
99 else
|
andrewm@0
|
100 controlMaxValue_ = maxValue;
|
andrewm@0
|
101 if(minValue < 0 || minValue > 16383)
|
andrewm@0
|
102 controlMinValue_ = 0;
|
andrewm@0
|
103 else
|
andrewm@0
|
104 controlMinValue_ = minValue;
|
andrewm@0
|
105 }
|
andrewm@0
|
106 else {
|
andrewm@0
|
107 if(maxValue < 0 || maxValue > 127)
|
andrewm@0
|
108 controlMaxValue_ = 127;
|
andrewm@0
|
109 else
|
andrewm@0
|
110 controlMaxValue_ = maxValue;
|
andrewm@0
|
111 if(minValue < 0 || minValue > 127)
|
andrewm@0
|
112 controlMinValue_ = 0;
|
andrewm@0
|
113 else
|
andrewm@0
|
114 controlMinValue_ = minValue;
|
andrewm@0
|
115 }
|
andrewm@0
|
116 }
|
andrewm@0
|
117
|
andrewm@0
|
118 void OscMidiConverter::listenToIncomingControl(int controller, int centerValue, bool use14BitControl) {
|
andrewm@0
|
119 if(controller < 0 || controller >= MidiKeyboardSegment::kControlMax) {
|
andrewm@0
|
120 incomingController_ = MidiKeyboardSegment::kControlDisabled;
|
andrewm@0
|
121 return;
|
andrewm@0
|
122 }
|
andrewm@0
|
123
|
andrewm@0
|
124 incomingController_ = controller;
|
andrewm@0
|
125
|
andrewm@0
|
126 // Assign the center value that will be subtacted from the incoming controller
|
andrewm@0
|
127 if(centerValue >= 0)
|
andrewm@0
|
128 incomingControllerCenterValue_ = centerValue;
|
andrewm@0
|
129 else if(controller == MidiKeyboardSegment::kControlPitchWheel)
|
andrewm@0
|
130 incomingControllerCenterValue_ = 8192; // Pitch wheel is centered at 8192
|
andrewm@0
|
131 else
|
andrewm@0
|
132 incomingControllerCenterValue_ = 0;
|
andrewm@0
|
133
|
andrewm@0
|
134 // Pitch wheel is always 14 bit. Aftertouch is always 7 bit.
|
andrewm@0
|
135 // Other CCs are selectable (though according to MIDI spec not every
|
andrewm@0
|
136 // controller has an LSB defined). And above 96 using "control+32"
|
andrewm@0
|
137 // for fine adjust no longer makes sense
|
andrewm@0
|
138 if(controller < 96)
|
andrewm@0
|
139 incomingControllerIs14Bit_ = use14BitControl;
|
andrewm@0
|
140 else if(controller == MidiKeyboardSegment::kControlPitchWheel)
|
andrewm@0
|
141 incomingControllerIs14Bit_ = true;
|
andrewm@0
|
142 else
|
andrewm@0
|
143 incomingControllerIs14Bit_ = false;
|
andrewm@0
|
144 }
|
andrewm@0
|
145
|
andrewm@0
|
146 // Resend the most recent value
|
andrewm@0
|
147 void OscMidiConverter::resend(int channel) {
|
andrewm@0
|
148 sendCurrentValue(keyboardSegment_.outputPort(), channel, -1, true);
|
andrewm@0
|
149 }
|
andrewm@0
|
150
|
andrewm@0
|
151 // Send the default value on the specified channel.
|
andrewm@0
|
152 void OscMidiConverter::sendDefaultValue(int channel) {
|
andrewm@0
|
153 if(midiOutputController_ == 0 || controller_ == MidiKeyboardSegment::kControlDisabled)
|
andrewm@0
|
154 return;
|
andrewm@0
|
155
|
andrewm@0
|
156 // Modulate the default value by the value of the incoming controller,
|
andrewm@0
|
157 // if one is enabled.
|
andrewm@0
|
158 int defaultValue = controlDefaultValue_;
|
andrewm@0
|
159 if(incomingController_ != MidiKeyboardSegment::kControlDisabled)
|
andrewm@0
|
160 defaultValue += keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_;
|
andrewm@0
|
161
|
andrewm@0
|
162 if(controller_ == MidiKeyboardSegment::kControlPitchWheel) {
|
andrewm@7
|
163 //sendPitchWheelRange(keyboardSegment_.outputPort(), channel);
|
andrewm@0
|
164 midiOutputController_->sendPitchWheel(keyboardSegment_.outputPort(), channel, defaultValue);
|
andrewm@0
|
165 }
|
andrewm@0
|
166 else if(controller_ == MidiKeyboardSegment::kControlChannelAftertouch) {
|
andrewm@0
|
167 midiOutputController_->sendAftertouchChannel(keyboardSegment_.outputPort(), channel, defaultValue);
|
andrewm@0
|
168 }
|
andrewm@0
|
169 else if(controller_ == MidiKeyboardSegment::kControlPolyphonicAftertouch) {
|
andrewm@0
|
170 // TODO
|
andrewm@0
|
171 }
|
andrewm@0
|
172 else if(controllerIs14Bit_) {
|
andrewm@0
|
173 midiOutputController_->sendControlChange(keyboardSegment_.outputPort(), channel, controller_, defaultValue >> 7);
|
andrewm@0
|
174 midiOutputController_->sendControlChange(keyboardSegment_.outputPort(), channel, controller_ + 32, defaultValue & 0x7F);
|
andrewm@0
|
175 }
|
andrewm@0
|
176 else
|
andrewm@0
|
177 midiOutputController_->sendControlChange(keyboardSegment_.outputPort(), channel, controller_, defaultValue);
|
andrewm@0
|
178 }
|
andrewm@0
|
179
|
andrewm@0
|
180 int OscMidiConverter::currentControllerValue(int channel) {
|
andrewm@0
|
181 float controlValue = (float)controlCenterValue_ + (float)controlMinValue_
|
andrewm@0
|
182 + currentValue_[channel]*(float)(controlMaxValue_ - controlMinValue_);
|
andrewm@0
|
183
|
andrewm@7
|
184 if(incomingController_ != MidiKeyboardSegment::kControlDisabled) {
|
andrewm@0
|
185 controlValue += keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_;
|
andrewm@7
|
186 #ifdef DEBUG_OSC_MIDI_CONVERTER
|
andrewm@7
|
187 std::cout << "current value " << currentValue_[channel] << " corresponds to " << controlValue;
|
andrewm@7
|
188 std::cout << " including incoming value " << (keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_) << std::endl;
|
andrewm@7
|
189 #endif
|
andrewm@7
|
190 }
|
andrewm@7
|
191 else {
|
andrewm@7
|
192 #ifdef DEBUG_OSC_MIDI_CONVERTER
|
andrewm@7
|
193 std::cout << "current value " << currentValue_[channel] << " corresponds to " << controlValue << std::endl;
|
andrewm@7
|
194 #endif
|
andrewm@7
|
195 }
|
andrewm@0
|
196
|
andrewm@41
|
197 // For 14-bit CC messages, multiply by 128 first to keep the same apparent range as
|
andrewm@41
|
198 // the 7-bit version but adding extra resolution on the second CC number. This should
|
andrewm@41
|
199 // not be done for pitch wheel where the values are already normalised to 14 bits.
|
andrewm@41
|
200 if(controllerIs14Bit_ && controller_ != MidiKeyboardSegment::kControlPitchWheel)
|
andrewm@41
|
201 controlValue *= 128.0;
|
andrewm@41
|
202
|
andrewm@20
|
203 int roundedControlValue = (int)floorf(controlValue + 0.5f);
|
andrewm@41
|
204 int maxValue = controllerIs14Bit_ ? 16383 : 127;
|
andrewm@41
|
205 if(roundedControlValue > maxValue)
|
andrewm@41
|
206 roundedControlValue = maxValue;
|
andrewm@41
|
207 if(roundedControlValue < 0)
|
andrewm@41
|
208 roundedControlValue = 0;
|
andrewm@0
|
209
|
andrewm@0
|
210 return roundedControlValue;
|
andrewm@0
|
211 }
|
andrewm@0
|
212
|
andrewm@0
|
213 // Add a new OSC input to this MIDI control
|
andrewm@0
|
214 void OscMidiConverter::addControl(const string& oscPath, int oscParamNumber, float oscMinValue,
|
andrewm@0
|
215 float oscMaxValue, float oscCenterValue, int outOfRangeBehavior) {
|
andrewm@0
|
216 // First remove any existing mapping with these exact parameters
|
andrewm@0
|
217 removeControl(oscPath);
|
andrewm@0
|
218
|
andrewm@0
|
219 #ifdef DEBUG_OSC_MIDI_CONVERTER
|
andrewm@0
|
220 std::cout << "OscMidiConverter: adding path " << oscPath << std::endl;
|
andrewm@0
|
221 #endif
|
andrewm@0
|
222
|
andrewm@0
|
223 // Insert the mapping
|
andrewm@0
|
224 OscInput input;
|
andrewm@0
|
225
|
andrewm@0
|
226 input.oscParamNumber = oscParamNumber;
|
andrewm@0
|
227 input.oscMinValue = oscMinValue;
|
andrewm@0
|
228 input.oscMaxValue = oscMaxValue;
|
andrewm@0
|
229
|
andrewm@0
|
230 // Calculate the normalized center value which will be subtracted
|
andrewm@0
|
231 // from the scaled input. Do this once now to save computation.
|
andrewm@0
|
232 if(oscMinValue == oscMaxValue)
|
andrewm@7
|
233 input.oscScaledCenterValue = 0.5;
|
andrewm@0
|
234 else {
|
andrewm@0
|
235 input.oscScaledCenterValue = (oscCenterValue - oscMinValue) / (oscMaxValue - oscMinValue);
|
andrewm@0
|
236 if(input.oscScaledCenterValue < 0)
|
andrewm@0
|
237 input.oscScaledCenterValue = 0;
|
andrewm@0
|
238 if(input.oscScaledCenterValue > 1.0)
|
andrewm@0
|
239 input.oscScaledCenterValue = 1.0;
|
andrewm@0
|
240 }
|
andrewm@0
|
241 input.outOfRangeBehavior = outOfRangeBehavior;
|
andrewm@0
|
242 input.uniqueId = lastUniqueId_++;
|
andrewm@0
|
243 inputs_[oscPath] = input;
|
andrewm@0
|
244
|
andrewm@0
|
245 // Register for the relevant OSC message
|
andrewm@0
|
246 addOscListener(oscPath);
|
andrewm@0
|
247 }
|
andrewm@0
|
248
|
andrewm@0
|
249 // Remove an existing OSC input
|
andrewm@0
|
250 void OscMidiConverter::removeControl(const string& oscPath) {
|
andrewm@0
|
251 // Find the affected control and its ID
|
andrewm@0
|
252 if(inputs_.count(oscPath) == 0)
|
andrewm@0
|
253 return;
|
andrewm@0
|
254 int controlId = inputs_[oscPath].uniqueId;
|
andrewm@0
|
255
|
andrewm@0
|
256 #ifdef DEBUG_OSC_MIDI_CONVERTER
|
andrewm@0
|
257 std::cout << "OscMidiConverter: removing path " << oscPath << std::endl;
|
andrewm@0
|
258 #endif
|
andrewm@0
|
259
|
andrewm@0
|
260 // Look for any active inputs on this channel
|
andrewm@0
|
261 for(int i = 0; i < 16; i++) {
|
andrewm@0
|
262 int channelModulatedId = idWithChannel(i, controlId);
|
andrewm@0
|
263 if(lastValues_.count(channelModulatedId) == 0)
|
andrewm@0
|
264 continue;
|
andrewm@0
|
265
|
andrewm@0
|
266 // Found a last value. Subtract it off and get new value
|
andrewm@0
|
267 float lastValueThisChannel = lastValues_[channelModulatedId];
|
andrewm@0
|
268 currentValue_[i] -= lastValueThisChannel;
|
andrewm@0
|
269
|
andrewm@0
|
270 // Remove this value from the set of active inputs
|
andrewm@0
|
271 lastValues_.erase(channelModulatedId);
|
andrewm@0
|
272
|
andrewm@0
|
273 // Send the new value after removing this one
|
andrewm@0
|
274 sendCurrentValue(keyboardSegment_.outputPort(), i, -1, true);
|
andrewm@0
|
275 }
|
andrewm@0
|
276
|
andrewm@0
|
277 // Having removed any active inputs, now remove the control itself
|
andrewm@0
|
278 // TODO: mutex protection
|
andrewm@0
|
279 inputs_.erase(oscPath);
|
andrewm@0
|
280
|
andrewm@0
|
281 removeOscListener(oscPath);
|
andrewm@0
|
282 }
|
andrewm@0
|
283
|
andrewm@0
|
284 void OscMidiConverter::removeAllControls() {
|
andrewm@0
|
285 // Clear all active inputs and send default values to all channels
|
andrewm@0
|
286 for(int i = 0; i < 16; i++)
|
andrewm@0
|
287 sendDefaultValue(i);
|
andrewm@0
|
288 lastValues_.clear();
|
andrewm@0
|
289 inputs_.clear();
|
andrewm@0
|
290 lastUniqueId_ = 0;
|
andrewm@0
|
291 removeAllOscListeners();
|
andrewm@0
|
292 }
|
andrewm@0
|
293
|
andrewm@0
|
294 // Update the minimum input value of an existing path
|
andrewm@0
|
295 void OscMidiConverter::setControlMinValue(const string& oscPath, float newValue) {
|
andrewm@0
|
296 if(inputs_.count(oscPath) == 0)
|
andrewm@0
|
297 return;
|
andrewm@0
|
298 inputs_[oscPath].oscMinValue = newValue;
|
andrewm@0
|
299 }
|
andrewm@0
|
300
|
andrewm@0
|
301 // Update the maximum input value of an existing path
|
andrewm@0
|
302 void OscMidiConverter::setControlMaxValue(const string& oscPath, float newValue) {
|
andrewm@0
|
303 if(inputs_.count(oscPath) == 0)
|
andrewm@0
|
304 return;
|
andrewm@0
|
305 inputs_[oscPath].oscMaxValue = newValue;
|
andrewm@0
|
306 }
|
andrewm@0
|
307
|
andrewm@0
|
308 // Update the center input value of an existing path
|
andrewm@0
|
309 void OscMidiConverter::setControlCenterValue(const string& oscPath, float newValue) {
|
andrewm@0
|
310 if(inputs_.count(oscPath) == 0)
|
andrewm@0
|
311 return;
|
andrewm@0
|
312 float minValue, maxValue, scaledCenterValue;
|
andrewm@0
|
313 minValue = inputs_[oscPath].oscMinValue;
|
andrewm@0
|
314 maxValue = inputs_[oscPath].oscMaxValue;
|
andrewm@0
|
315
|
andrewm@7
|
316 if(minValue == maxValue)
|
andrewm@7
|
317 scaledCenterValue = 0.0;
|
andrewm@7
|
318 else
|
andrewm@7
|
319 scaledCenterValue = (newValue - minValue) / (maxValue - minValue);
|
andrewm@0
|
320 if(scaledCenterValue < 0)
|
andrewm@0
|
321 scaledCenterValue = 0;
|
andrewm@0
|
322 if(scaledCenterValue > 1.0)
|
andrewm@0
|
323 scaledCenterValue = 1.0;
|
andrewm@0
|
324
|
andrewm@0
|
325 inputs_[oscPath].oscScaledCenterValue = scaledCenterValue;
|
andrewm@0
|
326 }
|
andrewm@0
|
327
|
andrewm@0
|
328 // Update the out of range behavior for an existing path
|
andrewm@0
|
329 void OscMidiConverter::setControlOutOfRangeBehavior(const string& oscPath, int newBehavior) {
|
andrewm@0
|
330 if(inputs_.count(oscPath) == 0)
|
andrewm@0
|
331 return;
|
andrewm@0
|
332 inputs_[oscPath].outOfRangeBehavior = newBehavior;
|
andrewm@0
|
333 }
|
andrewm@0
|
334
|
andrewm@0
|
335 // Reset any active previous values on the given channel
|
andrewm@0
|
336 // 'send' indicated whether to send the value when finished
|
andrewm@0
|
337 // even if it hasn't changed
|
andrewm@0
|
338 void OscMidiConverter::clearLastValues(int channel, bool send) {
|
andrewm@0
|
339 std::map<int, float>::iterator it = lastValues_.begin();
|
andrewm@0
|
340
|
andrewm@0
|
341 bool erased = false;
|
andrewm@0
|
342
|
andrewm@0
|
343 while(it != lastValues_.end()) {
|
andrewm@0
|
344 // Look for any last values matching this channel
|
andrewm@0
|
345 if((it->first & 0x0F) == channel) {
|
andrewm@0
|
346 lastValues_.erase(it++);
|
andrewm@0
|
347 erased = true;
|
andrewm@0
|
348 }
|
andrewm@0
|
349 else
|
andrewm@0
|
350 it++;
|
andrewm@0
|
351 }
|
andrewm@0
|
352
|
andrewm@0
|
353 currentValue_[channel] = 0;
|
andrewm@0
|
354 lastOutputValue_[channel] = -1;
|
andrewm@0
|
355
|
andrewm@0
|
356 // If any last values were erased and send is enabled,
|
andrewm@0
|
357 // resend the current values
|
andrewm@0
|
358 if(erased || send)
|
andrewm@0
|
359 sendDefaultValue(channel);
|
andrewm@0
|
360 }
|
andrewm@0
|
361
|
andrewm@0
|
362 // OSC Handler, called by the data source (PianoKeyboard in this case). Check path against stored
|
andrewm@0
|
363 // inputs and map to MIDI accordingly
|
andrewm@0
|
364
|
andrewm@0
|
365 bool OscMidiConverter::oscHandlerMethod(const char *path, const char *types, int numValues, lo_arg **values, void *data) {
|
andrewm@0
|
366 #ifdef DEBUG_OSC_MIDI_CONVERTER
|
andrewm@0
|
367 std::cout << "OscMidiConverter: received path " << path << std::endl;
|
andrewm@0
|
368 #endif
|
andrewm@0
|
369 if(midiOutputController_ == 0 || controller_ == MidiKeyboardSegment::kControlDisabled)
|
andrewm@0
|
370 return false;
|
andrewm@0
|
371
|
andrewm@0
|
372 //double before = Time::getMillisecondCounterHiRes(); // DEBUG
|
andrewm@0
|
373
|
andrewm@0
|
374 // First value should always be MIDI note number (integer type) so we can retrieve
|
andrewm@0
|
375 // information from the PianoKeyboard class
|
andrewm@0
|
376 if(numValues < 1)
|
andrewm@0
|
377 return false;
|
andrewm@0
|
378 if(types[0] != 'i')
|
andrewm@0
|
379 return false;
|
andrewm@0
|
380 int midiNoteNumber = values[0]->i;
|
andrewm@0
|
381
|
andrewm@0
|
382 // Get the MIDI retransmission channel from the note number
|
andrewm@0
|
383 if(keyboard_.key(midiNoteNumber) == 0)
|
andrewm@0
|
384 return false;
|
andrewm@0
|
385 int midiChannel = keyboard_.key(midiNoteNumber)->midiChannel();
|
andrewm@0
|
386 if(midiChannel < 0 || midiChannel > 15) {
|
andrewm@0
|
387 #ifdef DEBUG_OSC_MIDI_CONVERTER
|
andrewm@0
|
388 std::cout << "OscMidiConverter: no retransmission channel on note " << midiNoteNumber << std::endl;
|
andrewm@0
|
389 #endif
|
andrewm@0
|
390 return false;
|
andrewm@0
|
391 }
|
andrewm@0
|
392
|
andrewm@0
|
393 // Find the relevant input and make sure this OSC message has enough parameters
|
andrewm@0
|
394 if(inputs_.count(path) == 0)
|
andrewm@0
|
395 return false;
|
andrewm@0
|
396 OscInput const& input = inputs_[path];
|
andrewm@0
|
397 if(input.oscParamNumber >= numValues)
|
andrewm@0
|
398 return false;
|
andrewm@0
|
399
|
andrewm@0
|
400 // Find the relevant input value from this OSC message
|
andrewm@0
|
401 float oscParamValue;
|
andrewm@0
|
402 if(types[input.oscParamNumber] == 'f')
|
andrewm@0
|
403 oscParamValue = values[input.oscParamNumber]->f;
|
andrewm@0
|
404 else if(types[input.oscParamNumber] == 'i')
|
andrewm@0
|
405 oscParamValue = (float)values[input.oscParamNumber]->i;
|
andrewm@0
|
406 else
|
andrewm@0
|
407 return false;
|
andrewm@0
|
408
|
andrewm@7
|
409 // Scale input to a 0-1 range, then to the output range. There's a special case for MIDI pitch wheel,
|
andrewm@7
|
410 // where if the range is set to 0, it means to use the segment-wide pitch wheel range. This is done so
|
andrewm@7
|
411 // we don't have to cache multiple copies of the pitch wheel range in every OSC-MIDI converter.
|
andrewm@7
|
412 float scaledValue;
|
andrewm@7
|
413 if(controller_ == MidiKeyboardSegment::kControlPitchWheel &&
|
andrewm@7
|
414 input.oscMaxValue == 0 && input.oscMinValue == 0) {
|
andrewm@7
|
415 float pitchWheelRange = keyboardSegment_.midiPitchWheelRange();
|
andrewm@7
|
416 scaledValue = (oscParamValue + pitchWheelRange) / (2.0 * pitchWheelRange);
|
andrewm@7
|
417 }
|
andrewm@7
|
418 else
|
andrewm@7
|
419 scaledValue = (oscParamValue - input.oscMinValue) / (input.oscMaxValue - input.oscMinValue);
|
andrewm@0
|
420
|
andrewm@0
|
421 #ifdef DEBUG_OSC_MIDI_CONVERTER
|
andrewm@0
|
422 std::cout << "port " << keyboardSegment_.outputPort() << " received input " << oscParamValue << " which scales to " << scaledValue << std::endl;
|
andrewm@0
|
423 #endif
|
andrewm@0
|
424
|
andrewm@0
|
425 // Figure out what to do with an out of range value...
|
andrewm@0
|
426 if(scaledValue < 0.0 || scaledValue > 1.0) {
|
andrewm@0
|
427 if(input.outOfRangeBehavior == kOutOfRangeClip) {
|
andrewm@0
|
428 if(scaledValue < 0.0)
|
andrewm@0
|
429 scaledValue = 0.0;
|
andrewm@0
|
430 if(scaledValue > 1.0)
|
andrewm@0
|
431 scaledValue = 1.0;
|
andrewm@0
|
432 }
|
andrewm@0
|
433 else if(input.outOfRangeBehavior == kOutOfRangeExtrapolate) {
|
andrewm@0
|
434 // Do nothing
|
andrewm@0
|
435 }
|
andrewm@0
|
436 else // Ignore or unknown behavior
|
andrewm@0
|
437 return false;
|
andrewm@0
|
438 }
|
andrewm@0
|
439
|
andrewm@0
|
440 //double midpoint = Time::getMillisecondCounterHiRes(); // DEBUG
|
andrewm@0
|
441
|
andrewm@0
|
442 // Now subtract the normalized center value, which may put the range outside 0-1
|
andrewm@0
|
443 // but this is expected. For example, in a pitch wheel situation, we might move
|
andrewm@0
|
444 // to a range of -0.5 to 0.5.
|
andrewm@0
|
445 scaledValue -= input.oscScaledCenterValue;
|
andrewm@0
|
446
|
andrewm@0
|
447 // Look for previous input with this path and channel and remove it
|
andrewm@0
|
448 int channelModulatedId = idWithChannel(midiChannel, input.uniqueId);
|
andrewm@0
|
449 if(lastValues_.count(channelModulatedId) > 0) {
|
andrewm@0
|
450 // Found a last value. Subtract it off and replace with our current value
|
andrewm@0
|
451 float lastValueThisChannel = lastValues_[channelModulatedId];
|
andrewm@0
|
452 currentValue_[midiChannel] -= lastValueThisChannel;
|
andrewm@0
|
453 #ifdef DEBUG_OSC_MIDI_CONVERTER
|
andrewm@0
|
454 std::cout << "found and removed " << lastValueThisChannel << ", now have " << currentValue_[midiChannel] << std::endl;
|
andrewm@0
|
455 #endif
|
andrewm@0
|
456 }
|
andrewm@0
|
457
|
andrewm@0
|
458 lastValues_[channelModulatedId] = scaledValue;
|
andrewm@0
|
459 currentValue_[midiChannel] += scaledValue;
|
andrewm@0
|
460
|
andrewm@0
|
461 // Send the total current value as a MIDI controller
|
andrewm@0
|
462 sendCurrentValue(keyboardSegment_.outputPort(), midiChannel, midiNoteNumber, false);
|
andrewm@0
|
463
|
andrewm@0
|
464 //double after = Time::getMillisecondCounterHiRes(); // DEBUG
|
andrewm@0
|
465
|
andrewm@0
|
466 //std::cout << "OscMidiConverter: total " << after - before << "ms, of which " << after - midpoint << " in 2nd half\n";
|
andrewm@0
|
467
|
andrewm@0
|
468 return true;
|
andrewm@0
|
469 }
|
andrewm@0
|
470
|
andrewm@0
|
471 // Send the current sum value of all OSC inputs as a MIDI message
|
andrewm@0
|
472 void OscMidiConverter::sendCurrentValue(int port, int channel, int note, bool force) {
|
andrewm@0
|
473 if(midiOutputController_ == 0 || channel < 0 || channel > 15)
|
andrewm@0
|
474 return;
|
andrewm@0
|
475
|
andrewm@0
|
476 // TODO: what about values that are centered by default e.g. pitch?
|
andrewm@0
|
477 /*float controlValue = (float)controlCenterValue_ + (float)controlMinValue_
|
andrewm@0
|
478 + currentValue_[channel]*(float)(controlMaxValue_ - controlMinValue_);
|
andrewm@0
|
479
|
andrewm@0
|
480 if(incomingController_ != MidiKeyboardSegment::kControlDisabled)
|
andrewm@0
|
481 controlValue += keyboardSegment_.controllerValue(incomingController_) - incomingControllerCenterValue_;
|
andrewm@0
|
482
|
andrewm@0
|
483 std::cout << "sending value " << currentValue_[channel] << " as " << controlValue << std::endl;
|
andrewm@0
|
484 int roundedControlValue = (int)roundf(controlValue);
|
andrewm@0
|
485 if(roundedControlValue > controlMaxValue_)
|
andrewm@0
|
486 roundedControlValue = controlMaxValue_;
|
andrewm@0
|
487 if(roundedControlValue < controlMinValue_)
|
andrewm@0
|
488 roundedControlValue = controlMinValue_;*/
|
andrewm@0
|
489
|
andrewm@0
|
490 int roundedControlValue = currentControllerValue(channel);
|
andrewm@0
|
491
|
andrewm@0
|
492 // If this is the same ultimate CC value as before, don't resend unless forced
|
andrewm@0
|
493 if(roundedControlValue == lastOutputValue_[channel] && !force)
|
andrewm@0
|
494 return;
|
andrewm@0
|
495 lastOutputValue_[channel] = roundedControlValue;
|
andrewm@0
|
496
|
andrewm@0
|
497 // Four cases: Pitch Wheel messages, aftertouch, 14-bit controls (major and minor controllers), ordinary 7-bit controls
|
andrewm@0
|
498 if(controller_ == MidiKeyboardSegment::kControlPitchWheel) {
|
andrewm@0
|
499 midiOutputController_->sendPitchWheel(port, channel, roundedControlValue);
|
andrewm@0
|
500 }
|
andrewm@0
|
501 else if(controller_ == MidiKeyboardSegment::kControlChannelAftertouch) {
|
andrewm@0
|
502 midiOutputController_->sendAftertouchChannel(port, channel, roundedControlValue);
|
andrewm@0
|
503 }
|
andrewm@0
|
504 else if(controller_ == MidiKeyboardSegment::kControlPolyphonicAftertouch && note >= 0) {
|
andrewm@28
|
505 midiOutputController_->sendAftertouchPoly(port, channel, note + keyboardSegment_.outputTransposition(),
|
andrewm@28
|
506 roundedControlValue);
|
andrewm@0
|
507 }
|
andrewm@0
|
508 else if(controllerIs14Bit_) {
|
andrewm@0
|
509 // LSB for controllers 0-31 are found on controllers 32-63
|
andrewm@0
|
510 int lsb = roundedControlValue & 0x007F;
|
andrewm@0
|
511 int msb = (roundedControlValue >> 7) & 0x007F;
|
andrewm@0
|
512
|
andrewm@0
|
513 midiOutputController_->sendControlChange(port, channel, controller_, msb);
|
andrewm@0
|
514 midiOutputController_->sendControlChange(port, channel, controller_ + 32, lsb);
|
andrewm@0
|
515 }
|
andrewm@0
|
516 else { // 7 bit
|
andrewm@0
|
517 midiOutputController_->sendControlChange(port, channel, controller_, roundedControlValue);
|
andrewm@0
|
518 }
|
andrewm@0
|
519 }
|
andrewm@0
|
520
|