annotate CollidoscopeApp/include/Wave.h @ 5:75b744078d66

added license
author Fiore Martin <f.martin@qmul.ac.uk>
date Wed, 13 Jul 2016 16:06:46 +0200
parents 7fb593d53361
children a4a336624f5a
rev   line source
f@5 1 /*
f@5 2
f@5 3 Copyright (C) 2015 Fiore Martin
f@5 4 Copyright (C) 2016 Queen Mary University of London
f@5 5 Author: Fiore Martin
f@5 6
f@5 7 This file is part of Collidoscope.
f@5 8
f@5 9 Collidoscope is free software: you can redistribute it and/or modify
f@5 10 it under the terms of the GNU General Public License as published by
f@5 11 the Free Software Foundation, either version 3 of the License, or
f@5 12 (at your option) any later version.
f@5 13
f@5 14 This program is distributed in the hope that it will be useful,
f@5 15 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@5 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@5 17 GNU General Public License for more details.
f@5 18
f@5 19 You should have received a copy of the GNU General Public License
f@5 20 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@5 21
f@5 22 */
f@0 23 #pragma once
f@0 24
f@0 25
f@0 26 #include "cinder/app/App.h"
f@0 27 #include "cinder/gl/gl.h"
f@0 28 #include "cinder/gl/Batch.h"
f@0 29
f@0 30
f@0 31 #include "Chunk.h"
f@0 32 #include "DrawInfo.h"
f@0 33
f@0 34 #ifdef USE_PARTICLES
f@0 35 #include "ParticleController.h"
f@0 36 #endif
f@0 37
f@0 38 #include "cinder/Color.h"
f@0 39 #include "cinder/PolyLine.h"
f@0 40 #include "cinder/Rand.h"
f@0 41
f@0 42 #include <vector>
f@0 43 #include <map>
f@0 44
f@0 45
f@0 46 class DrawInfo;
f@0 47 typedef int SynthID;
f@0 48
f@0 49
f@0 50 using ci::ivec2;
f@0 51 using ci::vec2;
f@0 52 using ci::Color;
f@0 53 using ci::ColorA;
f@0 54
f@3 55 /**
f@3 56 * A Cursor is the white thingy that loops through the selection when Collidoscope is played.
f@3 57 */
f@0 58 struct Cursor {
f@0 59 static const int kNoPosition = -100;
f@0 60 int pos;
f@0 61 double lastUpdate;
f@0 62 };
f@0 63
f@3 64 /**
f@3 65 * Collidoscope's graphical wave
f@3 66 *
f@3 67 */
f@0 68 class Wave
f@0 69 {
f@5 70 friend class ParticleController;
f@0 71
f@0 72 public:
f@0 73
f@3 74 /**
f@3 75 * The selection of the wave that is controlled by the big horizontal knob
f@3 76 *
f@3 77 */
f@5 78 class Selection {
f@5 79
f@5 80 public:
f@0 81
f@0 82 Selection( Wave * w, Color color );
f@0 83
f@3 84 /** Sets the start of selection. start is the index of the first chunk of the selection */
f@0 85 void setStart( size_t start );
f@0 86
f@3 87 /** Sets the size of selection. size is the number of chunks the selection is made of */
f@5 88 void setSize( size_t size );
f@5 89
f@3 90 /** Particle spread is used to calculate the size of the cloud of particles */
f@0 91 void inline setParticleSpread( float spread ){
f@0 92 mParticleSpread = spread;
f@5 93 }
f@0 94
f@5 95 size_t getStart(void) const { return mSelectionStart; }
f@5 96
f@0 97 size_t getEnd(void) const { return mSelectionEnd; }
f@0 98
f@5 99 size_t inline getSize(void) const {
f@5 100 if (mNull)
f@5 101 return 0;
f@5 102 else
f@5 103 return 1 + mSelectionEnd - mSelectionStart;
f@5 104 }
f@0 105
f@5 106 float inline getParticleSpread() const { return mParticleSpread; }
f@0 107
f@3 108 /** When selection is null no selection is showed on the wave */
f@5 109 inline void setToNull(){
f@0 110 mParticleSpread = 1.0f;
f@5 111 mNull = true;
f@5 112 }
f@0 113
f@5 114 inline bool isNull() const{
f@5 115 return mNull;
f@5 116 }
f@0 117
f@5 118 inline const Color & getColor() const{
f@5 119 return mColor;
f@5 120 }
f@0 121
f@0 122 private:
f@0 123
f@0 124 size_t mSelectionStart;
f@0 125
f@0 126 size_t mSelectionEnd;
f@0 127
f@0 128 float mParticleSpread;
f@0 129
f@0 130 bool mNull = true;
f@0 131
f@0 132 Color mColor;
f@0 133
f@0 134 Wave * mWave;
f@0 135
f@5 136 }; // class Selection
f@0 137
f@5 138
f@0 139
f@0 140 #ifdef USE_PARTICLES
f@5 141 ParticleController mParticleController;
f@0 142 #endif
f@0 143
f@5 144
f@0 145
f@5 146 /* Maps id of the synth to cursor. There is one cursor for each Synth being played */
f@5 147 std::map < SynthID, Cursor > mCursors;
f@3 148 /** Holds the positions of the cursor, namely on which chunk the cursor is currently */
f@5 149 std::vector<int> mCursorsPos;
f@0 150
f@0 151 public:
f@5 152
f@3 153 // value used to identify the loop for cursor position
f@0 154 static const int kLoopNote = -1;
f@5 155 static const cinder::Color CURSOR_CLR;
f@5 156 /* must be in sync with supercollider durationFactor ControlSpec max */
f@5 157 static const int MAX_DURATION = 8;
f@0 158 #ifdef USE_PARTICLES
f@5 159 static const int PARTICLESIZE_COEFF = 40;
f@0 160 #endif
f@0 161
f@3 162 /** Resetting a wave makes it shrink until it disappears. Each time a new sample is recorder the wave is reset
f@3 163 * \param onlyChunks if false the selection is also set to null, if true only the chunks are reset
f@3 164 */
f@5 165 void reset(bool onlyChunks);
f@0 166
f@3 167 /** sets top and bottom values for the chunk.
f@3 168 * \a bottom and \a top are in audio coordinates [-1.0, 1.0]
f@3 169 */
f@5 170 void setChunk(size_t index, float bottom, float top);
f@0 171
f@5 172 const Chunk & getChunk(size_t index);
f@0 173
f@3 174 /** places the cursor on the wave. Every cursor is associated to a synth voice of the audio engine.
f@3 175 * The synth id identifies uniquely the cursor in the internal map of the wave.
f@3 176 * If the cursor doesn't exist it is created */
f@0 177 inline void setCursorPos( SynthID id, int pos, const DrawInfo& di ){
f@0 178
f@5 179 Cursor & cursor = mCursors[id];
f@5 180 cursor.pos = pos;
f@5 181 cursor.lastUpdate = ci::app::getElapsedSeconds();
f@0 182
f@0 183 #ifdef USE_PARTICLES
f@5 184 // The idea is that, if the duration is greater than 1.0, the cursor continues in form of particles
f@5 185 // The smaller the selection the more particles; the bigger the duration the more particles
f@5 186 if (mSelection.getParticleSpread() > 1.0f){
f@5 187 /* amountCoeff ranges from 1/8 to 1 */
f@0 188 const float amountCoeff = (mSelection.getParticleSpread() / MAX_DURATION);
f@0 189
f@0 190 /* get radom point within seleciton as center of the particle */
f@0 191 vec2 centrePoint; // was former getRandomPoint
f@0 192 const int randomChunkIndex = ci::Rand::randInt(mSelection.getStart(), mSelection.getEnd() );
f@0 193
f@0 194 centrePoint.x = di.flipX( 1 + (randomChunkIndex * (2 + Chunk::kWidth)) + Chunk::kWidth / 2 );
f@0 195 centrePoint.y = di.flipY( di.audioToHeigt(0.0) );
f@0 196
f@0 197 const float wavePixelLen = mNumChunks * ( 2 + Chunk::kWidth);
f@5 198 centrePoint.x *= float(di.getWindowWidth()) / wavePixelLen;
f@0 199
f@5 200 mParticleController.addParticles(
f@0 201 std::max( 1, (int)(amountCoeff * ParticleController::kMaxParticleAdd * mFilterCoeff) ), // amount of particles to add
f@5 202 centrePoint,
f@0 203 mSelection.getParticleSpread() * PARTICLESIZE_COEFF // size of the cloud
f@5 204 );
f@5 205 }
f@0 206 #endif
f@0 207
f@5 208
f@5 209 }
f@0 210
f@0 211 void update( double secondsPerChunk, const DrawInfo& di );
f@0 212
f@0 213 void removeCursor( SynthID id ) { mCursors.erase( id ); }
f@0 214
f@3 215 /** Sets the transparency of this wave. \a alpha ranges from 0 to 1 */
f@5 216 inline void setselectionAlpha(float alpha){ mFilterCoeff = alpha;}
f@0 217
f@0 218 void draw( const DrawInfo& di );
f@0 219
f@5 220 Selection& getSelection() { return mSelection; };
f@0 221
f@5 222 size_t getSize() const{ return mChunks.size(); }
f@0 223
f@5 224 void setScopePoint(int index, float audioVal);
f@0 225
f@0 226 Wave( size_t numChunks, Color selectionColor );
f@0 227
f@3 228 /** no copies */
f@0 229 Wave( const Wave &copy ) = delete;
f@0 230 Wave & operator=(const Wave &copy) = delete;
f@0 231
f@0 232
f@0 233 private:
f@0 234
f@0 235 const size_t mNumChunks;
f@0 236
f@0 237 std::vector<Chunk> mChunks;
f@0 238
f@0 239 Selection mSelection;
f@0 240
f@0 241 cinder::Color mColor;
f@0 242
f@0 243 float mFilterCoeff;
f@0 244
f@3 245 // cinder gl batch for batch drawing
f@0 246 ci::gl::BatchRef mChunkBatch;
f@0 247
f@0 248 };
f@0 249