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.8KB

  1. #ifndef STK_FLUTE_H
  2. #define STK_FLUTE_H
  3. #include "Instrmnt.h"
  4. #include "JetTable.h"
  5. #include "DelayL.h"
  6. #include "OnePole.h"
  7. #include "PoleZero.h"
  8. #include "Noise.h"
  9. #include "ADSR.h"
  10. #include "SineWave.h"
  11. namespace stk {
  12. /***************************************************/
  13. /*! \class Flute
  14. \brief STK flute physical model class.
  15. This class implements a simple flute
  16. physical model, as discussed by Karjalainen,
  17. Smith, Waryznyk, etc. The jet model uses
  18. a polynomial, a la Cook.
  19. This is a digital waveguide model, making its
  20. use possibly subject to patents held by Stanford
  21. University, Yamaha, and others.
  22. Control Change Numbers:
  23. - Jet Delay = 2
  24. - Noise Gain = 4
  25. - Vibrato Frequency = 11
  26. - Vibrato Gain = 1
  27. - Breath Pressure = 128
  28. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  29. */
  30. /***************************************************/
  31. class Flute : public Instrmnt
  32. {
  33. public:
  34. //! Class constructor, taking the lowest desired playing frequency.
  35. /*!
  36. An StkError will be thrown if the rawwave path is incorrectly set.
  37. */
  38. Flute( StkFloat lowestFrequency );
  39. //! Class destructor.
  40. ~Flute( void );
  41. //! Reset and clear all internal state.
  42. void clear( void );
  43. //! Set instrument parameters for a particular frequency.
  44. void setFrequency( StkFloat frequency );
  45. //! Set the reflection coefficient for the jet delay (-1.0 - 1.0).
  46. void setJetReflection( StkFloat coefficient ) { jetReflection_ = coefficient; };
  47. //! Set the reflection coefficient for the air column delay (-1.0 - 1.0).
  48. void setEndReflection( StkFloat coefficient ) { endReflection_ = coefficient; };
  49. //! Set the length of the jet delay in terms of a ratio of jet delay to air column delay lengths.
  50. void setJetDelay( StkFloat aRatio );
  51. //! Apply breath velocity to instrument with given amplitude and rate of increase.
  52. void startBlowing( StkFloat amplitude, StkFloat rate );
  53. //! Decrease breath velocity with given rate of decrease.
  54. void stopBlowing( StkFloat rate );
  55. //! Start a note with the given frequency and amplitude.
  56. void noteOn( StkFloat frequency, StkFloat amplitude );
  57. //! Stop a note with the given amplitude (speed of decay).
  58. void noteOff( StkFloat amplitude );
  59. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  60. void controlChange( int number, StkFloat value );
  61. //! Compute and return one output sample.
  62. StkFloat tick( unsigned int channel = 0 );
  63. //! Fill a channel of the StkFrames object with computed outputs.
  64. /*!
  65. The \c channel argument must be less than the number of
  66. channels in the StkFrames argument (the first channel is specified
  67. by 0). However, range checking is only performed if _STK_DEBUG_
  68. is defined during compilation, in which case an out-of-range value
  69. will trigger an StkError exception.
  70. */
  71. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  72. protected:
  73. DelayL jetDelay_;
  74. DelayL boreDelay_;
  75. JetTable jetTable_;
  76. OnePole filter_;
  77. PoleZero dcBlock_;
  78. Noise noise_;
  79. ADSR adsr_;
  80. SineWave vibrato_;
  81. StkFloat lastFrequency_;
  82. StkFloat maxPressure_;
  83. StkFloat jetReflection_;
  84. StkFloat endReflection_;
  85. StkFloat noiseGain_;
  86. StkFloat vibratoGain_;
  87. StkFloat outputGain_;
  88. StkFloat jetRatio_;
  89. };
  90. inline StkFloat Flute :: tick( unsigned int )
  91. {
  92. StkFloat pressureDiff;
  93. StkFloat breathPressure;
  94. // Calculate the breath pressure (envelope + noise + vibrato)
  95. breathPressure = maxPressure_ * adsr_.tick();
  96. breathPressure += breathPressure * ( noiseGain_ * noise_.tick() + vibratoGain_ * vibrato_.tick() );
  97. StkFloat temp = -filter_.tick( boreDelay_.lastOut() );
  98. temp = dcBlock_.tick( temp ); // Block DC on reflection.
  99. pressureDiff = breathPressure - (jetReflection_ * temp);
  100. pressureDiff = jetDelay_.tick( pressureDiff );
  101. pressureDiff = jetTable_.tick( pressureDiff ) + (endReflection_ * temp);
  102. lastFrame_[0] = (StkFloat) 0.3 * boreDelay_.tick( pressureDiff );
  103. lastFrame_[0] *= outputGain_;
  104. return lastFrame_[0];
  105. }
  106. inline StkFrames& Flute :: tick( StkFrames& frames, unsigned int channel )
  107. {
  108. unsigned int nChannels = lastFrame_.channels();
  109. #if defined(_STK_DEBUG_)
  110. if ( channel > frames.channels() - nChannels ) {
  111. oStream_ << "Flute::tick(): channel and StkFrames arguments are incompatible!";
  112. handleError( StkError::FUNCTION_ARGUMENT );
  113. }
  114. #endif
  115. StkFloat *samples = &frames[channel];
  116. unsigned int j, hop = frames.channels() - nChannels;
  117. if ( nChannels == 1 ) {
  118. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  119. *samples++ = tick();
  120. }
  121. else {
  122. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  123. *samples++ = tick();
  124. for ( j=1; j<nChannels; j++ )
  125. *samples++ = lastFrame_[j];
  126. }
  127. }
  128. return frames;
  129. }
  130. } // stk namespace
  131. #endif