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.

153 lines
4.3KB

  1. #ifndef STK_CLARINET_H
  2. #define STK_CLARINET_H
  3. #include "Instrmnt.h"
  4. #include "DelayL.h"
  5. #include "ReedTable.h"
  6. #include "OneZero.h"
  7. #include "Envelope.h"
  8. #include "Noise.h"
  9. #include "SineWave.h"
  10. namespace stk {
  11. /***************************************************/
  12. /*! \class Clarinet
  13. \brief STK clarinet physical model class.
  14. This class implements a simple clarinet
  15. physical model, as discussed by Smith (1986),
  16. McIntyre, Schumacher, Woodhouse (1983), and
  17. others.
  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. - Vibrato Frequency = 11
  25. - Vibrato Gain = 1
  26. - Breath Pressure = 128
  27. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  28. */
  29. /***************************************************/
  30. class Clarinet : public Instrmnt
  31. {
  32. public:
  33. //! Class constructor, taking the lowest desired playing frequency.
  34. /*!
  35. An StkError will be thrown if the rawwave path is incorrectly set.
  36. */
  37. Clarinet( StkFloat lowestFrequency = 8.0 );
  38. //! Class destructor.
  39. ~Clarinet( void );
  40. //! Reset and clear all internal state.
  41. void clear( void );
  42. //! Set instrument parameters for a particular frequency.
  43. void setFrequency( StkFloat frequency );
  44. //! Apply breath pressure to instrument with given amplitude and rate of increase.
  45. void startBlowing( StkFloat amplitude, StkFloat rate );
  46. //! Decrease breath pressure with given rate of decrease.
  47. void stopBlowing( StkFloat rate );
  48. //! Start a note with the given frequency and amplitude.
  49. void noteOn( StkFloat frequency, StkFloat amplitude );
  50. //! Stop a note with the given amplitude (speed of decay).
  51. void noteOff( StkFloat amplitude );
  52. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  53. void controlChange( int number, StkFloat value );
  54. //! Compute and return one output sample.
  55. StkFloat tick( unsigned int channel = 0 );
  56. //! Fill a channel of the StkFrames object with computed outputs.
  57. /*!
  58. The \c channel argument must be less than the number of
  59. channels in the StkFrames argument (the first channel is specified
  60. by 0). However, range checking is only performed if _STK_DEBUG_
  61. is defined during compilation, in which case an out-of-range value
  62. will trigger an StkError exception.
  63. */
  64. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  65. protected:
  66. DelayL delayLine_;
  67. ReedTable reedTable_;
  68. OneZero filter_;
  69. Envelope envelope_;
  70. Noise noise_;
  71. SineWave vibrato_;
  72. StkFloat outputGain_;
  73. StkFloat noiseGain_;
  74. StkFloat vibratoGain_;
  75. };
  76. inline StkFloat Clarinet :: tick( unsigned int )
  77. {
  78. StkFloat pressureDiff;
  79. StkFloat breathPressure;
  80. // Calculate the breath pressure (envelope + noise + vibrato)
  81. breathPressure = envelope_.tick();
  82. breathPressure += breathPressure * noiseGain_ * noise_.tick();
  83. breathPressure += breathPressure * vibratoGain_ * vibrato_.tick();
  84. // Perform commuted loss filtering.
  85. pressureDiff = -0.95 * filter_.tick( delayLine_.lastOut() );
  86. // Calculate pressure difference of reflected and mouthpiece pressures.
  87. pressureDiff = pressureDiff - breathPressure;
  88. // Perform non-linear scattering using pressure difference in reed function.
  89. lastFrame_[0] = delayLine_.tick(breathPressure + pressureDiff * reedTable_.tick(pressureDiff));
  90. // Apply output gain.
  91. lastFrame_[0] *= outputGain_;
  92. return lastFrame_[0];
  93. }
  94. inline StkFrames& Clarinet :: tick( StkFrames& frames, unsigned int channel )
  95. {
  96. unsigned int nChannels = lastFrame_.channels();
  97. #if defined(_STK_DEBUG_)
  98. if ( channel > frames.channels() - nChannels ) {
  99. oStream_ << "Clarinet::tick(): channel and StkFrames arguments are incompatible!";
  100. handleError( StkError::FUNCTION_ARGUMENT );
  101. }
  102. #endif
  103. StkFloat *samples = &frames[channel];
  104. unsigned int j, hop = frames.channels() - nChannels;
  105. if ( nChannels == 1 ) {
  106. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  107. *samples++ = tick();
  108. }
  109. else {
  110. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  111. *samples++ = tick();
  112. for ( j=1; j<nChannels; j++ )
  113. *samples++ = lastFrame_[j];
  114. }
  115. }
  116. return frames;
  117. }
  118. } // stk namespace
  119. #endif