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.h: 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: #ifndef __touchkeys__KeyPositionGraphDisplay__ andrewm@0: #define __touchkeys__KeyPositionGraphDisplay__ andrewm@0: andrewm@0: #include andrewm@0: #include andrewm@0: #include "../JuceLibraryCode/JuceHeader.h" andrewm@0: #include "../TouchKeys/PianoTypes.h" andrewm@0: #include "../Utility/Node.h" andrewm@0: #include "OpenGLDisplayBase.h" andrewm@0: andrewm@0: andrewm@0: andrewm@0: // This class uses OpenGL to handle drawing of a graph showing key position andrewm@0: // over time, for debugging or analysis purposes. andrewm@0: andrewm@0: class KeyPositionGraphDisplay : public OpenGLDisplayBase { andrewm@0: // Internal data structures and constants andrewm@0: private: andrewm@0: // Display margins andrewm@0: static const float kDisplaySideMargin; andrewm@0: static const float kDisplayBottomMargin; andrewm@0: static const float kDisplayTopMargin; andrewm@0: andrewm@0: // Size of the graph area andrewm@0: static const float kDisplayGraphWidth; andrewm@0: static const float kDisplayGraphHeight; andrewm@0: andrewm@0: typedef struct { andrewm@0: float x; andrewm@0: float y; andrewm@0: } Point; andrewm@0: andrewm@0: public: andrewm@0: KeyPositionGraphDisplay(); andrewm@0: andrewm@27: // Set canvas for triggering rendering; andrewm@27: void setCanvas(OpenGLJuceCanvas *canvas) { canvas_ = canvas; } andrewm@27: void tellCanvasToRepaint(); andrewm@27: andrewm@0: // Setup methods for display size and keyboard range andrewm@0: void setDisplaySize(float width, float height); andrewm@0: andrewm@0: // Drawing methods andrewm@0: void render(); andrewm@0: andrewm@0: // Interaction methods andrewm@0: void mouseDown(float x, float y); andrewm@0: void mouseDragged(float x, float y); andrewm@0: void mouseUp(float x, float y); andrewm@0: void rightMouseDown(float x, float y); andrewm@0: void rightMouseDragged(float x, float y); andrewm@0: void rightMouseUp(float x, float y); andrewm@0: andrewm@0: // Data update methods andrewm@0: void copyKeyDataFromBuffer(Node& keyBuffer, const Node::size_type startIndex, andrewm@0: const Node::size_type endIndex); andrewm@0: void setKeyPressStart(key_position position, timestamp_type timestamp) { andrewm@0: pressStartTimestamp_ = timestamp; andrewm@0: pressStartPosition_ = position; andrewm@27: tellCanvasToRepaint(); andrewm@0: } andrewm@0: void setKeyPressFinish(key_position position, timestamp_type timestamp) { andrewm@0: pressFinishTimestamp_ = timestamp; andrewm@0: pressFinishPosition_ = position; andrewm@27: tellCanvasToRepaint(); andrewm@0: } andrewm@0: void setKeyReleaseStart(key_position position, timestamp_type timestamp) { andrewm@0: releaseStartTimestamp_ = timestamp; andrewm@0: releaseStartPosition_ = position; andrewm@27: tellCanvasToRepaint(); andrewm@0: } andrewm@0: void setKeyReleaseFinish(key_position position, timestamp_type timestamp) { andrewm@0: releaseFinishTimestamp_ = timestamp; andrewm@0: releaseFinishPosition_ = position; andrewm@27: tellCanvasToRepaint(); andrewm@0: } andrewm@0: andrewm@0: private: andrewm@0: // Convert mathematical XY coordinate space to drawing positions andrewm@0: float graphToDisplayX(float x); andrewm@0: float graphToDisplayY(float y); andrewm@0: andrewm@0: void refreshViewport(); andrewm@0: andrewm@0: // Conversion from internal coordinate space to external pixel values and back andrewm@0: Point screenToInternal(Point& inPoint); andrewm@0: Point internalToScreen(Point& inPoint); andrewm@0: andrewm@27: private: andrewm@27: OpenGLJuceCanvas *canvas_; // Reference to canvas that renders OpenGL andrewm@0: andrewm@0: float displayPixelWidth_, displayPixelHeight_; // Pixel resolution of the surrounding window andrewm@0: float totalDisplayWidth_, totalDisplayHeight_; // Size of the internal view (centered around origin) andrewm@0: float xMin_, xMax_, yMin_, yMax_; // Coordinates for the graph axes andrewm@0: andrewm@0: CriticalSection displayMutex_; // Synchronize access between data and display threads andrewm@0: andrewm@0: std::vector keyPositions_; // Positions (0-1 normalized) of the key andrewm@0: std::vector keyTimestamps_; // Timestamps corresponding to the above positions andrewm@0: andrewm@0: // Details on features of key motion: start, end, release, etc. andrewm@0: key_position pressStartPosition_, pressFinishPosition_; andrewm@0: timestamp_type pressStartTimestamp_, pressFinishTimestamp_; andrewm@0: key_position releaseStartPosition_, releaseFinishPosition_; andrewm@0: timestamp_type releaseStartTimestamp_, releaseFinishTimestamp_; andrewm@0: }; andrewm@0: andrewm@0: andrewm@0: #endif /* defined(__touchkeys__KeyPositionGraphDisplay__) */