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.

125 lines
3.4KB

  1. #ifndef STK_RESONATE_H
  2. #define STK_RESONATE_H
  3. #include "Instrmnt.h"
  4. #include "ADSR.h"
  5. #include "BiQuad.h"
  6. #include "Noise.h"
  7. namespace stk {
  8. /***************************************************/
  9. /*! \class Resonate
  10. \brief STK noise driven formant filter.
  11. This instrument contains a noise source, which
  12. excites a biquad resonance filter, with volume
  13. controlled by an ADSR.
  14. Control Change Numbers:
  15. - Resonance Frequency (0-Nyquist) = 2
  16. - Pole Radii = 4
  17. - Notch Frequency (0-Nyquist) = 11
  18. - Zero Radii = 1
  19. - Envelope Gain = 128
  20. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  21. */
  22. /***************************************************/
  23. class Resonate : public Instrmnt
  24. {
  25. public:
  26. //! Class constructor.
  27. Resonate( void );
  28. //! Class destructor.
  29. ~Resonate( void );
  30. //! Set the filter for a resonance at the given frequency (Hz) and radius.
  31. void setResonance( StkFloat frequency, StkFloat radius );
  32. //! Set the filter for a notch at the given frequency (Hz) and radius.
  33. void setNotch( StkFloat frequency, StkFloat radius );
  34. //! Set the filter zero coefficients for contant resonance gain.
  35. void setEqualGainZeroes( void ) { filter_.setEqualGainZeroes(); };
  36. //! Initiate the envelope with a key-on event.
  37. void keyOn( void ) { adsr_.keyOn(); };
  38. //! Signal a key-off event to the envelope.
  39. void keyOff( void ) { adsr_.keyOff(); };
  40. //! Start a note with the given frequency and amplitude.
  41. void noteOn( StkFloat frequency, StkFloat amplitude );
  42. //! Stop a note with the given amplitude (speed of decay).
  43. void noteOff( StkFloat amplitude );
  44. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  45. void controlChange( int number, StkFloat value );
  46. //! Compute and return one output sample.
  47. StkFloat tick( unsigned int channel = 0 );
  48. //! Fill a channel of the StkFrames object with computed outputs.
  49. /*!
  50. The \c channel argument must be less than the number of
  51. channels in the StkFrames argument (the first channel is specified
  52. by 0). However, range checking is only performed if _STK_DEBUG_
  53. is defined during compilation, in which case an out-of-range value
  54. will trigger an StkError exception.
  55. */
  56. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  57. protected:
  58. ADSR adsr_;
  59. BiQuad filter_;
  60. Noise noise_;
  61. StkFloat poleFrequency_;
  62. StkFloat poleRadius_;
  63. StkFloat zeroFrequency_;
  64. StkFloat zeroRadius_;
  65. };
  66. inline StkFloat Resonate :: tick( unsigned int )
  67. {
  68. lastFrame_[0] = filter_.tick( noise_.tick() );
  69. lastFrame_[0] *= adsr_.tick();
  70. return lastFrame_[0];
  71. }
  72. inline StkFrames& Resonate :: tick( StkFrames& frames, unsigned int channel )
  73. {
  74. unsigned int nChannels = lastFrame_.channels();
  75. #if defined(_STK_DEBUG_)
  76. if ( channel > frames.channels() - nChannels ) {
  77. oStream_ << "Resonate::tick(): channel and StkFrames arguments are incompatible!";
  78. handleError( StkError::FUNCTION_ARGUMENT );
  79. }
  80. #endif
  81. StkFloat *samples = &frames[channel];
  82. unsigned int j, hop = frames.channels() - nChannels;
  83. if ( nChannels == 1 ) {
  84. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  85. *samples++ = tick();
  86. }
  87. else {
  88. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  89. *samples++ = tick();
  90. for ( j=1; j<nChannels; j++ )
  91. *samples++ = lastFrame_[j];
  92. }
  93. }
  94. return frames;
  95. }
  96. } // stk namespace
  97. #endif