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.

106 lines
2.5KB

  1. /***************************************************/
  2. /*! \class Simple
  3. \brief STK wavetable/noise instrument.
  4. This class combines a looped wave, a
  5. noise source, a biquad resonance filter,
  6. a one-pole filter, and an ADSR envelope
  7. to create some interesting sounds.
  8. Control Change Numbers:
  9. - Filter Pole Position = 2
  10. - Noise/Pitched Cross-Fade = 4
  11. - Envelope Rate = 11
  12. - Gain = 128
  13. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  14. */
  15. /***************************************************/
  16. #include "Simple.h"
  17. #include "SKINImsg.h"
  18. namespace stk {
  19. Simple :: Simple( void )
  20. {
  21. // Concatenate the STK rawwave path to the rawwave file
  22. loop_ = new FileLoop( (Stk::rawwavePath() + "impuls10.raw").c_str(), true );
  23. filter_.setPole( 0.5 );
  24. baseFrequency_ = 440.0;
  25. setFrequency( baseFrequency_ );
  26. loopGain_ = 0.5;
  27. }
  28. Simple :: ~Simple( void )
  29. {
  30. delete loop_;
  31. }
  32. void Simple :: keyOn( void )
  33. {
  34. adsr_.keyOn();
  35. }
  36. void Simple :: keyOff( void )
  37. {
  38. adsr_.keyOff();
  39. }
  40. void Simple :: noteOn( StkFloat frequency, StkFloat amplitude )
  41. {
  42. this->keyOn();
  43. this->setFrequency( frequency );
  44. filter_.setGain( amplitude );
  45. }
  46. void Simple :: noteOff( StkFloat amplitude )
  47. {
  48. this->keyOff();
  49. }
  50. void Simple :: setFrequency( StkFloat frequency )
  51. {
  52. #if defined(_STK_DEBUG_)
  53. if ( frequency <= 0.0 ) {
  54. oStream_ << "Simple::setFrequency: argument is less than or equal to zero!";
  55. handleError( StkError::WARNING ); return;
  56. }
  57. #endif
  58. biquad_.setResonance( frequency, 0.98, true );
  59. loop_->setFrequency( frequency );
  60. }
  61. void Simple :: controlChange( int number, StkFloat value )
  62. {
  63. #if defined(_STK_DEBUG_)
  64. if ( Stk::inRange( value, 0.0, 128.0 ) == false ) {
  65. oStream_ << "Simple::controlChange: value (" << value << ") is out of range!";
  66. handleError( StkError::WARNING ); return;
  67. }
  68. #endif
  69. StkFloat normalizedValue = value * ONE_OVER_128;
  70. if (number == __SK_Breath_) // 2
  71. filter_.setPole( 0.99 * (1.0 - (normalizedValue * 2.0)) );
  72. else if (number == __SK_NoiseLevel_) // 4
  73. loopGain_ = normalizedValue;
  74. else if (number == __SK_ModFrequency_) { // 11
  75. normalizedValue /= 0.2 * Stk::sampleRate();
  76. adsr_.setAttackRate( normalizedValue );
  77. adsr_.setDecayRate( normalizedValue );
  78. adsr_.setReleaseRate( normalizedValue );
  79. }
  80. else if (number == __SK_AfterTouch_Cont_) // 128
  81. adsr_.setTarget( normalizedValue );
  82. #if defined(_STK_DEBUG_)
  83. else {
  84. oStream_ << "Simple::controlChange: undefined control number (" << number << ")!";
  85. handleError( StkError::WARNING );
  86. }
  87. #endif
  88. }
  89. } // stk namespace