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.h: 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
|
andrewm@0
|
26 #ifndef __TouchKeys__TouchkeyKeyDivisionMapping__
|
andrewm@0
|
27 #define __TouchKeys__TouchkeyKeyDivisionMapping__
|
andrewm@0
|
28
|
andrewm@0
|
29 #include "../TouchkeyBaseMapping.h"
|
andrewm@0
|
30
|
andrewm@0
|
31 // This class handles the division of a key into multiple
|
andrewm@0
|
32 // subsections, for example to handle microtonal divisions
|
andrewm@0
|
33
|
andrewm@0
|
34 class TouchkeyKeyDivisionMapping : public TouchkeyBaseMapping {
|
andrewm@0
|
35 friend class TouchkeyKeyDivisionMappingFactory;
|
andrewm@0
|
36 public:
|
andrewm@0
|
37 enum {
|
andrewm@42
|
38 kDetectionParameterYPosition = 1,
|
andrewm@0
|
39 kDetectionParameterNumberOfTouches,
|
andrewm@51
|
40 kDetectionParameterYPositionAndNumberOfTouches,
|
andrewm@51
|
41 kDetectionParameterMaxValue
|
andrewm@0
|
42 };
|
andrewm@0
|
43
|
andrewm@0
|
44 private:
|
andrewm@0
|
45 // Default values
|
andrewm@0
|
46 static const int kDefaultNumberOfSegments;
|
andrewm@0
|
47 static const timestamp_diff_type kDefaultDetectionTimeout;
|
andrewm@0
|
48 static const int kDefaultDetectionParameter;
|
andrewm@0
|
49 static const int kDefaultRetriggerNumFrames;
|
andrewm@0
|
50
|
andrewm@0
|
51 public:
|
andrewm@0
|
52 // ***** Constructors *****
|
andrewm@0
|
53
|
andrewm@0
|
54 // Default constructor, passing the buffer on which to trigger
|
andrewm@0
|
55 TouchkeyKeyDivisionMapping(PianoKeyboard &keyboard, MappingFactory *factory, int noteNumber, Node<KeyTouchFrame>* touchBuffer,
|
andrewm@0
|
56 Node<key_position>* positionBuffer, KeyPositionTracker* positionTracker);
|
andrewm@0
|
57
|
andrewm@0
|
58 // ***** Modifiers *****
|
andrewm@0
|
59
|
andrewm@0
|
60 // Reset the state back initial values
|
andrewm@0
|
61 void reset();
|
andrewm@0
|
62
|
andrewm@0
|
63 // Resend the current state of all parameters
|
andrewm@0
|
64 void resend();
|
andrewm@0
|
65
|
andrewm@0
|
66 // ***** Evaluators *****
|
andrewm@0
|
67
|
andrewm@0
|
68 // This method receives triggers whenever events occur in the touch data or the
|
andrewm@0
|
69 // continuous key position (state changes only). It alters the behavior and scheduling
|
andrewm@0
|
70 // of the mapping but does not itself send OSC messages
|
andrewm@0
|
71 void triggerReceived(TriggerSource* who, timestamp_type timestamp);
|
andrewm@0
|
72
|
andrewm@0
|
73 // This method handles the OSC message transmission. It should be run in the Scheduler
|
andrewm@0
|
74 // thread provided by PianoKeyboard.
|
andrewm@0
|
75 timestamp_type performMapping();
|
andrewm@0
|
76
|
andrewm@0
|
77 // ***** Specific Methods *****
|
andrewm@0
|
78
|
andrewm@0
|
79 // Set the number of segments
|
andrewm@0
|
80 void setNumberOfSegments(int segments) {
|
andrewm@0
|
81 if(segments > 0)
|
andrewm@0
|
82 numberOfSegments_ = segments;
|
andrewm@0
|
83 }
|
andrewm@0
|
84
|
andrewm@0
|
85 // Set the default segment (upon timeout)
|
andrewm@0
|
86 void setDefaultSegment(int defaultSegment) {
|
andrewm@0
|
87 defaultSegment_ = defaultSegment;
|
andrewm@0
|
88 }
|
andrewm@0
|
89
|
andrewm@0
|
90 // Set the pitch bend values associated with each key segment
|
andrewm@0
|
91 void setSegmentPitchBends(const float *bendsInSemitones, int numBends);
|
andrewm@0
|
92
|
andrewm@0
|
93 // Set the detection timeout value (how long from MIDI note on to touch)
|
andrewm@0
|
94 void setTimeout(timestamp_diff_type timeout) {
|
andrewm@0
|
95 timeout_ = timeout;
|
andrewm@0
|
96 }
|
andrewm@0
|
97
|
andrewm@0
|
98 // Set which parameter is used to detect segment
|
andrewm@0
|
99 void setDetectionParameter(int parameter) {
|
andrewm@0
|
100 detectionParameter_ = parameter;
|
andrewm@0
|
101 }
|
andrewm@0
|
102
|
andrewm@0
|
103 // Set whether placing a second finger in the other segment triggers a
|
andrewm@0
|
104 // new note with that segment.
|
andrewm@0
|
105 void setRetriggerable(bool retrigger, int numFrames, bool keepOriginalVelocity) {
|
andrewm@0
|
106 retriggerable_ = retrigger;
|
andrewm@0
|
107 retriggerNumFrames_ = numFrames;
|
andrewm@0
|
108 retriggerKeepsVelocity_ = keepOriginalVelocity;
|
andrewm@0
|
109 }
|
andrewm@0
|
110
|
andrewm@0
|
111 private:
|
andrewm@0
|
112 // ***** Private Methods *****
|
andrewm@0
|
113
|
andrewm@0
|
114 void midiNoteOnReceived(int channel, int velocity);
|
andrewm@0
|
115 void midiNoteOffReceived(int channel);
|
andrewm@0
|
116
|
andrewm@0
|
117 void sendSegmentMessage(int segment, bool force = false);
|
andrewm@0
|
118 void sendPitchBendMessage(float pitchBendSemitones, bool force = false);
|
andrewm@0
|
119
|
andrewm@0
|
120 int segmentForLocation(float location);
|
andrewm@0
|
121 int segmentForNumTouches(int numTouches);
|
andrewm@0
|
122 float locationOfNewestTouch(KeyTouchFrame const& frame);
|
andrewm@0
|
123
|
andrewm@0
|
124 // ***** Member Variables *****
|
andrewm@0
|
125
|
andrewm@0
|
126 int numberOfSegments_; // How many segments to choose from total
|
andrewm@0
|
127 int candidateSegment_; // Which segment we would be in if the press were now
|
andrewm@0
|
128 int detectedSegment_; // Which segment of the key we detected
|
andrewm@0
|
129 int defaultSegment_; // Which segment to choose by default (on timeout)
|
andrewm@0
|
130 int detectionParameter_; // Which parameter is used to determine the segment
|
andrewm@0
|
131 bool retriggerable_; // Whether a second touch can retrigger this note
|
andrewm@0
|
132 int retriggerNumFrames_; // How many frames a new touch must be present to retrigger
|
andrewm@0
|
133 bool retriggerKeepsVelocity_; // Whether a retriggered note keeps the original velocity or a default
|
andrewm@0
|
134
|
andrewm@0
|
135 timestamp_type midiNoteOnTimestamp_; // When the MIDI note went on
|
andrewm@0
|
136 timestamp_diff_type timeout_; // How long to wait for a touch event
|
andrewm@0
|
137 int lastNumActiveTouches_; // How many touches were active before
|
andrewm@0
|
138
|
andrewm@0
|
139 std::vector<float> segmentBends_; // What the pitch bend values are for each segment
|
andrewm@0
|
140 };
|
andrewm@0
|
141
|
andrewm@0
|
142 #endif /* defined(__TouchKeys__TouchkeyKeyDivisionMapping__) */
|