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 Mapping.cpp: base class for a single-note mapping. The mapping will take in
|
andrewm@0
|
21 MIDI, touch and (optionally) continuous key position data, and generate
|
andrewm@0
|
22 specific OSC or MIDI messages. TouchKeys-specific mappings generally
|
andrewm@0
|
23 inherit from the TouchkeyBaseMapping subclass, which provides other
|
andrewm@0
|
24 useful generic methods.
|
andrewm@0
|
25 */
|
andrewm@0
|
26
|
andrewm@0
|
27 #include "Mapping.h"
|
andrewm@0
|
28 #include "MappingFactory.h"
|
andrewm@0
|
29 #include "MappingScheduler.h"
|
andrewm@0
|
30
|
andrewm@0
|
31 // Class constants
|
andrewm@0
|
32 const timestamp_diff_type Mapping::kDefaultUpdateInterval = microseconds_to_timestamp(5500);
|
andrewm@0
|
33
|
andrewm@0
|
34 // Main constructor takes references/pointers from objects which keep track
|
andrewm@0
|
35 // of touch location, continuous key position and the state detected from that
|
andrewm@0
|
36 // position. The PianoKeyboard object is strictly required as it gives access to
|
andrewm@0
|
37 // Scheduler and OSC methods. The others are optional since any given system may
|
andrewm@0
|
38 // contain only one of continuous key position or touch sensitivity
|
andrewm@0
|
39 Mapping::Mapping(PianoKeyboard &keyboard, MappingFactory *factory, int noteNumber, Node<KeyTouchFrame>* touchBuffer,
|
andrewm@0
|
40 Node<key_position>* positionBuffer, KeyPositionTracker* positionTracker)
|
andrewm@0
|
41 : keyboard_(keyboard), factory_(factory), noteNumber_(noteNumber), touchBuffer_(touchBuffer),
|
andrewm@0
|
42 positionBuffer_(positionBuffer), positionTracker_(positionTracker), engaged_(false),
|
andrewm@0
|
43 suspended_(false), updateInterval_(kDefaultUpdateInterval),
|
andrewm@0
|
44 nextScheduledTimestamp_(0)
|
andrewm@0
|
45 {
|
andrewm@0
|
46 // Create a statically bound call to the performMapping() method that
|
andrewm@0
|
47 // we use each time we schedule a new mapping
|
andrewm@0
|
48 mappingAction_ = boost::bind(&Mapping::performMapping, this);
|
andrewm@0
|
49 }
|
andrewm@0
|
50
|
andrewm@0
|
51 // Copy constructor
|
andrewm@0
|
52 Mapping::Mapping(Mapping const& obj) : keyboard_(obj.keyboard_), factory_(obj.factory_), noteNumber_(obj.noteNumber_),
|
andrewm@0
|
53 touchBuffer_(obj.touchBuffer_), positionBuffer_(obj.positionBuffer_), positionTracker_(obj.positionTracker_),
|
andrewm@0
|
54 engaged_(obj.engaged_), updateInterval_(obj.updateInterval_),
|
andrewm@0
|
55 nextScheduledTimestamp_(obj.nextScheduledTimestamp_)
|
andrewm@0
|
56 {
|
andrewm@0
|
57 // Create a statically bound call to the performMapping() method that
|
andrewm@0
|
58 // we use each time we schedule a new mapping
|
andrewm@0
|
59 mappingAction_ = boost::bind(&Mapping::performMapping, this);
|
andrewm@0
|
60
|
andrewm@0
|
61 // Register ourself if already engaged since the scheduler won't have a copy of this object
|
andrewm@0
|
62 if(engaged_) {
|
andrewm@0
|
63 #ifdef NEW_MAPPING_SCHEDULER
|
andrewm@0
|
64 keyboard_.mappingScheduler().scheduleNow(this);
|
andrewm@0
|
65 #else
|
andrewm@0
|
66 keyboard_.scheduleEvent(this, mappingAction_, keyboard_.schedulerCurrentTimestamp());
|
andrewm@0
|
67 #endif
|
andrewm@0
|
68 }
|
andrewm@0
|
69 }
|
andrewm@0
|
70
|
andrewm@0
|
71 // Destructor. IMPORTANT NOTE: any derived class of Mapping() needs to call disengage() in its
|
andrewm@0
|
72 // own destructor. It can't be called here, or there is a risk that the scheduled action will be
|
andrewm@0
|
73 // called between the destruction of the derived class and the destruction of Mapping. This
|
andrewm@0
|
74 // will result in a pure virtual function call and a crash.
|
andrewm@0
|
75 Mapping::~Mapping() {
|
andrewm@0
|
76 //std::cerr << "~Mapping(): " << this << std::endl;
|
andrewm@0
|
77 }
|
andrewm@0
|
78
|
andrewm@0
|
79 // Turn on mapping of data. Register for a callback and set a flag so
|
andrewm@0
|
80 // we continue to receive updates
|
andrewm@0
|
81 void Mapping::engage() {
|
andrewm@0
|
82 engaged_ = true;
|
andrewm@0
|
83
|
andrewm@0
|
84 //cout << "Mapping::engage(): before TS " << keyboard_.schedulerCurrentTimestamp() << std::endl;
|
andrewm@0
|
85 // Register for trigger updates from touch data and state updates if either one is present.
|
andrewm@0
|
86 // Don't register for triggers on each new key sample
|
andrewm@0
|
87 if(touchBuffer_ != 0)
|
andrewm@0
|
88 registerForTrigger(touchBuffer_);
|
andrewm@0
|
89 if(positionTracker_ != 0)
|
andrewm@0
|
90 registerForTrigger(positionTracker_);
|
andrewm@0
|
91 nextScheduledTimestamp_ = keyboard_.schedulerCurrentTimestamp();
|
andrewm@0
|
92 //cout << "Mapping::engage(): mid TS " << keyboard_.schedulerCurrentTimestamp() << std::endl;
|
andrewm@0
|
93 #ifdef NEW_MAPPING_SCHEDULER
|
andrewm@0
|
94 keyboard_.mappingScheduler().registerMapping(this);
|
andrewm@0
|
95 keyboard_.mappingScheduler().scheduleNow(this);
|
andrewm@0
|
96 #else
|
andrewm@0
|
97 keyboard_.scheduleEvent(this, mappingAction_, nextScheduledTimestamp_);
|
andrewm@0
|
98 #endif
|
andrewm@0
|
99 //cout << "Mapping::engage(): after TS " << keyboard_.schedulerCurrentTimestamp() << std::endl;
|
andrewm@0
|
100 }
|
andrewm@0
|
101
|
andrewm@0
|
102 // Turn off mapping of data. Remove our callback from the scheduler
|
andrewm@0
|
103 void Mapping::disengage(bool shouldDelete) {
|
andrewm@0
|
104 //std::cerr << "Mapping::disengage(): " << this << std::endl;
|
andrewm@0
|
105
|
andrewm@0
|
106 engaged_ = false;
|
andrewm@0
|
107 #ifndef NEW_MAPPING_SCHEDULER
|
andrewm@0
|
108 keyboard_.unscheduleEvent(this/*, nextScheduledTimestamp_*/);
|
andrewm@0
|
109 #endif
|
andrewm@0
|
110 // Unregister for updates from touch data
|
andrewm@0
|
111 if(touchBuffer_ != 0)
|
andrewm@0
|
112 unregisterForTrigger(touchBuffer_);
|
andrewm@0
|
113 if(positionTracker_ != 0)
|
andrewm@0
|
114 unregisterForTrigger(positionTracker_);
|
andrewm@0
|
115 //std::cerr << "Mapping::disengage(): done\n";
|
andrewm@0
|
116
|
andrewm@0
|
117 #ifdef NEW_MAPPING_SCHEDULER
|
andrewm@0
|
118 if(shouldDelete)
|
andrewm@0
|
119 keyboard_.mappingScheduler().unregisterAndDelete(this);
|
andrewm@0
|
120 else
|
andrewm@0
|
121 keyboard_.mappingScheduler().unregisterMapping(this);
|
andrewm@0
|
122 #endif
|
andrewm@0
|
123 }
|
andrewm@0
|
124
|
andrewm@0
|
125 // Reset state back to defaults
|
andrewm@0
|
126 void Mapping::reset() {
|
andrewm@0
|
127 updateInterval_ = kDefaultUpdateInterval;
|
andrewm@0
|
128 }
|