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.

109 lines
3.3KB

  1. #ifndef STK_INSTRMNT_H
  2. #define STK_INSTRMNT_H
  3. #include "Stk.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class Instrmnt
  7. \brief STK instrument abstract base class.
  8. This class provides a common interface for
  9. all STK instruments.
  10. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  11. */
  12. /***************************************************/
  13. class Instrmnt : public Stk
  14. {
  15. public:
  16. //! Class constructor.
  17. Instrmnt( void ) { lastFrame_.resize( 1, 1, 0.0 ); };
  18. //! Reset and clear all internal state (for subclasses).
  19. /*!
  20. Not all subclasses implement a clear() function.
  21. */
  22. virtual void clear( void ) {};
  23. //! Start a note with the given frequency and amplitude.
  24. virtual void noteOn( StkFloat frequency, StkFloat amplitude ) = 0;
  25. //! Stop a note with the given amplitude (speed of decay).
  26. virtual void noteOff( StkFloat amplitude ) = 0;
  27. //! Set instrument parameters for a particular frequency.
  28. virtual void setFrequency( StkFloat frequency );
  29. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  30. virtual void controlChange(int number, StkFloat value);
  31. //! Return the number of output channels for the class.
  32. unsigned int channelsOut( void ) const { return lastFrame_.channels(); };
  33. //! Return an StkFrames reference to the last output sample frame.
  34. const StkFrames& lastFrame( void ) const { return lastFrame_; };
  35. //! Return the specified channel value of the last computed frame.
  36. /*!
  37. The \c channel argument must be less than the number of output
  38. channels, which can be determined with the channelsOut() function
  39. (the first channel is specified by 0). However, range checking is
  40. only performed if _STK_DEBUG_ is defined during compilation, in
  41. which case an out-of-range value will trigger an StkError
  42. exception. \sa lastFrame()
  43. */
  44. StkFloat lastOut( unsigned int channel = 0 );
  45. //! Compute one sample frame and return the specified \c channel value.
  46. /*!
  47. For monophonic instruments, the \c channel argument is ignored.
  48. */
  49. virtual StkFloat tick( unsigned int channel = 0 ) = 0;
  50. //! Fill the StkFrames object with computed sample frames, starting at the specified channel.
  51. /*!
  52. The \c channel argument plus the number of output channels must
  53. be less than the number of channels in the StkFrames argument (the
  54. first channel is specified by 0). However, range checking is only
  55. performed if _STK_DEBUG_ is defined during compilation, in which
  56. case an out-of-range value will trigger an StkError exception.
  57. */
  58. virtual StkFrames& tick( StkFrames& frames, unsigned int channel = 0 ) = 0;
  59. protected:
  60. StkFrames lastFrame_;
  61. };
  62. inline void Instrmnt :: setFrequency( StkFloat frequency )
  63. {
  64. oStream_ << "Instrmnt::setFrequency: virtual setFrequency function call!";
  65. handleError( StkError::WARNING );
  66. }
  67. inline StkFloat Instrmnt :: lastOut( unsigned int channel )
  68. {
  69. #if defined(_STK_DEBUG_)
  70. if ( channel >= lastFrame_.channels() ) {
  71. oStream_ << "Instrmnt::lastOut(): channel argument is invalid!";
  72. handleError( StkError::FUNCTION_ARGUMENT );
  73. }
  74. #endif
  75. return lastFrame_[channel];
  76. }
  77. inline void Instrmnt :: controlChange( int number, StkFloat value )
  78. {
  79. oStream_ << "Instrmnt::controlChange: virtual function call!";
  80. handleError( StkError::WARNING );
  81. }
  82. } // stk namespace
  83. #endif