-
Notifications
You must be signed in to change notification settings - Fork 938
/
WaveTable.cpp
428 lines (334 loc) · 12.2 KB
/
WaveTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
Copyright (c) 2014, The Cinder Project
This code is intended to be used with the Cinder C++ library, http://libcinder.org
Redistribution and use in source and binary forms, with or without modification, are permitted provided that
the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "cinder/audio/WaveTable.h"
#include "cinder/audio/dsp/Dsp.h"
#include "cinder/audio/Utilities.h"
#include "cinder/CinderMath.h"
#include "cinder/Timer.h" // TEMP
using namespace std;
namespace {
// gibbs effect reduction based on http://www.musicdsp.org/files/bandlimited.pdf
inline float calcGibbsReduceCoeff( size_t partial, size_t numPartials )
{
if( numPartials <= 1 )
return 1;
float result = ci::math<float>::cos( float( partial * M_PI * 0.5 ) / (float)numPartials );
return result * result;
}
inline float calcTableIndex( float f0Midi, float minRange, float maxRange, size_t numTables )
{
const float midiRangePerTable = ( maxRange - minRange ) / ( numTables - 1 );
return 1 + ( f0Midi - minRange ) / midiRangePerTable;
}
#if 0
// truncate, phase range: 0-1
inline float tableLookup( const float *table, size_t tableSize, float phase )
{
return table[ size_t( phase * tableSize ) ];
}
#else
// linear interpolation, phase range: 0 - 1
inline float tableLookup( const float *table, size_t tableSize, float phase )
{
float lookup = phase * tableSize;
size_t index1 = (size_t)lookup;
size_t index2 = ( index1 + 1 ) & ( tableSize - 1 ); // faster mod that only works if tableSize is a power of 2
float val1 = table[index1];
float val2 = table[index2];
float frac = lookup - (float)index1;
return val2 + frac * ( val2 - val1 );
}
#endif
} // anonymous namespace
namespace cinder { namespace audio {
// ----------------------------------------------------------------------------------------------------
// WaveTable
// ----------------------------------------------------------------------------------------------------
WaveTable::WaveTable( size_t sampleRate, size_t tableSize )
: mTableSize( tableSize )
{
setSampleRate( sampleRate );
}
void WaveTable::setSampleRate( size_t sampleRate )
{
CI_ASSERT( sampleRate );
mSampleRate = sampleRate;
mSamplePeriod = 1.0f / (float)sampleRate;
}
void WaveTable::resize( size_t tableSize )
{
if( mTableSize == tableSize && mBuffer.getNumFrames() == tableSize )
return;
mTableSize = tableSize;
mBuffer.setSize( tableSize, 1 );
}
void WaveTable::fillSine()
{
resize( mTableSize );
vector<float> partials( 1, 1 );
fillSinesum( mBuffer.getData(), mTableSize, partials );
}
void WaveTable::fillSinesum( float *array, size_t length, const std::vector<float> &partials )
{
memset( array, 0, length * sizeof( float ) );
double phase = 0;
const double phaseIncr = ( 2.0 * M_PI ) / (double)length;
for( size_t i = 0; i < length; i++ ) {
double partialPhase = phase;
for( size_t p = 0; p < partials.size(); p++ ) {
array[i] += partials[p] * math<float>::sin( (float)partialPhase );
partialPhase += phase;
}
phase += phaseIncr;
}
}
float WaveTable::lookup( float phase ) const
{
return tableLookup( mBuffer.getData(), mTableSize, phase );
}
float WaveTable::lookup( float *outputArray, size_t outputLength, float currentPhase, float freq ) const
{
const float phaseIncr = freq * mSamplePeriod;
const float *table = mBuffer.getData();
const size_t tableSize = mTableSize;
for( size_t i = 0; i < outputLength; i++ ) {
outputArray[i] = tableLookup( table, tableSize, currentPhase );
currentPhase = fract( currentPhase + phaseIncr );
}
return currentPhase;
}
float WaveTable::lookup( float *outputArray, size_t outputLength, float currentPhase, const float *freqArray ) const
{
const size_t tableSize = mTableSize;
const float samplePeriod = mSamplePeriod;
const float *table = mBuffer.getData();
for( size_t i = 0; i < outputLength; i++ ) {
outputArray[i] = tableLookup( table, tableSize, currentPhase );
currentPhase = fract( currentPhase + freqArray[i] * samplePeriod );
}
return currentPhase;
}
void WaveTable::copyTo( float *array ) const
{
memcpy( array, mBuffer.getData(), mTableSize * sizeof( float ) );
}
void WaveTable::copyFrom( const float *array )
{
memcpy( mBuffer.getData(), array, mTableSize * sizeof( float ) );
}
// ----------------------------------------------------------------------------------------------------
// WaveTable2d
// ----------------------------------------------------------------------------------------------------
WaveTable2d::WaveTable2d( size_t sampleRate, size_t tableSize, size_t numTables )
: WaveTable( sampleRate, tableSize ), mNumTables( numTables )
{
}
void WaveTable2d::resize( size_t tableSize, size_t numTables )
{
bool needsResize = false;
if( mTableSize != tableSize || mBuffer.getNumFrames() != tableSize ) {
mTableSize = tableSize;
needsResize = true;
}
if( mNumTables != numTables || mBuffer.getNumChannels() != numTables ) {
mNumTables = numTables;
needsResize = true;
}
if( needsResize )
mBuffer.setSize( mTableSize, mNumTables );
}
void WaveTable2d::fillBandlimited( WaveformType type )
{
calcLimits();
Timer timer( true );
resize( mTableSize, mNumTables );
for( size_t i = 0; i < mNumTables; i++ ) {
float *table = mBuffer.getChannel( i );
// last table always has only one partial
if( i == mNumTables - 1 ) {
fillBandLimitedTable( type, table, 1 );
break;
}
size_t maxPartialsForFreq = getMaxHarmonicsForTable( i );
fillBandLimitedTable( type, table, maxPartialsForFreq );
}
}
// note: for at least sawtooth and square, this must be recomputed for every table so that gibbs reduction is accurate
void WaveTable2d::fillBandLimitedTable( WaveformType type, float *table, size_t numPartials )
{
vector<float> partials;
if( type == WaveformType::SINE )
partials.resize( 1 );
else
partials.resize( numPartials );
switch( type ) {
case WaveformType::SINE:
partials[0] = 1;
break;
case WaveformType::SQUARE:
// 1 / x for odd x
for( size_t x = 1; x <= partials.size(); x += 2 ) {
float m = calcGibbsReduceCoeff( x, partials.size() );
partials[x - 1] = m / float( x );
}
break;
case WaveformType::SAWTOOTH:
// 1 / x
for( size_t x = 1; x <= numPartials; x += 1 ) {
float m = calcGibbsReduceCoeff( x, partials.size() );
partials[x - 1] = m / float( x );
}
break;
case WaveformType::TRIANGLE: {
// 1 / x^2 for odd x, alternating + and -
float t = 1;
for( size_t x = 1; x <= partials.size(); x += 2 ) {
partials[x - 1] = t / float( x * x );
t *= -1;
}
break;
}
default:
CI_ASSERT_NOT_REACHABLE();
}
fillSinesum( table, mTableSize, partials );
dsp::normalize( table, mTableSize );
}
size_t WaveTable2d::getMaxHarmonicsForTable( size_t tableIndex ) const
{
const float nyquist = (float)mSampleRate * 0.5f;
const float midiRangePerTable = ( mMaxMidiRange - mMinMidiRange ) / ( mNumTables - 1 );
const float maxMidi = mMinMidiRange + tableIndex * midiRangePerTable;
const float maxF0 = midiToFreq( maxMidi );
size_t maxPartialsForFreq = size_t( nyquist / maxF0 );
return maxPartialsForFreq;
}
float WaveTable2d::calcBandlimitedTableIndex( float f0 ) const
{
const float f0Midi = freqToMidi( fabsf( f0 ) );
if( f0Midi <= mMinMidiRange )
return 0;
else if( f0Midi >= mMaxMidiRange )
return float( mNumTables - 1 );
return calcTableIndex( f0Midi, mMinMidiRange, mMaxMidiRange, mNumTables );
}
const float* WaveTable2d::getBandLimitedTable( float f0 ) const
{
size_t index = (size_t)calcBandlimitedTableIndex( f0 );
return mBuffer.getChannel( index );
}
std::tuple<const float*, const float*, float> WaveTable2d::getBandLimitedTablesLerp( float f0 ) const
{
float *table1, *table2;
float factor;
const float f0Midi = freqToMidi( fabsf( f0 ) );
if( f0Midi <= mMinMidiRange ) {
table1 = table2 = const_cast<float *>( mBuffer.getChannel( 0 ) );
factor = 0;
}
else if( f0Midi >= mMaxMidiRange ) {
table1 = table2 = const_cast<float *>( mBuffer.getChannel( mNumTables - 1 ) );
factor = 1;
}
else {
float index = calcTableIndex( f0Midi, mMinMidiRange, mMaxMidiRange, mNumTables );
size_t tableIndex1 = (size_t)index;
size_t tableIndex2 = ( tableIndex1 + 1 ) & ( mTableSize - 1 );
table1 = const_cast<float *>( mBuffer.getChannel( tableIndex1 ) );
table2 = const_cast<float *>( mBuffer.getChannel( tableIndex2 ) );
factor = index - (float)tableIndex1;
}
return make_tuple( table1, table2, factor );
}
float WaveTable2d::lookupBandlimited( float phase, float f0 ) const
{
const float *table = getBandLimitedTable( f0 );
return tableLookup( table, mTableSize, phase );
}
#if 1
// no table interpolation
float WaveTable2d::lookupBandlimited( float *outputArray, size_t outputLength, float currentPhase, float f0 ) const
{
const float phaseIncr = f0 * mSamplePeriod;
const float *table = getBandLimitedTable( f0 );
const size_t tableSize = mTableSize;
for( size_t i = 0; i < outputLength; i++ ) {
outputArray[i] = tableLookup( table, tableSize, currentPhase );
currentPhase = fract( currentPhase + phaseIncr );
}
return currentPhase;
}
float WaveTable2d::lookupBandlimited( float *outputArray, size_t outputLength, float currentPhase, const float *f0Array ) const
{
const size_t tableSize = mTableSize;
const float samplePeriod = mSamplePeriod;
for( size_t i = 0; i < outputLength; i++ ) {
const float f0 = f0Array[i];
const float *table = getBandLimitedTable( f0 );
outputArray[i] = tableLookup( table, tableSize, currentPhase );
currentPhase = fract( currentPhase + f0 * samplePeriod );
}
return currentPhase;
}
#else
// table interpoloation
float WaveTable2d::lookupBandlimited( float *outputArray, size_t outputLength, float currentPhase, float f0 ) const
{
const float phaseIncr = f0 * mSamplePeriod;
const size_t tableSize = mTableSize;
auto tables = getBandLimitedTablesLerp( f0 );
for( size_t i = 0; i < outputLength; i++ ) {
float a = tableLookup( get<0>( tables ), tableSize, currentPhase );
float b = tableLookup( get<1>( tables ), tableSize, currentPhase );
outputArray[i] = lerp( a, b, get<2>( tables ) );
currentPhase = fract( currentPhase + phaseIncr );
}
return currentPhase;
}
float WaveTable2d::lookupBandlimited( float *outputArray, size_t outputLength, float currentPhase, const float *f0Array ) const
{
const size_t tableSize = mTableSize;
const float samplePeriod = mSamplePeriod;
for( size_t i = 0; i < outputLength; i++ ) {
const float f0 = f0Array[i];
auto tables = getBandLimitedTablesLerp( f0 );
float a = tableLookup( get<0>( tables ), tableSize, currentPhase );
float b = tableLookup( get<1>( tables ), tableSize, currentPhase );
outputArray[i] = lerp( a, b, get<2>( tables ) );
currentPhase = fract( currentPhase + f0 * samplePeriod );
}
return currentPhase;
}
#endif
void WaveTable2d::copyTo( float *array, size_t tableIndex ) const
{
CI_ASSERT( tableIndex < mNumTables );
memcpy( array, mBuffer.getChannel( tableIndex ), mTableSize * sizeof( float ) );
}
void WaveTable2d::copyFrom( const float *array, size_t tableIndex )
{
CI_ASSERT( tableIndex < mNumTables );
memcpy( mBuffer.getChannel( tableIndex ), array, mTableSize * sizeof( float ) );
}
void WaveTable2d::calcLimits()
{
mMinMidiRange = freqToMidi( 20 );
mMaxMidiRange = freqToMidi( (float)mSampleRate / 4.0f ); // everything above can only have one partial
}
} } // namespace cinder::audio