f@0: #pragma once f@0: f@0: f@0: #include "cinder/app/App.h" f@0: #include "cinder/gl/gl.h" f@0: #include "cinder/gl/Batch.h" f@0: f@0: f@0: #include "Chunk.h" f@0: #include "DrawInfo.h" f@0: f@0: #ifdef USE_PARTICLES f@0: #include "ParticleController.h" f@0: #endif f@0: f@0: #include "cinder/Color.h" f@0: #include "cinder/PolyLine.h" f@0: #include "cinder/Rand.h" f@0: f@0: #include f@0: #include f@0: f@0: f@0: class DrawInfo; f@0: typedef int SynthID; f@0: f@0: f@0: using ci::ivec2; f@0: using ci::vec2; f@0: using ci::Color; f@0: using ci::ColorA; f@0: f@0: struct Cursor { f@0: static const int kNoPosition = -100; f@0: int pos; f@0: double lastUpdate; f@0: }; f@0: f@0: f@0: class Wave f@0: { f@0: friend class ParticleController; f@0: f@0: public: f@0: f@0: class Selection { f@0: f@0: public: f@0: f@0: Selection( Wave * w, Color color ); f@0: f@0: void setStart( size_t start ); f@0: f@0: void setSize( size_t size ); f@0: f@0: void inline setParticleSpread( float spread ){ f@0: mParticleSpread = spread; f@0: } f@0: f@0: size_t getStart(void) const { return mSelectionStart; } f@0: f@0: size_t getEnd(void) const { return mSelectionEnd; } f@0: f@0: size_t inline getSize(void) const { f@0: if (mNull) f@0: return 0; f@0: else f@0: return 1 + mSelectionEnd - mSelectionStart; f@0: } f@0: f@0: float inline getParticleSpread() const { return mParticleSpread; } f@0: f@0: inline void setToNull(){ f@0: mParticleSpread = 1.0f; f@0: mNull = true; f@0: } f@0: f@0: inline bool isNull() const{ f@0: return mNull; f@0: } f@0: f@0: inline const Color & getColor() const{ f@0: return mColor; f@0: } f@0: f@0: private: f@0: f@0: size_t mSelectionStart; f@0: f@0: size_t mSelectionEnd; f@0: f@0: float mParticleSpread; f@0: f@0: bool mNull = true; f@0: f@0: Color mColor; f@0: f@0: Wave * mWave; f@0: f@0: }; // class Selection f@0: f@0: f@0: f@0: #ifdef USE_PARTICLES f@0: ParticleController mParticleController; f@0: #endif f@0: f@0: f@0: f@0: /* there is one cursor for each Synth being played */ f@0: std::map < SynthID, Cursor > mCursors; f@0: std::vector mCursorsPos; f@0: f@0: public: f@0: f@0: // note used to identify the loop for cursor position f@0: static const int kLoopNote = -1; f@0: static const cinder::Color CURSOR_CLR; f@0: /* must be in sync with supercollider durationFactor ControlSpec max */ f@0: static const int MAX_DURATION = 8; f@0: #ifdef USE_PARTICLES f@0: static const int PARTICLESIZE_COEFF = 40; f@0: #endif f@0: f@0: void reset(bool onlyChunks); f@0: f@0: void setChunk(size_t index, float bottom, float top); f@0: f@0: const Chunk & getChunk(size_t index); f@0: f@0: inline void setCursorPos( SynthID id, int pos, const DrawInfo& di ){ f@0: f@0: Cursor & cursor = mCursors[id]; f@0: cursor.pos = pos; f@0: cursor.lastUpdate = ci::app::getElapsedSeconds(); f@0: f@0: #ifdef USE_PARTICLES f@0: /* if the duration is greater than 1.0 carry on the cursor as a particle f@0: the smaller the selection the more particles f@0: the bigger the duration the more particles */ f@0: if (mSelection.getParticleSpread() > 1.0f){ f@0: /* amountCoeff ranges from 1/8 to 1 */ f@0: const float amountCoeff = (mSelection.getParticleSpread() / MAX_DURATION); f@0: f@0: /* get radom point within seleciton as center of the particle */ f@0: vec2 centrePoint; // was former getRandomPoint f@0: const int randomChunkIndex = ci::Rand::randInt(mSelection.getStart(), mSelection.getEnd() ); f@0: f@0: centrePoint.x = di.flipX( 1 + (randomChunkIndex * (2 + Chunk::kWidth)) + Chunk::kWidth / 2 ); f@0: centrePoint.y = di.flipY( di.audioToHeigt(0.0) ); f@0: f@0: const float wavePixelLen = mNumChunks * ( 2 + Chunk::kWidth); f@0: centrePoint.x *= float(di.getWindowWidth()) / wavePixelLen; f@0: f@0: mParticleController.addParticles( f@0: std::max( 1, (int)(amountCoeff * ParticleController::kMaxParticleAdd * mFilterCoeff) ), // amount of particles to add f@0: centrePoint, f@0: mSelection.getParticleSpread() * PARTICLESIZE_COEFF // size of the cloud f@0: ); f@0: } f@0: #endif f@0: f@0: f@0: } f@0: f@0: void update( double secondsPerChunk, const DrawInfo& di ); f@0: f@0: void removeCursor( SynthID id ) { mCursors.erase( id ); } f@0: f@0: // parameter ranges from 0 to 1 f@0: inline void setselectionAlpha(float alpha){ mFilterCoeff = alpha;} f@0: f@0: void draw( const DrawInfo& di ); f@0: f@0: Selection& getSelection() { return mSelection; }; f@0: f@0: size_t getSize() const{ return mChunks.size(); } f@0: f@0: void setScopePoint(int index, float audioVal); f@0: f@0: Wave( size_t numChunks, Color selectionColor ); f@0: f@0: // no copies f@0: Wave( const Wave © ) = delete; f@0: Wave & operator=(const Wave ©) = delete; f@0: f@0: f@0: private: f@0: f@0: const size_t mNumChunks; f@0: f@0: std::vector mChunks; f@0: f@0: Selection mSelection; f@0: f@0: cinder::Color mColor; f@0: f@0: float mFilterCoeff; f@0: f@0: ci::gl::BatchRef mChunkBatch; f@0: f@0: }; f@0: