00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00125 if (freq <= 0.0f)
00126 return;
00127
00128 mFreq = freq;
00129
00130
00131
00132
00133
00134
00135 float x = 1.0f / (float)(int)(44100.0f / freq);
00136
00137
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;
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
00271
00273
00274 };
00275 }
00276 #endif