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 MIDIKeyPositionMapping.h: handles generating MIDI data out of continuous
|
andrewm@0
|
21 key position.
|
andrewm@0
|
22 */
|
andrewm@0
|
23
|
andrewm@0
|
24 #ifndef __touchkeys__MIDIKeyPositionMapping__
|
andrewm@0
|
25 #define __touchkeys__MIDIKeyPositionMapping__
|
andrewm@0
|
26
|
andrewm@0
|
27 #include <map>
|
andrewm@0
|
28 #include <boost/bind.hpp>
|
andrewm@0
|
29 #include "../TouchKeys/KeyTouchFrame.h"
|
andrewm@0
|
30 #include "../TouchKeys/KeyPositionTracker.h"
|
andrewm@0
|
31 #include "../TouchKeys/PianoKeyboard.h"
|
andrewm@0
|
32 #include "Mapping.h"
|
andrewm@0
|
33
|
andrewm@0
|
34 // This class handles the mapping from continuous key position to
|
andrewm@0
|
35 // MIDI messages: note on, note off, aftertouch.
|
andrewm@0
|
36
|
andrewm@0
|
37 class MIDIKeyPositionMapping : public Mapping {
|
andrewm@0
|
38 private:
|
andrewm@0
|
39 /*const int kDefaultMIDIChannel = 0;
|
andrewm@0
|
40 const float kDefaultAftertouchScaler = 127.0 / 0.03; // Default aftertouch sensitivity: MIDI 127 = 0.03
|
andrewm@0
|
41 const float kMinimumAftertouchPosition = 0.99; // Position at which aftertouch messages start
|
andrewm@0
|
42 const float kDefaultPercussivenessScaler = 1.0 / 300.0; // Default scaler from percussiveness feature to MIDI
|
andrewm@0
|
43 const key_velocity kPianoKeyVelocityForMaxMIDI = scale_key_velocity(40.0); // Press velocity for MIDI 127
|
andrewm@0
|
44 const key_velocity kPianoKeyReleaseVelocityForMaxMIDI = scale_key_velocity(-50.0); // Release velocity for MIDI 127*/
|
andrewm@0
|
45 static const int kDefaultMIDIChannel;
|
andrewm@0
|
46 static const float kDefaultAftertouchScaler; // Default aftertouch sensitivity: MIDI 127 = 0.03
|
andrewm@0
|
47 static const float kMinimumAftertouchPosition; // Position at which aftertouch messages start
|
andrewm@0
|
48 static const float kDefaultPercussivenessScaler; // Default scaler from percussiveness feature to MIDI
|
andrewm@0
|
49 static const key_velocity kPianoKeyVelocityForMaxMIDI; // Press velocity for MIDI 127
|
andrewm@0
|
50 static const key_velocity kPianoKeyReleaseVelocityForMaxMIDI; // Release velocity for MIDI 127
|
andrewm@0
|
51
|
andrewm@0
|
52 public:
|
andrewm@0
|
53 // ***** Constructors *****
|
andrewm@0
|
54
|
andrewm@0
|
55 // Default constructor, passing the buffer on which to trigger
|
andrewm@0
|
56 MIDIKeyPositionMapping(PianoKeyboard &keyboard, MappingFactory *factory, int noteNumber, Node<KeyTouchFrame>* touchBuffer,
|
andrewm@0
|
57 Node<key_position>* positionBuffer, KeyPositionTracker* positionTracker);
|
andrewm@0
|
58
|
andrewm@0
|
59 // Copy constructor
|
andrewm@0
|
60 MIDIKeyPositionMapping(MIDIKeyPositionMapping const& obj);
|
andrewm@0
|
61
|
andrewm@0
|
62 // ***** Destructor *****
|
andrewm@0
|
63
|
andrewm@0
|
64 ~MIDIKeyPositionMapping();
|
andrewm@0
|
65
|
andrewm@0
|
66 // ***** Modifiers *****
|
andrewm@0
|
67
|
andrewm@0
|
68 // Disable mappings from being sent
|
andrewm@0
|
69 void disengage();
|
andrewm@0
|
70
|
andrewm@0
|
71 // Reset the state back initial values
|
andrewm@0
|
72 void reset();
|
andrewm@0
|
73
|
andrewm@0
|
74 // Set the aftertouch sensitivity on continuous key position
|
andrewm@0
|
75 // 0 means no aftertouch, 1 means default sensitivity, upward
|
andrewm@0
|
76 // from there
|
andrewm@0
|
77 void setAftertouchSensitivity(float sensitivity);
|
andrewm@0
|
78
|
andrewm@0
|
79 // Get or set the MIDI channel (0-15)
|
andrewm@0
|
80 int midiChannel() { return midiChannel_; }
|
andrewm@0
|
81 void setMIDIChannel(int ch) {
|
andrewm@0
|
82 if(ch >= 0 && ch < 16)
|
andrewm@0
|
83 midiChannel_ = ch;
|
andrewm@0
|
84 }
|
andrewm@0
|
85
|
andrewm@0
|
86 // Get or set the MIDI channel for percussiveness messages
|
andrewm@0
|
87 int percussivenessMIDIChannel() { return midiPercussivenessChannel_; }
|
andrewm@0
|
88 void setPercussivenessMIDIChannel(int ch) {
|
andrewm@0
|
89 if(ch >= 0 && ch < 16)
|
andrewm@0
|
90 midiPercussivenessChannel_ = ch;
|
andrewm@0
|
91 else
|
andrewm@0
|
92 midiPercussivenessChannel_ = -1;
|
andrewm@0
|
93 }
|
andrewm@0
|
94 void disableMIDIPercussiveness() { midiPercussivenessChannel_ = -1; }
|
andrewm@0
|
95
|
andrewm@0
|
96 // ***** Evaluators *****
|
andrewm@0
|
97
|
andrewm@0
|
98 // This method receives triggers whenever events occur in the touch data or the
|
andrewm@0
|
99 // continuous key position (state changes only). It alters the behavior and scheduling
|
andrewm@0
|
100 // of the mapping but does not itself send OSC messages
|
andrewm@0
|
101 void triggerReceived(TriggerSource* who, timestamp_type timestamp);
|
andrewm@0
|
102
|
andrewm@0
|
103 // This method handles the OSC message transmission. It should be run in the Scheduler
|
andrewm@0
|
104 // thread provided by PianoKeyboard.
|
andrewm@0
|
105 timestamp_type performMapping();
|
andrewm@0
|
106
|
andrewm@0
|
107 private:
|
andrewm@0
|
108 // ***** Private Methods *****
|
andrewm@0
|
109
|
andrewm@0
|
110 void generateMidiNoteOn();
|
andrewm@0
|
111 void generateMidiNoteOff();
|
andrewm@0
|
112 void generateMidiPercussivenessNoteOn();
|
andrewm@0
|
113
|
andrewm@0
|
114 // ***** Member Variables *****
|
andrewm@0
|
115
|
andrewm@0
|
116 bool noteIsOn_; // Whether the MIDI note is active or not
|
andrewm@0
|
117 float aftertouchScaler_; // Scaler which affects aftertouch sensitivity
|
andrewm@0
|
118 int midiChannel_; // Channel on which to transmit MIDI messages
|
andrewm@0
|
119 int lastAftertouchValue_; // Value of the last aftertouch message
|
andrewm@0
|
120 int midiPercussivenessChannel_; // Whether and where to transmit percussiveness messages
|
andrewm@0
|
121 };
|
andrewm@0
|
122
|
andrewm@0
|
123
|
andrewm@0
|
124 #endif /* defined(__touchkeys__MIDIKeyPositionMapping__) */
|