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.

120 lines
3.3KB

  1. #ifndef STK_FM_H
  2. #define STK_FM_H
  3. #include "Instrmnt.h"
  4. #include "ADSR.h"
  5. #include "FileLoop.h"
  6. #include "SineWave.h"
  7. #include "TwoZero.h"
  8. namespace stk {
  9. /***************************************************/
  10. /*! \class FM
  11. \brief STK abstract FM synthesis base class.
  12. This class controls an arbitrary number of
  13. waves and envelopes, determined via a
  14. constructor argument.
  15. Control Change Numbers:
  16. - Control One = 2
  17. - Control Two = 4
  18. - LFO Speed = 11
  19. - LFO Depth = 1
  20. - ADSR 2 & 4 Target = 128
  21. The basic Chowning/Stanford FM patent expired
  22. in 1995, but there exist follow-on patents,
  23. mostly assigned to Yamaha. If you are of the
  24. type who should worry about this (making
  25. money) worry away.
  26. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  27. */
  28. /***************************************************/
  29. class FM : public Instrmnt
  30. {
  31. public:
  32. //! Class constructor, taking the number of wave/envelope operators to control.
  33. /*!
  34. An StkError will be thrown if the rawwave path is incorrectly set.
  35. */
  36. FM( unsigned int operators = 4 );
  37. //! Class destructor.
  38. virtual ~FM( void );
  39. //! Load the rawwave filenames in waves.
  40. void loadWaves( const char **filenames );
  41. //! Set instrument parameters for a particular frequency.
  42. virtual void setFrequency( StkFloat frequency );
  43. //! Set the frequency ratio for the specified wave.
  44. void setRatio( unsigned int waveIndex, StkFloat ratio );
  45. //! Set the gain for the specified wave.
  46. void setGain( unsigned int waveIndex, StkFloat gain );
  47. //! Set the modulation speed in Hz.
  48. void setModulationSpeed( StkFloat mSpeed ) { vibrato_.setFrequency( mSpeed ); };
  49. //! Set the modulation depth.
  50. void setModulationDepth( StkFloat mDepth ) { modDepth_ = mDepth; };
  51. //! Set the value of control1.
  52. void setControl1( StkFloat cVal ) { control1_ = cVal * 2.0; };
  53. //! Set the value of control1.
  54. void setControl2( StkFloat cVal ) { control2_ = cVal * 2.0; };
  55. //! Start envelopes toward "on" targets.
  56. void keyOn( void );
  57. //! Start envelopes toward "off" targets.
  58. void keyOff( void );
  59. //! Stop a note with the given amplitude (speed of decay).
  60. void noteOff( StkFloat amplitude );
  61. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  62. virtual void controlChange( int number, StkFloat value );
  63. //! Compute and return one output sample.
  64. virtual StkFloat tick( unsigned int ) = 0;
  65. //! Fill a channel of the StkFrames object with computed outputs.
  66. /*!
  67. The \c channel argument must be less than the number of
  68. channels in the StkFrames argument (the first channel is specified
  69. by 0). However, range checking is only performed if _STK_DEBUG_
  70. is defined during compilation, in which case an out-of-range value
  71. will trigger an StkError exception.
  72. */
  73. virtual StkFrames& tick( StkFrames& frames, unsigned int channel = 0 ) = 0;
  74. protected:
  75. std::vector<ADSR *> adsr_;
  76. std::vector<FileLoop *> waves_;
  77. SineWave vibrato_;
  78. TwoZero twozero_;
  79. unsigned int nOperators_;
  80. StkFloat baseFrequency_;
  81. std::vector<StkFloat> ratios_;
  82. std::vector<StkFloat> gains_;
  83. StkFloat modDepth_;
  84. StkFloat control1_;
  85. StkFloat control2_;
  86. StkFloat fmGains_[100];
  87. StkFloat fmSusLevels_[16];
  88. StkFloat fmAttTimes_[32];
  89. };
  90. } // stk namespace
  91. #endif