Arduino STK  4.6.2
Voicer.h
1 #ifndef STK_VOICER_H
2 #define STK_VOICER_H
3 
4 #include "Instrmnt.h"
5 #include <vector>
6 
7 namespace stk {
8 
9 /***************************************************/
32 /***************************************************/
33 
34 class Voicer : public Stk
35 {
36  public:
38  Voicer( StkFloat decayTime = 0.2 );
39 
41 
45  void addInstrument( Instrmnt *instrument, int group=0 );
46 
48 
53  void removeInstrument( Instrmnt *instrument );
54 
56 
64  long noteOn( StkFloat noteNumber, StkFloat amplitude, int group=0 );
65 
67 
70  void noteOff( StkFloat noteNumber, StkFloat amplitude, int group=0 );
71 
73 
76  void noteOff( long tag, StkFloat amplitude );
77 
79 
82  void setFrequency( StkFloat noteNumber, int group=0 );
83 
85 
88  void setFrequency( long tag, StkFloat noteNumber );
89 
91  void pitchBend( StkFloat value, int group=0 );
92 
94  void pitchBend( long tag, StkFloat value );
95 
97  void controlChange( int number, StkFloat value, int group=0 );
98 
100  void controlChange( long tag, int number, StkFloat value );
101 
103  void silence( void );
104 
106  unsigned int channelsOut( void ) const { return lastFrame_.channels(); };
107 
109  const StkFrames& lastFrame( void ) const { return lastFrame_; };
110 
112 
120  StkFloat lastOut( unsigned int channel = 0 );
121 
123 
131  StkFloat tick( unsigned int channel = 0 );
132 
134 
142  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
143 
144  protected:
145 
146  struct Voice {
147  Instrmnt *instrument;
148  long tag;
149  StkFloat noteNumber;
150  StkFloat frequency;
151  int sounding;
152  int group;
153 
154  // Default constructor.
155  Voice()
156  :instrument(0), tag(0), noteNumber(-1.0), frequency(0.0), sounding(0), group(0) {}
157  };
158 
159  std::vector<Voice> voices_;
160  long tags_;
161  int muteTime_;
162  StkFrames lastFrame_;
163 };
164 
165 inline StkFloat Voicer :: lastOut( unsigned int channel )
166 {
167 #if defined(_STK_DEBUG_)
168  if ( channel >= lastFrame_.channels() ) {
169  oStream_ << "Voicer::lastOut(): channel argument is invalid!";
170  handleError( StkError::FUNCTION_ARGUMENT );
171  }
172 #endif
173 
174  return lastFrame_[channel];
175 }
176 
177 
178 inline StkFloat Voicer :: tick( unsigned int channel )
179 {
180  unsigned int j;
181  for ( j=0; j<lastFrame_.channels(); j++ ) lastFrame_[j] = 0.0;
182  for ( unsigned int i=0; i<voices_.size(); i++ ) {
183  if ( voices_[i].sounding != 0 ) {
184  voices_[i].instrument->tick();
185  for ( j=0; j<voices_[i].instrument->channelsOut(); j++ ) lastFrame_[j] += voices_[i].instrument->lastOut( j );
186  }
187  if ( voices_[i].sounding < 0 )
188  voices_[i].sounding++;
189  if ( voices_[i].sounding == 0 )
190  voices_[i].noteNumber = -1;
191  }
192 
193  return lastFrame_[channel];
194 }
195 
196 inline StkFrames& Voicer :: tick( StkFrames& frames, unsigned int channel )
197 {
198  unsigned int nChannels = lastFrame_.channels();
199 #if defined(_STK_DEBUG_)
200  if ( channel > frames.channels() - nChannels ) {
201  oStream_ << "Voicer::tick(): channel and StkFrames arguments are incompatible!";
202  handleError( StkError::FUNCTION_ARGUMENT );
203  }
204 #endif
205 
206  StkFloat *samples = &frames[channel];
207  unsigned int j, hop = frames.channels() - nChannels;
208  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
209  tick();
210  for ( j=0; j<nChannels; j++ )
211  *samples++ = lastFrame_[j];
212  }
213 
214  return frames;
215 }
216 
217 } // stk namespace
218 
219 #endif
STK instrument abstract base class.
Definition: Instrmnt.h:20
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
STK base class.
Definition: Stk.h:144
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
STK voice manager class.
Definition: Voicer.h:35
void silence(void)
Send a noteOff message to all existing voices.
StkFloat tick(unsigned int channel=0)
Mix one sample frame of all sounding voices and return the specified channel value.
Definition: Voicer.h:178
void noteOff(StkFloat noteNumber, StkFloat amplitude, int group=0)
Send a noteOff to all voices having the given noteNumber and optional group (default group = 0).
Voicer(StkFloat decayTime=0.2)
Class constructor taking an optional note decay time (in seconds).
void setFrequency(long tag, StkFloat noteNumber)
Send a frequency update message to the voice with the given note tag.
void noteOff(long tag, StkFloat amplitude)
Send a noteOff to the voice with the given note tag.
void setFrequency(StkFloat noteNumber, int group=0)
Send a frequency update message to all voices assigned to the optional group argument (default group ...
StkFloat lastOut(unsigned int channel=0)
Return the specified channel value of the last computed frame.
Definition: Voicer.h:165
void pitchBend(long tag, StkFloat value)
Send a pitchBend message to the voice with the given note tag.
const StkFrames & lastFrame(void) const
Return an StkFrames reference to the last output sample frame.
Definition: Voicer.h:109
void controlChange(int number, StkFloat value, int group=0)
Send a controlChange to all instruments assigned to the optional group argument (default group = 0).
long noteOn(StkFloat noteNumber, StkFloat amplitude, int group=0)
Initiate a noteOn event with the given note number and amplitude and return a unique note tag.
void controlChange(long tag, int number, StkFloat value)
Send a controlChange to the voice with the given note tag.
void removeInstrument(Instrmnt *instrument)
Remove the given instrument pointer from the voice manager's control.
void pitchBend(StkFloat value, int group=0)
Send a pitchBend message to all voices assigned to the optional group argument (default group = 0).
unsigned int channelsOut(void) const
Return the current number of output channels.
Definition: Voicer.h:106
void addInstrument(Instrmnt *instrument, int group=0)
Add an instrument with an optional group number to the voice manager.
The STK namespace.
Definition: ADSR.h:8