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 #include "syn/Util/Generator.h"
00032 #include "syn/Core/SampleBufferRepos.h"
00033 #include "syn/Module/WaveTableOscModule.h"
00034
00035 namespace syn
00036 {
00037
00038 WaveTableOscModule::WaveTableOscModule(): Module(),
00039 mWriteBuf( NULL ), mReadBuf( NULL ),
00040 mFreqControl( 0.0f ), mFreqControlSens( 1.0f ),
00041 mRetrig( true ), mPlaying( false ), mLoop( true ),
00042 mInterp( LINEAR ), mSkipFreqInputs( 0 ),
00043 mIncrement( 1.0f ), mIncrementScalar( 1.0f )
00044 {
00045 mFreqControlInput = this->createInput( "freq" );
00046 mMonoAudioOutput = this->createOutput( "mono audio" );
00047 this->setFreqControl( 0 );
00048 }
00049
00050 WaveTableOscModule::~WaveTableOscModule()
00051 {
00052 }
00053
00054 bool WaveTableOscModule::isOpen() { return true; }
00055
00056 bool WaveTableOscModule::open()
00057 {
00058
00059 if (mWaveTable.size() == 0)
00060 {
00061 TriangleOsc osc;
00062 mWaveTable.resize( osc.samplesPerOsc() );
00063 for (unsigned int x = 0; x < osc.samplesPerOsc(); ++x)
00064 {
00065 mWaveTable[x] = osc.generate();
00066 }
00067 }
00068
00069
00070 mMonoAudioOutput->setHighMark( 2 );
00071 mMonoAudioOutput->setLowMark( 1 );
00072
00073 return true;
00074 }
00075
00076 void WaveTableOscModule::close()
00077 {
00078 }
00079
00081 void WaveTableOscModule::update()
00082 {
00083
00084
00085
00086 if ((!mFreqControlInput->done() && mFreqControlInput->empty()) || mMonoAudioOutput->high())
00087 {
00088 mMonoAudioOutput->setDone( false );
00089 return;
00090 }
00091
00092
00093
00094 this->setPutCount( 0 );
00095
00096
00097 if (mPlaying == true)
00098 {
00099
00100 if (!mFreqControlInput->done())
00101 {
00102 if (mReadBuf == NULL)
00103 {
00104 mReadBuf = mFreqControlInput->front();
00105 mFreqControlInput->pop();
00106 mReadBufIt = 0;
00107 }
00108
00109 if (mWriteBuf == NULL)
00110 {
00111 SampleBufferRepos::instance()->take( mWriteBuf );
00112 mWriteBufIt = 0;
00113 }
00114
00115
00116 switch (mInterp)
00117 {
00118
00119 case NONE:
00120 {
00121 while (mReadBufIt < mReadBuf->size() && mWriteBufIt < mWriteBuf->size() && mPlaying)
00122 {
00123
00124
00125 mSkipFreqInputs = (mSkipFreqInputs + 1) % 10;
00126 if (mSkipFreqInputs == 0)
00127 {
00128 mFreqControl = (*(mReadBuf))[mReadBufIt];
00129 mIncrementScalar = Math::fast_exp2( mFreqControl * mFreqControlSens );
00130 }
00131
00132
00133 (*mWriteBuf)[mWriteBufIt] = mWaveTable[(int)mWaveTableIterator];
00134
00135
00136
00137 mWaveTableIterator += mIncrement * mIncrementScalar;
00138 float next_wt_it = fmod( mWaveTableIterator, (float)mWaveTable.size() );
00139 if (mWaveTableIterator != next_wt_it)
00140 {
00141 mWaveTableIterator = next_wt_it;
00142 if (mLoop == false)
00143 mPlaying = false;
00144 }
00145
00146 ++mReadBufIt;
00147 ++mWriteBufIt;
00148 }
00149 }
00150 break;
00151
00152
00153 case LINEAR:
00154 {
00155 while (mReadBufIt < mReadBuf->size() && mWriteBufIt < mWriteBuf->size() && mPlaying)
00156 {
00157
00158
00159 mSkipFreqInputs = (mSkipFreqInputs + 1) % 10;
00160 if (mSkipFreqInputs == 0)
00161 {
00162 mFreqControl = (*(mReadBuf))[mReadBufIt];
00163 mIncrement = Math::fast_exp2( mFreqControl * mFreqControlSens );
00164 }
00165
00166
00167 float next = mWaveTableIterator + 1.0f;
00168 if (next >= (float)mWaveTable.size())
00169 next = next - (float)mWaveTable.size();
00170
00171 float first = mWaveTable[(int)mWaveTableIterator];
00172 float second = mWaveTable[(int)next];
00173 float weight = mWaveTableIterator - (float)(int)mWaveTableIterator;
00174 (*mWriteBuf)[mWriteBufIt] = first + ((second - first) * weight);
00175
00176
00177
00178 mWaveTableIterator += mIncrement * mIncrementScalar;
00179 float next_wt_it = fmod( mWaveTableIterator, (float)mWaveTable.size() );
00180 if (mWaveTableIterator != next_wt_it)
00181 {
00182 mWaveTableIterator = next_wt_it;
00183 if (mLoop == false)
00184 mPlaying = false;
00185 }
00186
00187 ++mReadBufIt;
00188 ++mWriteBufIt;
00189 }
00190 }
00191 break;
00192
00193 default:
00194 assert( false && "undefined interpolation param" );
00195 break;
00196 }
00197
00198
00199 if (mReadBufIt >= mReadBuf->size())
00200 {
00201 SampleBufferRepos::instance()->putback( mReadBuf );
00202 mReadBufIt = 0;
00203 mReadBuf = NULL;
00204 }
00205
00206
00207 if (mWriteBufIt >= mWriteBuf->size())
00208 {
00209 mMonoAudioOutput->push( mWriteBuf );
00210 mMonoAudioOutput->setDone( false );
00211 this->setPutCount( mWriteBuf->size() );
00212 mWriteBuf = NULL;
00213 mWriteBufIt = 0;
00214 }
00215 }
00216
00217
00218 else
00219 {
00220 SampleBufferRepos::instance()->take( mWriteBuf );
00221
00222
00223 switch (mInterp)
00224 {
00225
00226 case NONE:
00227 {
00228 for (unsigned int x = 0; x < mWriteBuf->size() && mPlaying; ++x)
00229 {
00230 (*mWriteBuf)[x] = mWaveTable[(int)mWaveTableIterator];
00231
00232
00233
00234 mWaveTableIterator += mIncrement * mIncrementScalar;
00235 float next_wt_it = fmod( mWaveTableIterator, (float)mWaveTable.size() );
00236 if (mWaveTableIterator != next_wt_it)
00237 {
00238 mWaveTableIterator = next_wt_it;
00239 if (mLoop == false)
00240 mPlaying = false;
00241 }
00242 }
00243 }
00244 break;
00245
00246
00247 case LINEAR:
00248 {
00249 for (unsigned int x = 0; x < mWriteBuf->size() && mPlaying; ++x)
00250 {
00251
00252 float next = mWaveTableIterator + 1.0f;
00253 if (next >= (float)mWaveTable.size())
00254 next = next - (float)mWaveTable.size();
00255
00256 float first = mWaveTable[(int)mWaveTableIterator];
00257 float second = mWaveTable[(int)next];
00258 float weight = mWaveTableIterator - (float)(int)mWaveTableIterator;
00259 (*mWriteBuf)[x] = first + ((second - first) * weight);
00260
00261
00262
00263 mWaveTableIterator += mIncrement * mIncrementScalar;
00264 float next_wt_it = fmod( mWaveTableIterator, (float)mWaveTable.size() );
00265 if (mWaveTableIterator != next_wt_it)
00266 {
00267 mWaveTableIterator = next_wt_it;
00268 if (mLoop == false)
00269 mPlaying = false;
00270 }
00271 }
00272 }
00273 break;
00274
00275 default:
00276 assert( false && "undefined interpolation param" );
00277 break;
00278 }
00279
00280 this->setPutCount( mWriteBuf->size() );
00281 mMonoAudioOutput->push( mWriteBuf );
00282 mWriteBuf = NULL;
00283 mMonoAudioOutput->setDone( false );
00284 }
00285 }
00286
00287
00288 else
00289 {
00290 mMonoAudioOutput->setDone( true );
00291 }
00292 }
00293 }