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.

186 lines
5.3KB

  1. #ifndef STK_BLOWHOLE_H
  2. #define STK_BLOWHOLE_H
  3. #include "Instrmnt.h"
  4. #include "DelayL.h"
  5. #include "ReedTable.h"
  6. #include "OneZero.h"
  7. #include "PoleZero.h"
  8. #include "Envelope.h"
  9. #include "Noise.h"
  10. #include "SineWave.h"
  11. namespace stk {
  12. /***************************************************/
  13. /*! \class BlowHole
  14. \brief STK clarinet physical model with one
  15. register hole and one tonehole.
  16. This class is based on the clarinet model,
  17. with the addition of a two-port register hole
  18. and a three-port dynamic tonehole
  19. implementation, as discussed by Scavone and
  20. Cook (1998).
  21. In this implementation, the distances between
  22. the reed/register hole and tonehole/bell are
  23. fixed. As a result, both the tonehole and
  24. register hole will have variable influence on
  25. the playing frequency, which is dependent on
  26. the length of the air column. In addition,
  27. the highest playing freqeuency is limited by
  28. these fixed lengths.
  29. This is a digital waveguide model, making its
  30. use possibly subject to patents held by Stanford
  31. University, Yamaha, and others.
  32. Control Change Numbers:
  33. - Reed Stiffness = 2
  34. - Noise Gain = 4
  35. - Tonehole State = 11
  36. - Register State = 1
  37. - Breath Pressure = 128
  38. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  39. */
  40. /***************************************************/
  41. class BlowHole : public Instrmnt
  42. {
  43. public:
  44. //! Class constructor.
  45. /*!
  46. An StkError will be thrown if the rawwave path is incorrectly set.
  47. */
  48. BlowHole( StkFloat lowestFrequency );
  49. //! Class destructor.
  50. ~BlowHole( void );
  51. //! Reset and clear all internal state.
  52. void clear( void );
  53. //! Set instrument parameters for a particular frequency.
  54. void setFrequency( StkFloat frequency );
  55. //! Set the tonehole state (0.0 = closed, 1.0 = fully open).
  56. void setTonehole( StkFloat newValue );
  57. //! Set the register hole state (0.0 = closed, 1.0 = fully open).
  58. void setVent( StkFloat newValue );
  59. //! Apply breath pressure to instrument with given amplitude and rate of increase.
  60. void startBlowing( StkFloat amplitude, StkFloat rate );
  61. //! Decrease breath pressure with given rate of decrease.
  62. void stopBlowing( StkFloat rate );
  63. //! Start a note with the given frequency and amplitude.
  64. void noteOn( StkFloat frequency, StkFloat amplitude );
  65. //! Stop a note with the given amplitude (speed of decay).
  66. void noteOff( StkFloat amplitude );
  67. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  68. void controlChange( int number, StkFloat value );
  69. //! Compute and return one output sample.
  70. StkFloat tick( unsigned int channel = 0 );
  71. //! Fill a channel of the StkFrames object with computed outputs.
  72. /*!
  73. The \c channel argument must be less than the number of
  74. channels in the StkFrames argument (the first channel is specified
  75. by 0). However, range checking is only performed if _STK_DEBUG_
  76. is defined during compilation, in which case an out-of-range value
  77. will trigger an StkError exception.
  78. */
  79. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  80. protected:
  81. DelayL delays_[3];
  82. ReedTable reedTable_;
  83. OneZero filter_;
  84. PoleZero tonehole_;
  85. PoleZero vent_;
  86. Envelope envelope_;
  87. Noise noise_;
  88. SineWave vibrato_;
  89. StkFloat scatter_;
  90. StkFloat thCoeff_;
  91. StkFloat rhGain_;
  92. StkFloat outputGain_;
  93. StkFloat noiseGain_;
  94. StkFloat vibratoGain_;
  95. };
  96. inline StkFloat BlowHole :: tick( unsigned int )
  97. {
  98. StkFloat pressureDiff;
  99. StkFloat breathPressure;
  100. StkFloat temp;
  101. // Calculate the breath pressure (envelope + noise + vibrato)
  102. breathPressure = envelope_.tick();
  103. breathPressure += breathPressure * noiseGain_ * noise_.tick();
  104. breathPressure += breathPressure * vibratoGain_ * vibrato_.tick();
  105. // Calculate the differential pressure = reflected - mouthpiece pressures
  106. pressureDiff = delays_[0].lastOut() - breathPressure;
  107. // Do two-port junction scattering for register vent
  108. StkFloat pa = breathPressure + pressureDiff * reedTable_.tick( pressureDiff );
  109. StkFloat pb = delays_[1].lastOut();
  110. vent_.tick( pa+pb );
  111. lastFrame_[0] = delays_[0].tick( vent_.lastOut()+pb );
  112. lastFrame_[0] *= outputGain_;
  113. // Do three-port junction scattering (under tonehole)
  114. pa += vent_.lastOut();
  115. pb = delays_[2].lastOut();
  116. StkFloat pth = tonehole_.lastOut();
  117. temp = scatter_ * (pa + pb - 2 * pth);
  118. delays_[2].tick( filter_.tick(pa + temp) * -0.95 );
  119. delays_[1].tick( pb + temp );
  120. tonehole_.tick( pa + pb - pth + temp );
  121. return lastFrame_[0];
  122. }
  123. inline StkFrames& BlowHole :: tick( StkFrames& frames, unsigned int channel )
  124. {
  125. unsigned int nChannels = lastFrame_.channels();
  126. #if defined(_STK_DEBUG_)
  127. if ( channel > frames.channels() - nChannels ) {
  128. oStream_ << "BlowHole::tick(): channel and StkFrames arguments are incompatible!";
  129. handleError( StkError::FUNCTION_ARGUMENT );
  130. }
  131. #endif
  132. StkFloat *samples = &frames[channel];
  133. unsigned int j, hop = frames.channels() - nChannels;
  134. if ( nChannels == 1 ) {
  135. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  136. *samples++ = tick();
  137. }
  138. else {
  139. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  140. *samples++ = tick();
  141. for ( j=1; j<nChannels; j++ )
  142. *samples++ = lastFrame_[j];
  143. }
  144. }
  145. return frames;
  146. }
  147. } // stk namespace
  148. #endif