andrewm@0: /* andrewm@0: TouchKeys: multi-touch musical keyboard control software andrewm@0: Copyright (c) 2013 Andrew McPherson andrewm@0: andrewm@0: This program is free software: you can redistribute it and/or modify andrewm@0: it under the terms of the GNU General Public License as published by andrewm@0: the Free Software Foundation, either version 3 of the License, or andrewm@0: (at your option) any later version. andrewm@0: andrewm@0: This program is distributed in the hope that it will be useful, andrewm@0: but WITHOUT ANY WARRANTY; without even the implied warranty of andrewm@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrewm@0: GNU General Public License for more details. andrewm@0: andrewm@0: You should have received a copy of the GNU General Public License andrewm@0: along with this program. If not, see . andrewm@0: andrewm@0: ===================================================================== andrewm@0: andrewm@0: Mapping.cpp: base class for a single-note mapping. The mapping will take in andrewm@0: MIDI, touch and (optionally) continuous key position data, and generate andrewm@0: specific OSC or MIDI messages. TouchKeys-specific mappings generally andrewm@0: inherit from the TouchkeyBaseMapping subclass, which provides other andrewm@0: useful generic methods. andrewm@0: */ andrewm@0: andrewm@0: #include "Mapping.h" andrewm@0: #include "MappingFactory.h" andrewm@0: #include "MappingScheduler.h" andrewm@0: andrewm@0: // Class constants andrewm@0: const timestamp_diff_type Mapping::kDefaultUpdateInterval = microseconds_to_timestamp(5500); andrewm@0: andrewm@0: // Main constructor takes references/pointers from objects which keep track andrewm@0: // of touch location, continuous key position and the state detected from that andrewm@0: // position. The PianoKeyboard object is strictly required as it gives access to andrewm@0: // Scheduler and OSC methods. The others are optional since any given system may andrewm@0: // contain only one of continuous key position or touch sensitivity andrewm@0: Mapping::Mapping(PianoKeyboard &keyboard, MappingFactory *factory, int noteNumber, Node* touchBuffer, andrewm@0: Node* positionBuffer, KeyPositionTracker* positionTracker) andrewm@0: : keyboard_(keyboard), factory_(factory), noteNumber_(noteNumber), touchBuffer_(touchBuffer), andrewm@0: positionBuffer_(positionBuffer), positionTracker_(positionTracker), engaged_(false), andrewm@0: suspended_(false), updateInterval_(kDefaultUpdateInterval), andrewm@0: nextScheduledTimestamp_(0) andrewm@0: { andrewm@0: // Create a statically bound call to the performMapping() method that andrewm@0: // we use each time we schedule a new mapping andrewm@0: mappingAction_ = boost::bind(&Mapping::performMapping, this); andrewm@0: } andrewm@0: andrewm@0: // Copy constructor andrewm@0: Mapping::Mapping(Mapping const& obj) : keyboard_(obj.keyboard_), factory_(obj.factory_), noteNumber_(obj.noteNumber_), andrewm@0: touchBuffer_(obj.touchBuffer_), positionBuffer_(obj.positionBuffer_), positionTracker_(obj.positionTracker_), andrewm@0: engaged_(obj.engaged_), updateInterval_(obj.updateInterval_), andrewm@0: nextScheduledTimestamp_(obj.nextScheduledTimestamp_) andrewm@0: { andrewm@0: // Create a statically bound call to the performMapping() method that andrewm@0: // we use each time we schedule a new mapping andrewm@0: mappingAction_ = boost::bind(&Mapping::performMapping, this); andrewm@0: andrewm@0: // Register ourself if already engaged since the scheduler won't have a copy of this object andrewm@0: if(engaged_) { andrewm@0: #ifdef NEW_MAPPING_SCHEDULER andrewm@0: keyboard_.mappingScheduler().scheduleNow(this); andrewm@0: #else andrewm@0: keyboard_.scheduleEvent(this, mappingAction_, keyboard_.schedulerCurrentTimestamp()); andrewm@0: #endif andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: // Destructor. IMPORTANT NOTE: any derived class of Mapping() needs to call disengage() in its andrewm@0: // own destructor. It can't be called here, or there is a risk that the scheduled action will be andrewm@0: // called between the destruction of the derived class and the destruction of Mapping. This andrewm@0: // will result in a pure virtual function call and a crash. andrewm@0: Mapping::~Mapping() { andrewm@0: //std::cerr << "~Mapping(): " << this << std::endl; andrewm@0: } andrewm@0: andrewm@0: // Turn on mapping of data. Register for a callback and set a flag so andrewm@0: // we continue to receive updates andrewm@0: void Mapping::engage() { andrewm@0: engaged_ = true; andrewm@0: andrewm@0: //cout << "Mapping::engage(): before TS " << keyboard_.schedulerCurrentTimestamp() << std::endl; andrewm@0: // Register for trigger updates from touch data and state updates if either one is present. andrewm@0: // Don't register for triggers on each new key sample andrewm@0: if(touchBuffer_ != 0) andrewm@0: registerForTrigger(touchBuffer_); andrewm@0: if(positionTracker_ != 0) andrewm@0: registerForTrigger(positionTracker_); andrewm@0: nextScheduledTimestamp_ = keyboard_.schedulerCurrentTimestamp(); andrewm@0: //cout << "Mapping::engage(): mid TS " << keyboard_.schedulerCurrentTimestamp() << std::endl; andrewm@0: #ifdef NEW_MAPPING_SCHEDULER andrewm@0: keyboard_.mappingScheduler().registerMapping(this); andrewm@0: keyboard_.mappingScheduler().scheduleNow(this); andrewm@0: #else andrewm@0: keyboard_.scheduleEvent(this, mappingAction_, nextScheduledTimestamp_); andrewm@0: #endif andrewm@0: //cout << "Mapping::engage(): after TS " << keyboard_.schedulerCurrentTimestamp() << std::endl; andrewm@0: } andrewm@0: andrewm@0: // Turn off mapping of data. Remove our callback from the scheduler andrewm@0: void Mapping::disengage(bool shouldDelete) { andrewm@0: //std::cerr << "Mapping::disengage(): " << this << std::endl; andrewm@0: andrewm@0: engaged_ = false; andrewm@0: #ifndef NEW_MAPPING_SCHEDULER andrewm@0: keyboard_.unscheduleEvent(this/*, nextScheduledTimestamp_*/); andrewm@0: #endif andrewm@0: // Unregister for updates from touch data andrewm@0: if(touchBuffer_ != 0) andrewm@0: unregisterForTrigger(touchBuffer_); andrewm@0: if(positionTracker_ != 0) andrewm@0: unregisterForTrigger(positionTracker_); andrewm@0: //std::cerr << "Mapping::disengage(): done\n"; andrewm@0: andrewm@0: #ifdef NEW_MAPPING_SCHEDULER andrewm@0: if(shouldDelete) andrewm@0: keyboard_.mappingScheduler().unregisterAndDelete(this); andrewm@0: else andrewm@0: keyboard_.mappingScheduler().unregisterMapping(this); andrewm@0: #endif andrewm@0: } andrewm@0: andrewm@0: // Reset state back to defaults andrewm@0: void Mapping::reset() { andrewm@0: updateInterval_ = kDefaultUpdateInterval; andrewm@0: }