To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / CollidoscopeApp / include / Oscilloscope.h @ 7:a4a336624f5a
History | View | Annotate | Download (2.98 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 | 7:a4a336624f5a | f | // map audio val from [-1.0, 1.0] to [0.0, 1.0]
|
| 61 | // then map the value obtained to the height of the wave tier ( window height / NUM_WAVES )
|
||
| 62 | 0:02467299402e | f | float yRatio = ((1 + audioVal) / 2.0f) * (di.getWindowHeight() / NUM_WAVES ); |
| 63 | float xRatio = index * (di.getWindowWidth() / (float)mLine.size()); |
||
| 64 | |||
| 65 | 7:a4a336624f5a | f | // this flips the coordinates for the second wave
|
| 66 | 0:02467299402e | f | mLine.getPoints()[index].x = float( di.flipX( int(xRatio) ) ); |
| 67 | mLine.getPoints()[index].y = float( di.flipY( int(yRatio) ) ); |
||
| 68 | |||
| 69 | // add the missing line to reach the right of the window
|
||
| 70 | // indeed the scope starts from 0 to size -1 and adds xRatio
|
||
| 71 | // to each new point to the line from n-1 to n is missing
|
||
| 72 | if (index == mNumPoints - 1){ |
||
| 73 | xRatio += ( di.getWindowWidth() / mNumPoints ); |
||
| 74 | xRatio = ceil( xRatio ); // ceil because the division might left one pixel out
|
||
| 75 | |||
| 76 | mLine.getPoints()[mNumPoints - 1].x = di.flipX( xRatio );
|
||
| 77 | mLine.getPoints()[mNumPoints - 1].y = di.flipY( yRatio );
|
||
| 78 | } |
||
| 79 | |||
| 80 | } |
||
| 81 | |||
| 82 | 2:dd889fff8423 | f | /**
|
| 83 | * Draws this oscilloscope as a cinder::PolyLine2f
|
||
| 84 | */
|
||
| 85 | 0:02467299402e | f | void draw()
|
| 86 | {
|
||
| 87 | ci::gl::color(1.0f, 1.0f, 1.0f); |
||
| 88 | ci::gl::draw( mLine ); |
||
| 89 | } |
||
| 90 | |||
| 91 | size_t getNumPoints() const
|
||
| 92 | {
|
||
| 93 | return mNumPoints;
|
||
| 94 | } |
||
| 95 | |||
| 96 | private:
|
||
| 97 | size_t mNumPoints; |
||
| 98 | ci::PolyLine2f mLine; |
||
| 99 | |||
| 100 | }; |