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 @ 0:02467299402e

History | View | Annotate | Download (1.71 KB)

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