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.

118 lines
3.2KB

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