Arduino STK 4.6.2
Drummer.h
1#ifndef STK_DRUMMER_H
2#define STK_DRUMMER_H
3
4#include "Instrmnt.h"
5#include "FileWvIn.h"
6#include "MemoryWvIn.h"
7#include "OnePole.h"
8
9namespace stk {
10
11/***************************************************/
26/***************************************************/
27
28const int DRUM_NUMWAVES = 11;
29const int DRUM_POLYPHONY = 4;
30
31class Drummer : public Instrmnt
32{
33 public:
35
38 Drummer( void );
39
41 ~Drummer( void );
42
44
50 void noteOn( StkFloat instrument, StkFloat amplitude );
51
53 void noteOff( StkFloat amplitude );
54
56 StkFloat tick( unsigned int channel = 0 );
57
59
66 StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
67
68 protected:
69#ifdef __RAW_ARRAYS__
70 MemoryWvIn waves_[DRUM_POLYPHONY];
71#else
72 FileWvIn waves_[DRUM_POLYPHONY];
73#endif
74
75 OnePole filters_[DRUM_POLYPHONY];
76 std::vector<int> soundOrder_;
77 std::vector<int> soundNumber_;
78 int nSounding_;
79};
80
81inline StkFloat Drummer :: tick( unsigned int )
82{
83 lastFrame_[0] = 0.0;
84 if ( nSounding_ == 0 ) return lastFrame_[0];
85
86 for ( int i=0; i<DRUM_POLYPHONY; i++ ) {
87 if ( soundOrder_[i] >= 0 ) {
88 if ( waves_[i].isFinished() ) {
89 // Re-order the list.
90 for ( int j=0; j<DRUM_POLYPHONY; j++ ) {
91 if ( soundOrder_[j] > soundOrder_[i] )
92 soundOrder_[j] -= 1;
93 }
94 soundOrder_[i] = -1;
95 nSounding_--;
96 }
97 else
98 lastFrame_[0] += filters_[i].tick( waves_[i].tick() );
99 }
100 }
101
102 return lastFrame_[0];
103}
104
105inline StkFrames& Drummer :: tick( StkFrames& frames, unsigned int channel )
106{
107 unsigned int nChannels = lastFrame_.channels();
108#if defined(_STK_DEBUG_)
109 if ( channel > frames.channels() - nChannels ) {
110 oStream_ << "Drummer::tick(): channel and StkFrames arguments are incompatible!";
111 handleError( StkError::FUNCTION_ARGUMENT );
112 }
113#endif
114
115 StkFloat *samples = &frames[channel];
116 unsigned int j, hop = frames.channels() - nChannels;
117 if ( nChannels == 1 ) {
118 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
119 *samples++ = tick();
120 }
121 else {
122 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
123 *samples++ = tick();
124 for ( j=1; j<nChannels; j++ )
125 *samples++ = lastFrame_[j];
126 }
127 }
128
129 return frames;
130}
131
132
133} // stk namespace
134
135#endif
~Drummer(void)
Class destructor.
void noteOn(StkFloat instrument, StkFloat amplitude)
Start a note with the given drum type and amplitude.
void noteOff(StkFloat amplitude)
Stop a note with the given amplitude (speed of decay).
StkFloat tick(unsigned int channel=0)
Compute and return one output sample.
Definition Drummer.h:81
Drummer(void)
Class constructor.
STK audio file input class.
Definition FileWvIn.h:53
Instrmnt(void)
Class constructor.
Definition Instrmnt.h:23
STK audio input class from Memory.
Definition MemoryWvIn.h:31
STK one-pole filter class.
Definition OnePole.h:21
An STK class to handle vectorized audio data.
Definition Stk.h:287
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition Stk.h:415
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition Stk.h:418
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
The STK namespace.
Definition ADSR.h:8