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.

148 lines
4.3KB

  1. #ifndef STK_BRASS_H
  2. #define STK_BRASS_H
  3. #include "Instrmnt.h"
  4. #include "DelayA.h"
  5. #include "BiQuad.h"
  6. #include "PoleZero.h"
  7. #include "ADSR.h"
  8. #include "SineWave.h"
  9. namespace stk {
  10. /***************************************************/
  11. /*! \class Brass
  12. \brief STK simple brass instrument class.
  13. This class implements a simple brass instrument
  14. waveguide model, a la Cook (TBone, HosePlayer).
  15. This is a digital waveguide model, making its
  16. use possibly subject to patents held by
  17. Stanford University, Yamaha, and others.
  18. Control Change Numbers:
  19. - Lip Tension = 2
  20. - Slide Length = 4
  21. - Vibrato Frequency = 11
  22. - Vibrato Gain = 1
  23. - Volume = 128
  24. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  25. */
  26. /***************************************************/
  27. class Brass: public Instrmnt
  28. {
  29. public:
  30. //! Class constructor, taking the lowest desired playing frequency.
  31. /*!
  32. An StkError will be thrown if the rawwave path is incorrectly set.
  33. */
  34. Brass( StkFloat lowestFrequency = 8.0 );
  35. //! Class destructor.
  36. ~Brass( );
  37. //! Reset and clear all internal state.
  38. void clear( );
  39. //! Set instrument parameters for a particular frequency.
  40. void setFrequency( StkFloat frequency );
  41. //! Set the lips frequency.
  42. void setLip( StkFloat frequency );
  43. //! Apply breath pressure to instrument with given amplitude and rate of increase.
  44. void startBlowing( StkFloat amplitude, StkFloat rate );
  45. //! Decrease breath pressure with given rate of decrease.
  46. void stopBlowing( StkFloat rate );
  47. //! Start a note with the given frequency and amplitude.
  48. void noteOn( StkFloat frequency, StkFloat amplitude );
  49. //! Stop a note with the given amplitude (speed of decay).
  50. void noteOff( StkFloat amplitude );
  51. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  52. void controlChange( int number, StkFloat value );
  53. //! Compute and return one output sample.
  54. StkFloat tick( unsigned int channel = 0 );
  55. //! Fill a channel of the StkFrames object with computed outputs.
  56. /*!
  57. The \c channel argument must be less than the number of
  58. channels in the StkFrames argument (the first channel is specified
  59. by 0). However, range checking is only performed if _STK_DEBUG_
  60. is defined during compilation, in which case an out-of-range value
  61. will trigger an StkError exception.
  62. */
  63. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  64. protected:
  65. DelayA delayLine_;
  66. BiQuad lipFilter_;
  67. PoleZero dcBlock_;
  68. ADSR adsr_;
  69. SineWave vibrato_;
  70. StkFloat lipTarget_;
  71. StkFloat slideTarget_;
  72. StkFloat vibratoGain_;
  73. StkFloat maxPressure_;
  74. };
  75. inline StkFloat Brass :: tick( unsigned int )
  76. {
  77. StkFloat breathPressure = maxPressure_ * adsr_.tick();
  78. breathPressure += vibratoGain_ * vibrato_.tick();
  79. StkFloat mouthPressure = 0.3 * breathPressure;
  80. StkFloat borePressure = 0.85 * delayLine_.lastOut();
  81. StkFloat deltaPressure = mouthPressure - borePressure; // Differential pressure.
  82. deltaPressure = lipFilter_.tick( deltaPressure ); // Force - > position.
  83. deltaPressure *= deltaPressure; // Basic position to area mapping.
  84. if ( deltaPressure > 1.0 ) deltaPressure = 1.0; // Non-linear saturation.
  85. // The following input scattering assumes the mouthPressure = area.
  86. lastFrame_[0] = deltaPressure * mouthPressure + ( 1.0 - deltaPressure) * borePressure;
  87. lastFrame_[0] = delayLine_.tick( dcBlock_.tick( lastFrame_[0] ) );
  88. return lastFrame_[0];
  89. }
  90. inline StkFrames& Brass :: tick( StkFrames& frames, unsigned int channel )
  91. {
  92. unsigned int nChannels = lastFrame_.channels();
  93. #if defined(_STK_DEBUG_)
  94. if ( channel > frames.channels() - nChannels ) {
  95. oStream_ << "Brass::tick(): channel and StkFrames arguments are incompatible!";
  96. handleError( StkError::FUNCTION_ARGUMENT );
  97. }
  98. #endif
  99. StkFloat *samples = &frames[channel];
  100. unsigned int j, hop = frames.channels() - nChannels;
  101. if ( nChannels == 1 ) {
  102. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  103. *samples++ = tick();
  104. }
  105. else {
  106. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  107. *samples++ = tick();
  108. for ( j=1; j<nChannels; j++ )
  109. *samples++ = lastFrame_[j];
  110. }
  111. }
  112. return frames;
  113. }
  114. } // stk namespace
  115. #endif