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.

225 lines
6.7KB

  1. /***************************************************/
  2. /*! \class Voicer
  3. \brief STK voice manager class.
  4. This class can be used to manage a group of STK instrument
  5. classes. Individual voices can be controlled via unique note
  6. tags. Instrument groups can be controlled by group number.
  7. A previously constructed STK instrument class is linked with a
  8. voice manager using the addInstrument() function. An optional
  9. group number argument can be specified to the addInstrument()
  10. function as well (default group = 0). The voice manager does not
  11. delete any instrument instances ... it is the responsibility of
  12. the user to allocate and deallocate all instruments.
  13. The tick() function returns the mix of all sounding voices. Each
  14. noteOn returns a unique tag (credits to the NeXT MusicKit), so you
  15. can send control changes to specific voices within an ensemble.
  16. Alternately, control changes can be sent to all voices in a given
  17. group.
  18. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  19. */
  20. /***************************************************/
  21. #include "Voicer.h"
  22. #include <cmath>
  23. namespace stk {
  24. Voicer :: Voicer( StkFloat decayTime )
  25. {
  26. if ( decayTime < 0.0 ) {
  27. oStream_ << "Voicer::Voicer: argument (" << decayTime << ") must be positive!";
  28. handleError( StkError::FUNCTION_ARGUMENT );
  29. }
  30. tags_ = 23456;
  31. muteTime_ = (int) ( decayTime * Stk::sampleRate() );
  32. lastFrame_.resize( 1, 1, 0.0 );
  33. }
  34. void Voicer :: addInstrument( Instrmnt *instrument, int group )
  35. {
  36. Voicer::Voice voice;
  37. voice.instrument = instrument;
  38. voice.group = group;
  39. voice.noteNumber = -1;
  40. voices_.push_back( voice );
  41. // Check output channels and resize lastFrame_ if necessary.
  42. if ( instrument->channelsOut() > lastFrame_.channels() ) {
  43. unsigned int startChannel = lastFrame_.channels();
  44. lastFrame_.resize( 1, instrument->channelsOut() );
  45. for ( unsigned int i=startChannel; i<lastFrame_.size(); i++ )
  46. lastFrame_[i] = 0.0;
  47. }
  48. }
  49. void Voicer :: removeInstrument( Instrmnt *instrument )
  50. {
  51. bool found = false;
  52. std::vector< Voicer::Voice >::iterator i;
  53. for ( i=voices_.begin(); i!=voices_.end(); ++i ) {
  54. if ( (*i).instrument != instrument ) continue;
  55. voices_.erase( i );
  56. found = true;
  57. break;
  58. }
  59. if ( found ) {
  60. // Check output channels and resize lastFrame_ if necessary.
  61. unsigned int maxChannels = 1;
  62. for ( i=voices_.begin(); i!=voices_.end(); ++i ) {
  63. if ( (*i).instrument->channelsOut() > maxChannels ) maxChannels = (*i).instrument->channelsOut();
  64. }
  65. if ( maxChannels < lastFrame_.channels() )
  66. lastFrame_.resize( 1, maxChannels );
  67. }
  68. else {
  69. oStream_ << "Voicer::removeInstrument: instrument pointer not found in current voices!";
  70. handleError( StkError::WARNING );
  71. }
  72. }
  73. long Voicer :: noteOn(StkFloat noteNumber, StkFloat amplitude, int group )
  74. {
  75. unsigned int i;
  76. StkFloat frequency = (StkFloat) 220.0 * pow( 2.0, (noteNumber - 57.0) / 12.0 );
  77. for ( i=0; i<voices_.size(); i++ ) {
  78. if (voices_[i].noteNumber < 0 && voices_[i].group == group) {
  79. voices_[i].tag = tags_++;
  80. voices_[i].group = group;
  81. voices_[i].noteNumber = noteNumber;
  82. voices_[i].frequency = frequency;
  83. voices_[i].instrument->noteOn( frequency, amplitude * ONE_OVER_128 );
  84. voices_[i].sounding = 1;
  85. return voices_[i].tag;
  86. }
  87. }
  88. // All voices are sounding, so interrupt the oldest voice.
  89. int voice = -1;
  90. for ( i=0; i<voices_.size(); i++ ) {
  91. if ( voices_[i].group == group ) {
  92. if ( voice == -1 ) voice = i;
  93. else if ( voices_[i].tag < voices_[voice].tag ) voice = (int) i;
  94. }
  95. }
  96. if ( voice >= 0 ) {
  97. voices_[voice].tag = tags_++;
  98. voices_[voice].group = group;
  99. voices_[voice].noteNumber = noteNumber;
  100. voices_[voice].frequency = frequency;
  101. voices_[voice].instrument->noteOn( frequency, amplitude * ONE_OVER_128 );
  102. voices_[voice].sounding = 1;
  103. return voices_[voice].tag;
  104. }
  105. return -1;
  106. }
  107. void Voicer :: noteOff( StkFloat noteNumber, StkFloat amplitude, int group )
  108. {
  109. for ( unsigned int i=0; i<voices_.size(); i++ ) {
  110. if ( voices_[i].noteNumber == noteNumber && voices_[i].group == group ) {
  111. voices_[i].instrument->noteOff( amplitude * ONE_OVER_128 );
  112. voices_[i].sounding = -muteTime_;
  113. }
  114. }
  115. }
  116. void Voicer :: noteOff( long tag, StkFloat amplitude )
  117. {
  118. for ( unsigned int i=0; i<voices_.size(); i++ ) {
  119. if ( voices_[i].tag == tag ) {
  120. voices_[i].instrument->noteOff( amplitude * ONE_OVER_128 );
  121. voices_[i].sounding = -muteTime_;
  122. break;
  123. }
  124. }
  125. }
  126. void Voicer :: setFrequency( StkFloat noteNumber, int group )
  127. {
  128. StkFloat frequency = (StkFloat) 220.0 * pow( 2.0, (noteNumber - 57.0) / 12.0 );
  129. for ( unsigned int i=0; i<voices_.size(); i++ ) {
  130. if ( voices_[i].group == group ) {
  131. voices_[i].noteNumber = noteNumber;
  132. voices_[i].frequency = frequency;
  133. voices_[i].instrument->setFrequency( frequency );
  134. }
  135. }
  136. }
  137. void Voicer :: setFrequency( long tag, StkFloat noteNumber )
  138. {
  139. StkFloat frequency = (StkFloat) 220.0 * pow( 2.0, (noteNumber - 57.0) / 12.0 );
  140. for ( unsigned int i=0; i<voices_.size(); i++ ) {
  141. if ( voices_[i].tag == tag ) {
  142. voices_[i].noteNumber = noteNumber;
  143. voices_[i].frequency = frequency;
  144. voices_[i].instrument->setFrequency( frequency );
  145. break;
  146. }
  147. }
  148. }
  149. void Voicer :: pitchBend( StkFloat value, int group )
  150. {
  151. StkFloat pitchScaler;
  152. if ( value < 8192.0 )
  153. pitchScaler = pow( 0.5, (8192.0-value) / 8192.0 );
  154. else
  155. pitchScaler = pow( 2.0, (value-8192.0) / 8192.0 );
  156. for ( unsigned int i=0; i<voices_.size(); i++ ) {
  157. if ( voices_[i].group == group )
  158. voices_[i].instrument->setFrequency( (StkFloat) (voices_[i].frequency * pitchScaler) );
  159. }
  160. }
  161. void Voicer :: pitchBend( long tag, StkFloat value )
  162. {
  163. StkFloat pitchScaler;
  164. if ( value < 8192.0 )
  165. pitchScaler = pow( 0.5, (8192.0-value) / 8192.0 );
  166. else
  167. pitchScaler = pow( 2.0, (value-8192.0) / 8192.0 );
  168. for ( unsigned int i=0; i<voices_.size(); i++ ) {
  169. if ( voices_[i].tag == tag ) {
  170. voices_[i].instrument->setFrequency( (StkFloat) (voices_[i].frequency * pitchScaler) );
  171. break;
  172. }
  173. }
  174. }
  175. void Voicer :: controlChange( int number, StkFloat value, int group )
  176. {
  177. for ( unsigned int i=0; i<voices_.size(); i++ ) {
  178. if ( voices_[i].group == group )
  179. voices_[i].instrument->controlChange( number, value );
  180. }
  181. }
  182. void Voicer :: controlChange( long tag, int number, StkFloat value )
  183. {
  184. for ( unsigned int i=0; i<voices_.size(); i++ ) {
  185. if ( voices_[i].tag == tag ) {
  186. voices_[i].instrument->controlChange( number, value );
  187. break;
  188. }
  189. }
  190. }
  191. void Voicer :: silence( void )
  192. {
  193. for ( unsigned int i=0; i<voices_.size(); i++ ) {
  194. if ( voices_[i].sounding > 0 )
  195. voices_[i].instrument->noteOff( 0.5 );
  196. }
  197. }
  198. } // stk namespace