Audio plugin host https://kx.studio/carla
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.

225 lines
6.5KB

  1. /***************************************************/
  2. /*! \class BlowHole
  3. \brief STK clarinet physical model with one
  4. register hole and one tonehole.
  5. This class is based on the clarinet model,
  6. with the addition of a two-port register hole
  7. and a three-port dynamic tonehole
  8. implementation, as discussed by Scavone and
  9. Cook (1998).
  10. In this implementation, the distances between
  11. the reed/register hole and tonehole/bell are
  12. fixed. As a result, both the tonehole and
  13. register hole will have variable influence on
  14. the playing frequency, which is dependent on
  15. the length of the air column. In addition,
  16. the highest playing freqeuency is limited by
  17. these fixed lengths.
  18. This is a digital waveguide model, making its
  19. use possibly subject to patents held by Stanford
  20. University, Yamaha, and others.
  21. Control Change Numbers:
  22. - Reed Stiffness = 2
  23. - Noise Gain = 4
  24. - Tonehole State = 11
  25. - Register State = 1
  26. - Breath Pressure = 128
  27. by Perry R. Cook and Gary P. Scavone, 1995-2011.
  28. */
  29. /***************************************************/
  30. #include "BlowHole.h"
  31. #include "SKINI.msg"
  32. #include <cmath>
  33. namespace stk {
  34. BlowHole :: BlowHole( StkFloat lowestFrequency )
  35. {
  36. if ( lowestFrequency <= 0.0 ) {
  37. oStream_ << "BlowHole::BlowHole: argument is less than or equal to zero!";
  38. handleError( StkError::FUNCTION_ARGUMENT );
  39. }
  40. unsigned long nDelays = (unsigned long) ( 0.5 * Stk::sampleRate() / lowestFrequency );
  41. // delays[0] is the delay line between the reed and the register vent.
  42. delays_[0].setDelay( 5.0 * Stk::sampleRate() / 22050.0 );
  43. // delays[1] is the delay line between the register vent and the tonehole.
  44. delays_[1].setMaximumDelay( nDelays + 1 );
  45. // delays[2] is the delay line between the tonehole and the end of the bore.
  46. delays_[2].setDelay( 4.0 * Stk::sampleRate() / 22050.0 );
  47. reedTable_.setOffset( 0.7 );
  48. reedTable_.setSlope( -0.3 );
  49. // Calculate the initial tonehole three-port scattering coefficient
  50. StkFloat rb = 0.0075; // main bore radius
  51. StkFloat rth = 0.003; // tonehole radius
  52. scatter_ = -pow(rth,2) / ( pow(rth,2) + 2*pow(rb,2) );
  53. // Calculate tonehole coefficients and set for initially open.
  54. StkFloat te = 1.4 * rth; // effective length of the open hole
  55. thCoeff_ = (te*2*Stk::sampleRate() - 347.23) / (te*2*Stk::sampleRate() + 347.23);
  56. tonehole_.setA1( -thCoeff_ );
  57. tonehole_.setB0( thCoeff_ );
  58. tonehole_.setB1( -1.0 );
  59. // Calculate register hole filter coefficients
  60. double r_rh = 0.0015; // register vent radius
  61. te = 1.4 * r_rh; // effective length of the open hole
  62. double xi = 0.0; // series resistance term
  63. double zeta = 347.23 + 2*PI*pow(rb,2)*xi/1.1769;
  64. double psi = 2*PI*pow(rb,2)*te / (PI*pow(r_rh,2));
  65. StkFloat rhCoeff = (zeta - 2 * Stk::sampleRate() * psi) / (zeta + 2 * Stk::sampleRate() * psi);
  66. rhGain_ = -347.23 / (zeta + 2 * Stk::sampleRate() * psi);
  67. vent_.setA1( rhCoeff );
  68. vent_.setB0( 1.0 );
  69. vent_.setB1( 1.0 );
  70. // Start with register vent closed
  71. vent_.setGain( 0.0 );
  72. vibrato_.setFrequency((StkFloat) 5.735);
  73. outputGain_ = 1.0;
  74. noiseGain_ = 0.2;
  75. vibratoGain_ = 0.01;
  76. this->setFrequency( 220.0 );
  77. this->clear();
  78. }
  79. BlowHole :: ~BlowHole( void )
  80. {
  81. }
  82. void BlowHole :: clear( void )
  83. {
  84. delays_[0].clear();
  85. delays_[1].clear();
  86. delays_[2].clear();
  87. filter_.tick( 0.0 );
  88. tonehole_.tick( 0.0 );
  89. vent_.tick( 0.0 );
  90. }
  91. void BlowHole :: setFrequency( StkFloat frequency )
  92. {
  93. #if defined(_STK_DEBUG_)
  94. if ( frequency <= 0.0 ) {
  95. oStream_ << "BlowHole::setFrequency: argument is less than or equal to zero!";
  96. handleError( StkError::WARNING ); return;
  97. }
  98. #endif
  99. // Account for approximate filter delays and one sample "lastOut" delay.
  100. StkFloat delay = ( Stk::sampleRate() / frequency ) * 0.5 - 3.5;
  101. delay -= delays_[0].getDelay() + delays_[2].getDelay();
  102. delays_[1].setDelay( delay );
  103. }
  104. void BlowHole :: setVent( StkFloat newValue )
  105. {
  106. // This method allows setting of the register vent "open-ness" at
  107. // any point between "Open" (newValue = 1) and "Closed"
  108. // (newValue = 0).
  109. StkFloat gain;
  110. if ( newValue <= 0.0 )
  111. gain = 0.0;
  112. else if ( newValue >= 1.0 )
  113. gain = rhGain_;
  114. else
  115. gain = newValue * rhGain_;
  116. vent_.setGain( gain );
  117. }
  118. void BlowHole :: setTonehole( StkFloat newValue )
  119. {
  120. // This method allows setting of the tonehole "open-ness" at
  121. // any point between "Open" (newValue = 1) and "Closed"
  122. // (newValue = 0).
  123. StkFloat new_coeff;
  124. if ( newValue <= 0.0 )
  125. new_coeff = 0.9995;
  126. else if ( newValue >= 1.0 )
  127. new_coeff = thCoeff_;
  128. else
  129. new_coeff = ( newValue * (thCoeff_ - 0.9995) ) + 0.9995;
  130. tonehole_.setA1( -new_coeff );
  131. tonehole_.setB0( new_coeff );
  132. }
  133. void BlowHole :: startBlowing( StkFloat amplitude, StkFloat rate )
  134. {
  135. if ( amplitude <= 0.0 || rate <= 0.0 ) {
  136. oStream_ << "BlowHole::startBlowing: one or more arguments is less than or equal to zero!";
  137. handleError( StkError::WARNING ); return;
  138. }
  139. envelope_.setRate( rate );
  140. envelope_.setTarget( amplitude );
  141. }
  142. void BlowHole :: stopBlowing( StkFloat rate )
  143. {
  144. if ( rate <= 0.0 ) {
  145. oStream_ << "BlowHole::stopBlowing: argument is less than or equal to zero!";
  146. handleError( StkError::WARNING ); return;
  147. }
  148. envelope_.setRate( rate );
  149. envelope_.setTarget( 0.0 );
  150. }
  151. void BlowHole :: noteOn( StkFloat frequency, StkFloat amplitude )
  152. {
  153. this->setFrequency( frequency );
  154. this->startBlowing( 0.55 + (amplitude * 0.30), amplitude * 0.005 );
  155. outputGain_ = amplitude + 0.001;
  156. }
  157. void BlowHole :: noteOff( StkFloat amplitude )
  158. {
  159. this->stopBlowing( amplitude * 0.01 );
  160. }
  161. void BlowHole :: controlChange( int number, StkFloat value )
  162. {
  163. #if defined(_STK_DEBUG_)
  164. if ( Stk::inRange( value, 0.0, 128.0 ) == false ) {
  165. oStream_ << "BlowHole::controlChange: value (" << value << ") is out of range!";
  166. handleError( StkError::WARNING ); return;
  167. }
  168. #endif
  169. StkFloat normalizedValue = value * ONE_OVER_128;
  170. if (number == __SK_ReedStiffness_) // 2
  171. reedTable_.setSlope( -0.44 + (0.26 * normalizedValue) );
  172. else if (number == __SK_NoiseLevel_) // 4
  173. noiseGain_ = ( normalizedValue * 0.4);
  174. else if (number == __SK_ModFrequency_) // 11
  175. this->setTonehole( normalizedValue );
  176. else if (number == __SK_ModWheel_) // 1
  177. this->setVent( normalizedValue );
  178. else if (number == __SK_AfterTouch_Cont_) // 128
  179. envelope_.setValue( normalizedValue );
  180. #if defined(_STK_DEBUG_)
  181. else {
  182. oStream_ << "BlowHole::controlChange: undefined control number (" << number << ")!";
  183. handleError( StkError::WARNING );
  184. }
  185. #endif
  186. }
  187. } // stk namespace