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.

92 lines
2.6KB

  1. /***************************************************/
  2. /*! \class BlitSaw
  3. \brief STK band-limited sawtooth wave class.
  4. This class generates a band-limited sawtooth waveform using a
  5. closed-form algorithm reported by Stilson and Smith in "Alias-Free
  6. Digital Synthesis of Classic Analog Waveforms", 1996. The user
  7. can specify both the fundamental frequency of the sawtooth and the
  8. number of harmonics contained in the resulting signal.
  9. If nHarmonics is 0, then the signal will contain all harmonics up
  10. to half the sample rate. Note, however, that this setting may
  11. produce aliasing in the signal when the frequency is changing (no
  12. automatic modification of the number of harmonics is performed by
  13. the setFrequency() function).
  14. Based on initial code of Robin Davies, 2005.
  15. Modified algorithm code by Gary Scavone, 2005.
  16. */
  17. /***************************************************/
  18. #include "BlitSaw.h"
  19. namespace stk {
  20. BlitSaw:: BlitSaw( StkFloat frequency )
  21. {
  22. if ( frequency <= 0.0 ) {
  23. oStream_ << "BlitSaw::BlitSaw: argument (" << frequency << ") must be positive!";
  24. handleError( StkError::FUNCTION_ARGUMENT );
  25. }
  26. nHarmonics_ = 0;
  27. this->reset();
  28. this->setFrequency( frequency );
  29. }
  30. BlitSaw :: ~BlitSaw()
  31. {
  32. }
  33. void BlitSaw :: reset()
  34. {
  35. phase_ = 0.0f;
  36. state_ = 0.0;
  37. lastFrame_[0] = 0.0;
  38. }
  39. void BlitSaw :: setFrequency( StkFloat frequency )
  40. {
  41. if ( frequency <= 0.0 ) {
  42. oStream_ << "BlitSaw::setFrequency: argument (" << frequency << ") must be positive!";
  43. handleError( StkError::WARNING ); return;
  44. }
  45. p_ = Stk::sampleRate() / frequency;
  46. C2_ = 1 / p_;
  47. rate_ = PI * C2_;
  48. this->updateHarmonics();
  49. }
  50. void BlitSaw :: setHarmonics( unsigned int nHarmonics )
  51. {
  52. nHarmonics_ = nHarmonics;
  53. this->updateHarmonics();
  54. // I found that the initial DC offset could be minimized with an
  55. // initial state setting as given below. This initialization should
  56. // only happen before starting the oscillator for the first time
  57. // (but after setting the frequency and number of harmonics). I
  58. // struggled a bit to decide where best to put this and finally
  59. // settled on here. In general, the user shouldn't be messing with
  60. // the number of harmonics once the oscillator is running because
  61. // this is automatically taken care of in the setFrequency()
  62. // function. (GPS - 1 October 2005)
  63. state_ = -0.5 * a_;
  64. }
  65. void BlitSaw :: updateHarmonics( void )
  66. {
  67. if ( nHarmonics_ <= 0 ) {
  68. unsigned int maxHarmonics = (unsigned int) floor( 0.5 * p_ );
  69. m_ = 2 * maxHarmonics + 1;
  70. }
  71. else
  72. m_ = 2 * nHarmonics_ + 1;
  73. a_ = m_ / p_;
  74. }
  75. } // stk namespace