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.

155 lines
4.2KB

  1. #ifndef STK_MODAL_H
  2. #define STK_MODAL_H
  3. #include "Instrmnt.h"
  4. #include "Envelope.h"
  5. #include "FileLoop.h"
  6. #include "SineWave.h"
  7. #include "BiQuad.h"
  8. #include "OnePole.h"
  9. namespace stk {
  10. /***************************************************/
  11. /*! \class Modal
  12. \brief STK resonance model abstract base class.
  13. This class contains an excitation wavetable,
  14. an envelope, an oscillator, and N resonances
  15. (non-sweeping BiQuad filters), where N is set
  16. during instantiation.
  17. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  18. */
  19. /***************************************************/
  20. class Modal : public Instrmnt
  21. {
  22. public:
  23. //! Class constructor, taking the desired number of modes to create.
  24. /*!
  25. An StkError will be thrown if the rawwave path is incorrectly set.
  26. */
  27. Modal( unsigned int modes = 4 );
  28. //! Class destructor.
  29. virtual ~Modal( void );
  30. //! Reset and clear all internal state.
  31. void clear( void );
  32. //! Set instrument parameters for a particular frequency.
  33. virtual void setFrequency( StkFloat frequency );
  34. //! Set the ratio and radius for a specified mode filter.
  35. void setRatioAndRadius( unsigned int modeIndex, StkFloat ratio, StkFloat radius );
  36. //! Set the master gain.
  37. void setMasterGain( StkFloat aGain ) { masterGain_ = aGain; };
  38. //! Set the direct gain.
  39. void setDirectGain( StkFloat aGain ) { directGain_ = aGain; };
  40. //! Set the gain for a specified mode filter.
  41. void setModeGain( unsigned int modeIndex, StkFloat gain );
  42. //! Initiate a strike with the given amplitude (0.0 - 1.0).
  43. virtual void strike( StkFloat amplitude );
  44. //! Damp modes with a given decay factor (0.0 - 1.0).
  45. void damp( StkFloat amplitude );
  46. //! Start a note with the given frequency and amplitude.
  47. void noteOn( StkFloat frequency, StkFloat amplitude );
  48. //! Stop a note with the given amplitude (speed of decay).
  49. void noteOff( StkFloat amplitude );
  50. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  51. virtual void controlChange( int number, StkFloat value ) = 0;
  52. //! Compute and return one output sample.
  53. StkFloat tick( unsigned int channel = 0 );
  54. //! Fill a channel of the StkFrames object with computed outputs.
  55. /*!
  56. The \c channel argument must be less than the number of
  57. channels in the StkFrames argument (the first channel is specified
  58. by 0). However, range checking is only performed if _STK_DEBUG_
  59. is defined during compilation, in which case an out-of-range value
  60. will trigger an StkError exception.
  61. */
  62. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  63. protected:
  64. Envelope envelope_;
  65. FileWvIn *wave_;
  66. BiQuad **filters_;
  67. OnePole onepole_;
  68. SineWave vibrato_;
  69. unsigned int nModes_;
  70. std::vector<StkFloat> ratios_;
  71. std::vector<StkFloat> radii_;
  72. StkFloat vibratoGain_;
  73. StkFloat masterGain_;
  74. StkFloat directGain_;
  75. StkFloat stickHardness_;
  76. StkFloat strikePosition_;
  77. StkFloat baseFrequency_;
  78. };
  79. inline StkFloat Modal :: tick( unsigned int )
  80. {
  81. StkFloat temp = masterGain_ * onepole_.tick( wave_->tick() * envelope_.tick() );
  82. StkFloat temp2 = 0.0;
  83. for ( unsigned int i=0; i<nModes_; i++ )
  84. temp2 += filters_[i]->tick(temp);
  85. temp2 -= temp2 * directGain_;
  86. temp2 += directGain_ * temp;
  87. if ( vibratoGain_ != 0.0 ) {
  88. // Calculate AM and apply to master out
  89. temp = 1.0 + ( vibrato_.tick() * vibratoGain_ );
  90. temp2 = temp * temp2;
  91. }
  92. lastFrame_[0] = temp2;
  93. return lastFrame_[0];
  94. }
  95. inline StkFrames& Modal :: tick( StkFrames& frames, unsigned int channel )
  96. {
  97. unsigned int nChannels = lastFrame_.channels();
  98. #if defined(_STK_DEBUG_)
  99. if ( channel > frames.channels() - nChannels ) {
  100. oStream_ << "Modal::tick(): channel and StkFrames arguments are incompatible!";
  101. handleError( StkError::FUNCTION_ARGUMENT );
  102. }
  103. #endif
  104. StkFloat *samples = &frames[channel];
  105. unsigned int j, hop = frames.channels() - nChannels;
  106. if ( nChannels == 1 ) {
  107. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  108. *samples++ = tick();
  109. }
  110. else {
  111. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  112. *samples++ = tick();
  113. for ( j=1; j<nChannels; j++ )
  114. *samples++ = lastFrame_[j];
  115. }
  116. }
  117. return frames;
  118. }
  119. } // stk namespace
  120. #endif