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
2.0KB

  1. /***************************************************/
  2. /*! \class Blit
  3. \brief STK band-limited impulse train class.
  4. This class generates a band-limited impulse train 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 impulse train
  8. and the number of harmonics contained in the resulting signal.
  9. The signal is normalized so that the peak value is +/-1.0.
  10. If nHarmonics is 0, then the signal will contain all harmonics up
  11. to half the sample rate. Note, however, that this setting may
  12. produce aliasing in the signal when the frequency is changing (no
  13. automatic modification of the number of harmonics is performed by
  14. the setFrequency() function).
  15. Original code by Robin Davies, 2005.
  16. Revisions by Gary Scavone for STK, 2005.
  17. */
  18. /***************************************************/
  19. #include "Blit.h"
  20. namespace stk {
  21. Blit:: Blit( StkFloat frequency )
  22. {
  23. if ( frequency <= 0.0 ) {
  24. oStream_ << "Blit::Blit: argument (" << frequency << ") must be positive!";
  25. handleError( StkError::FUNCTION_ARGUMENT );
  26. }
  27. nHarmonics_ = 0;
  28. this->setFrequency( frequency );
  29. this->reset();
  30. }
  31. Blit :: ~Blit()
  32. {
  33. }
  34. void Blit :: reset()
  35. {
  36. phase_ = 0.0;
  37. lastFrame_[0] = 0.0;
  38. }
  39. void Blit :: setFrequency( StkFloat frequency )
  40. {
  41. if ( frequency <= 0.0 ) {
  42. oStream_ << "Blit::setFrequency: argument (" << frequency << ") must be positive!";
  43. handleError( StkError::WARNING ); return;
  44. }
  45. p_ = Stk::sampleRate() / frequency;
  46. rate_ = PI / p_;
  47. this->updateHarmonics();
  48. }
  49. void Blit :: setHarmonics( unsigned int nHarmonics )
  50. {
  51. nHarmonics_ = nHarmonics;
  52. this->updateHarmonics();
  53. }
  54. void Blit :: updateHarmonics( void )
  55. {
  56. if ( nHarmonics_ <= 0 ) {
  57. unsigned int maxHarmonics = (unsigned int) floor( 0.5 * p_ );
  58. m_ = 2 * maxHarmonics + 1;
  59. }
  60. else
  61. m_ = 2 * nHarmonics_ + 1;
  62. }
  63. } // stk namespace