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.

220 lines
7.7KB

  1. #ifndef STK_VOICER_H
  2. #define STK_VOICER_H
  3. #include "Instrmnt.h"
  4. #include <vector>
  5. namespace stk {
  6. /***************************************************/
  7. /*! \class Voicer
  8. \brief STK voice manager class.
  9. This class can be used to manage a group of STK instrument
  10. classes. Individual voices can be controlled via unique note
  11. tags. Instrument groups can be controlled by group number.
  12. A previously constructed STK instrument class is linked with a
  13. voice manager using the addInstrument() function. An optional
  14. group number argument can be specified to the addInstrument()
  15. function as well (default group = 0). The voice manager does not
  16. delete any instrument instances ... it is the responsibility of
  17. the user to allocate and deallocate all instruments.
  18. The tick() function returns the mix of all sounding voices. Each
  19. noteOn returns a unique tag (credits to the NeXT MusicKit), so you
  20. can send control changes to specific voices within an ensemble.
  21. Alternately, control changes can be sent to all voices in a given
  22. group.
  23. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  24. */
  25. /***************************************************/
  26. class Voicer : public Stk
  27. {
  28. public:
  29. //! Class constructor taking an optional note decay time (in seconds).
  30. Voicer( StkFloat decayTime = 0.2 );
  31. //! Add an instrument with an optional group number to the voice manager.
  32. /*!
  33. A set of instruments can be grouped by group number and
  34. controlled via the functions that take a group number argument.
  35. */
  36. void addInstrument( Instrmnt *instrument, int group=0 );
  37. //! Remove the given instrument pointer from the voice manager's control.
  38. /*!
  39. It is important that any instruments which are to be deleted by
  40. the user while the voice manager is running be first removed from
  41. the manager's control via this function!!
  42. */
  43. void removeInstrument( Instrmnt *instrument );
  44. //! Initiate a noteOn event with the given note number and amplitude and return a unique note tag.
  45. /*!
  46. Send the noteOn message to the first available unused voice.
  47. If all voices are sounding, the oldest voice is interrupted and
  48. sent the noteOn message. If the optional group argument is
  49. non-zero, only voices in that group are used. If no voices are
  50. found for a specified non-zero group value, the function returns
  51. -1. The amplitude value should be in the range 0.0 - 128.0.
  52. */
  53. long noteOn( StkFloat noteNumber, StkFloat amplitude, int group=0 );
  54. //! Send a noteOff to all voices having the given noteNumber and optional group (default group = 0).
  55. /*!
  56. The amplitude value should be in the range 0.0 - 128.0.
  57. */
  58. void noteOff( StkFloat noteNumber, StkFloat amplitude, int group=0 );
  59. //! Send a noteOff to the voice with the given note tag.
  60. /*!
  61. The amplitude value should be in the range 0.0 - 128.0.
  62. */
  63. void noteOff( long tag, StkFloat amplitude );
  64. //! Send a frequency update message to all voices assigned to the optional group argument (default group = 0).
  65. /*!
  66. The \e noteNumber argument corresponds to a MIDI note number, though it is a floating-point value and can range beyond the normal 0-127 range.
  67. */
  68. void setFrequency( StkFloat noteNumber, int group=0 );
  69. //! Send a frequency update message to the voice with the given note tag.
  70. /*!
  71. The \e noteNumber argument corresponds to a MIDI note number, though it is a floating-point value and can range beyond the normal 0-127 range.
  72. */
  73. void setFrequency( long tag, StkFloat noteNumber );
  74. //! Send a pitchBend message to all voices assigned to the optional group argument (default group = 0).
  75. void pitchBend( StkFloat value, int group=0 );
  76. //! Send a pitchBend message to the voice with the given note tag.
  77. void pitchBend( long tag, StkFloat value );
  78. //! Send a controlChange to all instruments assigned to the optional group argument (default group = 0).
  79. void controlChange( int number, StkFloat value, int group=0 );
  80. //! Send a controlChange to the voice with the given note tag.
  81. void controlChange( long tag, int number, StkFloat value );
  82. //! Send a noteOff message to all existing voices.
  83. void silence( void );
  84. //! Return the current number of output channels.
  85. unsigned int channelsOut( void ) const { return lastFrame_.channels(); };
  86. //! Return an StkFrames reference to the last output sample frame.
  87. const StkFrames& lastFrame( void ) const { return lastFrame_; };
  88. //! Return the specified channel value of the last computed frame.
  89. /*!
  90. The \c channel argument must be less than the number of output
  91. channels, which can be determined with the channelsOut() function
  92. (the first channel is specified by 0). However, range checking is
  93. only performed if _STK_DEBUG_ is defined during compilation, in
  94. which case an out-of-range value will trigger an StkError
  95. exception. \sa lastFrame()
  96. */
  97. StkFloat lastOut( unsigned int channel = 0 );
  98. //! Mix one sample frame of all sounding voices and return the specified \c channel value.
  99. /*!
  100. The \c channel argument must be less than the number of output
  101. channels, which can be determined with the channelsOut() function
  102. (the first channel is specified by 0). However, range checking is
  103. only performed if _STK_DEBUG_ is defined during compilation, in
  104. which case an out-of-range value will trigger an StkError
  105. exception.
  106. */
  107. StkFloat tick( unsigned int channel = 0 );
  108. //! Fill the StkFrames argument with computed frames and return the same reference.
  109. /*!
  110. The number of channels in the StkFrames argument must equal
  111. the number of channels in the file data. However, this is only
  112. checked if _STK_DEBUG_ is defined during compilation, in which
  113. case an incompatibility will trigger an StkError exception. If no
  114. file data is loaded, the function does nothing (a warning will be
  115. issued if _STK_DEBUG_ is defined during compilation).
  116. */
  117. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  118. protected:
  119. struct Voice {
  120. Instrmnt *instrument;
  121. long tag;
  122. StkFloat noteNumber;
  123. StkFloat frequency;
  124. int sounding;
  125. int group;
  126. // Default constructor.
  127. Voice()
  128. :instrument(0), tag(0), noteNumber(-1.0), frequency(0.0), sounding(0), group(0) {}
  129. };
  130. std::vector<Voice> voices_;
  131. long tags_;
  132. int muteTime_;
  133. StkFrames lastFrame_;
  134. };
  135. inline StkFloat Voicer :: lastOut( unsigned int channel )
  136. {
  137. #if defined(_STK_DEBUG_)
  138. if ( channel >= lastFrame_.channels() ) {
  139. oStream_ << "Voicer::lastOut(): channel argument is invalid!";
  140. handleError( StkError::FUNCTION_ARGUMENT );
  141. }
  142. #endif
  143. return lastFrame_[channel];
  144. }
  145. inline StkFloat Voicer :: tick( unsigned int channel )
  146. {
  147. unsigned int j;
  148. for ( j=0; j<lastFrame_.channels(); j++ ) lastFrame_[j] = 0.0;
  149. for ( unsigned int i=0; i<voices_.size(); i++ ) {
  150. if ( voices_[i].sounding != 0 ) {
  151. voices_[i].instrument->tick();
  152. for ( j=0; j<voices_[i].instrument->channelsOut(); j++ ) lastFrame_[j] += voices_[i].instrument->lastOut( j );
  153. }
  154. if ( voices_[i].sounding < 0 )
  155. voices_[i].sounding++;
  156. if ( voices_[i].sounding == 0 )
  157. voices_[i].noteNumber = -1;
  158. }
  159. return lastFrame_[channel];
  160. }
  161. inline StkFrames& Voicer :: tick( StkFrames& frames, unsigned int channel )
  162. {
  163. unsigned int nChannels = lastFrame_.channels();
  164. #if defined(_STK_DEBUG_)
  165. if ( channel > frames.channels() - nChannels ) {
  166. oStream_ << "Voicer::tick(): channel and StkFrames arguments are incompatible!";
  167. handleError( StkError::FUNCTION_ARGUMENT );
  168. }
  169. #endif
  170. StkFloat *samples = &frames[channel];
  171. unsigned int j, hop = frames.channels() - nChannels;
  172. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  173. tick();
  174. for ( j=0; j<nChannels; j++ )
  175. *samples++ = lastFrame_[j];
  176. }
  177. return frames;
  178. }
  179. } // stk namespace
  180. #endif