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 KeyboardDisplay.h: displays the keyboard state, including active MIDI
|
andrewm@0
|
21 notes and current touch position and size.
|
andrewm@0
|
22 */
|
andrewm@0
|
23
|
andrewm@0
|
24 #ifndef KEYBOARD_DISPLAY_H
|
andrewm@0
|
25 #define KEYBOARD_DISPLAY_H
|
andrewm@0
|
26
|
andrewm@0
|
27 #include <iostream>
|
andrewm@0
|
28 #include <map>
|
andrewm@0
|
29 //#include <OpenGL/gl.h>
|
andrewm@0
|
30 #include "../JuceLibraryCode/JuceHeader.h"
|
andrewm@0
|
31 #include "../TouchKeys/KeyTouchFrame.h"
|
andrewm@0
|
32 #include "OpenGLDisplayBase.h"
|
andrewm@0
|
33
|
andrewm@0
|
34
|
andrewm@0
|
35 // This class uses OpenGL to implement the actual drawing of the piano keyboard graphics.
|
andrewm@0
|
36 // Graphics include the current state of each key and the touches on the surface.
|
andrewm@0
|
37
|
andrewm@0
|
38 class KeyboardDisplay : public OpenGLDisplayBase {
|
andrewm@0
|
39 // Internal data structures and constants
|
andrewm@0
|
40 private:
|
andrewm@0
|
41 // Display dimensions, normalized to the width of one white key
|
andrewm@0
|
42
|
andrewm@0
|
43 static const float kWhiteKeyFrontWidth;
|
andrewm@0
|
44 static const float kBlackKeyWidth;
|
andrewm@0
|
45 static const float kWhiteKeyFrontLength;
|
andrewm@0
|
46 static const float kWhiteKeyBackLength;
|
andrewm@0
|
47 static const float kBlackKeyLength;
|
andrewm@0
|
48 static const float kInterKeySpacing;
|
andrewm@0
|
49 static const float kAnalogSliderVerticalSpacing;
|
andrewm@0
|
50 static const float kAnalogSliderLength;
|
andrewm@0
|
51 static const float kAnalogSliderWidth;
|
andrewm@0
|
52 static const float kAnalogSliderMinimumValue;
|
andrewm@0
|
53 static const float kAnalogSliderMaximumValue;
|
andrewm@0
|
54 static const float kAnalogSliderZeroLocation;
|
andrewm@0
|
55 static const float kAnalogSliderOneLocation;
|
andrewm@0
|
56
|
andrewm@0
|
57 // Individual geometry for C, D, E, F, G, A, B, c'
|
andrewm@0
|
58
|
andrewm@0
|
59 static const float kWhiteKeyBackOffsets[9];
|
andrewm@0
|
60 static const float kWhiteKeyBackWidths[9];
|
andrewm@0
|
61
|
andrewm@0
|
62 // Display margins
|
andrewm@0
|
63
|
andrewm@0
|
64 static const float kDisplaySideMargin;
|
andrewm@0
|
65 static const float kDisplayBottomMargin;
|
andrewm@0
|
66 static const float kDisplayTopMargin;
|
andrewm@0
|
67
|
andrewm@0
|
68 // Key shape constants
|
andrewm@0
|
69
|
andrewm@0
|
70 static const int kShapeForNote[12];
|
andrewm@0
|
71 static const int kWhiteToChromatic[7];
|
andrewm@0
|
72 static const float kWhiteKeyFrontBackCutoff;
|
andrewm@0
|
73
|
andrewm@0
|
74 // Touch constants
|
andrewm@0
|
75 static const float kDisplayMinTouchSize;
|
andrewm@0
|
76 static const float kDisplayTouchSizeScaler;
|
andrewm@0
|
77
|
andrewm@0
|
78
|
andrewm@0
|
79 typedef struct {
|
andrewm@0
|
80 bool active;
|
andrewm@0
|
81 float locH;
|
andrewm@0
|
82 float locV1;
|
andrewm@0
|
83 float locV2;
|
andrewm@0
|
84 float locV3;
|
andrewm@0
|
85 float size1;
|
andrewm@0
|
86 float size2;
|
andrewm@0
|
87 float size3;
|
andrewm@0
|
88 } TouchInfo;
|
andrewm@0
|
89
|
andrewm@0
|
90 typedef struct {
|
andrewm@0
|
91 float x;
|
andrewm@0
|
92 float y;
|
andrewm@0
|
93 } Point;
|
andrewm@0
|
94
|
andrewm@0
|
95 public:
|
andrewm@0
|
96 KeyboardDisplay();
|
andrewm@0
|
97
|
andrewm@0
|
98 // Setup methods for display size and keyboard range
|
andrewm@0
|
99 void setKeyboardRange(int lowest, int highest);
|
andrewm@0
|
100 float keyboardAspectRatio() { return totalDisplayWidth_ / totalDisplayHeight_; }
|
andrewm@0
|
101 void setDisplaySize(float width, float height);
|
andrewm@0
|
102
|
andrewm@0
|
103 // Drawing methods
|
andrewm@0
|
104 bool needsRender() { return needsUpdate_; }
|
andrewm@0
|
105 void render();
|
andrewm@0
|
106
|
andrewm@0
|
107 // Interaction methods
|
andrewm@0
|
108 void mouseDown(float x, float y);
|
andrewm@0
|
109 void mouseDragged(float x, float y);
|
andrewm@0
|
110 void mouseUp(float x, float y);
|
andrewm@0
|
111 void rightMouseDown(float x, float y);
|
andrewm@0
|
112 void rightMouseDragged(float x, float y);
|
andrewm@0
|
113 void rightMouseUp(float x, float y);
|
andrewm@0
|
114
|
andrewm@0
|
115 // Take action associated with clicking a key. These are called within the mouse
|
andrewm@0
|
116 // methods but may also be called externally.
|
andrewm@0
|
117 void keyClicked(int key);
|
andrewm@0
|
118 void keyRightClicked(int key);
|
andrewm@0
|
119
|
andrewm@0
|
120 // State-change methods
|
andrewm@0
|
121 void setTouchForKey(int key, const KeyTouchFrame& touch);
|
andrewm@0
|
122 void clearTouchForKey(int key);
|
andrewm@0
|
123 void clearAllTouches();
|
andrewm@0
|
124 void setAnalogCalibrationStatusForKey(int key, bool isCalibrated);
|
andrewm@0
|
125 void setAnalogValueForKey(int key, float value);
|
andrewm@0
|
126 void clearAnalogData();
|
andrewm@0
|
127 void setMidiActive(int key, bool active);
|
andrewm@0
|
128 void clearMidiData();
|
andrewm@0
|
129
|
andrewm@0
|
130 void setAnalogSensorsPresent(bool present) { analogSensorsPresent_ = present; }
|
andrewm@0
|
131 void setTouchSensorPresentForKey(int key, bool present);
|
andrewm@0
|
132 void setTouchSensingEnabled(bool enabled);
|
andrewm@0
|
133
|
andrewm@0
|
134 private:
|
andrewm@0
|
135 void drawWhiteKey(float x, float y, int shape, bool first, bool last, bool highlighted);
|
andrewm@0
|
136 void drawBlackKey(float x, float y, bool highlighted);
|
andrewm@0
|
137
|
andrewm@0
|
138 void drawWhiteTouch(float x, float y, int shape, float touchLocH, float touchLocV, float touchSize);
|
andrewm@0
|
139 void drawBlackTouch(float x, float y, float touchLocH, float touchLocV, float touchSize);
|
andrewm@0
|
140
|
andrewm@0
|
141 void drawAnalogSlider(float x, float y, bool calibrated, bool whiteKey, float value);
|
andrewm@0
|
142
|
andrewm@0
|
143 // Indicate the shape of the given MIDI note. 0-6 for white keys C-B, -1 for black keys.
|
andrewm@0
|
144 // We handle unusual shaped keys at the top or bottom of the keyboard separately.
|
andrewm@0
|
145
|
andrewm@0
|
146 int keyShape(int key) {
|
andrewm@0
|
147 if(key < 0) return -1;
|
andrewm@0
|
148 return kShapeForNote[key % 12];
|
andrewm@0
|
149 }
|
andrewm@0
|
150
|
andrewm@0
|
151 void refreshViewport();
|
andrewm@0
|
152
|
andrewm@0
|
153 // Conversion from internal coordinate space to external pixel values and back
|
andrewm@0
|
154 Point screenToInternal(Point& inPoint);
|
andrewm@0
|
155 Point internalToScreen(Point& inPoint);
|
andrewm@0
|
156
|
andrewm@0
|
157 // Figure out which key (if any) the current point corresponds to
|
andrewm@0
|
158 int keyForLocation(Point& internalPoint);
|
andrewm@0
|
159
|
andrewm@0
|
160 private:
|
andrewm@0
|
161
|
andrewm@0
|
162 int lowestMidiNote_, highestMidiNote_; // Which keys should be displayed (use MIDI note numbers)
|
andrewm@0
|
163 float totalDisplayWidth_, totalDisplayHeight_; // Size of the internal view (centered around origin)
|
andrewm@0
|
164 float displayPixelWidth_, displayPixelHeight_; // Pixel resolution of the surrounding window
|
andrewm@0
|
165 bool needsUpdate_; // Whether the keyboard should be redrawn
|
andrewm@0
|
166 int currentHighlightedKey_; // What key is being clicked on at the moment
|
andrewm@0
|
167 bool touchSensingEnabled_; // Whether touch-sensitive keys are being used
|
andrewm@0
|
168 bool touchSensingPresentOnKey_[128]; // Whether the key with this MIDI note has a touch sensor
|
andrewm@0
|
169
|
andrewm@0
|
170 bool analogSensorsPresent_; // Whether the given device has analog sensors at all
|
andrewm@0
|
171 bool analogValueIsCalibratedForKey_[128]; // Whether the analog sensor is calibrated on this key
|
andrewm@0
|
172 float analogValueForKey_[128]; // Latest analog sensor value for each key
|
andrewm@0
|
173 bool midiActiveForKey_[128]; // Whether the MIDI note is on for each key
|
andrewm@0
|
174
|
andrewm@0
|
175 TouchInfo currentTouches_[128]; // Touch data for each key
|
andrewm@0
|
176 TouchInfo currentTouchesMirror_[128]; // Mirror of the above, used for active display
|
andrewm@0
|
177 //std::map<int, TouchInfo> currentTouches_; // Collection of current touch data
|
andrewm@0
|
178 CriticalSection displayMutex_; // Synchronize access between data and display threads
|
andrewm@0
|
179 };
|
andrewm@0
|
180
|
andrewm@0
|
181 #endif /* KEYBOARD_DISPLAY_H */ |