Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


Voicer.h

00001 #ifndef STK_VOICER_H
00002 #define STK_VOICER_H
00003 
00004 #include "Instrmnt.h"
00005 #include <vector>
00006 
00007 namespace stk {
00008 
00009 /***************************************************/
00032 /***************************************************/
00033 
00034 class Voicer : public Stk
00035 {
00036  public:
00038   Voicer( StkFloat decayTime = 0.2 );
00039 
00041 
00045   void addInstrument( Instrmnt *instrument, int group=0 );
00046 
00048 
00053   void removeInstrument( Instrmnt *instrument );
00054 
00056 
00064   long noteOn( StkFloat noteNumber, StkFloat amplitude, int group=0 );
00065 
00067 
00070   void noteOff( StkFloat noteNumber, StkFloat amplitude, int group=0 );
00071 
00073 
00076   void noteOff( long tag, StkFloat amplitude );
00077 
00079 
00082   void setFrequency( StkFloat noteNumber, int group=0 );
00083 
00085 
00088   void setFrequency( long tag, StkFloat noteNumber );
00089 
00091   void pitchBend( StkFloat value, int group=0 );
00092 
00094   void pitchBend( long tag, StkFloat value );
00095 
00097   void controlChange( int number, StkFloat value, int group=0 );
00098 
00100   void controlChange( long tag, int number, StkFloat value );
00101 
00103   void silence( void );
00104 
00106   unsigned int channelsOut( void ) const { return lastFrame_.channels(); };
00107 
00109   const StkFrames& lastFrame( void ) const { return lastFrame_; };
00110 
00112 
00120   StkFloat lastOut( unsigned int channel = 0 );
00121 
00123 
00131   StkFloat tick( unsigned int channel = 0 );
00132 
00134 
00142   StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
00143 
00144  protected:
00145 
00146   struct Voice {
00147     Instrmnt *instrument;
00148     long tag;
00149     StkFloat noteNumber;
00150     StkFloat frequency;
00151     int sounding;
00152     int group;
00153 
00154     // Default constructor.
00155     Voice()
00156       :instrument(0), tag(0), noteNumber(-1.0), frequency(0.0), sounding(0), group(0) {}
00157   };
00158 
00159   std::vector<Voice> voices_;
00160   long tags_;
00161   int muteTime_;
00162   StkFrames lastFrame_;
00163 };
00164 
00165 inline StkFloat Voicer :: lastOut( unsigned int channel )
00166 {
00167 #if defined(_STK_DEBUG_)
00168   if ( channel >= lastFrame_.channels() ) {
00169     errorString_ << "Voicer::lastOut(): channel argument is invalid!";
00170     handleError( StkError::FUNCTION_ARGUMENT );
00171   }
00172 #endif
00173 
00174   return lastFrame_[channel];
00175 }
00176 
00177 
00178 inline StkFloat Voicer :: tick( unsigned int channel )
00179 {
00180   unsigned int j;
00181   for ( j=0; j<lastFrame_.channels(); j++ ) lastFrame_[j] = 0.0;
00182   for ( unsigned int i=0; i<voices_.size(); i++ ) {
00183     if ( voices_[i].sounding != 0 ) {
00184       voices_[i].instrument->tick();
00185       for ( j=0; j<voices_[i].instrument->channelsOut(); j++ ) lastFrame_[j] += voices_[i].instrument->lastOut( j );
00186     }
00187     if ( voices_[i].sounding < 0 ) {
00188       voices_[i].sounding++;
00189       if ( voices_[i].sounding == 0 )
00190         voices_[i].noteNumber = -1;
00191     }
00192   }
00193 
00194   return lastFrame_[channel];
00195 }
00196 
00197 inline StkFrames& Voicer :: tick( StkFrames& frames, unsigned int channel )
00198 {
00199   unsigned int nChannels = lastFrame_.channels();
00200 #if defined(_STK_DEBUG_)
00201   if ( channel > frames.channels() - nChannels ) {
00202     errorString_ << "Voicer::tick(): channel and StkFrames arguments are incompatible!";
00203     handleError( StkError::FUNCTION_ARGUMENT );
00204   }
00205 #endif
00206 
00207   StkFloat *samples = &frames[channel];
00208   unsigned int j, hop = frames.channels() - nChannels;
00209   for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
00210     tick();
00211     for ( j=0; j<nChannels; j++ )
00212       *samples++ = lastFrame_[j];
00213   }
00214 
00215   return frames;
00216 }
00217 
00218 } // stk namespace
00219 
00220 #endif

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