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.

145 lines
3.8KB

  1. #ifndef STK_BLOWBOTL_H
  2. #define STK_BLOWBOTL_H
  3. #include "Instrmnt.h"
  4. #include "JetTable.h"
  5. #include "BiQuad.h"
  6. #include "PoleZero.h"
  7. #include "Noise.h"
  8. #include "ADSR.h"
  9. #include "SineWave.h"
  10. namespace stk {
  11. /***************************************************/
  12. /*! \class BlowBotl
  13. \brief STK blown bottle instrument class.
  14. This class implements a helmholtz resonator
  15. (biquad filter) with a polynomial jet
  16. excitation (a la Cook).
  17. Control Change Numbers:
  18. - Noise Gain = 4
  19. - Vibrato Frequency = 11
  20. - Vibrato Gain = 1
  21. - Volume = 128
  22. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  23. */
  24. /***************************************************/
  25. class BlowBotl : public Instrmnt
  26. {
  27. public:
  28. //! Class constructor.
  29. /*!
  30. An StkError will be thrown if the rawwave path is incorrectly set.
  31. */
  32. BlowBotl( void );
  33. //! Class destructor.
  34. ~BlowBotl( 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. //! Apply breath velocity to instrument with given amplitude and rate of increase.
  40. void startBlowing( StkFloat amplitude, StkFloat rate );
  41. //! Decrease breath velocity with given rate of decrease.
  42. void stopBlowing( StkFloat rate );
  43. //! Start a note with the given frequency and amplitude.
  44. void noteOn( StkFloat frequency, StkFloat amplitude );
  45. //! Stop a note with the given amplitude (speed of decay).
  46. void noteOff( StkFloat amplitude );
  47. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  48. void controlChange( int number, StkFloat value );
  49. //! Compute and return one output sample.
  50. StkFloat tick( unsigned int channel = 0 );
  51. //! Fill a channel of the StkFrames object with computed outputs.
  52. /*!
  53. The \c channel argument must be less than the number of
  54. channels in the StkFrames argument (the first channel is specified
  55. by 0). However, range checking is only performed if _STK_DEBUG_
  56. is defined during compilation, in which case an out-of-range value
  57. will trigger an StkError exception.
  58. */
  59. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  60. protected:
  61. JetTable jetTable_;
  62. BiQuad resonator_;
  63. PoleZero dcBlock_;
  64. Noise noise_;
  65. ADSR adsr_;
  66. SineWave vibrato_;
  67. StkFloat maxPressure_;
  68. StkFloat noiseGain_;
  69. StkFloat vibratoGain_;
  70. StkFloat outputGain_;
  71. };
  72. inline StkFloat BlowBotl :: tick( unsigned int )
  73. {
  74. StkFloat breathPressure;
  75. StkFloat randPressure;
  76. StkFloat pressureDiff;
  77. // Calculate the breath pressure (envelope + vibrato)
  78. breathPressure = maxPressure_ * adsr_.tick();
  79. breathPressure += vibratoGain_ * vibrato_.tick();
  80. pressureDiff = breathPressure - resonator_.lastOut();
  81. randPressure = noiseGain_ * noise_.tick();
  82. randPressure *= breathPressure;
  83. randPressure *= (1.0 + pressureDiff);
  84. resonator_.tick( breathPressure + randPressure - ( jetTable_.tick( pressureDiff ) * pressureDiff ) );
  85. lastFrame_[0] = 0.2 * outputGain_ * dcBlock_.tick( pressureDiff );
  86. return lastFrame_[0];
  87. }
  88. inline StkFrames& BlowBotl :: tick( StkFrames& frames, unsigned int channel )
  89. {
  90. unsigned int nChannels = lastFrame_.channels();
  91. #if defined(_STK_DEBUG_)
  92. if ( channel > frames.channels() - nChannels ) {
  93. oStream_ << "BlowBotl::tick(): channel and StkFrames arguments are incompatible!";
  94. handleError( StkError::FUNCTION_ARGUMENT );
  95. }
  96. #endif
  97. StkFloat *samples = &frames[channel];
  98. unsigned int j, hop = frames.channels() - nChannels;
  99. if ( nChannels == 1 ) {
  100. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  101. *samples++ = tick();
  102. }
  103. else {
  104. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  105. *samples++ = tick();
  106. for ( j=1; j<nChannels; j++ )
  107. *samples++ = lastFrame_[j];
  108. }
  109. }
  110. return frames;
  111. }
  112. } // stk namespace
  113. #endif