f@5: /* f@5: f@5: Copyright (C) 2016 Queen Mary University of London f@5: Author: Fiore Martin f@5: f@5: This file is part of Collidoscope. f@5: f@5: Collidoscope is free software: you can redistribute it and/or modify f@5: it under the terms of the GNU General Public License as published by f@5: the Free Software Foundation, either version 3 of the License, or f@5: (at your option) any later version. f@5: f@5: This program is distributed in the hope that it will be useful, f@5: but WITHOUT ANY WARRANTY; without even the implied warranty of f@5: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the f@5: GNU General Public License for more details. f@5: f@5: You should have received a copy of the GNU General Public License f@5: along with this program. If not, see . f@5: */ f@5: f@0: #pragma once f@0: f@0: #include "cinder/gl/gl.h" f@0: f@0: #include "DrawInfo.h" f@0: f@2: f@2: f@2: /** f@2: * The oscilloscope that oscillates when Collidoscope is played f@2: */ f@0: class Oscilloscope f@0: { f@0: f@0: public: f@0: f@2: /** f@2: * Constructor, accepts as argument the number of points that make up the oscilloscope line f@2: */ f@0: Oscilloscope( size_t numPoints ): f@0: mNumPoints( numPoints ), f@0: mLine( std::vector( numPoints, ci::vec2() ) ) f@0: {} f@0: f@2: /** f@16: * Sets the value of a point of the oscilloscope. The value is passed in audio coordinates [-1.0, 1.0]. f@16: * A reference to DrawInfo is passed to calculate the graphic coordinate of the point based on the audio values passed. f@2: */ f@0: void setPoint( int index, float audioVal, const DrawInfo &di ){ f@0: f@0: if ( audioVal > 1.0f ){ f@0: audioVal = 1.0f; f@0: } f@0: else if ( audioVal < -1.0f ){ f@0: audioVal = -1.0f; f@0: } f@0: f@0: audioVal *= 0.8f; f@7: // map audio val from [-1.0, 1.0] to [0.0, 1.0] f@7: // then map the value obtained to the height of the wave tier ( window height / NUM_WAVES ) f@0: float yRatio = ((1 + audioVal) / 2.0f) * (di.getWindowHeight() / NUM_WAVES ); f@0: float xRatio = index * (di.getWindowWidth() / (float)mLine.size()); f@0: f@7: // this flips the coordinates for the second wave f@0: mLine.getPoints()[index].x = float( di.flipX( int(xRatio) ) ); f@0: mLine.getPoints()[index].y = float( di.flipY( int(yRatio) ) ); f@0: f@16: // add the missing line to reach the right of the window. f@16: // Indeed, the scope starts from 0 to size-1 and adds xRatio f@16: // to each new point. The line from n-1 to n is therefore missing. f@0: if (index == mNumPoints - 1){ f@0: xRatio += ( di.getWindowWidth() / mNumPoints ); f@0: xRatio = ceil( xRatio ); // ceil because the division might left one pixel out f@0: f@0: mLine.getPoints()[mNumPoints - 1].x = di.flipX( xRatio ); f@0: mLine.getPoints()[mNumPoints - 1].y = di.flipY( yRatio ); f@0: } f@0: f@0: } f@0: f@2: /** f@2: * Draws this oscilloscope as a cinder::PolyLine2f f@2: */ f@0: void draw() f@0: { f@0: ci::gl::color(1.0f, 1.0f, 1.0f); f@0: ci::gl::draw( mLine ); f@0: } f@0: f@0: size_t getNumPoints() const f@0: { f@0: return mNumPoints; f@0: } f@0: f@0: private: f@0: size_t mNumPoints; f@0: ci::PolyLine2f mLine; f@0: f@0: };