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.

333 lines
9.6KB

  1. #ifndef STK_SHAKERS_H
  2. #define STK_SHAKERS_H
  3. #include "Instrmnt.h"
  4. #include <cmath>
  5. #include <stdlib.h>
  6. namespace stk {
  7. /***************************************************/
  8. /*! \class Shakers
  9. \brief PhISEM and PhOLIES class.
  10. PhISEM (Physically Informed Stochastic Event Modeling) is an
  11. algorithmic approach for simulating collisions of multiple
  12. independent sound producing objects. This class is a meta-model
  13. that can simulate a Maraca, Sekere, Cabasa, Bamboo Wind Chimes,
  14. Water Drops, Tambourine, Sleighbells, and a Guiro.
  15. PhOLIES (Physically-Oriented Library of Imitated Environmental
  16. Sounds) is a similar approach for the synthesis of environmental
  17. sounds. This class implements simulations of breaking sticks,
  18. crunchy snow (or not), a wrench, sandpaper, and more.
  19. Control Change Numbers:
  20. - Shake Energy = 2
  21. - System Decay = 4
  22. - Number Of Objects = 11
  23. - Resonance Frequency = 1
  24. - Shake Energy = 128
  25. - Instrument Selection = 1071
  26. - Maraca = 0
  27. - Cabasa = 1
  28. - Sekere = 2
  29. - Tambourine = 3
  30. - Sleigh Bells = 4
  31. - Bamboo Chimes = 5
  32. - Sand Paper = 6
  33. - Coke Can = 7
  34. - Sticks = 8
  35. - Crunch = 9
  36. - Big Rocks = 10
  37. - Little Rocks = 11
  38. - Next Mug = 12
  39. - Penny + Mug = 13
  40. - Nickle + Mug = 14
  41. - Dime + Mug = 15
  42. - Quarter + Mug = 16
  43. - Franc + Mug = 17
  44. - Peso + Mug = 18
  45. - Guiro = 19
  46. - Wrench = 20
  47. - Water Drops = 21
  48. - Tuned Bamboo Chimes = 22
  49. by Perry R. Cook with updates by Gary Scavone, 1995--2017.
  50. */
  51. /***************************************************/
  52. class Shakers : public Instrmnt
  53. {
  54. public:
  55. //! Class constructor taking instrument type argument.
  56. Shakers( int type = 0 );
  57. //! Start a note with the given instrument and amplitude.
  58. /*!
  59. Use the instrument numbers above, converted to frequency values
  60. as if MIDI note numbers, to select a particular instrument.
  61. */
  62. void noteOn( StkFloat instrument, StkFloat amplitude );
  63. //! Stop a note with the given amplitude (speed of decay).
  64. void noteOff( StkFloat amplitude );
  65. //! Perform the control change specified by \e number and \e value (0.0 - 128.0).
  66. void controlChange( int number, StkFloat value );
  67. //! Compute and return one output sample.
  68. StkFloat tick( unsigned int channel = 0 );
  69. //! Fill a channel of the StkFrames object with computed outputs.
  70. /*!
  71. The \c channel argument must be less than the number of
  72. channels in the StkFrames argument (the first channel is specified
  73. by 0). However, range checking is only performed if _STK_DEBUG_
  74. is defined during compilation, in which case an out-of-range value
  75. will trigger an StkError exception.
  76. */
  77. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  78. struct BiQuad {
  79. StkFloat gain;
  80. StkFloat b[3];
  81. StkFloat a[3]; // a0 term assumed equal to 1.0
  82. StkFloat inputs[3];
  83. StkFloat outputs[3];
  84. // Default constructor.
  85. BiQuad()
  86. {
  87. gain = 0.0;
  88. for ( int i=0; i<3; i++ ) {
  89. b[i] = 0.0;
  90. a[i] = 0.0;
  91. inputs[i] = 0.0;
  92. outputs[i] = 0.0;
  93. }
  94. }
  95. };
  96. protected:
  97. void setType( int type );
  98. void setResonance( BiQuad &filter, StkFloat frequency, StkFloat radius );
  99. StkFloat tickResonance( BiQuad &filter, StkFloat input );
  100. void setEqualization( StkFloat b0, StkFloat b1, StkFloat b2 );
  101. StkFloat tickEqualize( StkFloat input );
  102. int randomInt( int max );
  103. StkFloat randomFloat( StkFloat max = 1.0 );
  104. StkFloat noise( void );
  105. void waterDrop( void );
  106. int shakerType_;
  107. unsigned int nResonances_;
  108. StkFloat shakeEnergy_;
  109. StkFloat soundDecay_;
  110. StkFloat systemDecay_;
  111. StkFloat nObjects_;
  112. StkFloat sndLevel_;
  113. StkFloat baseGain_;
  114. StkFloat currentGain_;
  115. StkFloat baseDecay_;
  116. StkFloat baseObjects_;
  117. StkFloat decayScale_;
  118. BiQuad equalizer_;
  119. StkFloat ratchetCount_;
  120. StkFloat ratchetDelta_;
  121. StkFloat baseRatchetDelta_;
  122. int lastRatchetValue_;
  123. std::vector< BiQuad > filters_;
  124. std::vector< StkFloat > baseFrequencies_;
  125. std::vector< StkFloat > baseRadii_;
  126. std::vector< bool > doVaryFrequency_;
  127. std::vector< StkFloat > tempFrequencies_;
  128. StkFloat varyFactor_;
  129. };
  130. inline void Shakers :: setResonance( BiQuad &filter, StkFloat frequency, StkFloat radius )
  131. {
  132. filter.a[1] = -2.0 * radius * cos( TWO_PI * frequency / Stk::sampleRate());
  133. filter.a[2] = radius * radius;
  134. }
  135. inline StkFloat Shakers :: tickResonance( BiQuad &filter, StkFloat input )
  136. {
  137. filter.outputs[0] = input * filter.gain * currentGain_;
  138. filter.outputs[0] -= filter.a[1] * filter.outputs[1] + filter.a[2] * filter.outputs[2];
  139. filter.outputs[2] = filter.outputs[1];
  140. filter.outputs[1] = filter.outputs[0];
  141. return filter.outputs[0];
  142. }
  143. inline void Shakers :: setEqualization( StkFloat b0, StkFloat b1, StkFloat b2 )
  144. {
  145. equalizer_.b[0] = b0;
  146. equalizer_.b[1] = b1;
  147. equalizer_.b[2] = b2;
  148. }
  149. inline StkFloat Shakers :: tickEqualize( StkFloat input )
  150. {
  151. equalizer_.inputs[0] = input;
  152. equalizer_.outputs[0] = equalizer_.b[0] * equalizer_.inputs[0] + equalizer_.b[1] * equalizer_.inputs[1] + equalizer_.b[2] * equalizer_.inputs[2];
  153. equalizer_.inputs[2] = equalizer_.inputs[1];
  154. equalizer_.inputs[1] = equalizer_.inputs[0];
  155. return equalizer_.outputs[0];
  156. }
  157. inline int Shakers :: randomInt( int max ) // Return random integer between 0 and max-1
  158. {
  159. return (int) ((float)max * rand() / (RAND_MAX + 1.0) );
  160. }
  161. inline StkFloat Shakers :: randomFloat( StkFloat max ) // Return random float between 0.0 and max
  162. {
  163. return (StkFloat) (max * rand() / (RAND_MAX + 1.0) );
  164. }
  165. inline StkFloat Shakers :: noise( void ) // Return random StkFloat float between -1.0 and 1.0
  166. {
  167. return ( (StkFloat) ( 2.0 * rand() / (RAND_MAX + 1.0) ) - 1.0 );
  168. }
  169. const StkFloat MIN_ENERGY = 0.001;
  170. const StkFloat WATER_FREQ_SWEEP = 1.0001;
  171. inline void Shakers :: waterDrop( void )
  172. {
  173. if ( randomInt( 32767 ) < nObjects_) {
  174. sndLevel_ = shakeEnergy_;
  175. unsigned int j = randomInt( 3 );
  176. if ( j == 0 && filters_[0].gain == 0.0 ) { // don't change unless fully decayed
  177. tempFrequencies_[0] = baseFrequencies_[1] * (0.75 + (0.25 * noise()));
  178. filters_[0].gain = fabs( noise() );
  179. }
  180. else if (j == 1 && filters_[1].gain == 0.0) {
  181. tempFrequencies_[1] = baseFrequencies_[1] * (1.0 + (0.25 * noise()));
  182. filters_[1].gain = fabs( noise() );
  183. }
  184. else if ( filters_[2].gain == 0.0 ) {
  185. tempFrequencies_[2] = baseFrequencies_[1] * (1.25 + (0.25 * noise()));
  186. filters_[2].gain = fabs( noise() );
  187. }
  188. }
  189. // Sweep center frequencies.
  190. for ( unsigned int i=0; i<3; i++ ) { // WATER_RESONANCES = 3
  191. filters_[i].gain *= baseRadii_[i];
  192. if ( filters_[i].gain > 0.001 ) {
  193. tempFrequencies_[i] *= WATER_FREQ_SWEEP;
  194. filters_[i].a[1] = -2.0 * baseRadii_[i] * cos( TWO_PI * tempFrequencies_[i] / Stk::sampleRate() );
  195. }
  196. else
  197. filters_[i].gain = 0.0;
  198. }
  199. }
  200. inline StkFloat Shakers :: tick( unsigned int )
  201. {
  202. unsigned int iTube = 0;
  203. StkFloat input = 0.0;
  204. if ( shakerType_ == 19 || shakerType_ == 20 ) {
  205. if ( ratchetCount_ <= 0 ) return lastFrame_[0] = 0.0;
  206. shakeEnergy_ -= ( ratchetDelta_ + ( 0.002 * shakeEnergy_ ) );
  207. if ( shakeEnergy_ < 0.0 ) {
  208. shakeEnergy_ = 1.0;
  209. ratchetCount_--;
  210. }
  211. if ( randomFloat( 1024 ) < nObjects_ )
  212. sndLevel_ += shakeEnergy_ * shakeEnergy_;
  213. // Sound is enveloped noise
  214. input = sndLevel_ * noise() * shakeEnergy_;
  215. }
  216. else {
  217. if ( shakeEnergy_ < MIN_ENERGY ) return lastFrame_[0] = 0.0;
  218. // Exponential system decay
  219. shakeEnergy_ *= systemDecay_;
  220. // Random events
  221. if ( shakerType_ == 21 ) {
  222. waterDrop();
  223. input = sndLevel_;
  224. }
  225. else {
  226. if ( randomFloat( 1024.0 ) < nObjects_ ) {
  227. sndLevel_ += shakeEnergy_;
  228. input = sndLevel_;
  229. // Vary resonance frequencies if specified.
  230. for ( unsigned int i=0; i<nResonances_; i++ ) {
  231. if ( doVaryFrequency_[i] ) {
  232. StkFloat tempRand = baseFrequencies_[i] * ( 1.0 + ( varyFactor_ * noise() ) );
  233. filters_[i].a[1] = -2.0 * baseRadii_[i] * cos( TWO_PI * tempRand / Stk::sampleRate() );
  234. }
  235. }
  236. if ( shakerType_ == 22 ) iTube = randomInt( 7 ); // ANGKLUNG_RESONANCES
  237. }
  238. }
  239. }
  240. // Exponential sound decay
  241. sndLevel_ *= soundDecay_;
  242. // Do resonance filtering
  243. lastFrame_[0] = 0.0;
  244. if ( shakerType_ == 22 ) {
  245. for ( unsigned int i=0; i<nResonances_; i++ ) {
  246. if ( i == iTube )
  247. lastFrame_[0] += tickResonance( filters_[i], input );
  248. else
  249. lastFrame_[0] += tickResonance( filters_[i], 0.0 );
  250. }
  251. }
  252. else {
  253. for ( unsigned int i=0; i<nResonances_; i++ )
  254. lastFrame_[0] += tickResonance( filters_[i], input );
  255. }
  256. // Do final FIR filtering (lowpass or highpass)
  257. lastFrame_[0] = tickEqualize( lastFrame_[0] );
  258. //if ( std::abs(lastFrame_[0]) > 1.0 )
  259. // std::cout << "lastOutput = " << lastFrame_[0] << std::endl;
  260. return lastFrame_[0];
  261. }
  262. inline StkFrames& Shakers :: tick( StkFrames& frames, unsigned int channel )
  263. {
  264. unsigned int nChannels = lastFrame_.channels();
  265. #if defined(_STK_DEBUG_)
  266. if ( channel > frames.channels() - nChannels ) {
  267. oStream_ << "Shakers::tick(): channel and StkFrames arguments are incompatible!";
  268. handleError( StkError::FUNCTION_ARGUMENT );
  269. }
  270. #endif
  271. StkFloat *samples = &frames[channel];
  272. unsigned int j, hop = frames.channels() - nChannels;
  273. if ( nChannels == 1 ) {
  274. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  275. *samples++ = tick();
  276. }
  277. else {
  278. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  279. *samples++ = tick();
  280. for ( j=1; j<nChannels; j++ )
  281. *samples++ = lastFrame_[j];
  282. }
  283. }
  284. return frames;
  285. }
  286. } // stk namespace
  287. #endif