To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / CollidoscopeApp / include / Oscilloscope.h @ 2:dd889fff8423

History | View | Annotate | Download (2.21 KB)

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