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@2: * Sets the value of a point of the oscilloscope. The value is passed as an audio coordinate [-1.0, 1.0]. f@2: * A reference to DrawInfo is passed to calculate the graphic coordinate of the point based on the audio value 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@0: // this yRatio is for the bottom scope, the top will be drawn with a translation/4 f@0: // because it's half of the half of the tier where the wave is drawn 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@0: mLine.getPoints()[index].x = float( di.flipX( int(xRatio) ) ); f@0: mLine.getPoints()[index].y = float( di.flipY( int(yRatio) ) ); f@0: f@0: // add the missing line to reach the right of the window f@0: // indeed the scope starts from 0 to size -1 and adds xRatio f@0: // to each new point to the line from n-1 to n is 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: };