The Synthesis ToolKit in C++ (STK)

Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

# Sitar.h

    00001 #ifndef STK_SITAR_H
    00002 #define STK_SITAR_H
    00003
    00004 #include "Instrmnt.h"
    00005 #include "DelayA.h"
    00006 #include "OneZero.h"
    00007 #include "Noise.h"
    00008 #include "ADSR.h"
    00009 #include <cmath>
    00010
    00011 namespace stk {
    00012
    00013 /***************************************************/
    00030 /***************************************************/
    00031
    00032 class Sitar : public Instrmnt
    00033 {
    00034  public:
    00036   Sitar( StkFloat lowestFrequency = 8.0 );
    00037
    00039   ~Sitar( void );
    00040
    00042   void clear( void );
    00043
    00045   void setFrequency( StkFloat frequency );
    00046
    00048   void pluck( StkFloat amplitude );
    00049
    00051   void noteOn( StkFloat frequency, StkFloat amplitude );
    00052
    00054   void noteOff( StkFloat amplitude );
    00055
    00057   StkFloat tick( unsigned int channel = 0 );
    00058
    00060
    00067   StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
    00068
    00069  protected:
    00070
    00071   DelayA  delayLine_;
    00072   OneZero loopFilter_;
    00073   Noise   noise_;
    00074   ADSR    envelope_;
    00075
    00076   StkFloat loopGain_;
    00077   StkFloat amGain_;
    00078   StkFloat delay_;
    00079   StkFloat targetDelay_;
    00080
    00081 };
    00082
    00083 inline StkFloat Sitar :: tick( unsigned int )
    00084 {
    00085   if ( fabs(targetDelay_ - delay_) > 0.001 ) {
    00086     if ( targetDelay_ < delay_ )
    00087       delay_ *= 0.99999;
    00088     else
    00089       delay_ *= 1.00001;
    00090     delayLine_.setDelay( delay_ );
    00091   }
    00092
    00093   lastFrame_[0] = delayLine_.tick( loopFilter_.tick( delayLine_.lastOut() * loopGain_ ) +
    00094                                 (amGain_ * envelope_.tick() * noise_.tick()));
    00095
    00096   return lastFrame_[0];
    00097 }
    00098
    00099 inline StkFrames& Sitar :: tick( StkFrames& frames, unsigned int channel )
    00100 {
    00101   unsigned int nChannels = lastFrame_.channels();
    00102 #if defined(_STK_DEBUG_)
    00103   if ( channel > frames.channels() - nChannels ) {
    00104     oStream_ << "Sitar::tick(): channel and StkFrames arguments are incompatible!";
    00105     handleError( StkError::FUNCTION_ARGUMENT );
    00106   }
    00107 #endif
    00108
    00109   StkFloat *samples = &frames[channel];
    00110   unsigned int j, hop = frames.channels() - nChannels;
    00111   if ( nChannels == 1 ) {
    00112     for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
    00113       *samples++ = tick();
    00114   }
    00115   else {
    00116     for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
    00117       *samples++ = tick();
    00118       for ( j=1; j<nChannels; j++ )
    00119         *samples++ = lastFrame_[j];
    00120     }
    00121   }
    00122
    00123   return frames;
    00124 }
    00125
    00126 } // stk namespace
    00127
    00128 #endif
    00129

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