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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
/**
* OpenAL cross platform audio library
* Copyright (C) 2018 by Raul Herraiz.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#include "config.h"
#include <math.h>
#include <stdlib.h>
#include "alMain.h"
#include "alAuxEffectSlot.h"
#include "alError.h"
#include "alu.h"
#include "filters/defs.h"
#define STFT_SIZE 1024
#define STFT_HALF_SIZE (STFT_SIZE>>1)
#define OVERSAMP (1<<2)
#define STFT_STEP (STFT_SIZE / OVERSAMP)
#define FIFO_LATENCY (STFT_STEP * (OVERSAMP-1))
typedef struct ALcomplex {
ALfloat Real;
ALfloat Imag;
} ALcomplex;
typedef struct ALphasor {
ALfloat Amplitude;
ALfloat Phase;
} ALphasor;
typedef struct ALFrequencyDomain {
ALfloat Amplitude;
ALfloat Frequency;
} ALfrequencyDomain;
typedef struct ALpshifterState {
DERIVE_FROM_TYPE(ALeffectState);
/* Effect parameters */
ALsizei count;
ALfloat PitchShift;
ALfloat FreqBin;
/*Effects buffers*/
ALfloat InFIFO[STFT_SIZE];
ALfloat OutFIFO[STFT_STEP];
ALfloat LastPhase[STFT_HALF_SIZE+1];
ALfloat SumPhase[STFT_HALF_SIZE+1];
ALfloat OutputAccum[STFT_SIZE];
ALfloat window[STFT_SIZE];
ALcomplex FFTbuffer[STFT_SIZE];
ALfrequencyDomain Analysis_buffer[STFT_HALF_SIZE+1];
ALfrequencyDomain Syntesis_buffer[STFT_HALF_SIZE+1];
alignas(16) ALfloat BufferOut[BUFFERSIZE];
/* Effect gains for each output channel */
ALfloat CurrentGains[MAX_OUTPUT_CHANNELS];
ALfloat TargetGains[MAX_OUTPUT_CHANNELS];
} ALpshifterState;
static ALvoid ALpshifterState_Destruct(ALpshifterState *state);
static ALboolean ALpshifterState_deviceUpdate(ALpshifterState *state, ALCdevice *device);
static ALvoid ALpshifterState_update(ALpshifterState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props);
static ALvoid ALpshifterState_process(ALpshifterState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels);
DECLARE_DEFAULT_ALLOCATORS(ALpshifterState)
DEFINE_ALEFFECTSTATE_VTABLE(ALpshifterState);
/* Converts ALcomplex to ALphasor*/
static inline ALphasor rect2polar( ALcomplex number )
{
ALphasor polar;
polar.Amplitude = sqrtf ( number.Real*number.Real + number.Imag*number.Imag );
polar.Phase = atan2f( number.Imag , number.Real );
return polar;
}
/* Converts ALphasor to ALcomplex*/
static inline ALcomplex polar2rect( ALphasor number )
{
ALcomplex cartesian;
cartesian.Real = number.Amplitude * cosf( number.Phase );
cartesian.Imag = number.Amplitude * sinf( number.Phase );
return cartesian;
}
/* Addition of two complex numbers (ALcomplex format)*/
static inline ALcomplex complex_add( ALcomplex a, ALcomplex b )
{
ALcomplex result;
result.Real = ( a.Real + b.Real );
result.Imag = ( a.Imag + b.Imag );
return result;
}
/* Subtraction of two complex numbers (ALcomplex format)*/
static inline ALcomplex complex_sub( ALcomplex a, ALcomplex b )
{
ALcomplex result;
result.Real = ( a.Real - b.Real );
result.Imag = ( a.Imag - b.Imag );
return result;
}
/* Multiplication of two complex numbers (ALcomplex format)*/
static inline ALcomplex complex_mult( ALcomplex a, ALcomplex b )
{
ALcomplex result;
result.Real = ( a.Real * b.Real - a.Imag * b.Imag );
result.Imag = ( a.Imag * b.Real + a.Real * b.Imag );
return result;
}
/* Iterative implementation of 2-radix FFT (In-place algorithm). Sign = -1 is FFT and 1 is
iFFT (inverse). Fills FFTBuffer[0...FFTSize-1] with the Discrete Fourier Transform (DFT)
of the time domain data stored in FFTBuffer[0...FFTSize-1]. FFTBuffer is an array of
complex numbers (ALcomplex), FFTSize MUST BE power of two.*/
static inline ALvoid FFT(ALcomplex *FFTBuffer, ALsizei FFTSize, ALfloat Sign)
{
ALfloat arg;
ALsizei i, j, k, mask, step, step2;
ALcomplex temp, u, w;
/*bit-reversal permutation applied to a sequence of FFTSize items*/
for (i = 1; i < FFTSize-1; i++ )
{
for ( mask = 0x1, j = 0; mask < FFTSize; mask <<= 1 )
{
if ( ( i & mask ) != 0 ) j++;
j <<= 1;
}
j >>= 1;
if ( i < j )
{
temp = FFTBuffer[i];
FFTBuffer[i] = FFTBuffer[j];
FFTBuffer[j] = temp;
}
}
/* Iterative form of Danielson�Lanczos lemma */
for ( i = 1, step = 2; i < FFTSize; i<<=1, step <<= 1 )
{
step2 = step >> 1;
arg = F_PI / step2;
w.Real = cosf( arg );
w.Imag = sinf( arg ) * Sign;
u.Real = 1.0f;
u.Imag = 0.0f;
for ( j = 0; j < step2; j++ )
{
for ( k = j; k < FFTSize; k += step )
{
temp = complex_mult( FFTBuffer[k+step2], u );
FFTBuffer[k+step2] = complex_sub( FFTBuffer[k], temp );
FFTBuffer[k] = complex_add( FFTBuffer[k], temp );
}
u = complex_mult(u,w);
}
}
}
static void ALpshifterState_Construct(ALpshifterState *state)
{
ALsizei i;
ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
SET_VTABLE2(ALpshifterState, ALeffectState, state);
/* Create lockup table of the Hann window for the desired size, i.e. STFT_size */
for ( i = 0; i < STFT_SIZE>>1 ; i++ )
{
state->window[i] = state->window[STFT_SIZE-(i+1)]
= 0.5f * ( 1 - cosf(F_TAU*(ALfloat)i/(ALfloat)(STFT_SIZE-1)));
}
}
static ALvoid ALpshifterState_Destruct(ALpshifterState *state)
{
ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
}
static ALboolean ALpshifterState_deviceUpdate(ALpshifterState *state, ALCdevice *device)
{
/* (Re-)initializing parameters and clear the buffers. */
state->count = FIFO_LATENCY;
state->PitchShift = 1.0f;
state->FreqBin = device->Frequency / (ALfloat)STFT_SIZE;
memset(state->InFIFO, 0, sizeof(state->InFIFO));
memset(state->OutFIFO, 0, sizeof(state->OutFIFO));
memset(state->FFTbuffer, 0, sizeof(state->FFTbuffer));
memset(state->LastPhase, 0, sizeof(state->LastPhase));
memset(state->SumPhase, 0, sizeof(state->SumPhase));
memset(state->OutputAccum, 0, sizeof(state->OutputAccum));
memset(state->Analysis_buffer, 0, sizeof(state->Analysis_buffer));
memset(state->Syntesis_buffer, 0, sizeof(state->Syntesis_buffer));
memset(state->CurrentGains, 0, sizeof(state->CurrentGains));
memset(state->TargetGains, 0, sizeof(state->TargetGains));
return AL_TRUE;
}
static ALvoid ALpshifterState_update(ALpshifterState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props)
{
const ALCdevice *device = context->Device;
ALfloat coeffs[MAX_AMBI_COEFFS];
state->PitchShift = powf(2.0f,
(ALfloat)(props->Pshifter.CoarseTune*100 + props->Pshifter.FineTune) / 1200.0f
);
CalcAngleCoeffs(0.0f, 0.0f, 0.0f, coeffs);
ComputeDryPanGains(&device->Dry, coeffs, slot->Params.Gain, state->TargetGains);
}
static ALvoid ALpshifterState_process(ALpshifterState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
{
/* Pitch shifter engine based on the work of Stephan Bernsee.
* http://blogs.zynaptiq.com/bernsee/pitch-shifting-using-the-ft/
*/
static const ALfloat expected = F_TAU / (ALfloat)OVERSAMP;
const ALfloat freq_bin = state->FreqBin;
ALfloat *restrict bufferOut = state->BufferOut;
ALsizei count = state->count;
ALsizei i, j, k;
for(i = 0;i < SamplesToDo;)
{
do {
/* Fill FIFO buffer with samples data */
state->InFIFO[count] = SamplesIn[0][i];
bufferOut[i] = state->OutFIFO[count - FIFO_LATENCY];
count++;
} while(++i < SamplesToDo && count < STFT_SIZE);
/* Check whether FIFO buffer is filled */
if(count < STFT_SIZE) break;
count = FIFO_LATENCY;
/* Real signal windowing and store in FFTbuffer */
for(k = 0;k < STFT_SIZE;k++)
{
state->FFTbuffer[k].Real = state->InFIFO[k] * state->window[k];
state->FFTbuffer[k].Imag = 0.0f;
}
/* ANALYSIS */
/* Apply FFT to FFTbuffer data */
FFT(state->FFTbuffer, STFT_SIZE, -1.0f);
/* Analyze the obtained data. Since the real FFT is symmetric, only
* STFT_HALF_SIZE+1 samples are needed.
*/
for(k = 0;k < STFT_HALF_SIZE+1;k++)
{
ALphasor component;
ALfloat tmp;
/* Compute amplitude and phase */
component = rect2polar(state->FFTbuffer[k]);
/* Compute phase difference and subtract expected phase difference */
tmp = (component.Phase - state->LastPhase[k]) - (ALfloat)k*expected;
/* Map delta phase into +/- Pi interval */
j = fastf2i(tmp / F_PI);
tmp -= F_PI * (ALfloat)(j + (j%2));
/* Get deviation from bin frequency from the +/- Pi interval */
tmp /= expected;
/* Compute the k-th partials' true frequency, twice the amplitude
* for maintain the gain (because half of bins are used) and store
* amplitude and true frequency in analysis buffer.
*/
state->Analysis_buffer[k].Amplitude = 2.0f * component.Amplitude;
state->Analysis_buffer[k].Frequency = ((ALfloat)k + tmp) * freq_bin;
/* Store actual phase[k] for the calculations in the next frame*/
state->LastPhase[k] = component.Phase;
}
/* PROCESSING */
/* pitch shifting */
for(k = 0;k < STFT_HALF_SIZE+1;k++)
{
state->Syntesis_buffer[k].Amplitude = 0.0f;
state->Syntesis_buffer[k].Frequency = 0.0f;
}
for(k = 0;k < STFT_HALF_SIZE+1;k++)
{
j = fastf2i((ALfloat)k * state->PitchShift);
if(j >= STFT_HALF_SIZE+1) break;
state->Syntesis_buffer[j].Amplitude += state->Analysis_buffer[k].Amplitude;
state->Syntesis_buffer[j].Frequency = state->Analysis_buffer[k].Frequency *
state->PitchShift;
}
/* SYNTHESIS */
/* Synthesis the processing data */
for(k = 0;k < STFT_HALF_SIZE+1;k++)
{
ALphasor component;
ALfloat tmp;
/* Compute bin deviation from scaled freq */
tmp = state->Syntesis_buffer[k].Frequency/freq_bin - (ALfloat)k;
/* Calculate actual delta phase and accumulate it to get bin phase */
state->SumPhase[k] += ((ALfloat)k + tmp) * expected;
component.Amplitude = state->Syntesis_buffer[k].Amplitude;
component.Phase = state->SumPhase[k];
/* Compute phasor component to cartesian complex number and storage it into FFTbuffer*/
state->FFTbuffer[k] = polar2rect(component);
}
/* zero negative frequencies for recontruct a real signal */
for(k = STFT_HALF_SIZE+1;k < STFT_SIZE;k++)
{
state->FFTbuffer[k].Real = 0.0f;
state->FFTbuffer[k].Imag = 0.0f;
}
/* Apply iFFT to buffer data */
FFT(state->FFTbuffer, STFT_SIZE, 1.0f);
/* Windowing and add to output */
for(k = 0;k < STFT_SIZE;k++)
state->OutputAccum[k] += 2.0f * state->window[k]*state->FFTbuffer[k].Real /
(STFT_HALF_SIZE * OVERSAMP);
/* Shift accumulator, input & output FIFO */
for(k = 0;k < STFT_STEP;k++) state->OutFIFO[k] = state->OutputAccum[k];
for(j = 0;k < STFT_SIZE;k++,j++) state->OutputAccum[j] = state->OutputAccum[k];
for(;j < STFT_SIZE;j++) state->OutputAccum[j] = 0.0f;
for(k = 0;k < FIFO_LATENCY;k++)
state->InFIFO[k] = state->InFIFO[k+STFT_STEP];
}
state->count = count;
/* Now, mix the processed sound data to the output. */
MixSamples(bufferOut, NumChannels, SamplesOut, state->CurrentGains, state->TargetGains,
maxi(SamplesToDo, 512), 0, SamplesToDo);
}
typedef struct PshifterStateFactory {
DERIVE_FROM_TYPE(EffectStateFactory);
} PshifterStateFactory;
static ALeffectState *PshifterStateFactory_create(PshifterStateFactory *UNUSED(factory))
{
ALpshifterState *state;
NEW_OBJ0(state, ALpshifterState)();
if(!state) return NULL;
return STATIC_CAST(ALeffectState, state);
}
DEFINE_EFFECTSTATEFACTORY_VTABLE(PshifterStateFactory);
EffectStateFactory *PshifterStateFactory_getFactory(void)
{
static PshifterStateFactory PshifterFactory = { { GET_VTABLE2(PshifterStateFactory, EffectStateFactory) } };
return STATIC_CAST(EffectStateFactory, &PshifterFactory);
}
void ALpshifter_setParamf(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALfloat UNUSED(val))
{
alSetError( context, AL_INVALID_ENUM, "Invalid pitch shifter float property 0x%04x", param );
}
void ALpshifter_setParamfv(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, const ALfloat *UNUSED(vals))
{
alSetError( context, AL_INVALID_ENUM, "Invalid pitch shifter float-vector property 0x%04x", param );
}
void ALpshifter_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
{
ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_PITCH_SHIFTER_COARSE_TUNE:
if(!(val >= AL_PITCH_SHIFTER_MIN_COARSE_TUNE && val <= AL_PITCH_SHIFTER_MAX_COARSE_TUNE))
SETERR_RETURN(context, AL_INVALID_VALUE,,"Pitch shifter coarse tune out of range");
props->Pshifter.CoarseTune = val;
break;
case AL_PITCH_SHIFTER_FINE_TUNE:
if(!(val >= AL_PITCH_SHIFTER_MIN_FINE_TUNE && val <= AL_PITCH_SHIFTER_MAX_FINE_TUNE))
SETERR_RETURN(context, AL_INVALID_VALUE,,"Pitch shifter fine tune out of range");
props->Pshifter.FineTune = val;
break;
default:
alSetError(context, AL_INVALID_ENUM, "Invalid pitch shifter integer property 0x%04x", param);
}
}
void ALpshifter_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
{
ALpshifter_setParami(effect, context, param, vals[0]);
}
void ALpshifter_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
{
const ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_PITCH_SHIFTER_COARSE_TUNE:
*val = (ALint)props->Pshifter.CoarseTune;
break;
case AL_PITCH_SHIFTER_FINE_TUNE:
*val = (ALint)props->Pshifter.FineTune;
break;
default:
alSetError(context, AL_INVALID_ENUM, "Invalid pitch shifter integer property 0x%04x", param);
}
}
void ALpshifter_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
{
ALpshifter_getParami(effect, context, param, vals);
}
void ALpshifter_getParamf(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALfloat *UNUSED(val))
{
alSetError(context, AL_INVALID_ENUM, "Invalid pitch shifter float property 0x%04x", param);
}
void ALpshifter_getParamfv(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALfloat *UNUSED(vals))
{
alSetError(context, AL_INVALID_ENUM, "Invalid pitch shifter float vector-property 0x%04x", param);
}
DEFINE_ALEFFECT_VTABLE(ALpshifter);
|