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     unsigned long startPointer;
00144     unsigned int repeats;
00145     GrainState state;
00146 
00147     // Default constructor.
00148     Grain()
00149       :eScaler(0.0), eRate(0.0), attackCount(0), sustainCount(0), decayCount(0),
00150        delayCount(0), counter(0), pointer(0), startPointer(0), repeats(0), state(GRAIN_STOPPED) {}
00151   };
00152 
00153   void calculateGrain( Granulate::Grain& grain );
00154 
00155   StkFrames data_;
00156   std::vector<Grain> grains_;
00157   Noise noise;
00158   long gPointer_;
00159 
00160   // Global grain parameters.
00161   unsigned int gDuration_;
00162   unsigned int gRampPercent_;
00163   unsigned int gDelay_;
00164   unsigned int gStretch_;
00165   unsigned int stretchCounter_;
00166   int gOffset_;
00167   StkFloat gRandomFactor_;
00168   StkFloat gain_;
00169 
00170 };
00171 
00172 inline StkFloat Granulate :: lastOut( unsigned int channel )
00173 {
00174 #if defined(_STK_DEBUG_)
00175   if ( channel >= lastFrame_.channels() ) {
00176     errorString_ << "Granulate::lastOut(): channel argument is invalid!";
00177     handleError( StkError::FUNCTION_ARGUMENT );
00178   }
00179 #endif
00180 
00181   return lastFrame_[channel];
00182 }
00183 
00184 inline StkFrames& Granulate :: tick( StkFrames& frames, unsigned int channel )
00185 {
00186   unsigned int nChannels = lastFrame_.channels();
00187 #if defined(_STK_DEBUG_)
00188   if ( channel > frames.channels() - nChannels ) {
00189     errorString_ << "Granulate::tick(): channel and StkFrames arguments are incompatible!";
00190     handleError( StkError::FUNCTION_ARGUMENT );
00191   }
00192 #endif
00193 
00194   StkFloat *samples = &frames[channel];
00195   unsigned int j, hop = frames.channels() - nChannels;
00196   for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
00197     *samples++ = tick();
00198     for ( j=1; j<nChannels; j++ )
00199       *samples++ = lastFrame_[j];
00200   }
00201 
00202   return frames;
00203 }
00204 
00205 } // stk namespace
00206 
00207 #endif

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