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: KeyPositionGraphDisplay.cpp: implements a graph of key position over time
andrewm@0: for a specific key press, which is useful for analysis of continuous key
andrewm@0: position.
andrewm@0: */
andrewm@0:
andrewm@0: #include "KeyPositionGraphDisplay.h"
andrewm@27: #include "OpenGLJuceCanvas.h"
andrewm@0: #include
andrewm@0: #include
andrewm@0:
andrewm@0: // Class constants
andrewm@0: // Display margins
andrewm@0: const float KeyPositionGraphDisplay::kDisplaySideMargin = 0.5;
andrewm@0: const float KeyPositionGraphDisplay::kDisplayBottomMargin = 0.5;
andrewm@0: const float KeyPositionGraphDisplay::kDisplayTopMargin = 0.5;
andrewm@0:
andrewm@0: // Size of the graph area
andrewm@0: const float KeyPositionGraphDisplay::kDisplayGraphWidth = 20.0;
andrewm@0: const float KeyPositionGraphDisplay::kDisplayGraphHeight = 10.0;
andrewm@0:
andrewm@27: KeyPositionGraphDisplay::KeyPositionGraphDisplay() : canvas_(0),
andrewm@0: displayPixelWidth_(1.0), displayPixelHeight_(1.0), totalDisplayWidth_(1.0), totalDisplayHeight_(1.0),
andrewm@27: xMin_(0.0), xMax_(1.0), yMin_(-0.2), yMax_(1.2)
andrewm@0: {
andrewm@0: // Initialize OpenGL settings: 2D only
andrewm@0:
andrewm@0: //glMatrixMode(GL_PROJECTION);
andrewm@0: //glDisable(GL_DEPTH_TEST);
andrewm@0:
andrewm@0: pressStartTimestamp_ = pressFinishTimestamp_ = releaseStartTimestamp_ = releaseFinishTimestamp_ = missing_value::missing();
andrewm@0: pressStartPosition_ = pressFinishPosition_ = releaseStartPosition_ = releaseFinishPosition_ = missing_value::missing();
andrewm@0:
andrewm@0: totalDisplayWidth_ = kDisplaySideMargin*2 + kDisplayGraphWidth;
andrewm@0: totalDisplayHeight_ = kDisplayTopMargin + kDisplayBottomMargin + kDisplayGraphHeight;
andrewm@0: }
andrewm@0:
andrewm@27: // Tell the underlying canvas to repaint itself
andrewm@27: void KeyPositionGraphDisplay::tellCanvasToRepaint() {
andrewm@27: if(canvas_ != 0)
andrewm@27: canvas_->triggerRepaint();
andrewm@27: }
andrewm@27:
andrewm@0: void KeyPositionGraphDisplay::setDisplaySize(float width, float height) {
andrewm@0: ScopedLock sl(displayMutex_);
andrewm@0: displayPixelWidth_ = width;
andrewm@0: displayPixelHeight_ = height;
andrewm@0: refreshViewport();
andrewm@0: }
andrewm@0:
andrewm@0:
andrewm@0: // Render the keyboard display
andrewm@0:
andrewm@0: void KeyPositionGraphDisplay::render() {
andrewm@0: // Start with a light gray background
andrewm@0: glClearColor(0.8, 0.8, 0.8, 1.0);
andrewm@0: glClear(GL_COLOR_BUFFER_BIT);
andrewm@0: glLoadIdentity();
andrewm@0:
andrewm@0: float invAspectRatio = totalDisplayWidth_ / totalDisplayHeight_;
andrewm@0: float scaleValue = 2.0 / totalDisplayWidth_;
andrewm@0:
andrewm@0: glScalef(scaleValue, scaleValue * invAspectRatio, scaleValue);
andrewm@0: glTranslatef(-1.0 / scaleValue, -totalDisplayHeight_ / 2.0, 0);
andrewm@0: glTranslatef(kDisplaySideMargin, kDisplayBottomMargin, 0.0);
andrewm@0:
andrewm@0: ScopedLock sl(displayMutex_);
andrewm@0:
andrewm@0: // Draw the region where the graph will be plotted (white with black outline)
andrewm@0: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
andrewm@0: glColor3f(1.0, 1.0, 1.0);
andrewm@0:
andrewm@0: glBegin(GL_POLYGON);
andrewm@0: glVertex2f(0, 0);
andrewm@0: glVertex2f(0, kDisplayGraphHeight);
andrewm@0: glVertex2f(kDisplayGraphWidth, kDisplayGraphHeight);
andrewm@0: glVertex2f(kDisplayGraphWidth, 0);
andrewm@0: glEnd();
andrewm@0:
andrewm@0: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
andrewm@0: glColor3f(0.0, 0.0, 0.0);
andrewm@0: glLineWidth(1.0);
andrewm@0:
andrewm@0: glBegin(GL_POLYGON);
andrewm@0: glVertex2f(0, 0);
andrewm@0: glVertex2f(0, kDisplayGraphHeight);
andrewm@0: glVertex2f(kDisplayGraphWidth, kDisplayGraphHeight);
andrewm@0: glVertex2f(kDisplayGraphWidth, 0);
andrewm@0: glEnd();
andrewm@0:
andrewm@0: // Draw gray lines for the 0.0 and 1.0 key positions
andrewm@0: glColor3f(0.5, 0.5, 0.5);
andrewm@0: glBegin(GL_LINES);
andrewm@0: glVertex2f(0, graphToDisplayY(0.0));
andrewm@0: glVertex2f(kDisplayGraphWidth, graphToDisplayY(0.0));
andrewm@0: glEnd();
andrewm@0: glBegin(GL_LINES);
andrewm@0: glVertex2f(0, graphToDisplayY(1.0));
andrewm@0: glVertex2f(kDisplayGraphWidth, graphToDisplayY(1.0));
andrewm@0: glEnd();
andrewm@0:
andrewm@0: // Draw the graph of position over time (if data exists)
andrewm@0: if(!keyPositions_.empty() && !keyTimestamps_.empty()) {
andrewm@0: glColor3f(0.0, 0.0, 0.0);
andrewm@0: glBegin(GL_LINE_STRIP);
andrewm@0: for(int index = 0; index < keyPositions_.size() && index < keyTimestamps_.size(); index++) {
andrewm@0: glVertex2f(graphToDisplayX(keyTimestamps_[index]), graphToDisplayY(keyPositions_[index]));
andrewm@0: }
andrewm@0: glEnd();
andrewm@0: }
andrewm@0:
andrewm@0: // Draw the important features, if they exist
andrewm@0: if(!missing_value::isMissing(pressStartTimestamp_)) {
andrewm@0: glColor3f(0.0, 0.0, 1.0);
andrewm@0: glBegin(GL_LINES);
andrewm@0: glVertex2f(graphToDisplayX(pressStartTimestamp_), 0);
andrewm@0: glVertex2f(graphToDisplayX(pressStartTimestamp_), kDisplayGraphHeight);
andrewm@0: glEnd();
andrewm@0: }
andrewm@0: if(!missing_value::isMissing(pressFinishTimestamp_)) {
andrewm@0: glColor3f(1.0, 0.0, 0.0);
andrewm@0: glBegin(GL_LINES);
andrewm@0: glVertex2f(graphToDisplayX(pressFinishTimestamp_), 0);
andrewm@0: glVertex2f(graphToDisplayX(pressFinishTimestamp_), kDisplayGraphHeight);
andrewm@0: glEnd();
andrewm@0: }
andrewm@0: if(!missing_value::isMissing(releaseStartTimestamp_)) {
andrewm@0: glColor3f(0.0, 0.0, 1.0);
andrewm@0: glBegin(GL_LINES);
andrewm@0: glVertex2f(graphToDisplayX(releaseStartTimestamp_), 0);
andrewm@0: glVertex2f(graphToDisplayX(releaseStartTimestamp_), kDisplayGraphHeight);
andrewm@0: glEnd();
andrewm@0: }
andrewm@0: if(!missing_value::isMissing(releaseFinishTimestamp_)) {
andrewm@0: glColor3f(1.0, 0.0, 0.0);
andrewm@0: glBegin(GL_LINES);
andrewm@0: glVertex2f(graphToDisplayX(releaseFinishTimestamp_), 0);
andrewm@0: glVertex2f(graphToDisplayX(releaseFinishTimestamp_), kDisplayGraphHeight);
andrewm@0: glEnd();
andrewm@0: }
andrewm@0:
andrewm@0: glFlush();
andrewm@0: }
andrewm@0:
andrewm@0: // Mouse interaction methods
andrewm@0:
andrewm@0: void KeyPositionGraphDisplay::mouseDown(float x, float y) {
andrewm@0: //Point mousePoint = {x, y};
andrewm@0: //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0: }
andrewm@0:
andrewm@0: void KeyPositionGraphDisplay::mouseDragged(float x, float y) {
andrewm@0: //Point mousePoint = {x, y};
andrewm@0: //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0: }
andrewm@0:
andrewm@0: void KeyPositionGraphDisplay::mouseUp(float x, float y) {
andrewm@0: //Point mousePoint = {x, y};
andrewm@0: //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0: }
andrewm@0:
andrewm@0: void KeyPositionGraphDisplay::rightMouseDown(float x, float y) {
andrewm@0: //Point mousePoint = {x, y};
andrewm@0: //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0: }
andrewm@0:
andrewm@0: void KeyPositionGraphDisplay::rightMouseDragged(float x, float y) {
andrewm@0: //Point mousePoint = {x, y};
andrewm@0: //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0: }
andrewm@0:
andrewm@0: void KeyPositionGraphDisplay::rightMouseUp(float x, float y) {
andrewm@0: //Point mousePoint = {x, y};
andrewm@0: //Point scaledPoint = screenToInternal(mousePoint);
andrewm@0: }
andrewm@0:
andrewm@0: float KeyPositionGraphDisplay::graphToDisplayX(float x) {
andrewm@0: return kDisplayGraphWidth*(x - xMin_)/(xMax_ - xMin_);
andrewm@0: }
andrewm@0:
andrewm@0: float KeyPositionGraphDisplay::graphToDisplayY(float y) {
andrewm@0: return kDisplayGraphHeight*(y - yMin_)/(yMax_ - yMin_);
andrewm@0: }
andrewm@0:
andrewm@0: void KeyPositionGraphDisplay::refreshViewport() {
andrewm@0: glViewport(0, 0, displayPixelWidth_, displayPixelHeight_);
andrewm@0: }
andrewm@0:
andrewm@0: // Copy data from the circular buffer to our internal buffer for
andrewm@0: void KeyPositionGraphDisplay::copyKeyDataFromBuffer(Node& keyBuffer,
andrewm@0: const Node::size_type startIndex,
andrewm@0: const Node::size_type endIndex) {
andrewm@0: // Clear existing information
andrewm@0: keyPositions_.clear();
andrewm@0: keyTimestamps_.clear();
andrewm@0:
andrewm@0: Node::size_type index = startIndex;
andrewm@0: if(index < keyBuffer.beginIndex())
andrewm@0: index = keyBuffer.beginIndex();
andrewm@0:
andrewm@0: // Iterate through the buffer, copying positions and timestamps
andrewm@0: while(index < endIndex && index < keyBuffer.endIndex()) {
andrewm@0: keyPositions_.push_back(keyBuffer[index]);
andrewm@0: keyTimestamps_.push_back(keyBuffer.timestampAt(index));
andrewm@0: index++;
andrewm@0: }
andrewm@0:
andrewm@0: // Scale the axes according to what's being displayed. The Y axis should stay
andrewm@0: // more or less constant (for now), but X will change depending on timestamps
andrewm@0: xMin_ = keyTimestamps_.front();
andrewm@0: xMax_ = keyTimestamps_.back();
andrewm@0: yMin_ = -0.2;
andrewm@0: yMax_ = 1.2;
andrewm@27: tellCanvasToRepaint();
andrewm@0: }
andrewm@0:
andrewm@0: // Conversion from internal coordinate space to external pixel values and back
andrewm@0:
andrewm@0: // Pixel values go from 0,0 (lower left) to displayPixelWidth_, displayPixelHeight_ (upper right)
andrewm@0: // Internal values go from -totalDisplayWidth_/2, -totalDisplayHeight_/2 (lower left)
andrewm@0: // to totalDisplayWidth_/2, totalDisplayHeight_/2 (upper right)
andrewm@0:
andrewm@0: // Pixel value in --> OpenGL value out
andrewm@0: KeyPositionGraphDisplay::Point KeyPositionGraphDisplay::screenToInternal(Point& inPoint) {
andrewm@0: Point out;
andrewm@0:
andrewm@0: out.x = -totalDisplayWidth_*0.5 + (inPoint.x/displayPixelWidth_) * totalDisplayWidth_;
andrewm@0: out.y = -totalDisplayHeight_*0.5 + (inPoint.y/displayPixelHeight_) * totalDisplayHeight_;
andrewm@0:
andrewm@0: return out;
andrewm@0: }
andrewm@0:
andrewm@0: // OpenGL value in --> Pixel value out
andrewm@0: KeyPositionGraphDisplay::Point KeyPositionGraphDisplay::internalToScreen(Point& inPoint) {
andrewm@0: Point out;
andrewm@0:
andrewm@0: out.x = ((inPoint.x + totalDisplayWidth_*0.5)/totalDisplayWidth_) * displayPixelWidth_;
andrewm@0: out.y = ((inPoint.y + totalDisplayHeight_*0.5)/totalDisplayHeight_) * displayPixelHeight_;
andrewm@0:
andrewm@0: return out;
andrewm@0: }