Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


Granulate.h

00001 #ifndef STK_GRANULATE_H
00002 #define STK_GRANULATE_H
00003 
00004 #include <vector>
00005 #include "Generator.h"
00006 #include "Envelope.h"
00007 #include "Noise.h"
00008 
00009 namespace stk {
00010 
00011 /***************************************************/
00026 /***************************************************/
00027 
00028 class Granulate: public Generator
00029 {
00030  public:
00032   Granulate( void );
00033 
00035   Granulate( unsigned int nVoices, std::string fileName, bool typeRaw = false );
00036 
00038   ~Granulate( void );
00039 
00041 
00045   void openFile( std::string fileName, bool typeRaw = false );
00046 
00048 
00052   void reset( void );
00053 
00055 
00060   void setVoices( unsigned int nVoices = 1 );
00061 
00063 
00069   void setStretch( unsigned int stretchFactor = 1 );
00070 
00072 
00087   void setGrainParameters( unsigned int duration = 30, unsigned int rampPercent = 50,
00088                            int offset = 0, unsigned int delay = 0 );
00089 
00091 
00099   void setRandomFactor( StkFloat randomness = 0.1 );
00100 
00102 
00110   StkFloat lastOut( unsigned int channel = 0 );
00111 
00113   StkFloat tick( unsigned int channel = 0 );
00114 
00116 
00123   StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
00124 
00125   enum GrainState {
00126     GRAIN_STOPPED,
00127     GRAIN_FADEIN,
00128     GRAIN_SUSTAIN,
00129     GRAIN_FADEOUT
00130   };
00131 
00132  protected:
00133 
00134   struct Grain {
00135     StkFloat eScaler;
00136     StkFloat eRate;
00137     unsigned long attackCount;
00138     unsigned long sustainCount;
00139     unsigned long decayCount;
00140     unsigned long delayCount;
00141     unsigned long counter;
00142     //unsigned long pointer;
00143     StkFloat pointer;
00144     unsigned long startPointer;
00145     unsigned int repeats;
00146     GrainState state;
00147 
00148     // Default constructor.
00149     Grain()
00150       :eScaler(0.0), eRate(0.0), attackCount(0), sustainCount(0), decayCount(0),
00151        delayCount(0), counter(0), pointer(0), startPointer(0), repeats(0), state(GRAIN_STOPPED) {}
00152   };
00153 
00154   void calculateGrain( Granulate::Grain& grain );
00155 
00156   StkFrames data_;
00157   std::vector<Grain> grains_;
00158   Noise noise;
00159   //long gPointer_;
00160   StkFloat gPointer_;
00161 
00162   // Global grain parameters.
00163   unsigned int gDuration_;
00164   unsigned int gRampPercent_;
00165   unsigned int gDelay_;
00166   unsigned int gStretch_;
00167   unsigned int stretchCounter_;
00168   int gOffset_;
00169   StkFloat gRandomFactor_;
00170   StkFloat gain_;
00171 
00172 };
00173 
00174 inline StkFloat Granulate :: lastOut( unsigned int channel )
00175 {
00176 #if defined(_STK_DEBUG_)
00177   if ( channel >= lastFrame_.channels() ) {
00178     errorString_ << "Granulate::lastOut(): channel argument is invalid!";
00179     handleError( StkError::FUNCTION_ARGUMENT );
00180   }
00181 #endif
00182 
00183   return lastFrame_[channel];
00184 }
00185 
00186 inline StkFrames& Granulate :: tick( StkFrames& frames, unsigned int channel )
00187 {
00188   unsigned int nChannels = lastFrame_.channels();
00189 #if defined(_STK_DEBUG_)
00190   if ( channel > frames.channels() - nChannels ) {
00191     errorString_ << "Granulate::tick(): channel and StkFrames arguments are incompatible!";
00192     handleError( StkError::FUNCTION_ARGUMENT );
00193   }
00194 #endif
00195 
00196   StkFloat *samples = &frames[channel];
00197   unsigned int j, hop = frames.channels() - nChannels;
00198   for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
00199     *samples++ = tick();
00200     for ( j=1; j<nChannels; j++ )
00201       *samples++ = lastFrame_[j];
00202   }
00203 
00204   return frames;
00205 }
00206 
00207 } // stk namespace
00208 
00209 #endif

The Synthesis ToolKit in C++ (STK)
©1995-2010 Perry R. Cook and Gary P. Scavone. All Rights Reserved.