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.

158 lines
4.6KB

  1. #ifndef STK_BOWED_H
  2. #define STK_BOWED_H
  3. #include "Instrmnt.h"
  4. #include "DelayL.h"
  5. #include "BowTable.h"
  6. #include "OnePole.h"
  7. #include "BiQuad.h"
  8. #include "SineWave.h"
  9. #include "ADSR.h"
  10. namespace stk {
  11. /***************************************************/
  12. /*! \class Bowed
  13. \brief STK bowed string instrument class.
  14. This class implements a bowed string model, a
  15. la Smith (1986), after McIntyre, Schumacher,
  16. Woodhouse (1983).
  17. This is a digital waveguide model, making its
  18. use possibly subject to patents held by
  19. Stanford University, Yamaha, and others.
  20. Control Change Numbers:
  21. - Bow Pressure = 2
  22. - Bow Position = 4
  23. - Vibrato Frequency = 11
  24. - Vibrato Gain = 1
  25. - Bow Velocity = 100
  26. - Frequency = 101
  27. - Volume = 128
  28. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  29. Contributions by Esteban Maestre, 2011.
  30. */
  31. /***************************************************/
  32. class Bowed : public Instrmnt
  33. {
  34. public:
  35. //! Class constructor, taking the lowest desired playing frequency.
  36. Bowed( StkFloat lowestFrequency = 8.0 );
  37. //! Class destructor.
  38. ~Bowed( void );
  39. //! Reset and clear all internal state.
  40. void clear( void );
  41. //! Set instrument parameters for a particular frequency.
  42. void setFrequency( StkFloat frequency );
  43. //! Set vibrato gain.
  44. void setVibrato( StkFloat gain ) { vibratoGain_ = gain; };
  45. //! Apply breath pressure to instrument with given amplitude and rate of increase.
  46. void startBowing( StkFloat amplitude, StkFloat rate );
  47. //! Decrease breath pressure with given rate of decrease.
  48. void stopBowing( StkFloat rate );
  49. //! Start a note with the given frequency and amplitude.
  50. void noteOn( StkFloat frequency, StkFloat amplitude );
  51. //! Stop a note with the given amplitude (speed of decay).
  52. void noteOff( StkFloat amplitude );
  53. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  54. void controlChange( int number, StkFloat value );
  55. //! Compute and return one output sample.
  56. StkFloat tick( unsigned int channel = 0 );
  57. //! Fill a channel of the StkFrames object with computed outputs.
  58. /*!
  59. The \c channel argument must be less than the number of
  60. channels in the StkFrames argument (the first channel is specified
  61. by 0). However, range checking is only performed if _STK_DEBUG_
  62. is defined during compilation, in which case an out-of-range value
  63. will trigger an StkError exception.
  64. */
  65. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  66. protected:
  67. DelayL neckDelay_;
  68. DelayL bridgeDelay_;
  69. BowTable bowTable_;
  70. OnePole stringFilter_;
  71. BiQuad bodyFilters_[6];
  72. SineWave vibrato_;
  73. ADSR adsr_;
  74. bool bowDown_;
  75. StkFloat maxVelocity_;
  76. StkFloat baseDelay_;
  77. StkFloat vibratoGain_;
  78. StkFloat betaRatio_;
  79. };
  80. inline StkFloat Bowed :: tick( unsigned int )
  81. {
  82. StkFloat bowVelocity = maxVelocity_ * adsr_.tick();
  83. StkFloat bridgeReflection = -stringFilter_.tick( bridgeDelay_.lastOut() );
  84. StkFloat nutReflection = -neckDelay_.lastOut();
  85. StkFloat stringVelocity = bridgeReflection + nutReflection;
  86. StkFloat deltaV = bowVelocity - stringVelocity; // Differential velocity
  87. StkFloat newVelocity = 0.0;
  88. if ( bowDown_ )
  89. newVelocity = deltaV * bowTable_.tick( deltaV ); // Non-Linear bow function
  90. neckDelay_.tick( bridgeReflection + newVelocity); // Do string propagations
  91. bridgeDelay_.tick(nutReflection + newVelocity);
  92. if ( vibratoGain_ > 0.0 ) {
  93. neckDelay_.setDelay( (baseDelay_ * (1.0 - betaRatio_) ) +
  94. (baseDelay_ * vibratoGain_ * vibrato_.tick()) );
  95. }
  96. lastFrame_[0] = 0.1248 * bodyFilters_[5].tick( bodyFilters_[4].tick( bodyFilters_[3].tick( bodyFilters_[2].tick( bodyFilters_[1].tick( bodyFilters_[0].tick( bridgeDelay_.lastOut() ) ) ) ) ) );
  97. return lastFrame_[0];
  98. }
  99. inline StkFrames& Bowed :: tick( StkFrames& frames, unsigned int channel )
  100. {
  101. unsigned int nChannels = lastFrame_.channels();
  102. #if defined(_STK_DEBUG_)
  103. if ( channel > frames.channels() - nChannels ) {
  104. oStream_ << "Bowed::tick(): channel and StkFrames arguments are incompatible!";
  105. handleError( StkError::FUNCTION_ARGUMENT );
  106. }
  107. #endif
  108. StkFloat *samples = &frames[channel];
  109. unsigned int j, hop = frames.channels() - nChannels;
  110. if ( nChannels == 1 ) {
  111. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  112. *samples++ = tick();
  113. }
  114. else {
  115. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  116. *samples++ = tick();
  117. for ( j=1; j<nChannels; j++ )
  118. *samples++ = lastFrame_[j];
  119. }
  120. }
  121. return frames;
  122. }
  123. } // stk namespace
  124. #endif