f@0
|
1 #pragma once
|
f@0
|
2
|
f@0
|
3 #include "cinder/gl/gl.h"
|
f@0
|
4
|
f@0
|
5 #include "DrawInfo.h"
|
f@0
|
6
|
f@2
|
7
|
f@2
|
8
|
f@2
|
9 /**
|
f@2
|
10 * The oscilloscope that oscillates when Collidoscope is played
|
f@2
|
11 */
|
f@0
|
12 class Oscilloscope
|
f@0
|
13 {
|
f@0
|
14
|
f@0
|
15 public:
|
f@0
|
16
|
f@2
|
17 /**
|
f@2
|
18 * Constructor, accepts as argument the number of points that make up the oscilloscope line
|
f@2
|
19 */
|
f@0
|
20 Oscilloscope( size_t numPoints ):
|
f@0
|
21 mNumPoints( numPoints ),
|
f@0
|
22 mLine( std::vector<ci::vec2>( numPoints, ci::vec2() ) )
|
f@0
|
23 {}
|
f@0
|
24
|
f@2
|
25 /**
|
f@2
|
26 * Sets the value of a point of the oscilloscope. The value is passed as an audio coordinate [-1.0, 1.0].
|
f@2
|
27 * A reference to DrawInfo is passed to calculate the graphic coordinate of the point based on the audio value passed.
|
f@2
|
28 */
|
f@0
|
29 void setPoint( int index, float audioVal, const DrawInfo &di ){
|
f@0
|
30
|
f@0
|
31 if ( audioVal > 1.0f ){
|
f@0
|
32 audioVal = 1.0f;
|
f@0
|
33 }
|
f@0
|
34 else if ( audioVal < -1.0f ){
|
f@0
|
35 audioVal = -1.0f;
|
f@0
|
36 }
|
f@0
|
37
|
f@0
|
38 audioVal *= 0.8f;
|
f@0
|
39 // this yRatio is for the bottom scope, the top will be drawn with a translation/4
|
f@0
|
40 // because it's half of the half of the tier where the wave is drawn
|
f@0
|
41 float yRatio = ((1 + audioVal) / 2.0f) * (di.getWindowHeight() / NUM_WAVES );
|
f@0
|
42 float xRatio = index * (di.getWindowWidth() / (float)mLine.size());
|
f@0
|
43
|
f@0
|
44 mLine.getPoints()[index].x = float( di.flipX( int(xRatio) ) );
|
f@0
|
45 mLine.getPoints()[index].y = float( di.flipY( int(yRatio) ) );
|
f@0
|
46
|
f@0
|
47 // add the missing line to reach the right of the window
|
f@0
|
48 // indeed the scope starts from 0 to size -1 and adds xRatio
|
f@0
|
49 // to each new point to the line from n-1 to n is missing
|
f@0
|
50 if (index == mNumPoints - 1){
|
f@0
|
51 xRatio += ( di.getWindowWidth() / mNumPoints );
|
f@0
|
52 xRatio = ceil( xRatio ); // ceil because the division might left one pixel out
|
f@0
|
53
|
f@0
|
54 mLine.getPoints()[mNumPoints - 1].x = di.flipX( xRatio );
|
f@0
|
55 mLine.getPoints()[mNumPoints - 1].y = di.flipY( yRatio );
|
f@0
|
56 }
|
f@0
|
57
|
f@0
|
58 }
|
f@0
|
59
|
f@2
|
60 /**
|
f@2
|
61 * Draws this oscilloscope as a cinder::PolyLine2f
|
f@2
|
62 */
|
f@0
|
63 void draw()
|
f@0
|
64 {
|
f@0
|
65 ci::gl::color(1.0f, 1.0f, 1.0f);
|
f@0
|
66 ci::gl::draw( mLine );
|
f@0
|
67 }
|
f@0
|
68
|
f@0
|
69 size_t getNumPoints() const
|
f@0
|
70 {
|
f@0
|
71 return mNumPoints;
|
f@0
|
72 }
|
f@0
|
73
|
f@0
|
74 private:
|
f@0
|
75 size_t mNumPoints;
|
f@0
|
76 ci::PolyLine2f mLine;
|
f@0
|
77
|
f@0
|
78 };
|