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.

167 lines
4.9KB

  1. #ifndef STK_SAXOFONY_H
  2. #define STK_SAXOFONY_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 Saxofony
  13. \brief STK faux conical bore reed instrument class.
  14. This class implements a "hybrid" digital
  15. waveguide instrument that can generate a
  16. variety of wind-like sounds. It has also been
  17. referred to as the "blowed string" model. The
  18. waveguide section is essentially that of a
  19. string, with one rigid and one lossy
  20. termination. The non-linear function is a
  21. reed table. The string can be "blown" at any
  22. point between the terminations, though just as
  23. with strings, it is impossible to excite the
  24. system at either end. If the excitation is
  25. placed at the string mid-point, the sound is
  26. that of a clarinet. At points closer to the
  27. "bridge", the sound is closer to that of a
  28. saxophone. See Scavone (2002) for more details.
  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. - Reed Aperture = 26
  35. - Noise Gain = 4
  36. - Blow Position = 11
  37. - Vibrato Frequency = 29
  38. - Vibrato Gain = 1
  39. - Breath Pressure = 128
  40. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  41. */
  42. /***************************************************/
  43. class Saxofony : public Instrmnt
  44. {
  45. public:
  46. //! Class constructor, taking the lowest desired playing frequency.
  47. /*!
  48. An StkError will be thrown if the rawwave path is incorrectly set.
  49. */
  50. Saxofony( StkFloat lowestFrequency );
  51. //! Class destructor.
  52. ~Saxofony( void );
  53. //! Reset and clear all internal state.
  54. void clear( void );
  55. //! Set instrument parameters for a particular frequency.
  56. void setFrequency( StkFloat frequency );
  57. //! Set the "blowing" position between the air column terminations (0.0 - 1.0).
  58. void setBlowPosition( StkFloat aPosition );
  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_[2];
  82. ReedTable reedTable_;
  83. OneZero filter_;
  84. Envelope envelope_;
  85. Noise noise_;
  86. SineWave vibrato_;
  87. StkFloat outputGain_;
  88. StkFloat noiseGain_;
  89. StkFloat vibratoGain_;
  90. StkFloat position_;
  91. };
  92. inline StkFloat Saxofony :: tick( unsigned int )
  93. {
  94. StkFloat pressureDiff;
  95. StkFloat breathPressure;
  96. StkFloat temp;
  97. // Calculate the breath pressure (envelope + noise + vibrato)
  98. breathPressure = envelope_.tick();
  99. breathPressure += breathPressure * noiseGain_ * noise_.tick();
  100. breathPressure += breathPressure * vibratoGain_ * vibrato_.tick();
  101. temp = -0.95 * filter_.tick( delays_[0].lastOut() );
  102. lastFrame_[0] = temp - delays_[1].lastOut();
  103. pressureDiff = breathPressure - lastFrame_[0];
  104. delays_[1].tick( temp );
  105. delays_[0].tick( breathPressure - (pressureDiff * reedTable_.tick(pressureDiff)) - temp );
  106. lastFrame_[0] *= outputGain_;
  107. return lastFrame_[0];
  108. }
  109. inline StkFrames& Saxofony :: tick( StkFrames& frames, unsigned int channel )
  110. {
  111. unsigned int nChannels = lastFrame_.channels();
  112. #if defined(_STK_DEBUG_)
  113. if ( channel > frames.channels() - nChannels ) {
  114. oStream_ << "Saxofony::tick(): channel and StkFrames arguments are incompatible!";
  115. handleError( StkError::FUNCTION_ARGUMENT );
  116. }
  117. #endif
  118. StkFloat *samples = &frames[channel];
  119. unsigned int j, hop = frames.channels() - nChannels;
  120. if ( nChannels == 1 ) {
  121. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  122. *samples++ = tick();
  123. }
  124. else {
  125. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  126. *samples++ = tick();
  127. for ( j=1; j<nChannels; j++ )
  128. *samples++ = lastFrame_[j];
  129. }
  130. }
  131. return frames;
  132. }
  133. } // stk namespace
  134. #endif