You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.8KB

  1. /***************************************************/
  2. /*! \class SineWave
  3. \brief STK sinusoid oscillator class.
  4. This class computes and saves a static sine "table" that can be
  5. shared by multiple instances. It has an interface similar to the
  6. WaveLoop class but inherits from the Generator class. Output
  7. values are computed using linear interpolation.
  8. The "table" length, set in SineWave.h, is 2048 samples by default.
  9. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  10. */
  11. /***************************************************/
  12. #include "SineWave.h"
  13. #include <cmath>
  14. namespace stk {
  15. StkFrames SineWave :: table_;
  16. SineWave :: SineWave( void )
  17. : time_(0.0), rate_(1.0), phaseOffset_(0.0)
  18. {
  19. if ( table_.empty() ) {
  20. table_.resize( TABLE_SIZE + 1, 1 );
  21. StkFloat temp = 1.0 / TABLE_SIZE;
  22. for ( unsigned long i=0; i<=TABLE_SIZE; i++ )
  23. table_[i] = sin( TWO_PI * i * temp );
  24. }
  25. Stk::addSampleRateAlert( this );
  26. }
  27. SineWave :: ~SineWave()
  28. {
  29. Stk::removeSampleRateAlert( this );
  30. }
  31. void SineWave :: sampleRateChanged( StkFloat newRate, StkFloat oldRate )
  32. {
  33. if ( !ignoreSampleRateChange_ )
  34. this->setRate( oldRate * rate_ / newRate );
  35. }
  36. void SineWave :: reset( void )
  37. {
  38. time_ = 0.0;
  39. lastFrame_[0] = 0;
  40. }
  41. void SineWave :: setFrequency( StkFloat frequency )
  42. {
  43. // This is a looping frequency.
  44. this->setRate( TABLE_SIZE * frequency / Stk::sampleRate() );
  45. }
  46. void SineWave :: addTime( StkFloat time )
  47. {
  48. // Add an absolute time in samples.
  49. time_ += time;
  50. }
  51. void SineWave :: addPhase( StkFloat phase )
  52. {
  53. // Add a time in cycles (one cycle = TABLE_SIZE).
  54. time_ += TABLE_SIZE * phase;
  55. }
  56. void SineWave :: addPhaseOffset( StkFloat phaseOffset )
  57. {
  58. // Add a phase offset relative to any previous offset value.
  59. time_ += ( phaseOffset - phaseOffset_ ) * TABLE_SIZE;
  60. phaseOffset_ = phaseOffset;
  61. }
  62. } // stk namespace