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.

119 lines
3.1KB

  1. #ifndef STK_VOICDRUM_H
  2. #define STK_VOICDRUM_H
  3. #include "Instrmnt.h"
  4. #include "FileWvIn.h"
  5. #include "OnePole.h"
  6. namespace stk {
  7. /***************************************************/
  8. /*! \class VoicDrum
  9. \brief STK vocal drum sample player class.
  10. This class implements a drum sampling synthesizer using FileWvIn
  11. objects and one-pole filters. The drum rawwave files are sampled
  12. at 22050 Hz, but will be appropriately interpolated for other
  13. sample rates. You can specify the maximum polyphony (maximum
  14. number of simultaneous voices) in VoicDrum.h.
  15. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  16. */
  17. /***************************************************/
  18. const int VOICE_NUMWAVES = 11;
  19. const int VOICE_POLYPHONY = 4;
  20. class VoicDrum : public Instrmnt
  21. {
  22. public:
  23. //! Class constructor.
  24. VoicDrum( void );
  25. //! Class destructor.
  26. ~VoicDrum( void );
  27. //! Start a note with the given drum type and amplitude.
  28. void noteOn( StkFloat instrument, StkFloat amplitude );
  29. //! Stop a note with the given amplitude (speed of decay).
  30. void noteOff( StkFloat amplitude );
  31. //! Compute and return one output sample.
  32. StkFloat tick( unsigned int channel = 0 );
  33. //! Fill a channel of the StkFrames object with computed outputs.
  34. /*!
  35. The \c channel argument must be less than the number of
  36. channels in the StkFrames argument (the first channel is specified
  37. by 0). However, range checking is only performed if _STK_DEBUG_
  38. is defined during compilation, in which case an out-of-range value
  39. will trigger an StkError exception.
  40. */
  41. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  42. protected:
  43. FileWvIn waves_[VOICE_POLYPHONY];
  44. OnePole filters_[VOICE_POLYPHONY];
  45. std::vector<int> soundOrder_;
  46. std::vector<int> soundNumber_;
  47. int nSounding_;
  48. };
  49. inline StkFloat VoicDrum :: tick( unsigned int )
  50. {
  51. lastFrame_[0] = 0.0;
  52. if ( nSounding_ == 0 ) return lastFrame_[0];
  53. for ( int i=0; i<VOICE_POLYPHONY; i++ ) {
  54. if ( soundOrder_[i] >= 0 ) {
  55. if ( waves_[i].isFinished() ) {
  56. // Re-order the list.
  57. for ( int j=0; j<VOICE_POLYPHONY; j++ ) {
  58. if ( soundOrder_[j] > soundOrder_[i] )
  59. soundOrder_[j] -= 1;
  60. }
  61. soundOrder_[i] = -1;
  62. nSounding_--;
  63. }
  64. else
  65. lastFrame_[0] += filters_[i].tick( waves_[i].tick() );
  66. }
  67. }
  68. return lastFrame_[0];
  69. }
  70. inline StkFrames& VoicDrum :: tick( StkFrames& frames, unsigned int channel )
  71. {
  72. unsigned int nChannels = lastFrame_.channels();
  73. #if defined(_STK_DEBUG_)
  74. if ( channel > frames.channels() - nChannels ) {
  75. oStream_ << "VoicDrum::tick(): channel and StkFrames arguments are incompatible!";
  76. handleError( StkError::FUNCTION_ARGUMENT );
  77. }
  78. #endif
  79. StkFloat *samples = &frames[channel];
  80. unsigned int j, hop = frames.channels() - nChannels;
  81. if ( nChannels == 1 ) {
  82. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  83. *samples++ = tick();
  84. }
  85. else {
  86. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  87. *samples++ = tick();
  88. for ( j=1; j<nChannels; j++ )
  89. *samples++ = lastFrame_[j];
  90. }
  91. }
  92. return frames;
  93. }
  94. } // stk namespace
  95. #endif