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.

152 lines
4.6KB

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