Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


BowTable.h

00001 #ifndef STK_BOWTABL_H
00002 #define STK_BOWTABL_H
00003 
00004 #include "Function.h"
00005 #include <cmath>
00006 
00007 namespace stk {
00008 
00009 /***************************************************/
00018 /***************************************************/
00019 
00020 class BowTable : public Function
00021 {
00022 public:
00024   BowTable( void ) : offset_(0.0), slope_(0.1) {};
00025 
00027 
00033   void setOffset( StkFloat offset ) { offset_ = offset; };
00034 
00036 
00040   void setSlope( StkFloat slope ) { slope_ = slope; };
00041 
00043   StkFloat tick( StkFloat input );
00044 
00046 
00054   StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
00055 
00057 
00065   StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
00066 
00067 protected:
00068 
00069   StkFloat offset_;
00070   StkFloat slope_;
00071 
00072 };
00073 
00074 inline StkFloat BowTable :: tick( StkFloat input )
00075 {
00076   // The input represents differential string vs. bow velocity.
00077   StkFloat sample  = input + offset_;  // add bias to input
00078   sample *= slope_;          // then scale it
00079   lastFrame_[0] = (StkFloat) fabs( (double) sample ) + (StkFloat) 0.75;
00080   lastFrame_[0] = (StkFloat) pow( lastFrame_[0], (StkFloat) -4.0 );
00081 
00082   // Set minimum friction to 0.0
00083   // if ( lastFrame_[0] < 0.0 ) lastFrame_[0] = 0.0;
00084 
00085   // Set maximum friction to 1.0.
00086   if ( lastFrame_[0] > 1.0 ) lastFrame_[0] = (StkFloat) 1.0;
00087 
00088   return lastFrame_[0];
00089 }
00090 
00091 inline StkFrames& BowTable :: tick( StkFrames& frames, unsigned int channel )
00092 {
00093 #if defined(_STK_DEBUG_)
00094   if ( channel >= frames.channels() ) {
00095     errorString_ << "BowTable::tick(): channel and StkFrames arguments are incompatible!";
00096     handleError( StkError::FUNCTION_ARGUMENT );
00097   }
00098 #endif
00099 
00100   StkFloat *samples = &frames[channel];
00101   unsigned int hop = frames.channels();
00102   for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
00103     *samples = *samples + offset_;
00104     *samples *= slope_;
00105     *samples = (StkFloat) fabs( (double) *samples ) + 0.75;
00106     *samples = (StkFloat) pow( *samples, (StkFloat) -4.0 );
00107     if ( *samples > 1.0) *samples = 1.0;
00108   }
00109 
00110   lastFrame_[0] = *(samples-hop);
00111   return frames;
00112 }
00113 
00114 inline StkFrames& BowTable :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
00115 {
00116 #if defined(_STK_DEBUG_)
00117   if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
00118     errorString_ << "BowTable::tick(): channel and StkFrames arguments are incompatible!";
00119     handleError( StkError::FUNCTION_ARGUMENT );
00120   }
00121 #endif
00122 
00123   StkFloat *iSamples = &iFrames[iChannel];
00124   StkFloat *oSamples = &oFrames[oChannel];
00125   unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
00126   for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
00127     *oSamples = *iSamples + offset_;
00128     *oSamples *= slope_;
00129     *oSamples = (StkFloat) fabs( (double) *oSamples ) + 0.75;
00130     *oSamples = (StkFloat) pow( *oSamples, (StkFloat) -4.0 );
00131     if ( *oSamples > 1.0) *oSamples = 1.0;
00132   }
00133 
00134   lastFrame_[0] = *(oSamples-oHop);
00135   return iFrames;
00136 }
00137 
00138 } // stk namespace
00139 
00140 #endif

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