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 TouchkeyKeyDivisionMapping.cpp: per-note mapping for the split-key mapping
|
andrewm@0
|
21 which triggers different actions or pitches depending on where the key
|
andrewm@0
|
22 was struck.
|
andrewm@0
|
23 */
|
andrewm@0
|
24
|
andrewm@0
|
25 #include "TouchkeyKeyDivisionMapping.h"
|
andrewm@0
|
26 #include "TouchkeyKeyDivisionMappingFactory.h"
|
andrewm@0
|
27
|
andrewm@51
|
28 #undef DEBUG_KEY_DIVISION_MAPPING
|
andrewm@0
|
29
|
andrewm@0
|
30 const int TouchkeyKeyDivisionMapping::kDefaultNumberOfSegments = 2;
|
andrewm@0
|
31 const timestamp_diff_type TouchkeyKeyDivisionMapping::kDefaultDetectionTimeout = milliseconds_to_timestamp(25.0);
|
andrewm@46
|
32 const int TouchkeyKeyDivisionMapping::kDefaultDetectionParameter = kDetectionParameterYPositionAndNumberOfTouches;
|
andrewm@0
|
33 const int TouchkeyKeyDivisionMapping::kDefaultRetriggerNumFrames = 2;
|
andrewm@0
|
34
|
andrewm@0
|
35 // Main constructor takes references/pointers from objects which keep track
|
andrewm@0
|
36 // of touch location, continuous key position and the state detected from that
|
andrewm@0
|
37 // position. The PianoKeyboard object is strictly required as it gives access to
|
andrewm@0
|
38 // Scheduler and OSC methods. The others are optional since any given system may
|
andrewm@0
|
39 // contain only one of continuous key position or touch sensitivity
|
andrewm@0
|
40 TouchkeyKeyDivisionMapping::TouchkeyKeyDivisionMapping(PianoKeyboard &keyboard, MappingFactory *factory, int noteNumber, Node<KeyTouchFrame>* touchBuffer,
|
andrewm@0
|
41 Node<key_position>* positionBuffer, KeyPositionTracker* positionTracker)
|
andrewm@0
|
42 : TouchkeyBaseMapping(keyboard, factory, noteNumber, touchBuffer, positionBuffer, positionTracker),
|
andrewm@0
|
43 numberOfSegments_(kDefaultNumberOfSegments), candidateSegment_(-1), detectedSegment_(-1), defaultSegment_(0),
|
andrewm@46
|
44 detectionParameter_(kDefaultDetectionParameter), retriggerable_(true), retriggerNumFrames_(kDefaultRetriggerNumFrames),
|
andrewm@0
|
45 retriggerKeepsVelocity_(true),
|
andrewm@0
|
46 midiNoteOnTimestamp_(missing_value<timestamp_type>::missing()), timeout_(kDefaultDetectionTimeout),
|
andrewm@0
|
47 lastNumActiveTouches_(-1)
|
andrewm@0
|
48 {
|
andrewm@0
|
49 }
|
andrewm@0
|
50
|
andrewm@0
|
51 // Reset state back to defaults
|
andrewm@0
|
52 void TouchkeyKeyDivisionMapping::reset() {
|
andrewm@0
|
53 TouchkeyBaseMapping::reset();
|
andrewm@0
|
54
|
andrewm@0
|
55 candidateSegment_ = detectedSegment_ = -1;
|
andrewm@0
|
56 midiNoteOnTimestamp_ = missing_value<timestamp_type>::missing();
|
andrewm@0
|
57 }
|
andrewm@0
|
58
|
andrewm@0
|
59 // Resend all current parameters
|
andrewm@0
|
60 void TouchkeyKeyDivisionMapping::resend() {
|
andrewm@0
|
61 if(detectedSegment_ >= 0)
|
andrewm@0
|
62 sendSegmentMessage(detectedSegment_, true);
|
andrewm@0
|
63 }
|
andrewm@0
|
64
|
andrewm@0
|
65 // Set the pitch bend values (in semitones) for each segment. These
|
andrewm@0
|
66 // values are in relation to the pitch of this note
|
andrewm@0
|
67 void TouchkeyKeyDivisionMapping::setSegmentPitchBends(const float *bendsInSemitones, int numBends) {
|
andrewm@0
|
68 // Clear old values and refill the vector
|
andrewm@0
|
69 segmentBends_.clear();
|
andrewm@0
|
70 for(int i = 0; i < numBends; i++)
|
andrewm@0
|
71 segmentBends_.push_back(bendsInSemitones[i]);
|
andrewm@0
|
72 }
|
andrewm@0
|
73
|
andrewm@0
|
74 // This method receives data from the touch buffer or possibly the continuous key angle (not used here)
|
andrewm@0
|
75 void TouchkeyKeyDivisionMapping::triggerReceived(TriggerSource* who, timestamp_type timestamp) {
|
andrewm@0
|
76 if(who == touchBuffer_) {
|
andrewm@0
|
77 // If we get here, a new touch frame has been received and there is no segment detected
|
andrewm@0
|
78 // yet. We should come up with a candidate segment. If the MIDI note is on, activate this
|
andrewm@0
|
79 // segment right away. Otherwise, save it for later so when the MIDI note begins, we have
|
andrewm@0
|
80 // it ready to go.
|
andrewm@0
|
81 if(!touchBuffer_->empty()) {
|
andrewm@0
|
82 const KeyTouchFrame& frame = touchBuffer_->latest();
|
andrewm@0
|
83
|
andrewm@0
|
84 if(detectedSegment_ < 0) {
|
andrewm@0
|
85 int candidateBasedOnYPosition = -1, candidateBasedOnNumberOfTouches = -1;
|
andrewm@0
|
86
|
andrewm@0
|
87 // Find the first touch. TODO: eventually look for the largest touch
|
andrewm@0
|
88 float yPosition = frame.locs[0];
|
andrewm@0
|
89
|
andrewm@0
|
90 // Calculate two possible segments based on touch location and based on
|
andrewm@0
|
91 // number of touches.
|
andrewm@0
|
92 candidateBasedOnYPosition = segmentForLocation(yPosition);
|
andrewm@0
|
93 candidateBasedOnNumberOfTouches = segmentForNumTouches(frame.count);
|
andrewm@0
|
94
|
andrewm@0
|
95 if(detectionParameter_ == kDetectionParameterYPosition)
|
andrewm@0
|
96 candidateSegment_ = candidateBasedOnYPosition;
|
andrewm@0
|
97 else if(detectionParameter_ == kDetectionParameterNumberOfTouches)
|
andrewm@0
|
98 candidateSegment_ = candidateBasedOnNumberOfTouches;
|
andrewm@0
|
99 else if(detectionParameter_ == kDetectionParameterYPositionAndNumberOfTouches) {
|
andrewm@0
|
100 // Choose the maximum segment specified by the other two methods
|
andrewm@0
|
101 candidateSegment_ = candidateBasedOnNumberOfTouches > candidateBasedOnYPosition ? candidateBasedOnNumberOfTouches : candidateBasedOnYPosition;
|
andrewm@0
|
102 }
|
andrewm@0
|
103 else // Shouldn't happen
|
andrewm@0
|
104 candidateSegment_ = -1;
|
andrewm@0
|
105
|
andrewm@0
|
106 if(noteIsOn_) {
|
andrewm@0
|
107 detectedSegment_ = candidateSegment_;
|
andrewm@0
|
108 #ifdef DEBUG_KEY_DIVISION_MAPPING
|
andrewm@0
|
109 cout << "TouchkeyKeyDivisionMapping::triggerReceived(): detectedSegment_ = " << detectedSegment_ << endl;
|
andrewm@0
|
110 #endif
|
andrewm@0
|
111 sendSegmentMessage(detectedSegment_);
|
andrewm@0
|
112 }
|
andrewm@0
|
113 }
|
andrewm@0
|
114 else if(retriggerable_ &&
|
andrewm@0
|
115 (lastNumActiveTouches_ == 1 &&
|
andrewm@0
|
116 frame.count >= 2) && noteIsOn_) {
|
andrewm@0
|
117 // Here, there was one touch active before and now there are two. Look for the
|
andrewm@0
|
118 // location of the most recently added touch, and determine whether it matches a
|
andrewm@0
|
119 // segment different from the one we're in. If so, retrigger the MIDI note
|
andrewm@0
|
120 // with a different pitch bend
|
andrewm@0
|
121
|
andrewm@0
|
122 int newCandidate = segmentForLocation(locationOfNewestTouch(frame));
|
andrewm@0
|
123
|
andrewm@0
|
124 #ifdef DEBUG_KEY_DIVIOSION_MAPPING
|
andrewm@0
|
125 cout << "TouchkeyKeyDivisionMapping: touch added with candidate segment " << newCandidate << " (current is " << detectedSegment_ << ")\n";
|
andrewm@0
|
126 #endif
|
andrewm@0
|
127 if(newCandidate != detectedSegment_) {
|
andrewm@0
|
128 // Set up a new segment to retrigger and tell the scheduler to insert the mapping
|
andrewm@0
|
129 detectedSegment_ = newCandidate;
|
andrewm@0
|
130
|
andrewm@0
|
131 // Find the keyboard segment, which gives us the output port
|
andrewm@0
|
132 int outputPort = static_cast<TouchkeyKeyDivisionMappingFactory*>(factory_)->segment().outputPort();
|
andrewm@0
|
133
|
andrewm@0
|
134 // Send MIDI note-on on the same channel as previously
|
andrewm@0
|
135 int ch = keyboard_.key(noteNumber_)->midiChannel();
|
andrewm@0
|
136 int vel = 64;
|
andrewm@0
|
137 if(retriggerKeepsVelocity_)
|
andrewm@0
|
138 vel = keyboard_.key(noteNumber_)->midiVelocity();
|
andrewm@0
|
139 keyboard_.midiOutputController()->sendNoteOn(outputPort, ch, noteNumber_, vel);
|
andrewm@0
|
140 sendSegmentMessage(detectedSegment_);
|
andrewm@0
|
141 }
|
andrewm@0
|
142 }
|
andrewm@0
|
143
|
andrewm@0
|
144 // Save the number of active touches for next time
|
andrewm@0
|
145 lastNumActiveTouches_ = frame.count;
|
andrewm@0
|
146 }
|
andrewm@0
|
147 }
|
andrewm@0
|
148 }
|
andrewm@0
|
149
|
andrewm@0
|
150 // Mapping method. This actually does the real work of sending OSC data in response to the
|
andrewm@0
|
151 // latest information from the touch sensors or continuous key angle
|
andrewm@0
|
152 timestamp_type TouchkeyKeyDivisionMapping::performMapping() {
|
andrewm@0
|
153 timestamp_type currentTimestamp = keyboard_.schedulerCurrentTimestamp();
|
andrewm@0
|
154
|
andrewm@0
|
155 if(detectedSegment_ >= 0) {
|
andrewm@0
|
156 // Found segment; no need to keep sending mapping callbacks
|
andrewm@0
|
157 nextScheduledTimestamp_ = 0;
|
andrewm@0
|
158 return 0;
|
andrewm@0
|
159 }
|
andrewm@0
|
160
|
andrewm@0
|
161 if(currentTimestamp - midiNoteOnTimestamp_ > timeout_) {
|
andrewm@0
|
162 // Timeout occurred. Activate default segment
|
andrewm@0
|
163 #ifdef DEBUG_KEY_DIVISION_MAPPING
|
andrewm@0
|
164 cout << "TouchkeyKeyDivisionMapping: timeout\n";
|
andrewm@0
|
165 #endif
|
andrewm@0
|
166 detectedSegment_ = defaultSegment_;
|
andrewm@0
|
167 sendSegmentMessage(detectedSegment_);
|
andrewm@0
|
168 nextScheduledTimestamp_ = 0;
|
andrewm@0
|
169 return 0;
|
andrewm@0
|
170 }
|
andrewm@0
|
171
|
andrewm@0
|
172 // Register for the next update by returning its timestamp
|
andrewm@0
|
173 nextScheduledTimestamp_ = currentTimestamp + updateInterval_;
|
andrewm@0
|
174 return nextScheduledTimestamp_;
|
andrewm@0
|
175 }
|
andrewm@0
|
176
|
andrewm@0
|
177 // MIDI note-on received. If we have a candidate segment, activate it as the actual segment
|
andrewm@0
|
178 void TouchkeyKeyDivisionMapping::midiNoteOnReceived(int channel, int velocity) {
|
andrewm@0
|
179 midiNoteOnTimestamp_ = keyboard_.schedulerCurrentTimestamp();
|
andrewm@0
|
180
|
andrewm@0
|
181 if(detectedSegment_ < 0) {
|
andrewm@0
|
182 #ifdef DEBUG_KEY_DIVISION_MAPPING
|
andrewm@0
|
183 cout << "TouchkeyKeyDivisionMapping::midiNoteOnReceived(): candidateSegment_ = " << candidateSegment_ << endl;
|
andrewm@0
|
184 #endif
|
andrewm@0
|
185 detectedSegment_ = candidateSegment_;
|
andrewm@0
|
186 if(detectedSegment_ >= 0) {
|
andrewm@0
|
187 sendSegmentMessage(detectedSegment_);
|
andrewm@0
|
188 }
|
andrewm@0
|
189 }
|
andrewm@0
|
190 }
|
andrewm@0
|
191
|
andrewm@0
|
192 // MIDI note-off received. Reset back to the detecting state so we can assign the next note to a segment
|
andrewm@0
|
193 void TouchkeyKeyDivisionMapping::midiNoteOffReceived(int channel) {
|
andrewm@0
|
194 detectedSegment_ = candidateSegment_ = -1;
|
andrewm@0
|
195 }
|
andrewm@0
|
196
|
andrewm@0
|
197 void TouchkeyKeyDivisionMapping::sendSegmentMessage(int segment, bool force) {
|
andrewm@0
|
198 if(force || !suspended_) {
|
andrewm@0
|
199 keyboard_.sendMessage("/touchkeys/keysegment", "ii", noteNumber_, segment, LO_ARGS_END);
|
andrewm@0
|
200 if(segment < segmentBends_.size() && segment >= 0) {
|
andrewm@0
|
201 #ifdef DEBUG_KEY_DIVISION_MAPPING
|
andrewm@0
|
202 cout << "TouchkeyKeyDivisionMapping::sendSegmentMessage(): pitch bend = " << segmentBends_[segment] << endl;
|
andrewm@0
|
203 #endif
|
andrewm@0
|
204 sendPitchBendMessage(segmentBends_[segment], force);
|
andrewm@0
|
205 }
|
andrewm@0
|
206 else {
|
andrewm@0
|
207 #ifdef DEBUG_KEY_DIVISION_MAPPING
|
andrewm@0
|
208 cout << "TouchkeyKeyDivisionMapping::sendSegmentMessage(): no bend for segment " << segment << endl;
|
andrewm@0
|
209 #endif
|
andrewm@0
|
210 }
|
andrewm@0
|
211 }
|
andrewm@0
|
212 else {
|
andrewm@0
|
213 #ifdef DEBUG_KEY_DIVISION_MAPPING
|
andrewm@0
|
214 cout << "TouchkeyKeyDivisionMapping::sendSegmentMessage(): suspended, not sending segment " << segment << endl;
|
andrewm@0
|
215 #endif
|
andrewm@0
|
216 }
|
andrewm@0
|
217 }
|
andrewm@0
|
218
|
andrewm@0
|
219 // Send the pitch bend message of a given number of a semitones. Send by OSC,
|
andrewm@0
|
220 // which can be mapped to MIDI CC externally
|
andrewm@0
|
221 void TouchkeyKeyDivisionMapping::sendPitchBendMessage(float pitchBendSemitones, bool force) {
|
andrewm@0
|
222 if(force || !suspended_)
|
andrewm@0
|
223 keyboard_.sendMessage(controlName_.c_str(), "if", noteNumber_, pitchBendSemitones, LO_ARGS_END);
|
andrewm@0
|
224 }
|
andrewm@0
|
225
|
andrewm@0
|
226 // Find the segment corresponding to a (Y) touch location
|
andrewm@0
|
227 int TouchkeyKeyDivisionMapping::segmentForLocation(float location) {
|
andrewm@0
|
228 // Divide the key into evenly-spaced regions, and identify and candidate segment.
|
andrewm@0
|
229 // Since the location can go up to 1.0, make sure the top value doesn't overflow
|
andrewm@0
|
230 // the number of segments
|
andrewm@0
|
231 int segment = floorf(location * (float)numberOfSegments_);
|
andrewm@0
|
232 if(segment >= numberOfSegments_)
|
andrewm@0
|
233 segment = numberOfSegments_ - 1;
|
andrewm@0
|
234 return segment;
|
andrewm@0
|
235 }
|
andrewm@0
|
236
|
andrewm@0
|
237 // Find the segment corresponding to a number of touches
|
andrewm@0
|
238 int TouchkeyKeyDivisionMapping::segmentForNumTouches(int numTouches) {
|
andrewm@0
|
239 // Check the number of touches, which could divide the note into as many
|
andrewm@0
|
240 // as three segments.
|
andrewm@0
|
241 if(numTouches <= 0)
|
andrewm@0
|
242 return -1;
|
andrewm@0
|
243
|
andrewm@0
|
244 int segment = numTouches - 1;
|
andrewm@0
|
245 if(segment >= numberOfSegments_)
|
andrewm@0
|
246 segment = numberOfSegments_ - 1;
|
andrewm@0
|
247 return segment;
|
andrewm@0
|
248 }
|
andrewm@0
|
249
|
andrewm@0
|
250 // Return the location of the most recently added touch (indicated by the highest ID)
|
andrewm@0
|
251 float TouchkeyKeyDivisionMapping::locationOfNewestTouch(KeyTouchFrame const& frame) {
|
andrewm@0
|
252 if(frame.count == 0)
|
andrewm@0
|
253 return -1.0;
|
andrewm@0
|
254
|
andrewm@0
|
255 // Go through the active touches and find the one with the highest id
|
andrewm@0
|
256 int highestId = -1;
|
andrewm@0
|
257 float location = -1.0;
|
andrewm@0
|
258 for(int i = 0; i < frame.count; i++) {
|
andrewm@0
|
259 if(frame.ids[i] > highestId) {
|
andrewm@0
|
260 highestId = frame.ids[i];
|
andrewm@0
|
261 location = frame.locs[i];
|
andrewm@0
|
262 }
|
andrewm@0
|
263 }
|
andrewm@0
|
264
|
andrewm@0
|
265 return location;
|
andrewm@0
|
266 } |