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.

181 lines
5.2KB

  1. /***************************************************/
  2. /*! \class Bowed
  3. \brief STK bowed string instrument class.
  4. This class implements a bowed string model, a
  5. la Smith (1986), after McIntyre, Schumacher,
  6. Woodhouse (1983).
  7. This is a digital waveguide model, making its
  8. use possibly subject to patents held by
  9. Stanford University, Yamaha, and others.
  10. Control Change Numbers:
  11. - Bow Pressure = 2
  12. - Bow Position = 4
  13. - Vibrato Frequency = 11
  14. - Vibrato Gain = 1
  15. - Bow Velocity = 100
  16. - Frequency = 101
  17. - Volume = 128
  18. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  19. Contributions by Esteban Maestre, 2011.
  20. */
  21. /***************************************************/
  22. #include "Bowed.h"
  23. #include "SKINImsg.h"
  24. namespace stk {
  25. Bowed :: Bowed( StkFloat lowestFrequency )
  26. {
  27. if ( lowestFrequency <= 0.0 ) {
  28. oStream_ << "Bowed::Bowed: argument is less than or equal to zero!";
  29. handleError( StkError::FUNCTION_ARGUMENT );
  30. }
  31. unsigned long nDelays = (unsigned long) ( Stk::sampleRate() / lowestFrequency );
  32. neckDelay_.setMaximumDelay( nDelays + 1 );
  33. neckDelay_.setDelay( 100.0 );
  34. bridgeDelay_.setMaximumDelay( nDelays + 1 );
  35. bridgeDelay_.setDelay( 29.0 );
  36. bowTable_.setSlope( 3.0 );
  37. bowTable_.setOffset( 0.001);
  38. bowDown_ = false;
  39. maxVelocity_ = 0.25;
  40. vibrato_.setFrequency( 6.12723 );
  41. vibratoGain_ = 0.0;
  42. stringFilter_.setPole( 0.75 - (0.2 * 22050.0 / Stk::sampleRate()) );
  43. stringFilter_.setGain( 0.95 );
  44. // Old single body filter
  45. //bodyFilter_.setResonance( 500.0, 0.85, true );
  46. //bodyFilter_.setGain( 0.2 );
  47. // New body filter provided by Esteban Maestre (cascade of second-order sections)
  48. bodyFilters_[0].setCoefficients( 1.0, 1.5667, 0.3133, -0.5509, -0.3925 );
  49. bodyFilters_[1].setCoefficients( 1.0, -1.9537, 0.9542, -1.6357, 0.8697 );
  50. bodyFilters_[2].setCoefficients( 1.0, -1.6683, 0.8852, -1.7674, 0.8735 );
  51. bodyFilters_[3].setCoefficients( 1.0, -1.8585, 0.9653, -1.8498, 0.9516 );
  52. bodyFilters_[4].setCoefficients( 1.0, -1.9299, 0.9621, -1.9354, 0.9590 );
  53. bodyFilters_[5].setCoefficients( 1.0, -1.9800, 0.9888, -1.9867, 0.9923 );
  54. adsr_.setAllTimes( 0.02, 0.005, 0.9, 0.01 );
  55. betaRatio_ = 0.127236;
  56. // Necessary to initialize internal variables.
  57. this->setFrequency( 220.0 );
  58. this->clear();
  59. }
  60. Bowed :: ~Bowed( void )
  61. {
  62. }
  63. void Bowed :: clear( void )
  64. {
  65. neckDelay_.clear();
  66. bridgeDelay_.clear();
  67. stringFilter_.clear();
  68. for ( int i=0; i<6; i++ ) bodyFilters_[i].clear();
  69. }
  70. void Bowed :: setFrequency( StkFloat frequency )
  71. {
  72. #if defined(_STK_DEBUG_)
  73. if ( frequency <= 0.0 ) {
  74. oStream_ << "Bowed::setFrequency: argument is less than or equal to zero!";
  75. handleError( StkError::WARNING ); return;
  76. }
  77. #endif
  78. // Delay = length - approximate filter delay.
  79. baseDelay_ = Stk::sampleRate() / frequency - 4.0;
  80. if ( baseDelay_ <= 0.0 ) baseDelay_ = 0.3;
  81. bridgeDelay_.setDelay( baseDelay_ * betaRatio_ ); // bow to bridge length
  82. neckDelay_.setDelay( baseDelay_ * (1.0 - betaRatio_) ); // bow to nut (finger) length
  83. }
  84. void Bowed :: startBowing( StkFloat amplitude, StkFloat rate )
  85. {
  86. if ( amplitude <= 0.0 || rate <= 0.0 ) {
  87. oStream_ << "Bowed::startBowing: one or more arguments is less than or equal to zero!";
  88. handleError( StkError::WARNING ); return;
  89. }
  90. adsr_.setAttackRate( rate );
  91. adsr_.keyOn();
  92. maxVelocity_ = 0.03 + ( 0.2 * amplitude );
  93. bowDown_ = true;
  94. }
  95. void Bowed :: stopBowing( StkFloat rate )
  96. {
  97. if ( rate <= 0.0 ) {
  98. oStream_ << "Bowed::stopBowing: argument is less than or equal to zero!";
  99. handleError( StkError::WARNING ); return;
  100. }
  101. adsr_.setReleaseRate( rate );
  102. adsr_.keyOff();
  103. }
  104. void Bowed :: noteOn( StkFloat frequency, StkFloat amplitude )
  105. {
  106. this->startBowing( amplitude, amplitude * 0.001 );
  107. this->setFrequency( frequency );
  108. }
  109. void Bowed :: noteOff( StkFloat amplitude )
  110. {
  111. this->stopBowing( (1.0 - amplitude) * 0.005 );
  112. }
  113. void Bowed :: controlChange( int number, StkFloat value )
  114. {
  115. #if defined(_STK_DEBUG_)
  116. if ( value < 0 || ( number != 101 && value > 128.0 ) ) {
  117. oStream_ << "Bowed::controlChange: value (" << value << ") is out of range!";
  118. handleError( StkError::WARNING ); return;
  119. }
  120. #endif
  121. StkFloat normalizedValue = value * ONE_OVER_128;
  122. if ( number == __SK_BowPressure_ ) { // 2
  123. if ( normalizedValue > 0.0 ) bowDown_ = true;
  124. else bowDown_ = false;
  125. bowTable_.setSlope( 5.0 - (4.0 * normalizedValue) );
  126. }
  127. else if ( number == __SK_BowPosition_ ) { // 4
  128. betaRatio_ = normalizedValue;
  129. bridgeDelay_.setDelay( baseDelay_ * betaRatio_ );
  130. neckDelay_.setDelay( baseDelay_ * (1.0 - betaRatio_) );
  131. }
  132. else if ( number == __SK_ModFrequency_ ) // 11
  133. vibrato_.setFrequency( normalizedValue * 12.0 );
  134. else if ( number == __SK_ModWheel_ ) // 1
  135. vibratoGain_ = ( normalizedValue * 0.4 );
  136. else if ( number == 100 ) // 100: set instantaneous bow velocity
  137. adsr_.setTarget( normalizedValue );
  138. else if ( number == 101 ) // 101: set instantaneous value of frequency
  139. this->setFrequency( value );
  140. else if (number == __SK_AfterTouch_Cont_) // 128
  141. adsr_.setTarget( normalizedValue );
  142. #if defined(_STK_DEBUG_)
  143. else {
  144. oStream_ << "Bowed::controlChange: undefined control number (" << number << ")!";
  145. handleError( StkError::WARNING );
  146. }
  147. #endif
  148. }
  149. } // stk namespace