Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


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

The Synthesis ToolKit in C++ (STK)
©1995--2014 Perry R. Cook and Gary P. Scavone. All Rights Reserved.