f@0: /***************************************************/ f@0: /*! \class SineWave f@0: \brief STK sinusoid oscillator class. f@0: f@0: This class computes and saves a static sine "table" that can be f@0: shared by multiple instances. It has an interface similar to the f@0: WaveLoop class but inherits from the Generator class. Output f@0: values are computed using linear interpolation. f@0: f@0: The "table" length, set in SineWave.h, is 2048 samples by default. f@0: f@0: by Perry R. Cook and Gary P. Scavone, 1995--2014. f@0: */ f@0: /***************************************************/ f@0: f@0: #include "../include/SineWave.h" f@0: #include f@0: f@0: namespace stk { f@0: f@0: StkFrames SineWave :: table_; f@0: f@0: SineWave :: SineWave( void ) f@0: : time_(0.0), rate_(1.0), phaseOffset_(0.0) f@0: { f@0: if ( table_.empty() ) { f@0: table_.resize( TABLE_SIZE + 1, 1 ); f@0: StkFloat temp = 1.0 / TABLE_SIZE; f@0: for ( unsigned long i=0; i<=TABLE_SIZE; i++ ) f@0: table_[i] = sin( TWO_PI * i * temp ); f@0: } f@0: f@0: Stk::addSampleRateAlert( this ); f@0: } f@0: f@0: SineWave :: ~SineWave() f@0: { f@0: Stk::removeSampleRateAlert( this ); f@0: } f@0: f@0: void SineWave :: sampleRateChanged( StkFloat newRate, StkFloat oldRate ) f@0: { f@0: if ( !ignoreSampleRateChange_ ) f@0: this->setRate( oldRate * rate_ / newRate ); f@0: } f@0: f@0: void SineWave :: reset( void ) f@0: { f@0: time_ = 0.0; f@0: lastFrame_[0] = 0; f@0: } f@0: f@0: void SineWave :: setFrequency( StkFloat frequency ) f@0: { f@0: // This is a looping frequency. f@0: this->setRate( TABLE_SIZE * frequency / Stk::sampleRate() ); f@0: } f@0: f@0: void SineWave :: addTime( StkFloat time ) f@0: { f@0: // Add an absolute time in samples. f@0: time_ += time; f@0: } f@0: f@0: void SineWave :: addPhase( StkFloat phase ) f@0: { f@0: // Add a time in cycles (one cycle = TABLE_SIZE). f@0: time_ += TABLE_SIZE * phase; f@0: } f@0: f@0: void SineWave :: addPhaseOffset( StkFloat phaseOffset ) f@0: { f@0: // Add a phase offset relative to any previous offset value. f@0: time_ += ( phaseOffset - phaseOffset_ ) * TABLE_SIZE; f@0: phaseOffset_ = phaseOffset; f@0: } f@0: f@0: } // stk namespace