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.

126 lines
3.3KB

  1. #ifndef STK_MOOG_H
  2. #define STK_MOOG_H
  3. #include "Sampler.h"
  4. #include "FormSwep.h"
  5. namespace stk {
  6. /***************************************************/
  7. /*! \class Moog
  8. \brief STK moog-like swept filter sampling synthesis class.
  9. This instrument uses one attack wave, one
  10. looped wave, and an ADSR envelope (inherited
  11. from the Sampler class) and adds two sweepable
  12. formant (FormSwep) filters.
  13. Control Change Numbers:
  14. - Filter Q = 2
  15. - Filter Sweep Rate = 4
  16. - Vibrato Frequency = 11
  17. - Vibrato Gain = 1
  18. - Gain = 128
  19. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  20. */
  21. /***************************************************/
  22. class Moog : public Sampler
  23. {
  24. public:
  25. //! Class constructor.
  26. /*!
  27. An StkError will be thrown if the rawwave path is incorrectly set.
  28. */
  29. Moog( void );
  30. //! Class destructor.
  31. ~Moog( void );
  32. //! Set instrument parameters for a particular frequency.
  33. void setFrequency( StkFloat frequency );
  34. //! Start a note with the given frequency and amplitude.
  35. void noteOn( StkFloat frequency, StkFloat amplitude );
  36. //! Set the modulation (vibrato) speed in Hz.
  37. void setModulationSpeed( StkFloat mSpeed ) { loops_[1]->setFrequency( mSpeed ); };
  38. //! Set the modulation (vibrato) depth.
  39. void setModulationDepth( StkFloat mDepth ) { modDepth_ = mDepth * 0.5; };
  40. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  41. void controlChange( int number, StkFloat value );
  42. //! Compute and return one output sample.
  43. StkFloat tick( unsigned int channel = 0 );
  44. //! Fill a channel of the StkFrames object with computed outputs.
  45. /*!
  46. The \c channel argument must be less than the number of
  47. channels in the StkFrames argument (the first channel is specified
  48. by 0). However, range checking is only performed if _STK_DEBUG_
  49. is defined during compilation, in which case an out-of-range value
  50. will trigger an StkError exception.
  51. */
  52. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  53. protected:
  54. FormSwep filters_[2];
  55. StkFloat modDepth_;
  56. StkFloat filterQ_;
  57. StkFloat filterRate_;
  58. };
  59. inline StkFloat Moog :: tick( unsigned int )
  60. {
  61. StkFloat temp;
  62. if ( modDepth_ != 0.0 ) {
  63. temp = loops_[1]->tick() * modDepth_;
  64. loops_[0]->setFrequency( baseFrequency_ * (1.0 + temp) );
  65. }
  66. temp = attackGain_ * attacks_[0]->tick();
  67. temp += loopGain_ * loops_[0]->tick();
  68. temp = filter_.tick( temp );
  69. temp *= adsr_.tick();
  70. temp = filters_[0].tick( temp );
  71. lastFrame_[0] = filters_[1].tick( temp );
  72. return lastFrame_[0] * 6.0;
  73. }
  74. inline StkFrames& Moog :: tick( StkFrames& frames, unsigned int channel )
  75. {
  76. unsigned int nChannels = lastFrame_.channels();
  77. #if defined(_STK_DEBUG_)
  78. if ( channel > frames.channels() - nChannels ) {
  79. oStream_ << "Moog::tick(): channel and StkFrames arguments are incompatible!";
  80. handleError( StkError::FUNCTION_ARGUMENT );
  81. }
  82. #endif
  83. StkFloat *samples = &frames[channel];
  84. unsigned int j, hop = frames.channels() - nChannels;
  85. if ( nChannels == 1 ) {
  86. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  87. *samples++ = tick();
  88. }
  89. else {
  90. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  91. *samples++ = tick();
  92. for ( j=1; j<nChannels; j++ )
  93. *samples++ = lastFrame_[j];
  94. }
  95. }
  96. return frames;
  97. }
  98. } // stk namespace
  99. #endif