annotate Source/Mappings/PitchBend/TouchkeyPitchBendMapping.cpp @ 56:b4a2d2ae43cf tip

merge
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Fri, 23 Nov 2018 15:48:14 +0000
parents c6f30c1e2bda
children
rev   line source
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 TouchkeyPitchBendMapping.cpp: per-note mapping for the pitch-bend mapping,
andrewm@0 21 which handles changing pitch based on relative finger motion.
andrewm@0 22 */
andrewm@0 23
andrewm@0 24 #include "TouchkeyPitchBendMapping.h"
andrewm@0 25 #include "../../TouchKeys/MidiOutputController.h"
andrewm@0 26 #include <vector>
andrewm@0 27 #include <climits>
andrewm@0 28 #include <cmath>
andrewm@0 29 #include "../MappingScheduler.h"
andrewm@0 30
andrewm@0 31 #undef DEBUG_PITCHBEND_MAPPING
andrewm@0 32
andrewm@0 33 // Class constants
andrewm@0 34 const int TouchkeyPitchBendMapping::kDefaultMIDIChannel = 0;
andrewm@0 35 const int TouchkeyPitchBendMapping::kDefaultFilterBufferLength = 30;
andrewm@0 36
andrewm@7 37 const float TouchkeyPitchBendMapping::kDefaultBendRangeSemitones = 2.0;
andrewm@11 38 const float TouchkeyPitchBendMapping::kDefaultBendThresholdSemitones = 0.2;
andrewm@0 39 const float TouchkeyPitchBendMapping::kDefaultBendThresholdKeyLength = 0.1;
andrewm@0 40 const float TouchkeyPitchBendMapping::kDefaultSnapZoneSemitones = 0.5;
andrewm@0 41 const int TouchkeyPitchBendMapping::kDefaultPitchBendMode = TouchkeyPitchBendMapping::kPitchBendModeVariableEndpoints;
andrewm@0 42 const float TouchkeyPitchBendMapping::kDefaultFixedModeEnableDistance = 0.1;
andrewm@0 43 const float TouchkeyPitchBendMapping::kDefaultFixedModeBufferDistance = 0;
andrewm@0 44
andrewm@0 45 const bool TouchkeyPitchBendMapping::kDefaultIgnoresTwoFingers = false;
andrewm@0 46 const bool TouchkeyPitchBendMapping::kDefaultIgnoresThreeFingers = false;
andrewm@0 47
andrewm@0 48 // Main constructor takes references/pointers from objects which keep track
andrewm@0 49 // of touch location, continuous key position and the state detected from that
andrewm@0 50 // position. The PianoKeyboard object is strictly required as it gives access to
andrewm@0 51 // Scheduler and OSC methods. The others are optional since any given system may
andrewm@0 52 // contain only one of continuous key position or touch sensitivity
andrewm@0 53 TouchkeyPitchBendMapping::TouchkeyPitchBendMapping(PianoKeyboard &keyboard, MappingFactory *factory, int noteNumber, Node<KeyTouchFrame>* touchBuffer,
andrewm@0 54 Node<key_position>* positionBuffer, KeyPositionTracker* positionTracker)
andrewm@0 55 : TouchkeyBaseMapping(keyboard, factory, noteNumber, touchBuffer, positionBuffer, positionTracker),
andrewm@0 56 bendIsEngaged_(false), snapIsEngaged_(false),
andrewm@0 57 thresholdSemitones_(kDefaultBendThresholdSemitones), thresholdKeyLength_(kDefaultBendThresholdKeyLength),
andrewm@0 58 snapZoneSemitones_(kDefaultSnapZoneSemitones),
andrewm@0 59 bendMode_(kDefaultPitchBendMode), fixedModeMinEnableDistance_(kDefaultFixedModeEnableDistance),
andrewm@0 60 fixedModeBufferDistance_(kDefaultFixedModeBufferDistance),
andrewm@0 61 ignoresTwoFingers_(kDefaultIgnoresTwoFingers), ignoresThreeFingers_(kDefaultIgnoresThreeFingers),
andrewm@0 62 onsetLocationX_(missing_value<float>::missing()),
andrewm@0 63 onsetLocationY_(missing_value<float>::missing()),
andrewm@0 64 lastX_(missing_value<float>::missing()), lastY_(missing_value<float>::missing()),
andrewm@0 65 idOfCurrentTouch_(-1), lastTimestamp_(missing_value<timestamp_type>::missing()),
andrewm@0 66 lastProcessedIndex_(0), bendScalerPositive_(missing_value<float>::missing()),
andrewm@0 67 bendScalerNegative_(missing_value<float>::missing()),
andrewm@0 68 currentSnapDestinationSemitones_(missing_value<float>::missing()),
andrewm@0 69 bendRangeSemitones_(kDefaultBendRangeSemitones), lastPitchBendSemitones_(0),
andrewm@0 70 rawDistance_(kDefaultFilterBufferLength)
andrewm@0 71 {
andrewm@0 72 resetDetectionState();
andrewm@0 73 updateCombinedThreshold();
andrewm@0 74 }
andrewm@0 75
andrewm@0 76 TouchkeyPitchBendMapping::~TouchkeyPitchBendMapping() {
andrewm@0 77
andrewm@0 78 }
andrewm@0 79
andrewm@0 80 // Reset state back to defaults
andrewm@0 81 void TouchkeyPitchBendMapping::reset() {
andrewm@0 82 TouchkeyBaseMapping::reset();
andrewm@0 83 sendPitchBendMessage(0.0);
andrewm@0 84 resetDetectionState();
andrewm@0 85 }
andrewm@0 86
andrewm@0 87
andrewm@0 88 // Resend all current parameters
andrewm@0 89 void TouchkeyPitchBendMapping::resend() {
andrewm@0 90 sendPitchBendMessage(lastPitchBendSemitones_, true);
andrewm@0 91 }
andrewm@0 92
andrewm@0 93 // Set the range of vibrato
andrewm@0 94 void TouchkeyPitchBendMapping::setRange(float rangeSemitones) {
andrewm@0 95 bendRangeSemitones_ = rangeSemitones;
andrewm@0 96 }
andrewm@0 97
andrewm@0 98 // Set the vibrato detection thresholds
andrewm@0 99 void TouchkeyPitchBendMapping::setThresholds(float thresholdSemitones, float thresholdKeyLength) {
andrewm@0 100 thresholdSemitones_ = thresholdSemitones;
andrewm@0 101 thresholdKeyLength_ = thresholdKeyLength;
andrewm@0 102 updateCombinedThreshold();
andrewm@0 103 }
andrewm@0 104
andrewm@0 105 // Set the mode to bend a fixed amount up and down the key, regardless of where
andrewm@0 106 // the touch starts. minimumDistanceToEnable sets a floor below which the bend isn't
andrewm@0 107 // possible (for starting very close to an edge) and bufferAtEnd sets the amount
andrewm@0 108 // of key length beyond which no further bend takes place.
andrewm@0 109 void TouchkeyPitchBendMapping::setFixedEndpoints(float minimumDistanceToEnable, float bufferAtEnd) {
andrewm@0 110 bendMode_ = kPitchBendModeFixedEndpoints;
andrewm@0 111 fixedModeMinEnableDistance_ = minimumDistanceToEnable;
andrewm@0 112 fixedModeBufferDistance_ = bufferAtEnd;
andrewm@0 113 }
andrewm@0 114
andrewm@0 115 // Set the mode to bend an amount proportional to distance, which means
andrewm@0 116 // that the total range of bend will depend on where the finger started.
andrewm@0 117 void TouchkeyPitchBendMapping::setVariableEndpoints() {
andrewm@0 118 bendMode_ = kPitchBendModeVariableEndpoints;
andrewm@0 119 }
andrewm@0 120
andrewm@0 121 void TouchkeyPitchBendMapping::setIgnoresMultipleFingers(bool ignoresTwo, bool ignoresThree) {
andrewm@0 122 ignoresTwoFingers_ = ignoresTwo;
andrewm@0 123 ignoresThreeFingers_ = ignoresThree;
andrewm@0 124 }
andrewm@0 125
andrewm@0 126 // Trigger method. This receives updates from the TouchKey data or from state changes in
andrewm@0 127 // the continuous key position (KeyPositionTracker). It will potentially change the scheduled
andrewm@0 128 // behavior of future mapping calls, but the actual OSC messages should be transmitted in a different
andrewm@0 129 // thread.
andrewm@0 130 void TouchkeyPitchBendMapping::triggerReceived(TriggerSource* who, timestamp_type timestamp) {
andrewm@0 131 if(who == 0)
andrewm@0 132 return;
andrewm@0 133
andrewm@0 134 if(who == touchBuffer_) {
andrewm@0 135 if(!touchBuffer_->empty()) {
andrewm@0 136 // New touch data is available. Find the distance from the onset location.
andrewm@0 137 KeyTouchFrame frame = touchBuffer_->latest();
andrewm@0 138 lastTimestamp_ = timestamp;
andrewm@0 139
andrewm@0 140 if(frame.count == 0) {
andrewm@0 141 // No touches. Last values are "missing", and we're not tracking any
andrewm@0 142 // particular touch ID
andrewm@0 143 lastX_ = lastY_ = missing_value<float>::missing();
andrewm@0 144 idOfCurrentTouch_ = -1;
andrewm@0 145 #ifdef DEBUG_PITCHBEND_MAPPING
andrewm@0 146 std::cout << "Touch off\n";
andrewm@0 147 #endif
andrewm@0 148 }
andrewm@0 149 else if((frame.count == 2 && ignoresTwoFingers_)
andrewm@0 150 || (frame.count == 3 && ignoresThreeFingers_)) {
andrewm@0 151 // Multiple touches that we have chosen to ignore. Do nothing for now...
andrewm@0 152 }
andrewm@0 153 else {
andrewm@0 154 // At least one touch. Check if we are already tracking an ID and, if so,
andrewm@0 155 // use its coordinates. Otherwise grab the lowest current ID.
andrewm@0 156
andrewm@0 157 bool foundCurrentTouch = false;
andrewm@0 158
andrewm@0 159 if(idOfCurrentTouch_ >= 0) {
andrewm@0 160 for(int i = 0; i < frame.count; i++) {
andrewm@0 161 if(frame.ids[i] == idOfCurrentTouch_) {
andrewm@0 162 lastY_ = frame.locs[i];
andrewm@0 163 if(frame.locH < 0)
andrewm@0 164 lastX_ = missing_value<float>::missing();
andrewm@0 165 else
andrewm@0 166 lastX_ = frame.locH;
andrewm@0 167 foundCurrentTouch = true;
andrewm@0 168 break;
andrewm@0 169 }
andrewm@0 170 }
andrewm@0 171 }
andrewm@0 172
andrewm@0 173 if(!foundCurrentTouch) {
andrewm@0 174 // Assign a new touch to be tracked
andrewm@0 175 int lowestRemainingId = INT_MAX;
andrewm@0 176 int lowestIndex = 0;
andrewm@0 177
andrewm@0 178 for(int i = 0; i < frame.count; i++) {
andrewm@0 179 if(frame.ids[i] < lowestRemainingId) {
andrewm@0 180 lowestRemainingId = frame.ids[i];
andrewm@0 181 lowestIndex = i;
andrewm@0 182 }
andrewm@0 183 }
andrewm@0 184
andrewm@0 185 if(!bendIsEngaged_)
andrewm@0 186 onsetLocationX_ = onsetLocationY_ = missing_value<float>::missing();
andrewm@0 187 idOfCurrentTouch_ = lowestRemainingId;
andrewm@0 188 lastY_ = frame.locs[lowestIndex];
andrewm@0 189 if(frame.locH < 0)
andrewm@0 190 lastX_ = missing_value<float>::missing();
andrewm@0 191 else
andrewm@0 192 lastX_ = frame.locH;
andrewm@0 193 #ifdef DEBUG_PITCHBEND_MAPPING
andrewm@0 194 std::cout << "Previous touch stopped; now ID " << idOfCurrentTouch_ << " at (" << lastX_ << ", " << lastY_ << ")\n";
andrewm@0 195 #endif
andrewm@0 196 }
andrewm@0 197
andrewm@0 198 // Now we have an X and (maybe) a Y coordinate for the most recent touch.
andrewm@0 199 // Check whether we have an initial location (if the note is active).
andrewm@0 200 if(noteIsOn_) {
andrewm@0 201 //ScopedLock sl(distanceAccessMutex_);
andrewm@0 202
andrewm@0 203 if(missing_value<float>::isMissing(onsetLocationY_) ||
andrewm@0 204 (!foundCurrentTouch && !bendIsEngaged_)) {
andrewm@0 205 // Note is on but touch hasn't yet arrived --> this touch becomes
andrewm@0 206 // our onset location. Alternatively, the current touch is a different
andrewm@0 207 // ID from the previous one.
andrewm@0 208 onsetLocationY_ = lastY_;
andrewm@0 209 onsetLocationX_ = lastX_;
andrewm@0 210
andrewm@0 211 // Clear buffer and start with 0 distance for this point
andrewm@0 212 clearBuffers();
andrewm@0 213 #ifdef DEBUG_PITCHBEND_MAPPING
andrewm@0 214 std::cout << "Starting at (" << onsetLocationX_ << ", " << onsetLocationY_ << ")\n";
andrewm@0 215 #endif
andrewm@0 216 }
andrewm@0 217 else {
andrewm@0 218 float distance = 0.0;
andrewm@0 219
andrewm@0 220 // Note is on and a start location exists. Calculate distance between
andrewm@0 221 // start location and the current point.
andrewm@0 222
andrewm@0 223 if(missing_value<float>::isMissing(onsetLocationX_) &&
andrewm@0 224 !missing_value<float>::isMissing(lastX_)) {
andrewm@0 225 // No X location indicated for onset but we have one now.
andrewm@0 226 // Update the onset X location.
andrewm@0 227 onsetLocationX_ = lastX_;
andrewm@0 228 #ifdef DEBUG_PITCHBEND_MAPPING
andrewm@0 229 std::cout << "Found first X location at " << onsetLocationX_ << std::endl;
andrewm@0 230 #endif
andrewm@0 231 }
andrewm@0 232
andrewm@0 233 // Distance is based on Y location. TODO: do we need all the X location stuff??
andrewm@0 234 distance = lastY_ - onsetLocationY_;
andrewm@0 235
andrewm@0 236 // Insert raw distance into the buffer. The rest of the processing takes place
andrewm@0 237 // in the dedicated thread so as not to slow down commmunication with the hardware.
andrewm@0 238 rawDistance_.insert(distance, timestamp);
andrewm@0 239
andrewm@0 240 // Move the current scheduled event up to the present time.
andrewm@0 241 // FIXME: this may be more inefficient than just doing everything in the current thread!
andrewm@0 242 #ifdef NEW_MAPPING_SCHEDULER
andrewm@0 243 keyboard_.mappingScheduler().scheduleNow(this);
andrewm@0 244 #else
andrewm@0 245 keyboard_.unscheduleEvent(this);
andrewm@0 246 keyboard_.scheduleEvent(this, mappingAction_, keyboard_.schedulerCurrentTimestamp());
andrewm@0 247 #endif
andrewm@0 248
andrewm@0 249 //std::cout << "Raw distance " << distance << " filtered " << filteredDistance_.latest() << std::endl;
andrewm@0 250 }
andrewm@0 251 }
andrewm@0 252 }
andrewm@0 253 }
andrewm@0 254 }
andrewm@0 255 }
andrewm@0 256
andrewm@0 257 // Mapping method. This actually does the real work of sending OSC data in response to the
andrewm@0 258 // latest information from the touch sensors or continuous key angle
andrewm@0 259 timestamp_type TouchkeyPitchBendMapping::performMapping() {
andrewm@0 260 //ScopedLock sl(distanceAccessMutex_);
andrewm@0 261
andrewm@0 262 timestamp_type currentTimestamp = keyboard_.schedulerCurrentTimestamp();
andrewm@0 263 bool newSamplePresent = false;
andrewm@0 264 float lastProcessedDistance = missing_value<float>::missing();
andrewm@0 265
andrewm@0 266 // Go through the filtered distance samples that are remaining to process.
andrewm@0 267 if(lastProcessedIndex_ < rawDistance_.beginIndex() + 1) {
andrewm@0 268 // Fell off the beginning of the position buffer. Skip to the samples we have
andrewm@0 269 // (shouldn't happen except in cases of exceptional system load, and not too
andrewm@0 270 // consequential if it does happen).
andrewm@0 271 lastProcessedIndex_ = rawDistance_.beginIndex() + 1;
andrewm@0 272 }
andrewm@0 273
andrewm@0 274 while(lastProcessedIndex_ < rawDistance_.endIndex()) {
andrewm@0 275 float distance = lastProcessedDistance = rawDistance_[lastProcessedIndex_];
andrewm@0 276 //timestamp_type timestamp = rawDistance_.timestampAt(lastProcessedIndex_);
andrewm@0 277 newSamplePresent = true;
andrewm@0 278
andrewm@0 279 if(bendIsEngaged_) {
andrewm@0 280 /*
andrewm@0 281 // TODO: look for snapping
andrewm@0 282 // Raw distance is the distance from note onset. Adjusted distance takes into account
andrewm@0 283 // that the bend actually started on the cross of a threshold.
andrewm@0 284 float adjustedDistance = rawDistance_.latest() - bendEngageLocation_;
andrewm@0 285 float pitchBendSemitones = 0.0;
andrewm@0 286
andrewm@0 287 // Calculate pitch bend based on most recent distance
andrewm@0 288 if(adjustedDistance > 0.0)
andrewm@0 289 pitchBendSemitones = adjustedDistance * bendScalerPositive_;
andrewm@0 290 else
andrewm@0 291 pitchBendSemitones = adjustedDistance * bendScalerNegative_;
andrewm@0 292
andrewm@0 293 // Find the nearest semitone to the current value by rounding
andrewm@0 294 currentSnapDestinationSemitones_ = roundf(pitchBendSemitones);
andrewm@0 295
andrewm@0 296 if(snapIsEngaged_) {
andrewm@0 297 // TODO: check velocity conditions; if above minimum velocity, disengage
andrewm@0 298 }
andrewm@0 299 else {
andrewm@0 300 if(fabsf(pitchBendSemitones - currentSnapDestinationSemitones_) < snapZoneSemitones_) {
andrewm@0 301 // TODO: check velocity conditions; if below minimum velocity, engage
andrewm@0 302 //engageSnapping();
andrewm@0 303 }
andrewm@0 304 }
andrewm@0 305 */
andrewm@0 306 }
andrewm@0 307 else {
andrewm@0 308 // Check if bend should engage, using two thresholds: one as fraction of
andrewm@0 309 // key length, one as distance in semitones
andrewm@0 310 if(fabsf(distance) > thresholdCombinedMax_) {
andrewm@0 311 bendIsEngaged_ = true;
andrewm@0 312 #ifdef DEBUG_PITCHBEND_MAPPING
andrewm@0 313 std::cout << "engaging bend at distance " << distance << std::endl;
andrewm@0 314 #endif
andrewm@0 315 // Set up dynamic scaling based on fixed distances to edge of key.
andrewm@0 316 // TODO: make this more flexible, to always nail the nearest semitone (optionally)
andrewm@0 317
andrewm@0 318 // This is how far we would have had from the onset point to the edge of key.
andrewm@0 319 float distanceToPositiveEdgeWithoutThreshold = 1.0 - onsetLocationY_;
andrewm@0 320 float distanceToNegativeEdgeWithoutThreshold = onsetLocationY_;
andrewm@0 321
andrewm@0 322 // This is how far we actually have to go to the edge of the key
andrewm@0 323 float actualDistanceToPositiveEdge = 1.0 - (onsetLocationY_ + thresholdCombinedMax_);
andrewm@0 324 float actualDistanceToNegativeEdge = onsetLocationY_ - thresholdCombinedMax_;
andrewm@0 325
andrewm@0 326 // Make it so moving toward edge of key gets as far as it would have without
andrewm@0 327 // the distance lost by the threshold
andrewm@0 328
andrewm@0 329 if(bendMode_ == kPitchBendModeVariableEndpoints) {
andrewm@0 330 if(actualDistanceToPositiveEdge > 0.0)
andrewm@0 331 bendScalerPositive_ = bendRangeSemitones_ * distanceToPositiveEdgeWithoutThreshold / actualDistanceToPositiveEdge;
andrewm@0 332 else
andrewm@0 333 bendScalerPositive_ = bendRangeSemitones_; // Sanity check
andrewm@0 334 if(actualDistanceToNegativeEdge > 0.0)
andrewm@0 335 bendScalerNegative_ = bendRangeSemitones_ * distanceToNegativeEdgeWithoutThreshold / actualDistanceToNegativeEdge;
andrewm@0 336 else
andrewm@0 337 bendScalerNegative_ = bendRangeSemitones_; // Sanity check
andrewm@0 338 }
andrewm@0 339 else if(bendMode_ == kPitchBendModeFixedEndpoints) {
andrewm@0 340 // TODO: buffer distance at end
andrewm@0 341 if(actualDistanceToPositiveEdge > fixedModeMinEnableDistance_)
andrewm@0 342 bendScalerPositive_ = bendRangeSemitones_ / actualDistanceToPositiveEdge;
andrewm@0 343 else
andrewm@0 344 bendScalerPositive_ = 0.0;
andrewm@0 345 if(actualDistanceToNegativeEdge > fixedModeMinEnableDistance_)
andrewm@0 346 bendScalerNegative_ = bendRangeSemitones_ / actualDistanceToNegativeEdge;
andrewm@0 347 else
andrewm@0 348 bendScalerNegative_ = 0.0;
andrewm@0 349 }
andrewm@0 350 else // unknown mode
andrewm@0 351 bendScalerPositive_ = bendScalerNegative_ = 0.0;
andrewm@0 352 }
andrewm@0 353 }
andrewm@0 354
andrewm@0 355 lastProcessedIndex_++;
andrewm@0 356 }
andrewm@0 357
andrewm@0 358 if(bendIsEngaged_ && !missing_value<float>::isMissing(lastProcessedDistance)) {
andrewm@0 359 // Having processed every sample individually for detection, send a pitch bend message based on the most
andrewm@0 360 // recent one (no sense in sending multiple pitch bend messages simultaneously).
andrewm@0 361 if(newSamplePresent) {
andrewm@0 362 // Raw distance is the distance from note onset. Adjusted distance takes into account
andrewm@0 363 // that the bend actually started on the cross of a threshold.
andrewm@0 364 float pitchBendSemitones;
andrewm@0 365
andrewm@0 366 if(lastProcessedDistance > thresholdCombinedMax_)
andrewm@0 367 pitchBendSemitones = (lastProcessedDistance - thresholdCombinedMax_) * bendScalerPositive_;
andrewm@0 368 else if(lastProcessedDistance < -thresholdCombinedMax_)
andrewm@0 369 pitchBendSemitones = (lastProcessedDistance + thresholdCombinedMax_) * bendScalerNegative_;
andrewm@0 370 else
andrewm@0 371 pitchBendSemitones = 0.0;
andrewm@0 372
andrewm@0 373 sendPitchBendMessage(pitchBendSemitones);
andrewm@0 374 lastPitchBendSemitones_ = pitchBendSemitones;
andrewm@0 375 }
andrewm@0 376 else if(snapIsEngaged_) {
andrewm@0 377 // We may have arrived here without a new touch, just based on timing. Even so, if pitch snapping
andrewm@0 378 // is engaged we need to continue to update the pitch
andrewm@0 379
andrewm@0 380 // TODO: calculate the next filtered pitch based on snapping
andrewm@0 381 }
andrewm@0 382 }
andrewm@0 383
andrewm@0 384 // Register for the next update by returning its timestamp
andrewm@0 385 nextScheduledTimestamp_ = currentTimestamp + updateInterval_;
andrewm@0 386 return nextScheduledTimestamp_;
andrewm@0 387 }
andrewm@0 388
andrewm@0 389 // MIDI note-on message received
andrewm@0 390 void TouchkeyPitchBendMapping::midiNoteOnReceived(int channel, int velocity) {
andrewm@0 391 // MIDI note has gone on. Set the starting location to be most recent
andrewm@0 392 // location. It's possible there has been no touch data before this,
andrewm@0 393 // in which case lastX and lastY will hold missing values.
andrewm@0 394 onsetLocationX_ = lastX_;
andrewm@0 395 onsetLocationY_ = lastY_;
andrewm@0 396 bendIsEngaged_ = false;
andrewm@0 397 if(!missing_value<float>::isMissing(onsetLocationY_)) {
andrewm@0 398 // Already have touch data. Clear the buffer here.
andrewm@0 399 // Clear buffer and start with 0 distance for this point
andrewm@0 400 clearBuffers();
andrewm@0 401 #ifdef DEBUG_PITCHBEND_MAPPING
andrewm@0 402 std::cout << "MIDI on: starting at (" << onsetLocationX_ << ", " << onsetLocationY_ << ")\n";
andrewm@0 403 #endif
andrewm@0 404 }
andrewm@0 405 else {
andrewm@0 406 #ifdef DEBUG_PITCHBEND_MAPPING
andrewm@0 407 std::cout << "MIDI on but no touch\n";
andrewm@0 408 #endif
andrewm@0 409 }
andrewm@0 410 }
andrewm@0 411
andrewm@0 412 // MIDI note-off message received
andrewm@0 413 void TouchkeyPitchBendMapping::midiNoteOffReceived(int channel) {
andrewm@0 414 if(bendIsEngaged_) {
andrewm@0 415 // TODO: should anything happen here? No new samples processed anyway,
andrewm@0 416 // but we may want the snapping algorithm to still continue its work.
andrewm@0 417 }
andrewm@0 418 }
andrewm@0 419
andrewm@0 420 // Reset variables involved in detecting a pitch bend gesture
andrewm@0 421 void TouchkeyPitchBendMapping::resetDetectionState() {
andrewm@0 422 bendIsEngaged_ = false;
andrewm@0 423 snapIsEngaged_ = false;
andrewm@0 424 }
andrewm@0 425
andrewm@0 426 // Clear the buffers that hold distance measurements
andrewm@0 427 void TouchkeyPitchBendMapping::clearBuffers() {
andrewm@0 428 rawDistance_.clear();
andrewm@0 429 rawDistance_.insert(0.0, lastTimestamp_);
andrewm@0 430 lastProcessedIndex_ = 0;
andrewm@0 431 }
andrewm@0 432
andrewm@0 433 // Engage the snapping algorithm to pull the pitch into the nearest semitone
andrewm@0 434 void TouchkeyPitchBendMapping::engageSnapping() {
andrewm@0 435 snapIsEngaged_ = true;
andrewm@0 436 }
andrewm@0 437
andrewm@0 438 // Disengage the snapping algorithm
andrewm@0 439 void TouchkeyPitchBendMapping::disengageSnapping() {
andrewm@0 440 snapIsEngaged_ = false;
andrewm@0 441 }
andrewm@0 442
andrewm@0 443 // Set the combined threshold based on the two independent parameters
andrewm@0 444 // relating to semitones and key length
andrewm@0 445 void TouchkeyPitchBendMapping::updateCombinedThreshold() {
andrewm@0 446 if(thresholdKeyLength_ > thresholdSemitones_ / bendRangeSemitones_)
andrewm@0 447 thresholdCombinedMax_ = thresholdKeyLength_;
andrewm@0 448 else
andrewm@0 449 thresholdCombinedMax_ = thresholdSemitones_ / bendRangeSemitones_;
andrewm@0 450 }
andrewm@0 451
andrewm@0 452
andrewm@0 453 // Send the pitch bend message of a given number of a semitones. Send by OSC,
andrewm@0 454 // which can be mapped to MIDI CC externally
andrewm@0 455 void TouchkeyPitchBendMapping::sendPitchBendMessage(float pitchBendSemitones, bool force) {
andrewm@0 456 if(force || !suspended_)
andrewm@0 457 keyboard_.sendMessage(controlName_.c_str(), "if", noteNumber_, pitchBendSemitones, LO_ARGS_END);
andrewm@0 458 }
andrewm@0 459