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.

117 lines
3.3KB

  1. /***************************************************/
  2. /*! \class VoicDrum
  3. \brief STK vocal drum sample player class.
  4. This class implements a drum sampling synthesizer using FileWvIn
  5. objects and one-pole filters. The drum rawwave files are sampled
  6. at 22050 Hz, but will be appropriately interpolated for other
  7. sample rates. You can specify the maximum polyphony (maximum
  8. number of simultaneous voices) in VoicDrum.h.
  9. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  10. */
  11. /***************************************************/
  12. #include "VoicDrum.h"
  13. #include <cmath>
  14. namespace stk {
  15. VoicDrum :: VoicDrum( void ) : Instrmnt()
  16. {
  17. // This counts the number of sounding voices.
  18. nSounding_ = 0;
  19. soundOrder_ = std::vector<int> (VOICE_POLYPHONY, -1);
  20. soundNumber_ = std::vector<int> (VOICE_POLYPHONY, -1);
  21. }
  22. VoicDrum :: ~VoicDrum( void )
  23. {
  24. }
  25. char voiceNames[VOICE_NUMWAVES][11] =
  26. {
  27. "tak2.raw",
  28. "tak1.raw",
  29. "bee1.raw",
  30. "dee1.raw",
  31. "dee2.raw",
  32. "din1.raw",
  33. "gun1.raw",
  34. "jun1.raw",
  35. "jun2.raw",
  36. "tak3.raw",
  37. "tak4.raw"
  38. };
  39. void VoicDrum :: noteOn( StkFloat instrument, StkFloat amplitude )
  40. {
  41. if ( amplitude < 0.0 || amplitude > 1.0 ) {
  42. oStream_ << "VoicDrum::noteOn: amplitude parameter is out of bounds!";
  43. handleError( StkError::WARNING ); return;
  44. }
  45. int noteNumber = ( (int) instrument ) % 11;
  46. // If we already have a wave of this note number loaded, just reset
  47. // it. Otherwise, look first for an unused wave or preempt the
  48. // oldest if already at maximum polyphony.
  49. int iWave;
  50. for ( iWave=0; iWave<VOICE_POLYPHONY; iWave++ ) {
  51. if ( soundNumber_[iWave] == noteNumber ) {
  52. if ( waves_[iWave].isFinished() ) {
  53. soundOrder_[iWave] = nSounding_;
  54. nSounding_++;
  55. }
  56. waves_[iWave].reset();
  57. filters_[iWave].setPole( 0.999 - (amplitude * 0.6) );
  58. filters_[iWave].setGain( amplitude );
  59. break;
  60. }
  61. }
  62. if ( iWave == VOICE_POLYPHONY ) { // This note number is not currently loaded.
  63. if ( nSounding_ < VOICE_POLYPHONY ) {
  64. for ( iWave=0; iWave<VOICE_POLYPHONY; iWave++ )
  65. if ( soundOrder_[iWave] < 0 ) break;
  66. nSounding_ += 1;
  67. }
  68. else {
  69. for ( iWave=0; iWave<VOICE_POLYPHONY; iWave++ )
  70. if ( soundOrder_[iWave] == 0 ) break;
  71. // Re-order the list.
  72. for ( int j=0; j<VOICE_POLYPHONY; j++ ) {
  73. if ( soundOrder_[j] > soundOrder_[iWave] )
  74. soundOrder_[j] -= 1;
  75. }
  76. }
  77. soundOrder_[iWave] = nSounding_ - 1;
  78. soundNumber_[iWave] = noteNumber;
  79. // Concatenate the rawwave path to the rawwave file
  80. waves_[iWave].openFile( (std::string("rawwaves/") + voiceNames[ noteNumber ]).c_str(), true );
  81. if ( Stk::sampleRate() != 22050.0 )
  82. waves_[iWave].setRate( 22050.0 / Stk::sampleRate() );
  83. filters_[iWave].setPole( 0.999 - (amplitude * 0.6) );
  84. filters_[iWave].setGain( amplitude );
  85. }
  86. /*
  87. #if defined(_STK_DEBUG_)
  88. oStream << "VoicDrum::noteOn: number sounding = " << nSounding_ << '\n';
  89. for (int i=0; i<nSounding_; i++) oStream << soundNumber_[i] << " ";
  90. oStream << '\n';
  91. handleError( StkError::WARNING );
  92. #endif
  93. */
  94. }
  95. void VoicDrum :: noteOff( StkFloat amplitude )
  96. {
  97. // Set all sounding wave filter gains low.
  98. int i = 0;
  99. while ( i < nSounding_ ) filters_[i++].setGain( amplitude * 0.01 );
  100. }
  101. } // stk namespace