Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


Modal.h

00001 #ifndef STK_MODAL_H
00002 #define STK_MODAL_H
00003 
00004 #include "Instrmnt.h"
00005 #include "Envelope.h"
00006 #include "FileLoop.h"
00007 #include "SineWave.h"
00008 #include "BiQuad.h"
00009 #include "OnePole.h"
00010 
00011 namespace stk {
00012 
00013 /***************************************************/
00024 /***************************************************/
00025 
00026 class Modal : public Instrmnt
00027 {
00028 public:
00030 
00033   Modal( unsigned int modes = 4 );
00034 
00036   virtual ~Modal( void );
00037 
00039   void clear( void );
00040 
00042   virtual void setFrequency( StkFloat frequency );
00043 
00045   void setRatioAndRadius( unsigned int modeIndex, StkFloat ratio, StkFloat radius );
00046 
00048   void setMasterGain( StkFloat aGain ) { masterGain_ = aGain; };
00049 
00051   void setDirectGain( StkFloat aGain ) { directGain_ = aGain; };
00052 
00054   void setModeGain( unsigned int modeIndex, StkFloat gain );
00055 
00057   virtual void strike( StkFloat amplitude );
00058 
00060   void damp( StkFloat amplitude );
00061 
00063   void noteOn( StkFloat frequency, StkFloat amplitude );
00064 
00066   void noteOff( StkFloat amplitude );
00067 
00069   virtual void controlChange( int number, StkFloat value ) = 0;
00070 
00072   StkFloat tick( unsigned int channel = 0 );
00073 
00075 
00082   StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
00083 
00084 protected:
00085 
00086   Envelope envelope_; 
00087   FileWvIn *wave_;
00088   BiQuad **filters_;
00089   OnePole  onepole_;
00090   SineWave vibrato_;
00091 
00092   unsigned int nModes_;
00093   std::vector<StkFloat> ratios_;
00094   std::vector<StkFloat> radii_;
00095 
00096   StkFloat vibratoGain_;
00097   StkFloat masterGain_;
00098   StkFloat directGain_;
00099   StkFloat stickHardness_;
00100   StkFloat strikePosition_;
00101   StkFloat baseFrequency_;
00102 };
00103 
00104 inline StkFloat Modal :: tick( unsigned int )
00105 {
00106   StkFloat temp = masterGain_ * onepole_.tick( wave_->tick() * envelope_.tick() );
00107 
00108   StkFloat temp2 = 0.0;
00109   for ( unsigned int i=0; i<nModes_; i++ )
00110     temp2 += filters_[i]->tick(temp);
00111 
00112   temp2  -= temp2 * directGain_;
00113   temp2 += directGain_ * temp;
00114 
00115   if ( vibratoGain_ != 0.0 ) {
00116     // Calculate AM and apply to master out
00117     temp = 1.0 + ( vibrato_.tick() * vibratoGain_ );
00118     temp2 = temp * temp2;
00119   }
00120     
00121   lastFrame_[0] = temp2;
00122   return lastFrame_[0];
00123 }
00124 
00125 inline StkFrames& Modal :: tick( StkFrames& frames, unsigned int channel )
00126 {
00127   unsigned int nChannels = lastFrame_.channels();
00128 #if defined(_STK_DEBUG_)
00129   if ( channel > frames.channels() - nChannels ) {
00130     oStream_ << "Modal::tick(): channel and StkFrames arguments are incompatible!";
00131     handleError( StkError::FUNCTION_ARGUMENT );
00132   }
00133 #endif
00134 
00135   StkFloat *samples = &frames[channel];
00136   unsigned int j, hop = frames.channels() - nChannels;
00137   if ( nChannels == 1 ) {
00138     for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
00139       *samples++ = tick();
00140   }
00141   else {
00142     for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
00143       *samples++ = tick();
00144       for ( j=1; j<nChannels; j++ )
00145         *samples++ = lastFrame_[j];
00146     }
00147   }
00148 
00149   return frames;
00150 }
00151 
00152 } // stk namespace
00153 
00154 #endif

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