Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

/root/src/subsynth/syn/Module/WaveTableOscModule.h

Go to the documentation of this file.
00001 
00002 /****************** <SYN heading BEGIN do not edit this line> *****************
00003  *
00004  * subsynth - modular audio synthesizer
00005  * subsynth is (C) Copyright 2001-2002 by Kevin Meinert
00006  *
00007  * Original Author: Kevin Meinert
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Library General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2 of the License, or (at your option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Library General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Library General Public
00020  * License along with this library; if not, write to the
00021  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00022  * Boston, MA 02111-1307, USA.
00023  *
00024  * -----------------------------------------------------------------
00025  * File:          $RCSfile: WaveTableOscModule.h,v $
00026  * Date modified: $Date: 2002/04/15 07:28:46 $
00027  * Version:       $Revision: 1.16 $
00028  * -----------------------------------------------------------------
00029  *
00030  ****************** <SYN heading END do not edit this line> ******************/
00031 
00032 #ifndef SUBSYNTH_WAVETABLE_OSC_MODULE_H
00033 #define SUBSYNTH_WAVETABLE_OSC_MODULE_H
00034 
00035 #include <map>
00036 #include "syn/Stream/AudioIStreamPtr.h"
00037 #include "syn/Stream/WavAudioIStream.h"
00038 #include "syn/Core/SampleBuffer.h"
00039 #include "syn/Core/SampleBufferQueue.h"
00040 #include "syn/Core/Module.h"
00041 #include "syn/Core/Terminal.h"
00042 
00043 namespace syn
00044 {
00052    class WaveTableOscModule : public Module
00053    {
00054    public:
00056       WaveTableOscModule();
00057    
00059       virtual ~WaveTableOscModule();
00060 
00062       virtual void update();
00063 
00065       virtual bool open();
00066 
00068       virtual bool isOpen();
00069       
00071       virtual void close();
00072 
00074       virtual Module* clone() const
00075       {
00076          WaveTableOscModule* mod = new WaveTableOscModule;
00077          mod->setWaveTable( mWaveTable );
00078          mod->setFreq( this->freq() );
00079          mod->setFreqControl( this->freqControl() );
00080          mod->setFreqControlSensitivity( this->freqControlSensitivity() );
00081          mod->setInterp( this->interp() );
00082          mod->setLoop( this->loop() );
00083          mod->setRetriggerable( this->retriggerable() );
00084          return mod;
00085       }
00086        
00087       void loadStream( AudioIStreamPtr stream, int numsamps )
00088       {
00089          mWaveTable.resize( numsamps );
00090          stream->read( mWaveTable.data(), mWaveTable.size() );
00091          
00092          mWaveTableIterator = 0;
00093       }
00094       
00095       void loadFile( const std::string& name )
00096       {
00097          WavAudioIStream* wav = new WavAudioIStream;
00098          wav->setName( name );
00099          wav->open( AudioFormat( AudioFormat::FLOAT32, 1, 44100 ) );
00100          AudioIStreamPtr stream = syn::AudioIStreamPtr( wav );
00101          this->loadStream( stream, wav->numsamps() );
00102          wav->close();
00103          
00104          mWaveTableIterator = 0;
00105       }
00106 
00107       void setWaveTable( const SampleBuffer1f& wt )
00108       {
00109          mWaveTable = wt;
00110       }
00111       
00122       virtual void setFreq( float freq )
00123       {
00124          // be graceful (ignore bad input, don't dump)
00125          if (freq <= 0.0f) 
00126             return;
00127          
00128          mFreq = freq;
00129          
00130          // get the floor (because the size of the wavetable
00131          // is often calculated from the floor of samprate / freq)
00132          // then invert it to get the freq / samprate for the EQ
00133          // this ensures that a wavetable produces from 80Hz wave
00134          // played back at 80hz will produce an increment of exactly 1
00135          float x = 1.0f / (float)(int)(44100.0f / freq);
00136          
00137          // equation can be found in CMT p.93 eq #1
00138          mIncrement = ((float)mWaveTable.size()) * x;
00139       }
00140       
00153       void setFreqControl( float param )
00154       {
00155          mFreqControl = param;
00156          mIncrementScalar = Math::fast_exp2( mFreqControl * mFreqControlSens );
00157       }    
00158       
00162       void setFreqControlSensitivity( float octaves = 1.0f )
00163       {
00164          mFreqControlSens = octaves;
00165          mIncrementScalar = Math::fast_exp2( mFreqControl * mFreqControlSens );
00166       } 
00167       
00168       enum Interp
00169       {
00170          NONE, LINEAR
00171       };
00172          
00173       void setInterp( Interp in )
00174       {
00175          mInterp = in;
00176       }
00177       
00178       void setLoop( bool state )
00179       {
00180          mLoop = state;
00181       }
00182       
00183       void setRetriggerable( bool state )
00184       {
00185          mRetrig = state;
00186       }
00187       
00188       void trigger()
00189       {
00190          if (mRetrig == true || mPlaying == false)
00191             mWaveTableIterator = 0; // reset wave pointer
00192          mPlaying = true;
00193       }
00194       
00196       void release()
00197       {
00198          mPlaying = false;
00199       }
00200       
00201 
00202       float freq() const { return mFreq; }
00203       float freqControl() const { return mFreqControl; }
00204       float freqControlSensitivity() const { return mFreqControlSens; }
00205       Interp interp() const { return mInterp; }
00206       bool loop() const { return mLoop; }
00207       bool retriggerable() const { return mRetrig; }
00208       
00209 
00213       virtual void getParam( const std::string& key, MultivariateType& value )
00214       {
00215          if (key == "freq")
00216             value.setValue<float>( mFreq );
00217          else if (key == "freqcontrol")
00218             value.setValue<float>( mFreqControl );
00219          else if (key == "freqcontrolsens")
00220             value.setValue<float>( mFreqControlSens );
00221          else if (key == "interp")
00222             value.setValue<unsigned int>( mInterp );
00223          else if (key == "loop")
00224             value.setValue<bool>( mLoop );
00225          else if (key == "filename")
00226             value.setValue<std::string>( "unknown" );
00227       }
00228 
00230       virtual void setParam( const std::string& key, const MultivariateType& value )
00231       {
00232          if (key == "freq")
00233             this->setFreq( value.getValue<float>() );
00234          else if (key == "freqcontrol")
00235             this->setFreqControl( value.getValue<float>() );
00236          else if (key == "trigger")
00237             this->trigger();
00238          else if (key == "release")
00239             this->release();
00240          else if (key == "freqcontrolsens")
00241             this->setFreqControl( value.getValue<float>() );
00242          else if (key == "interp")
00243             this->setInterp( (Interp)value.getValue<unsigned int>() );
00244          else if (key == "loop")
00245             this->setLoop( value.getValue<bool>() );
00246          else if (key == "filename")
00247             this->loadFile( value );
00248       }
00250       
00251    private:
00252       TerminalPtr mFreqControlInput;
00253       TerminalPtr mMonoAudioOutput;
00254 
00255       int mTrigger;
00256       SampleBuffer1f mWaveTable;
00257       float mWaveTableIterator;
00258       SampleBuffer1f* mWriteBuf, *mReadBuf;
00259       unsigned int mWriteBufIt, mReadBufIt;
00260       float mFreqControl, mFreqControlSens;
00261       bool mRetrig, mPlaying, mLoop;
00262       
00263       Interp mInterp;
00264       unsigned int mSkipFreqInputs;
00265 
00266       float mIncrement, mIncrementScalar;
00267       float mFreq;
00268       
00270       /*#  Terminal lnkTerminal; */
00271 
00273       /*#  Terminal lnkTerminal; */
00274    };  
00275 }
00276 #endif

Generated at Mon Apr 15 09:26:07 2002 for subsynth by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001