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 @ 5:75b744078d66

History | View | Annotate | Download (2.94 KB)

1 5:75b744078d66 f
/*
2

3
 Copyright (C) 2016  Queen Mary University of London
4
 Author: Fiore Martin
5

6
 This file is part of Collidoscope.
7

8
 Collidoscope is free software: you can redistribute it and/or modify
9
 it under the terms of the GNU General Public License as published by
10
 the Free Software Foundation, either version 3 of the License, or
11
 (at your option) any later version.
12

13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 GNU General Public License for more details.
17

18
 You should have received a copy of the GNU General Public License
19
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
*/
21
22 0:02467299402e f
#pragma once
23
24
#include "cinder/gl/gl.h"
25
26
#include "DrawInfo.h"
27
28 2:dd889fff8423 f
29
30
/**
31
 * The oscilloscope that oscillates when Collidoscope is played
32
 */
33 0:02467299402e f
class Oscilloscope
34
{
35
36
public:
37
38 2:dd889fff8423 f
    /**
39
     * Constructor, accepts as argument the number of points that make up the oscilloscope line
40
     */
41 0:02467299402e f
    Oscilloscope( size_t numPoints ):
42
        mNumPoints( numPoints ),
43
        mLine( std::vector<ci::vec2>( numPoints, ci::vec2() ) )
44
        {}
45
46 2:dd889fff8423 f
    /**
47
     * Sets the value of a point of the oscilloscope. The value is passed as an audio coordinate [-1.0, 1.0].
48
     * A reference to DrawInfo is passed to calculate the graphic coordinate of the point based on the audio value passed.
49
     */
50 0:02467299402e f
    void  setPoint( int index, float audioVal, const DrawInfo &di ){
51
52
        if ( audioVal > 1.0f ){
53
            audioVal = 1.0f;
54
        }
55
        else if ( audioVal < -1.0f ){
56
            audioVal = -1.0f;
57
        }
58
59
        audioVal *= 0.8f;
60
        // this yRatio is for the bottom scope, the top will be drawn with a translation/4
61
        // because it's half of the half of the tier where the wave is drawn
62
        float yRatio = ((1 + audioVal) / 2.0f) * (di.getWindowHeight() / NUM_WAVES );
63
        float xRatio = index * (di.getWindowWidth() / (float)mLine.size());
64
65
        mLine.getPoints()[index].x = float( di.flipX( int(xRatio) ) );
66
        mLine.getPoints()[index].y = float( di.flipY( int(yRatio) ) );
67
68
        // add the missing line to reach the right of the window
69
        // indeed the scope starts from 0 to size -1 and adds xRatio
70
        // to each new point to the line from n-1 to n is missing
71
        if (index == mNumPoints - 1){
72
            xRatio += ( di.getWindowWidth() / mNumPoints );
73
            xRatio = ceil( xRatio ); // ceil because the division might left one pixel out
74
75
            mLine.getPoints()[mNumPoints - 1].x = di.flipX( xRatio );
76
            mLine.getPoints()[mNumPoints - 1].y = di.flipY( yRatio );
77
        }
78
79
    }
80
81 2:dd889fff8423 f
    /**
82
     * Draws this oscilloscope as a cinder::PolyLine2f
83
     */
84 0:02467299402e f
    void draw()
85
    {
86
        ci::gl::color(1.0f, 1.0f, 1.0f);
87
        ci::gl::draw( mLine );
88
    }
89
90
    size_t getNumPoints() const
91
    {
92
        return mNumPoints;
93
    }
94
95
private:
96
    size_t mNumPoints;
97
    ci::PolyLine2f  mLine;
98
99
};