annotate Source/Mappings/MultiFingerTrigger/TouchkeyMultiFingerTriggerMapping.cpp @ 56:b4a2d2ae43cf tip

merge
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Fri, 23 Nov 2018 15:48:14 +0000
parents 1526d2fbe01e
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 TouchkeyMultiFingerTriggerMapping.cpp: per-note mapping for the multiple-
andrewm@0 21 finger trigger mapping, which performs actions when two or more fingers
andrewm@0 22 are added or removed from the key.
andrewm@0 23 */
andrewm@0 24
andrewm@0 25 #include "TouchkeyMultiFingerTriggerMapping.h"
andrewm@42 26 #include "TouchkeyMultiFingerTriggerMappingFactory.h"
andrewm@0 27 #include "../../TouchKeys/MidiOutputController.h"
andrewm@0 28
andrewm@0 29 // Class constants
andrewm@0 30 const int TouchkeyMultiFingerTriggerMapping::kDefaultFilterBufferLength = 30;
andrewm@0 31 const int TouchkeyMultiFingerTriggerMapping::kDefaultNumTouchesForTrigger = 2;
andrewm@0 32 const int TouchkeyMultiFingerTriggerMapping::kDefaultNumFramesForTrigger = 2;
andrewm@0 33 const int TouchkeyMultiFingerTriggerMapping::kDefaultNumConsecutiveTapsForTrigger = 1;
andrewm@42 34 const timestamp_diff_type TouchkeyMultiFingerTriggerMapping::kDefaultMaxTapSpacing = milliseconds_to_timestamp(300.0);
andrewm@42 35 const int TouchkeyMultiFingerTriggerMapping::kDefaultTriggerOnAction = TouchkeyMultiFingerTriggerMapping::kActionNoteOn;
andrewm@42 36 const int TouchkeyMultiFingerTriggerMapping::kDefaultTriggerOffAction = TouchkeyMultiFingerTriggerMapping::kActionNone;
andrewm@42 37 const int TouchkeyMultiFingerTriggerMapping::kDefaultTriggerOnNoteNum = -1;
andrewm@42 38 const int TouchkeyMultiFingerTriggerMapping::kDefaultTriggerOffNoteNum = -1;
andrewm@42 39 const int TouchkeyMultiFingerTriggerMapping::kDefaultTriggerOnNoteVel = -1;
andrewm@42 40 const int TouchkeyMultiFingerTriggerMapping::kDefaultTriggerOffNoteVel = -1;
andrewm@0 41
andrewm@0 42 // Main constructor takes references/pointers from objects which keep track
andrewm@0 43 // of touch location, continuous key position and the state detected from that
andrewm@0 44 // position. The PianoKeyboard object is strictly required as it gives access to
andrewm@0 45 // Scheduler and OSC methods. The others are optional since any given system may
andrewm@0 46 // contain only one of continuous key position or touch sensitivity
andrewm@0 47 TouchkeyMultiFingerTriggerMapping::TouchkeyMultiFingerTriggerMapping(PianoKeyboard &keyboard, MappingFactory *factory, int noteNumber, Node<KeyTouchFrame>* touchBuffer,
andrewm@0 48 Node<key_position>* positionBuffer, KeyPositionTracker* positionTracker)
andrewm@0 49 : TouchkeyBaseMapping(keyboard, factory, noteNumber, touchBuffer, positionBuffer, positionTracker),
andrewm@0 50 numTouchesForTrigger_(kDefaultNumTouchesForTrigger), numFramesForTrigger_(kDefaultNumFramesForTrigger),
andrewm@0 51 numConsecutiveTapsForTrigger_(kDefaultNumConsecutiveTapsForTrigger), maxTapSpacing_(kDefaultMaxTapSpacing),
andrewm@42 52 needsMidiNoteOn_(true), triggerOnAction_(kDefaultTriggerOnAction), triggerOffAction_(kDefaultTriggerOffAction),
andrewm@42 53 triggerOnNoteNum_(kDefaultTriggerOnNoteNum), triggerOffNoteNum_(kDefaultTriggerOffNoteNum),
andrewm@42 54 triggerOnNoteVel_(kDefaultTriggerOnNoteVel), triggerOffNoteVel_(kDefaultTriggerOffNoteVel),
andrewm@42 55 pastSamples_(kDefaultFilterBufferLength)
andrewm@0 56 {
andrewm@0 57 reset();
andrewm@0 58 }
andrewm@0 59
andrewm@42 60 // Turn off mapping of data.
andrewm@42 61 void TouchkeyMultiFingerTriggerMapping::disengage(bool shouldDelete) {
andrewm@42 62 // Send note off messages for anything currently on
andrewm@42 63 std::set<std::pair<int, int> >::iterator it;
andrewm@42 64 int port = static_cast<TouchkeyMultiFingerTriggerMappingFactory*>(factory_)->segment().outputPort();
andrewm@42 65
andrewm@42 66 for(it = otherNotesOn_.begin(); it != otherNotesOn_.end(); ++it) {
andrewm@42 67 int ch = it->first;
andrewm@42 68 int note = it->second;
andrewm@42 69
andrewm@42 70 keyboard_.midiOutputController()->sendNoteOn(port, ch, note, 0);
andrewm@42 71 }
andrewm@42 72
andrewm@42 73 otherNotesOn_.clear();
andrewm@42 74 TouchkeyBaseMapping::disengage(shouldDelete);
andrewm@42 75 }
andrewm@42 76
andrewm@0 77 // Reset state back to defaults
andrewm@0 78 void TouchkeyMultiFingerTriggerMapping::reset() {
andrewm@0 79 ScopedLock sl(sampleBufferMutex_);
andrewm@0 80
andrewm@0 81 TouchkeyBaseMapping::reset();
andrewm@0 82 pastSamples_.clear();
andrewm@0 83
andrewm@0 84 lastNumActiveTouches_ = 0;
andrewm@0 85 lastActiveTouchLocations_[0] = lastActiveTouchLocations_[1] = lastActiveTouchLocations_[2] = 0;
andrewm@0 86 framesCount_ = 0;
andrewm@0 87 tapsCount_ = 0;
andrewm@0 88 hasGeneratedTap_ = false;
andrewm@0 89 lastTapStartTimestamp_ = missing_value<timestamp_type>::missing();
andrewm@0 90 hasTriggered_ = false;
andrewm@0 91 }
andrewm@0 92
andrewm@0 93 // Resend all current parameters
andrewm@0 94 void TouchkeyMultiFingerTriggerMapping::resend() {
andrewm@0 95 // Message is only sent at release; resend may not apply here.
andrewm@0 96 }
andrewm@0 97
andrewm@42 98 void TouchkeyMultiFingerTriggerMapping::setTouchesForTrigger(int touches) {
andrewm@42 99 numTouchesForTrigger_ = touches;
andrewm@42 100 }
andrewm@42 101
andrewm@42 102 void TouchkeyMultiFingerTriggerMapping::setFramesForTrigger(int frames) {
andrewm@42 103 numFramesForTrigger_ = frames;
andrewm@42 104 }
andrewm@42 105
andrewm@42 106 void TouchkeyMultiFingerTriggerMapping::setConsecutiveTapsForTrigger(int taps) {
andrewm@42 107 numConsecutiveTapsForTrigger_ = taps;
andrewm@42 108 }
andrewm@42 109
andrewm@42 110 void TouchkeyMultiFingerTriggerMapping::setMaxTimeBetweenTapsForTrigger(timestamp_diff_type timeDiff) {
andrewm@42 111 maxTapSpacing_ = timeDiff;
andrewm@42 112 }
andrewm@42 113
andrewm@42 114 void TouchkeyMultiFingerTriggerMapping::setNeedsMidiNoteOn(bool needsMidi) {
andrewm@42 115 needsMidiNoteOn_ = needsMidi;
andrewm@42 116 }
andrewm@42 117
andrewm@42 118 void TouchkeyMultiFingerTriggerMapping::setTriggerOnAction(int action) {
andrewm@42 119 triggerOnAction_ = action;
andrewm@42 120 }
andrewm@42 121
andrewm@42 122 void TouchkeyMultiFingerTriggerMapping::setTriggerOffAction(int action) {
andrewm@42 123 triggerOffAction_ = action;
andrewm@42 124 }
andrewm@42 125
andrewm@42 126 void TouchkeyMultiFingerTriggerMapping::setTriggerOnNoteNumber(int note) {
andrewm@42 127 triggerOnNoteNum_ = note;
andrewm@42 128 }
andrewm@42 129
andrewm@42 130 void TouchkeyMultiFingerTriggerMapping::setTriggerOffNoteNumber(int note) {
andrewm@42 131 triggerOffNoteNum_ = note;
andrewm@42 132 }
andrewm@42 133
andrewm@42 134 void TouchkeyMultiFingerTriggerMapping::setTriggerOnNoteVelocity(int velocity) {
andrewm@42 135 triggerOnNoteVel_ = velocity;
andrewm@42 136 }
andrewm@42 137
andrewm@42 138 void TouchkeyMultiFingerTriggerMapping::setTriggerOffNoteVelocity(int velocity) {
andrewm@42 139 triggerOffNoteVel_ = velocity;
andrewm@42 140 }
andrewm@42 141
andrewm@0 142 // This method receives data from the touch buffer or possibly the continuous key angle (not used here)
andrewm@0 143 void TouchkeyMultiFingerTriggerMapping::triggerReceived(TriggerSource* who, timestamp_type timestamp) {
andrewm@0 144 if(needsMidiNoteOn_ && !noteIsOn_) {
andrewm@0 145 framesCount_ = 0;
andrewm@0 146 hasGeneratedTap_ = false;
andrewm@0 147 return;
andrewm@0 148 }
andrewm@0 149
andrewm@0 150 if(who == touchBuffer_) {
andrewm@0 151 if(!touchBuffer_->empty()) {
andrewm@0 152 // Find the current number of touches
andrewm@0 153 KeyTouchFrame frame = touchBuffer_->latest();
andrewm@0 154 int count = frame.count;
andrewm@0 155
andrewm@0 156 if(count < numTouchesForTrigger_) {
andrewm@0 157 framesCount_ = 0;
andrewm@0 158 hasGeneratedTap_ = false;
andrewm@0 159 if(hasTriggered_) {
andrewm@0 160 generateTriggerOff(timestamp);
andrewm@0 161 hasTriggered_ = false;
andrewm@0 162 }
andrewm@0 163 }
andrewm@0 164 else if(count == numTouchesForTrigger_) {
andrewm@0 165 framesCount_++;
andrewm@0 166 if(framesCount_ >= numFramesForTrigger_ && !hasGeneratedTap_) {
andrewm@0 167 // Enough frames have elapsed to consider this a tap
andrewm@0 168 // Figure out if it is a multiple consecutive tap or the first
andrewm@0 169 // of a set.
andrewm@0 170 if(!missing_value<timestamp_diff_type>::isMissing(lastTapStartTimestamp_)) {
andrewm@0 171 if(timestamp - lastTapStartTimestamp_ < maxTapSpacing_) {
andrewm@0 172 tapsCount_++;
andrewm@0 173 }
andrewm@0 174 else
andrewm@0 175 tapsCount_ = 1;
andrewm@0 176 }
andrewm@0 177 else
andrewm@0 178 tapsCount_ = 1;
andrewm@0 179
andrewm@0 180 // Check if the right number of taps has elapsed
andrewm@0 181 if(tapsCount_ >= numConsecutiveTapsForTrigger_ && !hasTriggered_) {
andrewm@0 182 hasTriggered_ = true;
andrewm@0 183
andrewm@0 184 // Find the ID of the newest touch and compare its location
andrewm@0 185 // to the immediately preceding touch(es) to find the distance
andrewm@0 186 int newest = 0, oldest = 0, newestId = -1, oldestId = 1000000;
andrewm@0 187 for(int i = 0; i < count; i++) {
andrewm@0 188 if(frame.ids[i] > newestId) {
andrewm@0 189 newest = i;
andrewm@0 190 newestId = frame.ids[i];
andrewm@0 191 }
andrewm@0 192 if(frame.ids[i] < oldestId) {
andrewm@0 193 oldest = i;
andrewm@0 194 oldestId = frame.ids[i];
andrewm@0 195 }
andrewm@0 196 }
andrewm@0 197
andrewm@0 198 // Find the distance between the point before this tap and the
andrewm@0 199 // point that was added to create the tap. If this is a 3-touch
andrewm@0 200 // tap, find the distance between the farthest two points, with
andrewm@0 201 // the direction determined by which end is older.
andrewm@0 202 float distance = frame.locs[newest] - frame.locs[oldest];
andrewm@0 203 if(count == 3) {
andrewm@0 204 if(fabsf(frame.locs[2] - frame.locs[0]) > fabsf(distance)) {
andrewm@0 205 if(frame.ids[2] > frame.ids[0])
andrewm@0 206 distance = frame.locs[2] - frame.locs[0];
andrewm@0 207 else
andrewm@0 208 distance = frame.locs[0] - frame.locs[2];
andrewm@0 209 }
andrewm@0 210 }
andrewm@0 211
andrewm@0 212 // Generate the trigger. If a multi-tap gesture, also indicate the timing
andrewm@0 213 if(numConsecutiveTapsForTrigger_ <= 1)
andrewm@0 214 generateTriggerOn(timestamp, 0, distance);
andrewm@0 215 else
andrewm@0 216 generateTriggerOn(timestamp, timestamp - lastTapStartTimestamp_, distance);
andrewm@0 217 }
andrewm@0 218
andrewm@0 219 hasGeneratedTap_ = true;
andrewm@0 220 lastTapStartTimestamp_ = timestamp;
andrewm@0 221 }
andrewm@0 222 }
andrewm@0 223
andrewm@0 224 // Save the count locations for next time
andrewm@0 225 lastNumActiveTouches_ = frame.count;
andrewm@0 226 for(int i = 0; i < count; i++) {
andrewm@0 227 lastActiveTouchLocations_[i] = frame.locs[i];
andrewm@0 228 }
andrewm@0 229 }
andrewm@0 230 }
andrewm@0 231 }
andrewm@0 232
andrewm@0 233 // Mapping method. This actually does the real work of sending OSC data in response to the
andrewm@0 234 // latest information from the touch sensors or continuous key angle
andrewm@0 235 timestamp_type TouchkeyMultiFingerTriggerMapping::performMapping() {
andrewm@0 236 // Nothing to do here until note is released.
andrewm@0 237 // Register for the next update by returning its timestamp
andrewm@0 238 // TODO: do we even need this? Check Mapping::engage() and Mapping::disengage()
andrewm@0 239 timestamp_type currentTimestamp = keyboard_.schedulerCurrentTimestamp();
andrewm@0 240 nextScheduledTimestamp_ = currentTimestamp + updateInterval_;
andrewm@0 241 return nextScheduledTimestamp_;
andrewm@0 242 }
andrewm@0 243
andrewm@0 244 void TouchkeyMultiFingerTriggerMapping::generateTriggerOn(timestamp_type timestamp, timestamp_diff_type timeBetweenTaps, float distanceBetweenPoints) {
andrewm@0 245 if(!suspended_) {
andrewm@42 246 if(triggerOnAction_ == kActionNoteOn ||
andrewm@42 247 triggerOnAction_ == kActionNoteOff) {
andrewm@42 248 // Send a MIDI note on message with given note number and velocity
andrewm@42 249 int port = static_cast<TouchkeyMultiFingerTriggerMappingFactory*>(factory_)->segment().outputPort();
andrewm@0 250 int ch = keyboard_.key(noteNumber_)->midiChannel();
andrewm@42 251 int vel = triggerOnNoteVel_;
andrewm@42 252 int note = triggerOnNoteNum_;
andrewm@42 253 if(note < 0) // note < 0 means current note
andrewm@42 254 note = noteNumber_;
andrewm@42 255 if(note < 128) {
andrewm@42 256 if(triggerOnAction_ == kActionNoteOn) {
andrewm@42 257 // Can't send notes above 127...
andrewm@42 258 if(vel < 0) // vel < 0 means same as current
andrewm@42 259 vel = keyboard_.key(noteNumber_)->midiVelocity();
andrewm@42 260 if(vel > 127)
andrewm@42 261 vel = 127;
andrewm@42 262
andrewm@42 263 // Register that this note has been turned on
andrewm@42 264 if(note != noteNumber_)
andrewm@42 265 otherNotesOn_.insert(std::pair<int,int>(ch, note));
andrewm@42 266 }
andrewm@42 267 else {
andrewm@42 268 // Note off
andrewm@42 269 vel = 0;
andrewm@42 270 if(note != noteNumber_) {
andrewm@42 271 // Unregister this note if we are turning it off
andrewm@42 272 if(otherNotesOn_.count(std::pair<int,int>(ch, note)) > 0) {
andrewm@42 273 otherNotesOn_.erase(std::pair<int,int>(ch, note));
andrewm@42 274 }
andrewm@42 275 }
andrewm@42 276 }
andrewm@42 277
andrewm@42 278 keyboard_.midiOutputController()->sendNoteOn(port, ch, note, vel);
andrewm@42 279 }
andrewm@0 280 }
andrewm@0 281 }
andrewm@0 282 }
andrewm@0 283
andrewm@0 284 void TouchkeyMultiFingerTriggerMapping::generateTriggerOff(timestamp_type timestamp) {
andrewm@0 285 if(!suspended_) {
andrewm@42 286 if(triggerOffAction_ == kActionNoteOn ||
andrewm@42 287 triggerOffAction_ == kActionNoteOff) {
andrewm@42 288 // Send a MIDI note on message with given note number and velocity
andrewm@42 289 int port = static_cast<TouchkeyMultiFingerTriggerMappingFactory*>(factory_)->segment().outputPort();
andrewm@42 290 int ch = keyboard_.key(noteNumber_)->midiChannel();
andrewm@42 291 int vel = triggerOffNoteVel_;
andrewm@42 292 int note = triggerOffNoteNum_;
andrewm@42 293 if(note < 0) // note < 0 means current note
andrewm@42 294 note = noteNumber_;
andrewm@42 295 if(note < 128) {
andrewm@42 296 if(triggerOffAction_ == kActionNoteOn) {
andrewm@42 297 // Can't send notes above 127...
andrewm@42 298 if(vel < 0) // vel < 0 means same as current
andrewm@42 299 vel = keyboard_.key(noteNumber_)->midiVelocity();
andrewm@42 300 if(vel > 127)
andrewm@42 301 vel = 127;
andrewm@42 302
andrewm@42 303 // Register that this note has been turned on
andrewm@42 304 if(note != noteNumber_)
andrewm@42 305 otherNotesOn_.insert(std::pair<int,int>(ch, note));
andrewm@42 306 }
andrewm@42 307 else {
andrewm@42 308 // Note off
andrewm@42 309 vel = 0;
andrewm@42 310 if(note != noteNumber_) {
andrewm@42 311 // Unregister this note if we are turning it off
andrewm@42 312 if(otherNotesOn_.count(std::pair<int,int>(ch, note)) > 0) {
andrewm@42 313 otherNotesOn_.erase(std::pair<int,int>(ch, note));
andrewm@42 314 }
andrewm@42 315 }
andrewm@42 316 }
andrewm@42 317
andrewm@42 318 keyboard_.midiOutputController()->sendNoteOn(port, ch, note, vel);
andrewm@42 319 }
andrewm@42 320 }
andrewm@0 321 }
andrewm@0 322 }
andrewm@0 323
andrewm@0 324 // MIDI note-off message received
andrewm@0 325 void TouchkeyMultiFingerTriggerMapping::midiNoteOffReceived(int channel) {
andrewm@42 326 // int ch = keyboard_.key(noteNumber_)->midiChannel();
andrewm@42 327 // keyboard_.midiOutputController()->sendControlChange(0, ch, 73, 0);
andrewm@0 328 }