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.

171 lines
5.6KB

  1. #ifndef STK_BLITSQUARE_H
  2. #define STK_BLITSQUARE_H
  3. #include "Generator.h"
  4. #include <cmath>
  5. #include <limits>
  6. namespace stk {
  7. /***************************************************/
  8. /*! \class BlitSquare
  9. \brief STK band-limited square wave class.
  10. This class generates a band-limited square wave signal. It is
  11. derived in part from the approach reported by Stilson and Smith in
  12. "Alias-Free Digital Synthesis of Classic Analog Waveforms", 1996.
  13. The algorithm implemented in this class uses a SincM function with
  14. an even M value to achieve a bipolar bandlimited impulse train.
  15. This signal is then integrated to achieve a square waveform. The
  16. integration process has an associated DC offset so a DC blocking
  17. filter is applied at the output.
  18. The user can specify both the fundamental frequency of the
  19. waveform and the number of harmonics contained in the resulting
  20. signal.
  21. If nHarmonics is 0, then the signal will contain all harmonics up
  22. to half the sample rate. Note, however, that this setting may
  23. produce aliasing in the signal when the frequency is changing (no
  24. automatic modification of the number of harmonics is performed by
  25. the setFrequency() function). Also note that the harmonics of a
  26. square wave fall at odd integer multiples of the fundamental, so
  27. aliasing will happen with a lower fundamental than with the other
  28. Blit waveforms. This class is not guaranteed to be well behaved
  29. in the presence of significant aliasing.
  30. Based on initial code of Robin Davies, 2005.
  31. Modified algorithm code by Gary Scavone, 2005 - 2006.
  32. */
  33. /***************************************************/
  34. class BlitSquare: public Generator
  35. {
  36. public:
  37. //! Default constructor that initializes BLIT frequency to 220 Hz.
  38. BlitSquare( StkFloat frequency = 220.0 );
  39. //! Class destructor.
  40. ~BlitSquare();
  41. //! Resets the oscillator state and phase to 0.
  42. void reset();
  43. //! Set the phase of the signal.
  44. /*!
  45. Set the phase of the signal, in the range 0 to 1.
  46. */
  47. void setPhase( StkFloat phase ) { phase_ = PI * phase; };
  48. //! Get the current phase of the signal.
  49. /*!
  50. Get the phase of the signal, in the range [0 to 1.0).
  51. */
  52. StkFloat getPhase() const { return phase_ / PI; };
  53. //! Set the impulse train rate in terms of a frequency in Hz.
  54. void setFrequency( StkFloat frequency );
  55. //! Set the number of harmonics generated in the signal.
  56. /*!
  57. This function sets the number of harmonics contained in the
  58. resulting signal. It is equivalent to (2 * M) + 1 in the BLIT
  59. algorithm. The default value of 0 sets the algorithm for maximum
  60. harmonic content (harmonics up to half the sample rate). This
  61. parameter is not checked against the current sample rate and
  62. fundamental frequency. Thus, aliasing can result if one or more
  63. harmonics for a given fundamental frequency exceeds fs / 2. This
  64. behavior was chosen over the potentially more problematic solution
  65. of automatically modifying the M parameter, which can produce
  66. audible clicks in the signal.
  67. */
  68. void setHarmonics( unsigned int nHarmonics = 0 );
  69. //! Return the last computed output value.
  70. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  71. //! Compute and return one output sample.
  72. StkFloat tick( void );
  73. //! Fill a channel of the StkFrames object with computed outputs.
  74. /*!
  75. The \c channel argument must be less than the number of
  76. channels in the StkFrames argument (the first channel is specified
  77. by 0). However, range checking is only performed if _STK_DEBUG_
  78. is defined during compilation, in which case an out-of-range value
  79. will trigger an StkError exception.
  80. */
  81. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  82. protected:
  83. void updateHarmonics( void );
  84. unsigned int nHarmonics_;
  85. unsigned int m_;
  86. StkFloat rate_;
  87. StkFloat phase_;
  88. StkFloat p_;
  89. StkFloat a_;
  90. StkFloat lastBlitOutput_;
  91. StkFloat dcbState_;
  92. };
  93. inline StkFloat BlitSquare :: tick( void )
  94. {
  95. StkFloat temp = lastBlitOutput_;
  96. // A fully optimized version of this would replace the two sin calls
  97. // with a pair of fast sin oscillators, for which stable fast
  98. // two-multiply algorithms are well known. In the spirit of STK,
  99. // which favors clarity over performance, the optimization has
  100. // not been made here.
  101. // Avoid a divide by zero, or use of a denomralized divisor
  102. // at the sinc peak, which has a limiting value of 1.0.
  103. StkFloat denominator = sin( phase_ );
  104. if ( fabs( denominator ) < std::numeric_limits<StkFloat>::epsilon() ) {
  105. // Inexact comparison safely distinguishes betwen *close to zero*, and *close to PI*.
  106. if ( phase_ < 0.1f || phase_ > TWO_PI - 0.1f )
  107. lastBlitOutput_ = a_;
  108. else
  109. lastBlitOutput_ = -a_;
  110. }
  111. else {
  112. lastBlitOutput_ = sin( m_ * phase_ );
  113. lastBlitOutput_ /= p_ * denominator;
  114. }
  115. lastBlitOutput_ += temp;
  116. // Now apply DC blocker.
  117. lastFrame_[0] = lastBlitOutput_ - dcbState_ + 0.999 * lastFrame_[0];
  118. dcbState_ = lastBlitOutput_;
  119. phase_ += rate_;
  120. if ( phase_ >= TWO_PI ) phase_ -= TWO_PI;
  121. return lastFrame_[0];
  122. }
  123. inline StkFrames& BlitSquare :: tick( StkFrames& frames, unsigned int channel )
  124. {
  125. #if defined(_STK_DEBUG_)
  126. if ( channel >= frames.channels() ) {
  127. oStream_ << "BlitSquare::tick(): channel and StkFrames arguments are incompatible!";
  128. handleError( StkError::FUNCTION_ARGUMENT );
  129. }
  130. #endif
  131. StkFloat *samples = &frames[channel];
  132. unsigned int hop = frames.channels();
  133. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  134. *samples = BlitSquare::tick();
  135. return frames;
  136. }
  137. } // stk namespace
  138. #endif