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.

130 lines
3.4KB

  1. #ifndef STK_SITAR_H
  2. #define STK_SITAR_H
  3. #include "Instrmnt.h"
  4. #include "DelayA.h"
  5. #include "OneZero.h"
  6. #include "Noise.h"
  7. #include "ADSR.h"
  8. #include <cmath>
  9. namespace stk {
  10. /***************************************************/
  11. /*! \class Sitar
  12. \brief STK sitar string model class.
  13. This class implements a sitar plucked string
  14. physical model based on the Karplus-Strong
  15. algorithm.
  16. This is a digital waveguide model, making its
  17. use possibly subject to patents held by
  18. Stanford University, Yamaha, and others.
  19. There exist at least two patents, assigned to
  20. Stanford, bearing the names of Karplus and/or
  21. Strong.
  22. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  23. */
  24. /***************************************************/
  25. class Sitar : public Instrmnt
  26. {
  27. public:
  28. //! Class constructor, taking the lowest desired playing frequency.
  29. Sitar( StkFloat lowestFrequency = 8.0 );
  30. //! Class destructor.
  31. ~Sitar( void );
  32. //! Reset and clear all internal state.
  33. void clear( void );
  34. //! Set instrument parameters for a particular frequency.
  35. void setFrequency( StkFloat frequency );
  36. //! Pluck the string with the given amplitude using the current frequency.
  37. void pluck( StkFloat amplitude );
  38. //! Start a note with the given frequency and amplitude.
  39. void noteOn( StkFloat frequency, StkFloat amplitude );
  40. //! Stop a note with the given amplitude (speed of decay).
  41. void noteOff( StkFloat amplitude );
  42. //! Compute and return one output sample.
  43. StkFloat tick( unsigned int channel = 0 );
  44. //! Fill a channel of the StkFrames object with computed outputs.
  45. /*!
  46. The \c channel argument must be less than the number of
  47. channels in the StkFrames argument (the first channel is specified
  48. by 0). However, range checking is only performed if _STK_DEBUG_
  49. is defined during compilation, in which case an out-of-range value
  50. will trigger an StkError exception.
  51. */
  52. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  53. protected:
  54. DelayA delayLine_;
  55. OneZero loopFilter_;
  56. Noise noise_;
  57. ADSR envelope_;
  58. StkFloat loopGain_;
  59. StkFloat amGain_;
  60. StkFloat delay_;
  61. StkFloat targetDelay_;
  62. };
  63. inline StkFloat Sitar :: tick( unsigned int )
  64. {
  65. if ( fabs(targetDelay_ - delay_) > 0.001 ) {
  66. if ( targetDelay_ < delay_ )
  67. delay_ *= 0.99999;
  68. else
  69. delay_ *= 1.00001;
  70. delayLine_.setDelay( delay_ );
  71. }
  72. lastFrame_[0] = delayLine_.tick( loopFilter_.tick( delayLine_.lastOut() * loopGain_ ) +
  73. (amGain_ * envelope_.tick() * noise_.tick()));
  74. return lastFrame_[0];
  75. }
  76. inline StkFrames& Sitar :: tick( StkFrames& frames, unsigned int channel )
  77. {
  78. unsigned int nChannels = lastFrame_.channels();
  79. #if defined(_STK_DEBUG_)
  80. if ( channel > frames.channels() - nChannels ) {
  81. oStream_ << "Sitar::tick(): channel and StkFrames arguments are incompatible!";
  82. handleError( StkError::FUNCTION_ARGUMENT );
  83. }
  84. #endif
  85. StkFloat *samples = &frames[channel];
  86. unsigned int j, hop = frames.channels() - nChannels;
  87. if ( nChannels == 1 ) {
  88. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  89. *samples++ = tick();
  90. }
  91. else {
  92. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  93. *samples++ = tick();
  94. for ( j=1; j<nChannels; j++ )
  95. *samples++ = lastFrame_[j];
  96. }
  97. }
  98. return frames;
  99. }
  100. } // stk namespace
  101. #endif