annotate Source/Display/KeyboardDisplay.h @ 56:b4a2d2ae43cf tip

merge
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Fri, 23 Nov 2018 15:48:14 +0000
parents 73576f49ad1c
children
rev   line source
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@17 40 protected:
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@44 94
andrewm@44 95 typedef struct {
andrewm@44 96 int noteLow;
andrewm@44 97 int noteHigh;
andrewm@44 98 int divisions;
andrewm@44 99 } KeyDivision;
andrewm@0 100
andrewm@0 101 public:
andrewm@0 102 KeyboardDisplay();
andrewm@17 103 virtual ~KeyboardDisplay() {}
andrewm@27 104
andrewm@27 105 // Set canvas for triggering rendering;
andrewm@27 106 void setCanvas(OpenGLJuceCanvas *canvas) { canvas_ = canvas; }
andrewm@27 107 void tellCanvasToRepaint();
andrewm@0 108
andrewm@0 109 // Setup methods for display size and keyboard range
andrewm@0 110 void setKeyboardRange(int lowest, int highest);
andrewm@0 111 float keyboardAspectRatio() { return totalDisplayWidth_ / totalDisplayHeight_; }
andrewm@17 112 virtual void setDisplaySize(float width, float height);
andrewm@0 113
andrewm@0 114 // Drawing methods
andrewm@17 115 virtual void render();
andrewm@0 116
andrewm@0 117 // Interaction methods
andrewm@17 118 virtual void mouseDown(float x, float y);
andrewm@17 119 virtual void mouseDragged(float x, float y);
andrewm@17 120 virtual void mouseUp(float x, float y);
andrewm@17 121 virtual void rightMouseDown(float x, float y);
andrewm@17 122 virtual void rightMouseDragged(float x, float y);
andrewm@17 123 virtual void rightMouseUp(float x, float y);
andrewm@0 124
andrewm@0 125 // Take action associated with clicking a key. These are called within the mouse
andrewm@0 126 // methods but may also be called externally.
andrewm@17 127 virtual void keyClicked(int key);
andrewm@17 128 virtual void keyRightClicked(int key);
andrewm@0 129
andrewm@0 130 // State-change methods
andrewm@0 131 void setTouchForKey(int key, const KeyTouchFrame& touch);
andrewm@0 132 void clearTouchForKey(int key);
andrewm@0 133 void clearAllTouches();
andrewm@0 134 void setAnalogCalibrationStatusForKey(int key, bool isCalibrated);
andrewm@0 135 void setAnalogValueForKey(int key, float value);
andrewm@0 136 void clearAnalogData();
andrewm@0 137 void setMidiActive(int key, bool active);
andrewm@0 138 void clearMidiData();
andrewm@0 139
andrewm@0 140 void setAnalogSensorsPresent(bool present) { analogSensorsPresent_ = present; }
andrewm@0 141 void setTouchSensorPresentForKey(int key, bool present);
andrewm@0 142 void setTouchSensingEnabled(bool enabled);
andrewm@44 143
andrewm@44 144 // Key division methods
andrewm@44 145 void addKeyDivision(void *who, int noteLow, int noteHigh, int divisions);
andrewm@44 146 void removeKeyDivision(void *who);
andrewm@0 147
andrewm@17 148 protected:
andrewm@44 149 void drawWhiteKey(float x, float y, int shape, bool first, bool last, bool highlighted, int divisions);
andrewm@44 150 void drawBlackKey(float x, float y, bool highlighted, int divisions);
andrewm@0 151
andrewm@0 152 void drawWhiteTouch(float x, float y, int shape, float touchLocH, float touchLocV, float touchSize);
andrewm@0 153 void drawBlackTouch(float x, float y, float touchLocH, float touchLocV, float touchSize);
andrewm@0 154
andrewm@0 155 void drawAnalogSlider(float x, float y, bool calibrated, bool whiteKey, float value);
andrewm@0 156
andrewm@0 157 // Indicate the shape of the given MIDI note. 0-6 for white keys C-B, -1 for black keys.
andrewm@0 158 // We handle unusual shaped keys at the top or bottom of the keyboard separately.
andrewm@0 159
andrewm@0 160 int keyShape(int key) {
andrewm@0 161 if(key < 0) return -1;
andrewm@0 162 return kShapeForNote[key % 12];
andrewm@0 163 }
andrewm@0 164
andrewm@0 165 void refreshViewport();
andrewm@0 166
andrewm@0 167 // Conversion from internal coordinate space to external pixel values and back
andrewm@0 168 Point screenToInternal(Point& inPoint);
andrewm@0 169 Point internalToScreen(Point& inPoint);
andrewm@0 170
andrewm@0 171 // Figure out which key (if any) the current point corresponds to
andrewm@0 172 int keyForLocation(Point& internalPoint);
andrewm@44 173
andrewm@44 174 // Convert key division map into a number of divisions for each key
andrewm@44 175 void recalculateKeyDivisions();
andrewm@0 176
andrewm@17 177 protected:
andrewm@27 178 OpenGLJuceCanvas *canvas_; // Reference to object which handles rendering
andrewm@27 179
andrewm@0 180 int lowestMidiNote_, highestMidiNote_; // Which keys should be displayed (use MIDI note numbers)
andrewm@0 181 float totalDisplayWidth_, totalDisplayHeight_; // Size of the internal view (centered around origin)
andrewm@0 182 float displayPixelWidth_, displayPixelHeight_; // Pixel resolution of the surrounding window
andrewm@0 183 int currentHighlightedKey_; // What key is being clicked on at the moment
andrewm@0 184 bool touchSensingEnabled_; // Whether touch-sensitive keys are being used
andrewm@0 185 bool touchSensingPresentOnKey_[128]; // Whether the key with this MIDI note has a touch sensor
andrewm@0 186
andrewm@0 187 bool analogSensorsPresent_; // Whether the given device has analog sensors at all
andrewm@0 188 bool analogValueIsCalibratedForKey_[128]; // Whether the analog sensor is calibrated on this key
andrewm@0 189 float analogValueForKey_[128]; // Latest analog sensor value for each key
andrewm@0 190 bool midiActiveForKey_[128]; // Whether the MIDI note is on for each key
andrewm@0 191
andrewm@0 192 TouchInfo currentTouches_[128]; // Touch data for each key
andrewm@0 193 TouchInfo currentTouchesMirror_[128]; // Mirror of the above, used for active display
andrewm@44 194 std::map<void*, KeyDivision> keyDivisions_; // Division of keys into more than one segment, for certain mappings
andrewm@44 195 int keyDivisionsForNote_[128]; // Number of key divisions per note
andrewm@0 196 CriticalSection displayMutex_; // Synchronize access between data and display threads
andrewm@0 197 };
andrewm@0 198
andrewm@0 199 #endif /* KEYBOARD_DISPLAY_H */