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