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: TouchkeyKeyDivisionMapping.h: per-note mapping for the split-key mapping andrewm@0: which triggers different actions or pitches depending on where the key andrewm@0: was struck. andrewm@0: */ andrewm@0: andrewm@0: andrewm@0: #ifndef __TouchKeys__TouchkeyKeyDivisionMapping__ andrewm@0: #define __TouchKeys__TouchkeyKeyDivisionMapping__ andrewm@0: andrewm@0: #include "../TouchkeyBaseMapping.h" andrewm@0: andrewm@0: // This class handles the division of a key into multiple andrewm@0: // subsections, for example to handle microtonal divisions andrewm@0: andrewm@0: class TouchkeyKeyDivisionMapping : public TouchkeyBaseMapping { andrewm@0: friend class TouchkeyKeyDivisionMappingFactory; andrewm@0: public: andrewm@0: enum { andrewm@42: kDetectionParameterYPosition = 1, andrewm@0: kDetectionParameterNumberOfTouches, andrewm@51: kDetectionParameterYPositionAndNumberOfTouches, andrewm@51: kDetectionParameterMaxValue andrewm@0: }; andrewm@0: andrewm@0: private: andrewm@0: // Default values andrewm@0: static const int kDefaultNumberOfSegments; andrewm@0: static const timestamp_diff_type kDefaultDetectionTimeout; andrewm@0: static const int kDefaultDetectionParameter; andrewm@0: static const int kDefaultRetriggerNumFrames; andrewm@0: andrewm@0: public: andrewm@0: // ***** Constructors ***** andrewm@0: andrewm@0: // Default constructor, passing the buffer on which to trigger andrewm@0: TouchkeyKeyDivisionMapping(PianoKeyboard &keyboard, MappingFactory *factory, int noteNumber, Node* touchBuffer, andrewm@0: Node* positionBuffer, KeyPositionTracker* positionTracker); andrewm@0: andrewm@0: // ***** Modifiers ***** andrewm@0: andrewm@0: // Reset the state back initial values andrewm@0: void reset(); andrewm@0: andrewm@0: // Resend the current state of all parameters andrewm@0: void resend(); andrewm@0: andrewm@0: // ***** Evaluators ***** andrewm@0: andrewm@0: // This method receives triggers whenever events occur in the touch data or the andrewm@0: // continuous key position (state changes only). It alters the behavior and scheduling andrewm@0: // of the mapping but does not itself send OSC messages andrewm@0: void triggerReceived(TriggerSource* who, timestamp_type timestamp); andrewm@0: andrewm@0: // This method handles the OSC message transmission. It should be run in the Scheduler andrewm@0: // thread provided by PianoKeyboard. andrewm@0: timestamp_type performMapping(); andrewm@0: andrewm@0: // ***** Specific Methods ***** andrewm@0: andrewm@0: // Set the number of segments andrewm@0: void setNumberOfSegments(int segments) { andrewm@0: if(segments > 0) andrewm@0: numberOfSegments_ = segments; andrewm@0: } andrewm@0: andrewm@0: // Set the default segment (upon timeout) andrewm@0: void setDefaultSegment(int defaultSegment) { andrewm@0: defaultSegment_ = defaultSegment; andrewm@0: } andrewm@0: andrewm@0: // Set the pitch bend values associated with each key segment andrewm@0: void setSegmentPitchBends(const float *bendsInSemitones, int numBends); andrewm@0: andrewm@0: // Set the detection timeout value (how long from MIDI note on to touch) andrewm@0: void setTimeout(timestamp_diff_type timeout) { andrewm@0: timeout_ = timeout; andrewm@0: } andrewm@0: andrewm@0: // Set which parameter is used to detect segment andrewm@0: void setDetectionParameter(int parameter) { andrewm@0: detectionParameter_ = parameter; andrewm@0: } andrewm@0: andrewm@0: // Set whether placing a second finger in the other segment triggers a andrewm@0: // new note with that segment. andrewm@0: void setRetriggerable(bool retrigger, int numFrames, bool keepOriginalVelocity) { andrewm@0: retriggerable_ = retrigger; andrewm@0: retriggerNumFrames_ = numFrames; andrewm@0: retriggerKeepsVelocity_ = keepOriginalVelocity; andrewm@0: } andrewm@0: andrewm@0: private: andrewm@0: // ***** Private Methods ***** andrewm@0: andrewm@0: void midiNoteOnReceived(int channel, int velocity); andrewm@0: void midiNoteOffReceived(int channel); andrewm@0: andrewm@0: void sendSegmentMessage(int segment, bool force = false); andrewm@0: void sendPitchBendMessage(float pitchBendSemitones, bool force = false); andrewm@0: andrewm@0: int segmentForLocation(float location); andrewm@0: int segmentForNumTouches(int numTouches); andrewm@0: float locationOfNewestTouch(KeyTouchFrame const& frame); andrewm@0: andrewm@0: // ***** Member Variables ***** andrewm@0: andrewm@0: int numberOfSegments_; // How many segments to choose from total andrewm@0: int candidateSegment_; // Which segment we would be in if the press were now andrewm@0: int detectedSegment_; // Which segment of the key we detected andrewm@0: int defaultSegment_; // Which segment to choose by default (on timeout) andrewm@0: int detectionParameter_; // Which parameter is used to determine the segment andrewm@0: bool retriggerable_; // Whether a second touch can retrigger this note andrewm@0: int retriggerNumFrames_; // How many frames a new touch must be present to retrigger andrewm@0: bool retriggerKeepsVelocity_; // Whether a retriggered note keeps the original velocity or a default andrewm@0: andrewm@0: timestamp_type midiNoteOnTimestamp_; // When the MIDI note went on andrewm@0: timestamp_diff_type timeout_; // How long to wait for a touch event andrewm@0: int lastNumActiveTouches_; // How many touches were active before andrewm@0: andrewm@0: std::vector segmentBends_; // What the pitch bend values are for each segment andrewm@0: }; andrewm@0: andrewm@0: #endif /* defined(__TouchKeys__TouchkeyKeyDivisionMapping__) */