annotate Source/Display/KeyboardDisplay.cpp @ 16:61e3c9df4674

Fix bug where TouchKeys standalone mode turns off when mode is changed.
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Mon, 25 Nov 2013 21:36:02 +0000
parents 3580ffe87dc8
children c8387e4f119f
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.cpp: 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 #include "KeyboardDisplay.h"
andrewm@0 25 #include <iostream>
andrewm@0 26 #include <cmath>
andrewm@0 27
andrewm@0 28 // Class constants
andrewm@0 29
andrewm@0 30 const float KeyboardDisplay::kWhiteKeyFrontWidth = 1.0;
andrewm@0 31 const float KeyboardDisplay::kBlackKeyWidth = 0.5;
andrewm@0 32 const float KeyboardDisplay::kWhiteKeyFrontLength = 2.3;
andrewm@0 33 const float KeyboardDisplay::kWhiteKeyBackLength = 4.1;
andrewm@0 34 const float KeyboardDisplay::kBlackKeyLength = 4.0;
andrewm@0 35 const float KeyboardDisplay::kInterKeySpacing = 0.1;
andrewm@0 36 const float KeyboardDisplay::kAnalogSliderVerticalSpacing = 0.2;
andrewm@0 37 const float KeyboardDisplay::kAnalogSliderLength = 3.0;
andrewm@0 38 const float KeyboardDisplay::kAnalogSliderWidth = 0.4;
andrewm@0 39 const float KeyboardDisplay::kAnalogSliderMinimumValue = -0.2;
andrewm@0 40 const float KeyboardDisplay::kAnalogSliderMaximumValue = 1.2;
andrewm@0 41 const float KeyboardDisplay::kAnalogSliderZeroLocation = kAnalogSliderLength * (0.0 - kAnalogSliderMinimumValue) / (kAnalogSliderMaximumValue - kAnalogSliderMinimumValue);
andrewm@0 42 const float KeyboardDisplay::kAnalogSliderOneLocation = kAnalogSliderLength * (1.0 - kAnalogSliderMinimumValue) / (kAnalogSliderMaximumValue - kAnalogSliderMinimumValue);
andrewm@0 43
andrewm@0 44 // Individual geometry for C, D, E, F, G, A, B, c'
andrewm@0 45
andrewm@0 46 const float KeyboardDisplay::kWhiteKeyBackOffsets[9] = {0, 0.22, 0.42, 0, 0.14, 0.3, 0.44, 0.22, 0};
andrewm@0 47 const float KeyboardDisplay::kWhiteKeyBackWidths[9] = {0.6, 0.58, 0.58, 0.56, 0.56, 0.56, 0.56, 0.58, 1.0};
andrewm@0 48
andrewm@0 49 // Display margins
andrewm@0 50
andrewm@0 51 const float KeyboardDisplay::kDisplaySideMargin = 0.4;
andrewm@0 52 const float KeyboardDisplay::kDisplayBottomMargin = 0.8;
andrewm@0 53 const float KeyboardDisplay::kDisplayTopMargin = 0.8;
andrewm@0 54
andrewm@0 55 // Key shape constants
andrewm@0 56
andrewm@0 57 const int KeyboardDisplay::kShapeForNote[12] = {0, -1, 1, -1, 2, 3, -1, 4, -1, 5, -1, 6};
andrewm@0 58 const int KeyboardDisplay::kWhiteToChromatic[7] = {0, 2, 4, 5, 7, 9, 11};
andrewm@0 59 const float KeyboardDisplay::kWhiteKeyFrontBackCutoff = (6.5 / 19.0);
andrewm@0 60
andrewm@0 61 // Touch constants
andrewm@0 62 const float KeyboardDisplay::kDisplayMinTouchSize = 0.1;
andrewm@0 63 const float KeyboardDisplay::kDisplayTouchSizeScaler = 0.5;
andrewm@0 64
andrewm@0 65 KeyboardDisplay::KeyboardDisplay() : lowestMidiNote_(0), highestMidiNote_(0),
andrewm@0 66 totalDisplayWidth_(1.0), totalDisplayHeight_(1.0), displayPixelWidth_(1.0), displayPixelHeight_(1.0),
andrewm@0 67 needsUpdate_(true), currentHighlightedKey_(-1), touchSensingEnabled_(false), analogSensorsPresent_(false) {
andrewm@0 68 // Initialize OpenGL settings: 2D only
andrewm@0 69
andrewm@0 70 //glMatrixMode(GL_PROJECTION);
andrewm@0 71 //glDisable(GL_DEPTH_TEST);
andrewm@0 72
andrewm@0 73 clearAllTouches();
andrewm@0 74 for(int i = 0; i < 128; i++)
andrewm@0 75 midiActiveForKey_[i] = false;
andrewm@0 76 }
andrewm@0 77
andrewm@0 78 void KeyboardDisplay::setKeyboardRange(int lowest, int highest) {
andrewm@0 79 if(lowest < 0 || highest < 0)
andrewm@0 80 return;
andrewm@0 81
andrewm@0 82 ScopedLock sl(displayMutex_);
andrewm@0 83
andrewm@0 84 lowestMidiNote_ = lowest;
andrewm@0 85 if(keyShape(lowest) < 0) // Lowest key must always be a white key for display to
andrewm@0 86 lowest++; // render properly
andrewm@0 87
andrewm@0 88 highestMidiNote_ = highest;
andrewm@0 89
andrewm@0 90 // Recalculate relevant display parameters
andrewm@0 91 // Display size is based on the number of white keys
andrewm@0 92
andrewm@0 93 int numKeys = 0;
andrewm@0 94 for(int i = lowestMidiNote_; i <= highestMidiNote_; i++) {
andrewm@0 95 if(keyShape(i) >= 0)
andrewm@0 96 numKeys++;
andrewm@0 97 if(i >= 0 && i < 128) {
andrewm@0 98 analogValueForKey_[i] = 0.0;
andrewm@0 99 analogValueIsCalibratedForKey_[i] = false;
andrewm@0 100 }
andrewm@0 101 }
andrewm@0 102
andrewm@0 103 if(numKeys == 0) {
andrewm@0 104 return;
andrewm@0 105 }
andrewm@0 106
andrewm@0 107 // Width: N keys, N-1 interkey spaces, 2 side margins
andrewm@0 108 totalDisplayWidth_ = (float)numKeys * (kWhiteKeyFrontWidth + kInterKeySpacing)
andrewm@0 109 - kInterKeySpacing + 2.0 * kDisplaySideMargin;
andrewm@0 110
andrewm@0 111 // Height: white key height plus top and bottom margins
andrewm@0 112 if(analogSensorsPresent_)
andrewm@0 113 totalDisplayHeight_ = kDisplayTopMargin + kDisplayBottomMargin + kWhiteKeyFrontLength + kWhiteKeyBackLength
andrewm@0 114 + kAnalogSliderVerticalSpacing + kAnalogSliderLength;
andrewm@0 115 else
andrewm@0 116 totalDisplayHeight_ = kDisplayTopMargin + kDisplayBottomMargin + kWhiteKeyFrontLength + kWhiteKeyBackLength;
andrewm@0 117 }
andrewm@0 118
andrewm@0 119 void KeyboardDisplay::setDisplaySize(float width, float height) {
andrewm@0 120 ScopedLock sl(displayMutex_);
andrewm@0 121
andrewm@0 122 displayPixelWidth_ = width;
andrewm@0 123 displayPixelHeight_ = height;
andrewm@0 124 refreshViewport();
andrewm@0 125 }
andrewm@0 126
andrewm@0 127
andrewm@0 128 // Render the keyboard display
andrewm@0 129
andrewm@0 130 void KeyboardDisplay::render() {
andrewm@0 131 if(lowestMidiNote_ == highestMidiNote_)
andrewm@0 132 return;
andrewm@0 133
andrewm@0 134 // Start with a light gray background
andrewm@0 135 glClearColor(0.8, 0.8, 0.8, 1.0);
andrewm@0 136 glClear(GL_COLOR_BUFFER_BIT);
andrewm@0 137 glLoadIdentity();
andrewm@0 138
andrewm@0 139 float invAspectRatio = totalDisplayWidth_ / totalDisplayHeight_; //displayPixelWidth_ / displayPixelHeight_;
andrewm@0 140 float scaleValue = 2.0 / totalDisplayWidth_;
andrewm@0 141
andrewm@0 142 glScalef(scaleValue, scaleValue * invAspectRatio, scaleValue);
andrewm@0 143 glTranslatef(-1.0 / scaleValue, -totalDisplayHeight_ / 2.0, 0);
andrewm@0 144 glTranslatef(kDisplaySideMargin, kDisplayBottomMargin, 0.0);
andrewm@0 145
andrewm@0 146 //ScopedLock sl(displayMutex_);
andrewm@0 147
andrewm@0 148 glPushMatrix();
andrewm@0 149
andrewm@0 150 // Draw the keys themselves first, with analog values if present, then draw the touches
andrewm@0 151 for(int key = lowestMidiNote_; key <= highestMidiNote_; key++) {
andrewm@0 152 if(keyShape(key) >= 0) {
andrewm@0 153 // White keys: draw and move the frame over for the next key
andrewm@0 154 drawWhiteKey(0, 0, keyShape(key), key == lowestMidiNote_, key == highestMidiNote_,
andrewm@0 155 /*(key == currentHighlightedKey_) ||*/ midiActiveForKey_[key]);
andrewm@0 156 // Analog slider should be centered with respect to the back of the white key
andrewm@0 157 if(analogSensorsPresent_ && keyShape(key) >= 0) {
andrewm@0 158 float sliderOffset = kWhiteKeyBackOffsets[keyShape(key)] + (kWhiteKeyBackWidths[keyShape(key)] - kAnalogSliderWidth) * 0.5;
andrewm@0 159 drawAnalogSlider(sliderOffset, kWhiteKeyFrontLength + kWhiteKeyBackLength + kAnalogSliderVerticalSpacing,
andrewm@0 160 analogValueIsCalibratedForKey_[key], true, analogValueForKey_[key]);
andrewm@0 161 }
andrewm@0 162 glTranslatef(kWhiteKeyFrontWidth + kInterKeySpacing, 0, 0);
andrewm@0 163 }
andrewm@0 164 else {
andrewm@0 165 // Black keys: draw and leave the frame in place
andrewm@0 166 int previousWhiteKeyShape = keyShape(key - 1);
andrewm@0 167 float offsetH = -1.0 + kWhiteKeyBackOffsets[previousWhiteKeyShape] + kWhiteKeyBackWidths[previousWhiteKeyShape];
andrewm@0 168 float offsetV = kWhiteKeyFrontLength + kWhiteKeyBackLength - kBlackKeyLength;
andrewm@0 169
andrewm@0 170 glTranslatef(offsetH, offsetV, 0.0);
andrewm@0 171 drawBlackKey(0, 0, /*(key == currentHighlightedKey_) ||*/ midiActiveForKey_[key]);
andrewm@0 172 if(analogSensorsPresent_) {
andrewm@0 173 drawAnalogSlider((kBlackKeyWidth - kAnalogSliderWidth) * 0.5, kBlackKeyLength + kAnalogSliderVerticalSpacing,
andrewm@0 174 analogValueIsCalibratedForKey_[key], false, analogValueForKey_[key]);
andrewm@0 175 }
andrewm@0 176 glTranslatef(-offsetH, -offsetV, 0.0);
andrewm@0 177 }
andrewm@0 178 }
andrewm@0 179
andrewm@0 180 // Restore to the original location we used when drawing the keys
andrewm@0 181 glPopMatrix();
andrewm@0 182
andrewm@0 183 // Transfer the touch display data from storage buffer to a local copy we can use for display.
andrewm@0 184 // This avoids OpenGL calls happening while the mutex is locked, which stalls the data producer thread
andrewm@0 185 displayMutex_.enter();
andrewm@0 186 memcpy(currentTouchesMirror_, currentTouches_, 128*sizeof(TouchInfo));
andrewm@0 187 displayMutex_.exit();
andrewm@0 188
andrewm@0 189 // Draw touches
andrewm@0 190 for(int key = lowestMidiNote_; key <= highestMidiNote_; key++) {
andrewm@0 191 if(keyShape(key) >= 0) {
andrewm@0 192 // Check whether there are any current touches for this key
andrewm@0 193 //if(currentTouches_.count(key) > 0) {
andrewm@0 194 if(currentTouchesMirror_[key].active) {
andrewm@0 195 TouchInfo& t = currentTouchesMirror_[key];
andrewm@0 196
andrewm@0 197 if(t.locV1 >= 0)
andrewm@0 198 drawWhiteTouch(0, 0, keyShape(key), t.locH, t.locV1, t.size1);
andrewm@0 199 if(t.locV2 >= 0)
andrewm@0 200 drawWhiteTouch(0, 0, keyShape(key), t.locH, t.locV2, t.size2);
andrewm@0 201 if(t.locV3 >= 0)
andrewm@0 202 drawWhiteTouch(0, 0, keyShape(key), t.locH, t.locV3, t.size3);
andrewm@0 203 }
andrewm@0 204
andrewm@0 205 glTranslatef(kWhiteKeyFrontWidth + kInterKeySpacing, 0, 0);
andrewm@0 206 }
andrewm@0 207 else {
andrewm@0 208 // Black keys: draw and leave the frame in place
andrewm@0 209 int previousWhiteKeyShape = keyShape(key - 1);
andrewm@0 210 float offsetH = -1.0 + kWhiteKeyBackOffsets[previousWhiteKeyShape] + kWhiteKeyBackWidths[previousWhiteKeyShape];
andrewm@0 211 float offsetV = kWhiteKeyFrontLength + kWhiteKeyBackLength - kBlackKeyLength;
andrewm@0 212
andrewm@0 213 glTranslatef(offsetH, offsetV, 0.0);
andrewm@0 214
andrewm@0 215 // Check whether there are any current touches for this key
andrewm@0 216 //if(currentTouches_.count(key) > 0) {
andrewm@0 217 if(currentTouchesMirror_[key].active) {
andrewm@0 218 TouchInfo& t = currentTouchesMirror_[key];
andrewm@0 219
andrewm@0 220 if(t.locV1 >= 0)
andrewm@0 221 drawBlackTouch(0, 0, t.locH, t.locV1, t.size1);
andrewm@0 222 if(t.locV2 >= 0)
andrewm@0 223 drawBlackTouch(0, 0, t.locH, t.locV2, t.size2);
andrewm@0 224 if(t.locV3 >= 0)
andrewm@0 225 drawBlackTouch(0, 0, t.locH, t.locV3, t.size3);
andrewm@0 226 }
andrewm@0 227
andrewm@0 228 glTranslatef(-offsetH, -offsetV, 0.0);
andrewm@0 229 }
andrewm@0 230 }
andrewm@0 231
andrewm@0 232 needsUpdate_ = false;
andrewm@0 233
andrewm@0 234 glFlush();
andrewm@0 235 }
andrewm@0 236
andrewm@0 237 // Mouse interaction methods
andrewm@0 238
andrewm@0 239 void KeyboardDisplay::mouseDown(float x, float y) {
andrewm@0 240 Point mousePoint = {x, y};
andrewm@0 241 Point scaledPoint = screenToInternal(mousePoint);
andrewm@0 242
andrewm@0 243 currentHighlightedKey_ = keyForLocation(scaledPoint);
andrewm@0 244 needsUpdate_ = true;
andrewm@0 245 }
andrewm@0 246
andrewm@0 247 void KeyboardDisplay::mouseDragged(float x, float y) {
andrewm@0 248 Point mousePoint = {x, y};
andrewm@0 249 Point scaledPoint = screenToInternal(mousePoint);
andrewm@0 250
andrewm@0 251 currentHighlightedKey_ = keyForLocation(scaledPoint);
andrewm@0 252 needsUpdate_ = true;
andrewm@0 253 }
andrewm@0 254
andrewm@0 255 void KeyboardDisplay::mouseUp(float x, float y) {
andrewm@0 256 //Point mousePoint = {x, y};
andrewm@0 257 //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0 258
andrewm@0 259 // When the mouse is released, see if it was over a key. If so, take any action
andrewm@0 260 // associated with clicking that key.
andrewm@0 261
andrewm@0 262 if(currentHighlightedKey_ != -1)
andrewm@0 263 keyClicked(currentHighlightedKey_);
andrewm@0 264
andrewm@0 265 //currentHighlightedKey_ = -1;
andrewm@0 266 needsUpdate_ = true;
andrewm@0 267 }
andrewm@0 268
andrewm@0 269 void KeyboardDisplay::rightMouseDown(float x, float y) {
andrewm@0 270 Point mousePoint = {x, y};
andrewm@0 271 Point scaledPoint = screenToInternal(mousePoint);
andrewm@0 272
andrewm@0 273 int key = keyForLocation(scaledPoint);
andrewm@0 274 if(key != -1)
andrewm@0 275 keyRightClicked(key);
andrewm@0 276
andrewm@0 277 needsUpdate_ = true;
andrewm@0 278 }
andrewm@0 279
andrewm@0 280 void KeyboardDisplay::rightMouseDragged(float x, float y) {
andrewm@0 281 //Point mousePoint = {x, y};
andrewm@0 282 //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0 283 }
andrewm@0 284
andrewm@0 285 void KeyboardDisplay::rightMouseUp(float x, float y) {
andrewm@0 286 //Point mousePoint = {x, y};
andrewm@0 287 //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0 288 }
andrewm@0 289
andrewm@0 290 void KeyboardDisplay::keyClicked(int key) {
andrewm@0 291
andrewm@0 292 }
andrewm@0 293
andrewm@0 294 void KeyboardDisplay::keyRightClicked(int key) {
andrewm@0 295
andrewm@0 296 }
andrewm@0 297
andrewm@0 298 // Insert new touch information for the given key and request a display update.
andrewm@0 299
andrewm@0 300 void KeyboardDisplay::setTouchForKey(int key, const KeyTouchFrame& touch) {
andrewm@0 301 if(key < lowestMidiNote_ || key > highestMidiNote_)
andrewm@0 302 return;
andrewm@0 303
andrewm@0 304 ScopedLock sl(displayMutex_);
andrewm@0 305
andrewm@0 306 //TouchInfo t = {touch.locH, touch.locs[0], touch.locs[1], touch.locs[2], touch.sizes[0], touch.sizes[1], touch.sizes[2]};
andrewm@0 307 //currentTouches_[key] = t;
andrewm@0 308 currentTouches_[key] = {true, touch.locH, touch.locs[0], touch.locs[1], touch.locs[2], touch.sizes[0], touch.sizes[1], touch.sizes[2]};
andrewm@0 309
andrewm@0 310 needsUpdate_ = true;
andrewm@0 311 }
andrewm@0 312
andrewm@0 313 // Clear touch information for this key
andrewm@0 314
andrewm@0 315 void KeyboardDisplay::clearTouchForKey(int key) {
andrewm@0 316 ScopedLock sl(displayMutex_);
andrewm@0 317
andrewm@0 318 //currentTouches_.erase(key);
andrewm@0 319 currentTouches_[key].active = 0;
andrewm@0 320
andrewm@0 321 needsUpdate_ = true;
andrewm@0 322 }
andrewm@0 323
andrewm@0 324 // Clear all current touch information
andrewm@0 325
andrewm@0 326 void KeyboardDisplay::clearAllTouches() {
andrewm@0 327 ScopedLock sl(displayMutex_);
andrewm@0 328
andrewm@0 329 //currentTouches_.clear();
andrewm@0 330 for(int i = 0; i < 128; i++)
andrewm@0 331 currentTouches_[i].active = false;
andrewm@0 332
andrewm@0 333 needsUpdate_ = true;
andrewm@0 334 }
andrewm@0 335
andrewm@0 336 // Indicate whether the given key is calibrated or not
andrewm@0 337
andrewm@0 338 void KeyboardDisplay::setAnalogCalibrationStatusForKey(int key, bool isCalibrated) {
andrewm@0 339 if(key < 0 || key > 127)
andrewm@0 340 return;
andrewm@0 341 analogValueIsCalibratedForKey_[key] = isCalibrated;
andrewm@0 342 needsUpdate_ = true;
andrewm@0 343 }
andrewm@0 344
andrewm@0 345 // Set the current value of the analog sensor for the given key.
andrewm@0 346 // Whether calibrated or not, the data should be in the range 0.0-1.0
andrewm@0 347 // with a bit of room for deviation outside that range (i.e. for extra key
andrewm@0 348 // pressure > 1.0, or for resting key states slightly miscalibrated < 0.0).
andrewm@0 349
andrewm@0 350 void KeyboardDisplay::setAnalogValueForKey(int key, float value) {
andrewm@0 351 if(key < 0 || key > 127)
andrewm@0 352 return;
andrewm@0 353 analogValueForKey_[key] = value;
andrewm@0 354 needsUpdate_ = true;
andrewm@0 355 }
andrewm@0 356
andrewm@0 357 // Clear all the analog data for all keys
andrewm@0 358 void KeyboardDisplay::clearAnalogData() {
andrewm@0 359 for(int key = 0; key < 128; key++) {
andrewm@0 360 analogValueForKey_[key] = 0.0;
andrewm@0 361 }
andrewm@0 362 needsUpdate_ = true;
andrewm@0 363 }
andrewm@0 364
andrewm@0 365 void KeyboardDisplay::setMidiActive(int key, bool active) {
andrewm@0 366 if(key < 0 || key > 127)
andrewm@0 367 return;
andrewm@0 368 midiActiveForKey_[key] = active;
andrewm@0 369 needsUpdate_ = true;
andrewm@0 370 }
andrewm@0 371
andrewm@0 372 void KeyboardDisplay::clearMidiData() {
andrewm@0 373 for(int key = 0; key < 128; key++)
andrewm@0 374 midiActiveForKey_[key] = false;
andrewm@0 375 needsUpdate_ = true;
andrewm@0 376 }
andrewm@0 377
andrewm@0 378 // Indicate whether a given key has touch sensing capability
andrewm@0 379
andrewm@0 380 void KeyboardDisplay::setTouchSensorPresentForKey(int key, bool present) {
andrewm@0 381 if(key < 0 || key > 127 || !touchSensingEnabled_)
andrewm@0 382 return;
andrewm@0 383 touchSensingPresentOnKey_[key] = present;
andrewm@0 384 }
andrewm@0 385
andrewm@0 386 // Indicate whether touch sensing is active at all on the keyboard.
andrewm@0 387 // Clear all key-specific information on whether a touch-sensing key is connected
andrewm@0 388
andrewm@0 389 void KeyboardDisplay::setTouchSensingEnabled(bool enabled) {
andrewm@0 390 touchSensingEnabled_ = enabled;
andrewm@0 391
andrewm@0 392 for(int i = 0; i < 128; i++)
andrewm@0 393 touchSensingPresentOnKey_[i] = false;
andrewm@0 394 }
andrewm@0 395
andrewm@0 396 // Draw the outline of a white key. Shape ranges from 0-7, giving the type of white key to draw
andrewm@0 397 // Coordinates give the lower-left corner of the key
andrewm@0 398
andrewm@0 399 void KeyboardDisplay::drawWhiteKey(float x, float y, int shape, bool first, bool last, bool highlighted) {
andrewm@0 400 // First and last keys will have special geometry since there is no black key below
andrewm@0 401 // Figure out the precise geometry in this case...
andrewm@0 402
andrewm@0 403 float backOffset, backWidth;
andrewm@0 404
andrewm@0 405 if(first) {
andrewm@0 406 backOffset = 0.0;
andrewm@0 407 backWidth = kWhiteKeyBackOffsets[shape] + kWhiteKeyBackWidths[shape];
andrewm@0 408 }
andrewm@0 409 else if(last) {
andrewm@0 410 backOffset = kWhiteKeyBackOffsets[shape];
andrewm@0 411 backWidth = 1.0 - kWhiteKeyBackOffsets[shape];
andrewm@0 412 }
andrewm@0 413 else {
andrewm@0 414 backOffset = kWhiteKeyBackOffsets[shape];
andrewm@0 415 backWidth = kWhiteKeyBackWidths[shape];
andrewm@0 416 }
andrewm@0 417
andrewm@0 418 // First draw white fill as two squares
andrewm@0 419 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
andrewm@0 420 if(highlighted)
andrewm@0 421 glColor3f(1.0, 0.8, 0.8);
andrewm@0 422 else
andrewm@0 423 glColor3f(1.0, 1.0, 1.0);
andrewm@0 424 glBegin(GL_QUADS);
andrewm@0 425
andrewm@0 426 glVertex2f(x, y);
andrewm@0 427 glVertex2f(x, y + kWhiteKeyFrontLength);
andrewm@0 428 glVertex2f(x + kWhiteKeyFrontWidth, y + kWhiteKeyFrontLength);
andrewm@0 429 glVertex2f(x + kWhiteKeyFrontWidth, y);
andrewm@0 430
andrewm@0 431 glVertex2f(x + backOffset, y + kWhiteKeyFrontLength);
andrewm@0 432 glVertex2f(x + backOffset, y + kWhiteKeyFrontLength + kWhiteKeyBackLength);
andrewm@0 433 glVertex2f(x + backOffset + backWidth, y + kWhiteKeyFrontLength + kWhiteKeyBackLength);
andrewm@0 434 glVertex2f(x + backOffset + backWidth, y + kWhiteKeyFrontLength);
andrewm@0 435
andrewm@0 436 glEnd();
andrewm@0 437
andrewm@0 438 // Now draw the outline as black line segments
andrewm@0 439 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
andrewm@0 440 glColor3f(0.0, 0.0, 0.0);
andrewm@0 441 glBegin(GL_POLYGON);
andrewm@0 442
andrewm@0 443 glVertex2f(x, y);
andrewm@0 444 glVertex2f(x, y + kWhiteKeyFrontLength);
andrewm@0 445 glVertex2f(x + backOffset, y + kWhiteKeyFrontLength);
andrewm@0 446 glVertex2f(x + backOffset, y + kWhiteKeyFrontLength + kWhiteKeyBackLength);
andrewm@0 447 glVertex2f(x + backOffset + backWidth, y + kWhiteKeyFrontLength + kWhiteKeyBackLength);
andrewm@0 448 glVertex2f(x + backOffset + backWidth, y + kWhiteKeyFrontLength);
andrewm@0 449 glVertex2f(x + kWhiteKeyFrontWidth, y + kWhiteKeyFrontLength);
andrewm@0 450 glVertex2f(x + kWhiteKeyFrontWidth, y);
andrewm@0 451
andrewm@0 452 glEnd();
andrewm@0 453 }
andrewm@0 454
andrewm@0 455 // Draw the outline of a black key, given its lower-left corner
andrewm@0 456
andrewm@0 457 void KeyboardDisplay::drawBlackKey(float x, float y, bool highlighted) {
andrewm@0 458 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
andrewm@0 459 if(highlighted)
andrewm@0 460 glColor3f(0.7, 0.0, 0.0);
andrewm@0 461 else
andrewm@0 462 glColor3f(0.0, 0.0, 0.0); // Display color black
andrewm@0 463 glBegin(GL_POLYGON);
andrewm@0 464
andrewm@0 465 glVertex2f(x, y);
andrewm@0 466 glVertex2f(x, y + kBlackKeyLength);
andrewm@0 467 glVertex2f(x + kBlackKeyWidth, y + kBlackKeyLength);
andrewm@0 468 glVertex2f(x + kBlackKeyWidth, y);
andrewm@0 469
andrewm@0 470 glEnd();
andrewm@0 471 }
andrewm@0 472
andrewm@0 473 // Draw a circle indicating a touch on the white key surface
andrewm@0 474
andrewm@0 475 void KeyboardDisplay::drawWhiteTouch(float x, float y, int shape, float touchLocH, float touchLocV, float touchSize) {
andrewm@0 476 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
andrewm@0 477 glColor3f(1.0, 0.0, 1.0);
andrewm@0 478
andrewm@0 479 glBegin(GL_POLYGON);
andrewm@0 480 if(/*touchLocV < kWhiteKeyFrontBackCutoff && */touchLocH >= 0.0) { // FIXME: find a more permanent solution
andrewm@0 481 // Here, the touch is in a location that has both horizontal and vertical information.
andrewm@0 482 for(int i = 0; i < 360; i += 5) {
andrewm@0 483 glVertex2f(x + cosf((float)i*3.14159/180.0)*(kDisplayMinTouchSize + touchSize*kDisplayTouchSizeScaler)
andrewm@0 484 + touchLocH*kWhiteKeyFrontWidth,
andrewm@0 485 y + sinf((float)i*3.14159/180.0)*(kDisplayMinTouchSize + touchSize*kDisplayTouchSizeScaler)
andrewm@0 486 + kWhiteKeyFrontLength*(touchLocV/kWhiteKeyFrontBackCutoff));
andrewm@0 487 }
andrewm@0 488 }
andrewm@0 489 else {
andrewm@0 490 // The touch is in the back part of the key, or for some reason lacks horizontal information
andrewm@0 491 for(int i = 0; i < 360; i += 5) {
andrewm@0 492 glVertex2f(x + cosf((float)i*3.14159/180.0)*(kDisplayMinTouchSize + touchSize*kDisplayTouchSizeScaler)
andrewm@0 493 + kWhiteKeyBackOffsets[shape] + kWhiteKeyBackWidths[shape]/2,
andrewm@0 494 y + sinf((float)i*3.14159/180.0)*(kDisplayMinTouchSize + touchSize*kDisplayTouchSizeScaler)
andrewm@0 495 + kWhiteKeyFrontLength + (kWhiteKeyBackLength*
andrewm@0 496 ((touchLocV-kWhiteKeyFrontBackCutoff)/(1.0-kWhiteKeyFrontBackCutoff))));
andrewm@0 497 }
andrewm@0 498 }
andrewm@0 499 glEnd();
andrewm@0 500 }
andrewm@0 501
andrewm@0 502 // Draw a circle indicating a touch on the black key surface
andrewm@0 503
andrewm@0 504 void KeyboardDisplay::drawBlackTouch(float x, float y, float touchLocH, float touchLocV, float touchSize) {
andrewm@0 505 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
andrewm@0 506 glColor3f(0.0, 1.0, 0.0);
andrewm@0 507
andrewm@0 508 glBegin(GL_POLYGON);
andrewm@0 509
andrewm@0 510 if(touchLocH < 0.0)
andrewm@0 511 touchLocH = 0.5;
andrewm@0 512
andrewm@0 513 for(int i = 0; i < 360; i += 5) {
andrewm@0 514 glVertex2f(x + cosf((float)i*3.14159/180.0)*(kDisplayMinTouchSize + touchSize*kDisplayTouchSizeScaler)
andrewm@0 515 + touchLocH * kBlackKeyWidth,
andrewm@0 516 y + sinf((float)i*3.14159/180.0)*(kDisplayMinTouchSize + touchSize*kDisplayTouchSizeScaler) + kBlackKeyLength*touchLocV);
andrewm@0 517 }
andrewm@0 518
andrewm@0 519 glEnd();
andrewm@0 520 }
andrewm@0 521
andrewm@0 522 // Draw a slider bar indicating the current key analog position
andrewm@0 523
andrewm@0 524 void KeyboardDisplay::drawAnalogSlider(float x, float y, bool calibrated, bool whiteKey, float value) {
andrewm@0 525 // First some gray lines indicating the 0.0 and 1.0 marks
andrewm@0 526 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
andrewm@0 527 glColor3f(0.5, 0.5, 0.5);
andrewm@0 528
andrewm@0 529 glBegin(GL_POLYGON);
andrewm@0 530 glVertex2f(x, y + kAnalogSliderZeroLocation);
andrewm@0 531 glVertex2f(x, y + kAnalogSliderOneLocation);
andrewm@0 532 glVertex2f(x + kAnalogSliderWidth, y + kAnalogSliderOneLocation);
andrewm@0 533 glVertex2f(x + kAnalogSliderWidth, y + kAnalogSliderZeroLocation);
andrewm@0 534 glEnd();
andrewm@0 535
andrewm@0 536 // Draw a red box at the top for uncalibrated values
andrewm@0 537 if(!calibrated) {
andrewm@0 538 glColor3f(1.0, 0.0, 0.0);
andrewm@0 539 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
andrewm@0 540 glBegin(GL_POLYGON);
andrewm@0 541 glVertex2f(x, y + kAnalogSliderOneLocation);
andrewm@0 542 glVertex2f(x, y + kAnalogSliderLength);
andrewm@0 543 glVertex2f(x + kAnalogSliderWidth, y + kAnalogSliderLength);
andrewm@0 544 glVertex2f(x + kAnalogSliderWidth, y + kAnalogSliderOneLocation);
andrewm@0 545 glEnd();
andrewm@0 546 }
andrewm@0 547
andrewm@0 548 // Next the filled part indicating the specific value (same color as touches), then the outline
andrewm@0 549 if(whiteKey)
andrewm@0 550 glColor3f(1.0, 0.0, 1.0);
andrewm@0 551 else
andrewm@0 552 glColor3f(0.0, 1.0, 0.0);
andrewm@0 553 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
andrewm@0 554 glBegin(GL_POLYGON);
andrewm@0 555
andrewm@0 556 float locationForValue = kAnalogSliderLength * (value - kAnalogSliderMinimumValue) / (kAnalogSliderMaximumValue - kAnalogSliderMinimumValue);
andrewm@0 557 if(locationForValue < 0.0)
andrewm@0 558 locationForValue = 0.0;
andrewm@0 559 if(locationForValue > kAnalogSliderLength)
andrewm@0 560 locationForValue = kAnalogSliderLength;
andrewm@0 561
andrewm@0 562 // Draw solid box from 0.0 to current value
andrewm@0 563 glVertex2f(x, y + kAnalogSliderZeroLocation);
andrewm@0 564 glVertex2f(x, y + locationForValue);
andrewm@0 565 glVertex2f(x + kAnalogSliderWidth, y + locationForValue);
andrewm@0 566 glVertex2f(x + kAnalogSliderWidth, y + kAnalogSliderZeroLocation);
andrewm@0 567 glEnd();
andrewm@0 568
andrewm@0 569 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
andrewm@0 570 glColor3f(0.0, 0.0, 0.0);
andrewm@0 571
andrewm@0 572 glBegin(GL_POLYGON);
andrewm@0 573 glVertex2f(x, y);
andrewm@0 574 glVertex2f(x, y + kAnalogSliderLength);
andrewm@0 575 glVertex2f(x + kAnalogSliderWidth, y + kAnalogSliderLength);
andrewm@0 576 glVertex2f(x + kAnalogSliderWidth, y);
andrewm@0 577 glEnd();
andrewm@0 578 }
andrewm@0 579
andrewm@0 580 void KeyboardDisplay::refreshViewport() {
andrewm@0 581 //glViewport(0, 0, displayPixelWidth_, displayPixelHeight_);
andrewm@0 582 }
andrewm@0 583
andrewm@0 584 // Conversion from internal coordinate space to external pixel values and back
andrewm@0 585
andrewm@0 586 // Pixel values go from 0,0 (lower left) to displayPixelWidth_, displayPixelHeight_ (upper right)
andrewm@0 587 // Internal values go from -totalDisplayWidth_/2, -totalDisplayHeight_/2 (lower left)
andrewm@0 588 // to totalDisplayWidth_/2, totalDisplayHeight_/2 (upper right)
andrewm@0 589
andrewm@0 590 // Pixel value in --> OpenGL value out
andrewm@0 591 KeyboardDisplay::Point KeyboardDisplay::screenToInternal(Point& inPoint) {
andrewm@0 592 Point out;
andrewm@0 593
andrewm@0 594 out.x = -totalDisplayWidth_*0.5 + (inPoint.x/displayPixelWidth_) * totalDisplayWidth_;
andrewm@0 595 out.y = -totalDisplayHeight_*0.5 + (inPoint.y/displayPixelHeight_) * totalDisplayHeight_;
andrewm@0 596
andrewm@0 597 return out;
andrewm@0 598 }
andrewm@0 599
andrewm@0 600 // OpenGL value in --> Pixel value out
andrewm@0 601 KeyboardDisplay::Point KeyboardDisplay::internalToScreen(Point& inPoint) {
andrewm@0 602 Point out;
andrewm@0 603
andrewm@0 604 out.x = ((inPoint.x + totalDisplayWidth_*0.5)/totalDisplayWidth_) * displayPixelWidth_;
andrewm@0 605 out.y = ((inPoint.y + totalDisplayHeight_*0.5)/totalDisplayHeight_) * displayPixelHeight_;
andrewm@0 606
andrewm@0 607 return out;
andrewm@0 608 }
andrewm@0 609
andrewm@0 610 // Given an internal-coordinate representation, return the number of the key that it belongs
andrewm@0 611 // in, otherwise return -1 if no key matches.
andrewm@0 612
andrewm@0 613 int KeyboardDisplay::keyForLocation(Point& internalPoint) {
andrewm@0 614 // First, check that the point is within the overall bounding box of the keyboard
andrewm@0 615 if(internalPoint.y < -totalDisplayHeight_*0.5 + kDisplayBottomMargin ||
andrewm@0 616 internalPoint.y > totalDisplayHeight_*0.5 - kDisplayTopMargin)
andrewm@0 617 return -1;
andrewm@0 618 if(internalPoint.x < -totalDisplayWidth_*0.5 + kDisplaySideMargin ||
andrewm@0 619 internalPoint.x > totalDisplayWidth_*0.5 - kDisplaySideMargin)
andrewm@0 620 return -1;
andrewm@0 621
andrewm@0 622 // Now, look for the key region corresponding to this horizontal location
andrewm@0 623 // hLoc indicates the relative distance from the beginning of the first key
andrewm@0 624
andrewm@0 625 float hLoc = internalPoint.x + totalDisplayWidth_*0.5 - kDisplaySideMargin;
andrewm@0 626
andrewm@0 627 if(hLoc < 0.0)
andrewm@0 628 return -1;
andrewm@0 629
andrewm@0 630 // normalizedHLoc indicates the index of the white key this touch is near.
andrewm@0 631 float normalizedHLoc = hLoc / (kWhiteKeyFrontWidth + kInterKeySpacing);
andrewm@0 632
andrewm@0 633 // Two relevant regions: front of the white keys, back of the white keys with black keys
andrewm@0 634 // Distinguish them by vertical position.
andrewm@0 635
andrewm@0 636 int shapeOfBottomKey = keyShape(lowestMidiNote_); // White key index of lowest key
andrewm@0 637 int lowestC = (lowestMidiNote_ / 12) * 12; // C below lowest key
andrewm@0 638 int whiteKeyNumber = floorf(normalizedHLoc); // Number of white key
andrewm@0 639 int whiteOctaveNumber = (whiteKeyNumber + shapeOfBottomKey) / 7; // Octave the key is in
andrewm@0 640 int chromaticKeyNumber = 12 * whiteOctaveNumber + kWhiteToChromatic[(whiteKeyNumber + shapeOfBottomKey) % 7];
andrewm@0 641
andrewm@0 642 // Check if we're on the front area of the white keys, and if so, ignore points located in the gaps
andrewm@0 643 // between the keys
andrewm@0 644
andrewm@0 645 if(internalPoint.y + totalDisplayHeight_*0.5 - kDisplayBottomMargin <= kWhiteKeyFrontLength) {
andrewm@0 646 if(normalizedHLoc - floorf(normalizedHLoc) > kWhiteKeyFrontWidth / (kWhiteKeyFrontWidth + kInterKeySpacing))
andrewm@0 647 return -1;
andrewm@0 648 return lowestC + chromaticKeyNumber;
andrewm@0 649 }
andrewm@0 650 else {
andrewm@0 651 // Back of white keys, or black keys
andrewm@0 652
andrewm@0 653 int whiteKeyShape = keyShape(chromaticKeyNumber);
andrewm@0 654 if(whiteKeyShape < 0) // Shouldn't happen
andrewm@0 655 return -1;
andrewm@0 656
andrewm@0 657 float locRelativeToLeft = (normalizedHLoc - floorf(normalizedHLoc)) * (kWhiteKeyFrontWidth + kInterKeySpacing);
andrewm@0 658
andrewm@0 659 // Check if we are in the back region of the white key. Handle the lowest and highest notes specially since
andrewm@0 660 // the white keys are generally wider on account of no adjacent black key.
andrewm@0 661 if(lowestC + chromaticKeyNumber == lowestMidiNote_) {
andrewm@0 662 if(locRelativeToLeft <= kWhiteKeyBackOffsets[whiteKeyShape] + kWhiteKeyBackWidths[whiteKeyShape])
andrewm@0 663 return lowestC + chromaticKeyNumber;
andrewm@0 664 }
andrewm@0 665 else if(lowestC + chromaticKeyNumber == highestMidiNote_) {
andrewm@0 666 if(locRelativeToLeft >= kWhiteKeyBackOffsets[whiteKeyShape])
andrewm@0 667 return lowestC + chromaticKeyNumber;
andrewm@0 668 }
andrewm@0 669 else if(locRelativeToLeft >= kWhiteKeyBackOffsets[whiteKeyShape] &&
andrewm@0 670 locRelativeToLeft <= kWhiteKeyBackOffsets[whiteKeyShape] + kWhiteKeyBackWidths[whiteKeyShape]) {
andrewm@0 671 return lowestC + chromaticKeyNumber;
andrewm@0 672 }
andrewm@0 673
andrewm@0 674 // By now, we've established that we're not on top of a white key. See if we align to a black key.
andrewm@0 675 // Watch the vertical gap between white and black keys
andrewm@0 676 if(internalPoint.y + totalDisplayHeight_*0.5 - kDisplayBottomMargin <=
andrewm@0 677 kWhiteKeyFrontLength + kWhiteKeyBackLength - kBlackKeyLength)
andrewm@0 678 return -1;
andrewm@0 679
andrewm@0 680 // Is there a black key below this white key?
andrewm@0 681 if(keyShape(chromaticKeyNumber - 1) < 0) {
andrewm@0 682 if(locRelativeToLeft <= kWhiteKeyBackOffsets[whiteKeyShape] - kInterKeySpacing &&
andrewm@0 683 lowestC + chromaticKeyNumber > lowestMidiNote_)
andrewm@0 684 return lowestC + chromaticKeyNumber - 1;
andrewm@0 685 }
andrewm@0 686 // Or is there a black key above this white key?
andrewm@0 687 if(keyShape(chromaticKeyNumber + 1) < 0) {
andrewm@0 688 if(locRelativeToLeft >= kWhiteKeyBackOffsets[whiteKeyShape] + kWhiteKeyBackWidths[whiteKeyShape] + kInterKeySpacing
andrewm@0 689 && lowestC + chromaticKeyNumber < highestMidiNote_)
andrewm@0 690 return lowestC + chromaticKeyNumber + 1;
andrewm@0 691 }
andrewm@0 692 }
andrewm@0 693
andrewm@0 694 // If all else fails, assume we're not on any key
andrewm@0 695 return -1;
andrewm@0 696 }