Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial
00001 #ifndef STK_SINEWAVE_H 00002 #define STK_SINEWAVE_H 00003 00004 const unsigned long TABLE_SIZE = 2048; 00005 00006 #include "Generator.h" 00007 00008 namespace stk { 00009 00010 /***************************************************/ 00023 /***************************************************/ 00024 00025 class SineWave : public Generator 00026 { 00027 public: 00029 SineWave( void ); 00030 00032 ~SineWave( void ); 00033 00035 void reset( void ); 00036 00038 00041 void setRate( StkFloat rate ) { rate_ = rate; }; 00042 00044 00050 void setFrequency( StkFloat frequency ); 00051 00053 void addTime( StkFloat time ); 00054 00056 00061 void addPhase( StkFloat phase ); 00062 00064 00068 void addPhaseOffset( StkFloat phaseOffset ); 00069 00071 StkFloat lastOut( void ) const { return lastFrame_[0]; }; 00072 00074 StkFloat tick( void ); 00075 00077 00084 StkFrames& tick( StkFrames& frames, unsigned int channel = 0 ); 00085 00086 protected: 00087 00088 void sampleRateChanged( StkFloat newRate, StkFloat oldRate ); 00089 00090 static StkFrames table_; 00091 StkFloat time_; 00092 StkFloat rate_; 00093 StkFloat phaseOffset_; 00094 unsigned int iIndex_; 00095 StkFloat alpha_; 00096 00097 }; 00098 00099 inline StkFloat SineWave :: tick( void ) 00100 { 00101 // Check limits of time address ... if necessary, recalculate modulo 00102 // TABLE_SIZE. 00103 while ( time_ < 0.0 ) 00104 time_ += TABLE_SIZE; 00105 while ( time_ >= TABLE_SIZE ) 00106 time_ -= TABLE_SIZE; 00107 00108 iIndex_ = (unsigned int) time_; 00109 alpha_ = time_ - iIndex_; 00110 StkFloat tmp = table_[ iIndex_ ]; 00111 tmp += ( alpha_ * ( table_[ iIndex_ + 1 ] - tmp ) ); 00112 00113 // Increment time, which can be negative. 00114 time_ += rate_; 00115 00116 lastFrame_[0] = tmp; 00117 return lastFrame_[0]; 00118 } 00119 00120 inline StkFrames& SineWave :: tick( StkFrames& frames, unsigned int channel ) 00121 { 00122 #if defined(_STK_DEBUG_) 00123 if ( channel >= frames.channels() ) { 00124 errorString_ << "SineWave::tick(): channel and StkFrames arguments are incompatible!"; 00125 handleError( StkError::FUNCTION_ARGUMENT ); 00126 } 00127 #endif 00128 00129 StkFloat *samples = &frames[channel]; 00130 StkFloat tmp = 0.0; 00131 00132 unsigned int hop = frames.channels(); 00133 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) { 00134 00135 // Check limits of time address ... if necessary, recalculate modulo 00136 // TABLE_SIZE. 00137 while ( time_ < 0.0 ) 00138 time_ += TABLE_SIZE; 00139 while ( time_ >= TABLE_SIZE ) 00140 time_ -= TABLE_SIZE; 00141 00142 iIndex_ = (unsigned int) time_; 00143 alpha_ = time_ - iIndex_; 00144 tmp = table_[ iIndex_ ]; 00145 tmp += ( alpha_ * ( table_[ iIndex_ + 1 ] - tmp ) ); 00146 *samples = tmp; 00147 00148 // Increment time, which can be negative. 00149 time_ += rate_; 00150 } 00151 00152 lastFrame_[0] = tmp; 00153 return frames; 00154 } 00155 00156 } // stk namespace 00157 00158 #endif 00159
The Synthesis ToolKit in C++ (STK) |
©1995-2010 Perry R. Cook and Gary P. Scavone. All Rights Reserved. |