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 / Wave.h @ 0:02467299402e
History | View | Annotate | Download (4.38 KB)
| 1 | 0:02467299402e | f | #pragma once
|
|---|---|---|---|
| 2 | |||
| 3 | |||
| 4 | #include "cinder/app/App.h" |
||
| 5 | #include "cinder/gl/gl.h" |
||
| 6 | #include "cinder/gl/Batch.h" |
||
| 7 | |||
| 8 | |||
| 9 | #include "Chunk.h" |
||
| 10 | #include "DrawInfo.h" |
||
| 11 | |||
| 12 | #ifdef USE_PARTICLES
|
||
| 13 | #include "ParticleController.h" |
||
| 14 | #endif
|
||
| 15 | |||
| 16 | #include "cinder/Color.h" |
||
| 17 | #include "cinder/PolyLine.h" |
||
| 18 | #include "cinder/Rand.h" |
||
| 19 | |||
| 20 | #include <vector> |
||
| 21 | #include <map> |
||
| 22 | |||
| 23 | |||
| 24 | class DrawInfo; |
||
| 25 | typedef int SynthID; |
||
| 26 | |||
| 27 | |||
| 28 | using ci::ivec2; |
||
| 29 | using ci::vec2; |
||
| 30 | using ci::Color; |
||
| 31 | using ci::ColorA; |
||
| 32 | |||
| 33 | struct Cursor {
|
||
| 34 | static const int kNoPosition = -100; |
||
| 35 | int pos;
|
||
| 36 | double lastUpdate;
|
||
| 37 | }; |
||
| 38 | |||
| 39 | |||
| 40 | class Wave |
||
| 41 | {
|
||
| 42 | friend class ParticleController; |
||
| 43 | |||
| 44 | public:
|
||
| 45 | |||
| 46 | class Selection {
|
||
| 47 | |||
| 48 | public:
|
||
| 49 | |||
| 50 | Selection( Wave * w, Color color ); |
||
| 51 | |||
| 52 | void setStart( size_t start );
|
||
| 53 | |||
| 54 | void setSize( size_t size );
|
||
| 55 | |||
| 56 | void inline setParticleSpread( float spread ){ |
||
| 57 | mParticleSpread = spread; |
||
| 58 | } |
||
| 59 | |||
| 60 | size_t getStart(void) const { return mSelectionStart; } |
||
| 61 | |||
| 62 | size_t getEnd(void) const { return mSelectionEnd; } |
||
| 63 | |||
| 64 | size_t inline getSize(void) const { |
||
| 65 | if (mNull)
|
||
| 66 | return 0; |
||
| 67 | else
|
||
| 68 | return 1 + mSelectionEnd - mSelectionStart; |
||
| 69 | } |
||
| 70 | |||
| 71 | float inline getParticleSpread() const { return mParticleSpread; } |
||
| 72 | |||
| 73 | inline void setToNull(){ |
||
| 74 | mParticleSpread = 1.0f; |
||
| 75 | mNull = true;
|
||
| 76 | } |
||
| 77 | |||
| 78 | inline bool isNull() const{ |
||
| 79 | return mNull;
|
||
| 80 | } |
||
| 81 | |||
| 82 | inline const Color & getColor() const{ |
||
| 83 | return mColor;
|
||
| 84 | } |
||
| 85 | |||
| 86 | private:
|
||
| 87 | |||
| 88 | size_t mSelectionStart; |
||
| 89 | |||
| 90 | size_t mSelectionEnd; |
||
| 91 | |||
| 92 | float mParticleSpread;
|
||
| 93 | |||
| 94 | bool mNull = true; |
||
| 95 | |||
| 96 | Color mColor; |
||
| 97 | |||
| 98 | Wave * mWave; |
||
| 99 | |||
| 100 | }; // class Selection
|
||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | #ifdef USE_PARTICLES
|
||
| 105 | ParticleController mParticleController; |
||
| 106 | #endif
|
||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | /* there is one cursor for each Synth being played */
|
||
| 111 | std::map < SynthID, Cursor > mCursors; |
||
| 112 | std::vector<int> mCursorsPos;
|
||
| 113 | |||
| 114 | public:
|
||
| 115 | |||
| 116 | // note used to identify the loop for cursor position
|
||
| 117 | static const int kLoopNote = -1; |
||
| 118 | static const cinder::Color CURSOR_CLR; |
||
| 119 | /* must be in sync with supercollider durationFactor ControlSpec max */
|
||
| 120 | static const int MAX_DURATION = 8; |
||
| 121 | #ifdef USE_PARTICLES
|
||
| 122 | static const int PARTICLESIZE_COEFF = 40; |
||
| 123 | #endif
|
||
| 124 | |||
| 125 | void reset(bool onlyChunks); |
||
| 126 | |||
| 127 | void setChunk(size_t index, float bottom, float top); |
||
| 128 | |||
| 129 | const Chunk & getChunk(size_t index);
|
||
| 130 | |||
| 131 | inline void setCursorPos( SynthID id, int pos, const DrawInfo& di ){ |
||
| 132 | |||
| 133 | Cursor & cursor = mCursors[id]; |
||
| 134 | cursor.pos = pos; |
||
| 135 | cursor.lastUpdate = ci::app::getElapsedSeconds(); |
||
| 136 | |||
| 137 | #ifdef USE_PARTICLES
|
||
| 138 | /* if the duration is greater than 1.0 carry on the cursor as a particle
|
||
| 139 | the smaller the selection the more particles
|
||
| 140 | the bigger the duration the more particles */
|
||
| 141 | if (mSelection.getParticleSpread() > 1.0f){ |
||
| 142 | /* amountCoeff ranges from 1/8 to 1 */
|
||
| 143 | const float amountCoeff = (mSelection.getParticleSpread() / MAX_DURATION); |
||
| 144 | |||
| 145 | /* get radom point within seleciton as center of the particle */
|
||
| 146 | vec2 centrePoint; // was former getRandomPoint
|
||
| 147 | const int randomChunkIndex = ci::Rand::randInt(mSelection.getStart(), mSelection.getEnd() ); |
||
| 148 | |||
| 149 | centrePoint.x = di.flipX( 1 + (randomChunkIndex * (2 + Chunk::kWidth)) + Chunk::kWidth / 2 ); |
||
| 150 | centrePoint.y = di.flipY( di.audioToHeigt(0.0) ); |
||
| 151 | |||
| 152 | const float wavePixelLen = mNumChunks * ( 2 + Chunk::kWidth); |
||
| 153 | centrePoint.x *= float(di.getWindowWidth()) / wavePixelLen;
|
||
| 154 | |||
| 155 | mParticleController.addParticles( |
||
| 156 | std::max( 1, (int)(amountCoeff * ParticleController::kMaxParticleAdd * mFilterCoeff) ), // amount of particles to add |
||
| 157 | centrePoint, |
||
| 158 | mSelection.getParticleSpread() * PARTICLESIZE_COEFF // size of the cloud
|
||
| 159 | ); |
||
| 160 | } |
||
| 161 | #endif
|
||
| 162 | |||
| 163 | |||
| 164 | } |
||
| 165 | |||
| 166 | void update( double secondsPerChunk, const DrawInfo& di ); |
||
| 167 | |||
| 168 | void removeCursor( SynthID id ) { mCursors.erase( id ); }
|
||
| 169 | |||
| 170 | // parameter ranges from 0 to 1
|
||
| 171 | inline void setselectionAlpha(float alpha){ mFilterCoeff = alpha;} |
||
| 172 | |||
| 173 | void draw( const DrawInfo& di ); |
||
| 174 | |||
| 175 | Selection& getSelection() { return mSelection; };
|
||
| 176 | |||
| 177 | size_t getSize() const{ return mChunks.size(); } |
||
| 178 | |||
| 179 | void setScopePoint(int index, float audioVal); |
||
| 180 | |||
| 181 | Wave( size_t numChunks, Color selectionColor ); |
||
| 182 | |||
| 183 | // no copies
|
||
| 184 | Wave( const Wave © ) = delete;
|
||
| 185 | Wave & operator=(const Wave ©) = delete;
|
||
| 186 | |||
| 187 | |||
| 188 | private:
|
||
| 189 | |||
| 190 | const size_t mNumChunks;
|
||
| 191 | |||
| 192 | std::vector<Chunk> mChunks; |
||
| 193 | |||
| 194 | Selection mSelection; |
||
| 195 | |||
| 196 | cinder::Color mColor; |
||
| 197 | |||
| 198 | float mFilterCoeff;
|
||
| 199 | |||
| 200 | ci::gl::BatchRef mChunkBatch; |
||
| 201 | |||
| 202 | }; |