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.

149 lines
4.7KB

  1. #ifndef STK_BLITSAW_H
  2. #define STK_BLITSAW_H
  3. #include "Generator.h"
  4. #include <cmath>
  5. #include <limits>
  6. namespace stk {
  7. /***************************************************/
  8. /*! \class BlitSaw
  9. \brief STK band-limited sawtooth wave class.
  10. This class generates a band-limited sawtooth waveform using a
  11. closed-form algorithm reported by Stilson and Smith in "Alias-Free
  12. Digital Synthesis of Classic Analog Waveforms", 1996. The user
  13. can specify both the fundamental frequency of the sawtooth and the
  14. number of harmonics contained in the resulting signal.
  15. If nHarmonics is 0, then the signal will contain all harmonics up
  16. to half the sample rate. Note, however, that this setting may
  17. produce aliasing in the signal when the frequency is changing (no
  18. automatic modification of the number of harmonics is performed by
  19. the setFrequency() function).
  20. Based on initial code of Robin Davies, 2005.
  21. Modified algorithm code by Gary Scavone, 2005.
  22. */
  23. /***************************************************/
  24. class BlitSaw: public Generator
  25. {
  26. public:
  27. //! Class constructor.
  28. BlitSaw( StkFloat frequency = 220.0 );
  29. //! Class destructor.
  30. ~BlitSaw();
  31. //! Resets the oscillator state and phase to 0.
  32. void reset();
  33. //! Set the sawtooth oscillator rate in terms of a frequency in Hz.
  34. void setFrequency( StkFloat frequency );
  35. //! Set the number of harmonics generated in the signal.
  36. /*!
  37. This function sets the number of harmonics contained in the
  38. resulting signal. It is equivalent to (2 * M) + 1 in the BLIT
  39. algorithm. The default value of 0 sets the algorithm for maximum
  40. harmonic content (harmonics up to half the sample rate). This
  41. parameter is not checked against the current sample rate and
  42. fundamental frequency. Thus, aliasing can result if one or more
  43. harmonics for a given fundamental frequency exceeds fs / 2. This
  44. behavior was chosen over the potentially more problematic solution
  45. of automatically modifying the M parameter, which can produce
  46. audible clicks in the signal.
  47. */
  48. void setHarmonics( unsigned int nHarmonics = 0 );
  49. //! Return the last computed output value.
  50. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  51. //! Compute and return one output sample.
  52. StkFloat tick( void );
  53. //! Fill a channel of the StkFrames object with computed outputs.
  54. /*!
  55. The \c channel argument must be less than the number of
  56. channels in the StkFrames argument (the first channel is specified
  57. by 0). However, range checking is only performed if _STK_DEBUG_
  58. is defined during compilation, in which case an out-of-range value
  59. will trigger an StkError exception.
  60. */
  61. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  62. protected:
  63. void updateHarmonics( void );
  64. unsigned int nHarmonics_;
  65. unsigned int m_;
  66. StkFloat rate_;
  67. StkFloat phase_;
  68. StkFloat p_;
  69. StkFloat C2_;
  70. StkFloat a_;
  71. StkFloat state_;
  72. };
  73. inline StkFloat BlitSaw :: tick( void )
  74. {
  75. // The code below implements the BLIT algorithm of Stilson and
  76. // Smith, followed by a summation and filtering operation to produce
  77. // a sawtooth waveform. After experimenting with various approaches
  78. // to calculate the average value of the BLIT over one period, I
  79. // found that an estimate of C2_ = 1.0 / period (in samples) worked
  80. // most consistently. A "leaky integrator" is then applied to the
  81. // difference of the BLIT output and C2_. (GPS - 1 October 2005)
  82. // A fully optimized version of this code would replace the two sin
  83. // calls with a pair of fast sin oscillators, for which stable fast
  84. // two-multiply algorithms are well known. In the spirit of STK,
  85. // which favors clarity over performance, the optimization has
  86. // not been made here.
  87. // Avoid a divide by zero, or use of a denormalized divisor
  88. // at the sinc peak, which has a limiting value of m_ / p_.
  89. StkFloat tmp, denominator = sin( phase_ );
  90. if ( fabs(denominator) <= std::numeric_limits<StkFloat>::epsilon() )
  91. tmp = a_;
  92. else {
  93. tmp = sin( m_ * phase_ );
  94. tmp /= p_ * denominator;
  95. }
  96. tmp += state_ - C2_;
  97. state_ = tmp * 0.995;
  98. phase_ += rate_;
  99. if ( phase_ >= PI ) phase_ -= PI;
  100. lastFrame_[0] = tmp;
  101. return lastFrame_[0];
  102. }
  103. inline StkFrames& BlitSaw :: tick( StkFrames& frames, unsigned int channel )
  104. {
  105. #if defined(_STK_DEBUG_)
  106. if ( channel >= frames.channels() ) {
  107. oStream_ << "BlitSaw::tick(): channel and StkFrames arguments are incompatible!";
  108. handleError( StkError::FUNCTION_ARGUMENT );
  109. }
  110. #endif
  111. StkFloat *samples = &frames[channel];
  112. unsigned int hop = frames.channels();
  113. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  114. *samples = BlitSaw::tick();
  115. return frames;
  116. }
  117. } // stk namespace
  118. #endif