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.

127 lines
3.3KB

  1. #ifndef STK_SIMPLE_H
  2. #define STK_SIMPLE_H
  3. #include "Instrmnt.h"
  4. #include "ADSR.h"
  5. #include "FileLoop.h"
  6. #include "OnePole.h"
  7. #include "BiQuad.h"
  8. #include "Noise.h"
  9. namespace stk {
  10. /***************************************************/
  11. /*! \class Simple
  12. \brief STK wavetable/noise instrument.
  13. This class combines a looped wave, a
  14. noise source, a biquad resonance filter,
  15. a one-pole filter, and an ADSR envelope
  16. to create some interesting sounds.
  17. Control Change Numbers:
  18. - Filter Pole Position = 2
  19. - Noise/Pitched Cross-Fade = 4
  20. - Envelope Rate = 11
  21. - Gain = 128
  22. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  23. */
  24. /***************************************************/
  25. class Simple : public Instrmnt
  26. {
  27. public:
  28. //! Class constructor.
  29. /*!
  30. An StkError will be thrown if the rawwave path is incorrectly set.
  31. */
  32. Simple( void );
  33. //! Class destructor.
  34. ~Simple( void );
  35. //! Set instrument parameters for a particular frequency.
  36. void setFrequency( StkFloat frequency );
  37. //! Start envelope toward "on" target.
  38. void keyOn( void );
  39. //! Start envelope toward "off" target.
  40. void keyOff( void );
  41. //! Start a note with the given frequency and amplitude.
  42. void noteOn( StkFloat frequency, StkFloat amplitude );
  43. //! Stop a note with the given amplitude (speed of decay).
  44. void noteOff( StkFloat amplitude );
  45. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  46. void controlChange( int number, StkFloat value );
  47. //! Compute and return one output sample.
  48. StkFloat tick( unsigned int channel = 0 );
  49. //! Fill a channel of the StkFrames object with computed outputs.
  50. /*!
  51. The \c channel argument must be less than the number of
  52. channels in the StkFrames argument (the first channel is specified
  53. by 0). However, range checking is only performed if _STK_DEBUG_
  54. is defined during compilation, in which case an out-of-range value
  55. will trigger an StkError exception.
  56. */
  57. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  58. protected:
  59. ADSR adsr_;
  60. FileLoop *loop_;
  61. OnePole filter_;
  62. BiQuad biquad_;
  63. Noise noise_;
  64. StkFloat baseFrequency_;
  65. StkFloat loopGain_;
  66. };
  67. inline StkFloat Simple :: tick( unsigned int )
  68. {
  69. lastFrame_[0] = loopGain_ * loop_->tick();
  70. biquad_.tick( noise_.tick() );
  71. lastFrame_[0] += (1.0 - loopGain_) * biquad_.lastOut();
  72. lastFrame_[0] = filter_.tick( lastFrame_[0] );
  73. lastFrame_[0] *= adsr_.tick();
  74. return lastFrame_[0];
  75. }
  76. inline StkFrames& Simple :: tick( StkFrames& frames, unsigned int channel )
  77. {
  78. unsigned int nChannels = lastFrame_.channels();
  79. #if defined(_STK_DEBUG_)
  80. if ( channel > frames.channels() - nChannels ) {
  81. oStream_ << "Simple::tick(): channel and StkFrames arguments are incompatible!";
  82. handleError( StkError::FUNCTION_ARGUMENT );
  83. }
  84. #endif
  85. StkFloat *samples = &frames[channel];
  86. unsigned int j, hop = frames.channels() - nChannels;
  87. if ( nChannels == 1 ) {
  88. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  89. *samples++ = tick();
  90. }
  91. else {
  92. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  93. *samples++ = tick();
  94. for ( j=1; j<nChannels; j++ )
  95. *samples++ = lastFrame_[j];
  96. }
  97. }
  98. return frames;
  99. }
  100. } // stk namespace
  101. #endif