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.

131 lines
3.5KB

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