f@5
|
1 /*
|
f@5
|
2
|
f@5
|
3 Copyright (C) 2016 Queen Mary University of London
|
f@5
|
4 Author: Fiore Martin
|
f@5
|
5
|
f@5
|
6 This file is part of Collidoscope.
|
f@5
|
7
|
f@5
|
8 Collidoscope is free software: you can redistribute it and/or modify
|
f@5
|
9 it under the terms of the GNU General Public License as published by
|
f@5
|
10 the Free Software Foundation, either version 3 of the License, or
|
f@5
|
11 (at your option) any later version.
|
f@5
|
12
|
f@5
|
13 This program is distributed in the hope that it will be useful,
|
f@5
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
f@5
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
f@5
|
16 GNU General Public License for more details.
|
f@5
|
17
|
f@5
|
18 You should have received a copy of the GNU General Public License
|
f@5
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
f@5
|
20 */
|
f@5
|
21
|
f@0
|
22 #pragma once
|
f@0
|
23
|
f@0
|
24 #include "cinder/gl/gl.h"
|
f@0
|
25
|
f@0
|
26 #include "DrawInfo.h"
|
f@0
|
27
|
f@2
|
28
|
f@2
|
29
|
f@2
|
30 /**
|
f@2
|
31 * The oscilloscope that oscillates when Collidoscope is played
|
f@2
|
32 */
|
f@0
|
33 class Oscilloscope
|
f@0
|
34 {
|
f@0
|
35
|
f@0
|
36 public:
|
f@0
|
37
|
f@2
|
38 /**
|
f@2
|
39 * Constructor, accepts as argument the number of points that make up the oscilloscope line
|
f@2
|
40 */
|
f@0
|
41 Oscilloscope( size_t numPoints ):
|
f@0
|
42 mNumPoints( numPoints ),
|
f@0
|
43 mLine( std::vector<ci::vec2>( numPoints, ci::vec2() ) )
|
f@0
|
44 {}
|
f@0
|
45
|
f@2
|
46 /**
|
f@16
|
47 * Sets the value of a point of the oscilloscope. The value is passed in audio coordinates [-1.0, 1.0].
|
f@16
|
48 * A reference to DrawInfo is passed to calculate the graphic coordinate of the point based on the audio values passed.
|
f@2
|
49 */
|
f@0
|
50 void setPoint( int index, float audioVal, const DrawInfo &di ){
|
f@0
|
51
|
f@0
|
52 if ( audioVal > 1.0f ){
|
f@0
|
53 audioVal = 1.0f;
|
f@0
|
54 }
|
f@0
|
55 else if ( audioVal < -1.0f ){
|
f@0
|
56 audioVal = -1.0f;
|
f@0
|
57 }
|
f@0
|
58
|
f@0
|
59 audioVal *= 0.8f;
|
f@7
|
60 // map audio val from [-1.0, 1.0] to [0.0, 1.0]
|
f@7
|
61 // then map the value obtained to the height of the wave tier ( window height / NUM_WAVES )
|
f@0
|
62 float yRatio = ((1 + audioVal) / 2.0f) * (di.getWindowHeight() / NUM_WAVES );
|
f@0
|
63 float xRatio = index * (di.getWindowWidth() / (float)mLine.size());
|
f@0
|
64
|
f@7
|
65 // this flips the coordinates for the second wave
|
f@0
|
66 mLine.getPoints()[index].x = float( di.flipX( int(xRatio) ) );
|
f@0
|
67 mLine.getPoints()[index].y = float( di.flipY( int(yRatio) ) );
|
f@0
|
68
|
f@16
|
69 // add the missing line to reach the right of the window.
|
f@16
|
70 // Indeed, the scope starts from 0 to size-1 and adds xRatio
|
f@16
|
71 // to each new point. The line from n-1 to n is therefore missing.
|
f@0
|
72 if (index == mNumPoints - 1){
|
f@0
|
73 xRatio += ( di.getWindowWidth() / mNumPoints );
|
f@0
|
74 xRatio = ceil( xRatio ); // ceil because the division might left one pixel out
|
f@0
|
75
|
f@0
|
76 mLine.getPoints()[mNumPoints - 1].x = di.flipX( xRatio );
|
f@0
|
77 mLine.getPoints()[mNumPoints - 1].y = di.flipY( yRatio );
|
f@0
|
78 }
|
f@0
|
79
|
f@0
|
80 }
|
f@0
|
81
|
f@2
|
82 /**
|
f@2
|
83 * Draws this oscilloscope as a cinder::PolyLine2f
|
f@2
|
84 */
|
f@0
|
85 void draw()
|
f@0
|
86 {
|
f@0
|
87 ci::gl::color(1.0f, 1.0f, 1.0f);
|
f@0
|
88 ci::gl::draw( mLine );
|
f@0
|
89 }
|
f@0
|
90
|
f@0
|
91 size_t getNumPoints() const
|
f@0
|
92 {
|
f@0
|
93 return mNumPoints;
|
f@0
|
94 }
|
f@0
|
95
|
f@0
|
96 private:
|
f@0
|
97 size_t mNumPoints;
|
f@0
|
98 ci::PolyLine2f mLine;
|
f@0
|
99
|
f@0
|
100 };
|