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.

155 lines
4.2KB

  1. #ifndef STK_STIFKARP_H
  2. #define STK_STIFKARP_H
  3. #include "Instrmnt.h"
  4. #include "DelayL.h"
  5. #include "DelayA.h"
  6. #include "OneZero.h"
  7. #include "Noise.h"
  8. #include "BiQuad.h"
  9. namespace stk {
  10. /***************************************************/
  11. /*! \class StifKarp
  12. \brief STK plucked stiff string instrument.
  13. This class implements a simple plucked string
  14. algorithm (Karplus Strong) with enhancements
  15. (Jaffe-Smith, Smith, and others), including
  16. string stiffness and pluck position controls.
  17. The stiffness is modeled with allpass filters.
  18. This is a digital waveguide model, making its
  19. use possibly subject to patents held by
  20. Stanford University, Yamaha, and others.
  21. Control Change Numbers:
  22. - Pickup Position = 4
  23. - String Sustain = 11
  24. - String Stretch = 1
  25. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  26. */
  27. /***************************************************/
  28. class StifKarp : public Instrmnt
  29. {
  30. public:
  31. //! Class constructor, taking the lowest desired playing frequency.
  32. StifKarp( StkFloat lowestFrequency = 8.0 );
  33. //! Class destructor.
  34. ~StifKarp( void );
  35. //! Reset and clear all internal state.
  36. void clear( void );
  37. //! Set instrument parameters for a particular frequency.
  38. void setFrequency( StkFloat frequency );
  39. //! Set the stretch "factor" of the string (0.0 - 1.0).
  40. void setStretch( StkFloat stretch );
  41. //! Set the pluck or "excitation" position along the string (0.0 - 1.0).
  42. void setPickupPosition( StkFloat position );
  43. //! Set the base loop gain.
  44. /*!
  45. The actual loop gain is set according to the frequency.
  46. Because of high-frequency loop filter roll-off, higher
  47. frequency settings have greater loop gains.
  48. */
  49. void setBaseLoopGain( StkFloat aGain );
  50. //! Pluck the string with the given amplitude using the current frequency.
  51. void pluck( StkFloat amplitude );
  52. //! Start a note with the given frequency and amplitude.
  53. void noteOn( StkFloat frequency, StkFloat amplitude );
  54. //! Stop a note with the given amplitude (speed of decay).
  55. void noteOff( StkFloat amplitude );
  56. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  57. void controlChange( int number, StkFloat value );
  58. //! Compute and return one output sample.
  59. StkFloat tick( unsigned int channel = 0 );
  60. //! Fill a channel of the StkFrames object with computed outputs.
  61. /*!
  62. The \c channel argument must be less than the number of
  63. channels in the StkFrames argument (the first channel is specified
  64. by 0). However, range checking is only performed if _STK_DEBUG_
  65. is defined during compilation, in which case an out-of-range value
  66. will trigger an StkError exception.
  67. */
  68. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  69. protected:
  70. DelayA delayLine_;
  71. DelayL combDelay_;
  72. OneZero filter_;
  73. Noise noise_;
  74. BiQuad biquad_[4];
  75. unsigned long length_;
  76. StkFloat loopGain_;
  77. StkFloat baseLoopGain_;
  78. StkFloat lastFrequency_;
  79. StkFloat lastLength_;
  80. StkFloat stretching_;
  81. StkFloat pluckAmplitude_;
  82. StkFloat pickupPosition_;
  83. };
  84. inline StkFloat StifKarp :: tick( unsigned int )
  85. {
  86. StkFloat temp = delayLine_.lastOut() * loopGain_;
  87. // Calculate allpass stretching.
  88. for (int i=0; i<4; i++)
  89. temp = biquad_[i].tick(temp);
  90. // Moving average filter.
  91. temp = filter_.tick(temp);
  92. lastFrame_[0] = delayLine_.tick(temp);
  93. lastFrame_[0] = lastFrame_[0] - combDelay_.tick( lastFrame_[0] );
  94. return lastFrame_[0];
  95. }
  96. inline StkFrames& StifKarp :: tick( StkFrames& frames, unsigned int channel )
  97. {
  98. unsigned int nChannels = lastFrame_.channels();
  99. #if defined(_STK_DEBUG_)
  100. if ( channel > frames.channels() - nChannels ) {
  101. oStream_ << "StifKarp::tick(): channel and StkFrames arguments are incompatible!";
  102. handleError( StkError::FUNCTION_ARGUMENT );
  103. }
  104. #endif
  105. StkFloat *samples = &frames[channel];
  106. unsigned int j, hop = frames.channels() - nChannels;
  107. if ( nChannels == 1 ) {
  108. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  109. *samples++ = tick();
  110. }
  111. else {
  112. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  113. *samples++ = tick();
  114. for ( j=1; j<nChannels; j++ )
  115. *samples++ = lastFrame_[j];
  116. }
  117. }
  118. return frames;
  119. }
  120. } // stk namespace
  121. #endif