summaryrefslogtreecommitdiffstats
path: root/Alc/effects/equalizer.c
blob: 3a77c63f61140c55877a3c56a0322debbab12008 (plain)
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
/**
 * OpenAL cross platform audio library
 * Copyright (C) 2013 by Mike Gorchak
 * 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., 59 Temple Place - Suite 330,
 *  Boston, MA  02111-1307, USA.
 * Or go to http://www.gnu.org/copyleft/lgpl.html
 */

#include "config.h"

#include <math.h>
#include <stdlib.h>

#include "alMain.h"
#include "alFilter.h"
#include "alAuxEffectSlot.h"
#include "alError.h"
#include "alu.h"


typedef struct ALequalizerStateFactory {
    DERIVE_FROM_TYPE(ALeffectStateFactory);
} ALequalizerStateFactory;

static ALequalizerStateFactory EqualizerFactory;


/*  The document  "Effects Extension Guide.pdf"  says that low and high  *
 *  frequencies are cutoff frequencies. This is not fully correct, they  *
 *  are corner frequencies for low and high shelf filters. If they were  *
 *  just cutoff frequencies, there would be no need in cutoff frequency  *
 *  gains, which are present.  Documentation for  "Creative Proteus X2"  *
 *  software describes  4-band equalizer functionality in a much better  *
 *  way.  This equalizer seems  to be a predecessor  of  OpenAL  4-band  *
 *  equalizer.  With low and high  shelf filters  we are able to cutoff  *
 *  frequencies below and/or above corner frequencies using attenuation  *
 *  gains (below 1.0) and amplify all low and/or high frequencies using  *
 *  gains above 1.0.                                                     *
 *                                                                       *
 *     Low-shelf       Low Mid Band      High Mid Band     High-shelf    *
 *      corner            center             center          corner      *
 *     frequency        frequency          frequency       frequency     *
 *    50Hz..800Hz     200Hz..3000Hz      1000Hz..8000Hz  4000Hz..16000Hz *
 *                                                                       *
 *          |               |                  |               |         *
 *          |               |                  |               |         *
 *   B -----+            /--+--\            /--+--\            +-----    *
 *   O      |\          |   |   |          |   |   |          /|         *
 *   O      | \        -    |    -        -    |    -        / |         *
 *   S +    |  \      |     |     |      |     |     |      /  |         *
 *   T      |   |    |      |      |    |      |      |    |   |         *
 * ---------+---------------+------------------+---------------+-------- *
 *   C      |   |    |      |      |    |      |      |    |   |         *
 *   U -    |  /      |     |     |      |     |     |      \  |         *
 *   T      | /        -    |    -        -    |    -        \ |         *
 *   O      |/          |   |   |          |   |   |          \|         *
 *   F -----+            \--+--/            \--+--/            +-----    *
 *   F      |               |                  |               |         *
 *          |               |                  |               |         *
 *                                                                       *
 * Gains vary from 0.126 up to 7.943, which means from -18dB attenuation *
 * up to +18dB amplification. Band width varies from 0.01 up to 1.0 in   *
 * octaves for two mid bands.                                            *
 *                                                                       *
 * Implementation is based on the "Cookbook formulae for audio EQ biquad *
 * filter coefficients" by Robert Bristow-Johnson                        *
 * http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt                   */

typedef enum ALEQFilterType {
    LOW_SHELF,
    HIGH_SHELF,
    PEAKING
} ALEQFilterType;

typedef struct ALEQFilter {
    ALEQFilterType type;
    ALfloat x[2]; /* History of two last input samples  */
    ALfloat y[2]; /* History of two last output samples */
    ALfloat a[3]; /* Transfer function coefficients "a" */
    ALfloat b[3]; /* Transfer function coefficients "b" */
} ALEQFilter;

typedef struct ALequalizerState {
    DERIVE_FROM_TYPE(ALeffectState);

    /* Effect gains for each channel */
    ALfloat Gain[MaxChannels];

    /* Effect parameters */
    ALEQFilter bandfilter[4];
} ALequalizerState;

static ALvoid ALequalizerState_Destruct(ALequalizerState *state)
{
    (void)state;
}

static ALboolean ALequalizerState_DeviceUpdate(ALequalizerState *state, ALCdevice *device)
{
    return AL_TRUE;
    (void)state;
    (void)device;
}

static ALvoid ALequalizerState_Update(ALequalizerState *state, ALCdevice *device, const ALeffectslot *slot)
{
    ALfloat frequency = (ALfloat)device->Frequency;
    ALfloat gain = sqrtf(1.0f / device->NumChan) * slot->Gain;
    ALuint it;

    for(it = 0;it < MaxChannels;it++)
        state->Gain[it] = 0.0f;
    for(it = 0; it < device->NumChan; it++)
    {
        enum Channel chan = device->Speaker2Chan[it];
        state->Gain[chan] = gain;
    }

    /* Calculate coefficients for the each type of filter */
    for(it = 0; it < 4; it++)
    {
        ALfloat gain;
        ALfloat filter_frequency;
        ALfloat bandwidth = 0.0f;
        ALfloat w0;
        ALfloat alpha = 0.0f;

        /* convert linear gains to filter gains */
        switch (it)
        {
            case 0: /* Low Shelf */
                 gain = powf(10.0f, (20.0f * log10f(slot->EffectProps.Equalizer.LowGain)) / 40.0f);
                 filter_frequency = slot->EffectProps.Equalizer.LowCutoff;
                 break;
            case 1: /* Peaking */
                 gain = powf(10.0f, (20.0f * log10f(slot->EffectProps.Equalizer.Mid1Gain)) / 40.0f);
                 filter_frequency = slot->EffectProps.Equalizer.Mid1Center;
                 bandwidth = slot->EffectProps.Equalizer.Mid1Width;
                 break;
            case 2: /* Peaking */
                 gain = powf(10.0f, (20.0f * log10f(slot->EffectProps.Equalizer.Mid2Gain)) / 40.0f);
                 filter_frequency = slot->EffectProps.Equalizer.Mid2Center;
                 bandwidth = slot->EffectProps.Equalizer.Mid2Width;
                 break;
            case 3: /* High Shelf */
                 gain = powf(10.0f, (20.0f * log10f(slot->EffectProps.Equalizer.HighGain)) / 40.0f);
                 filter_frequency = slot->EffectProps.Equalizer.HighCutoff;
                 break;
        }

        w0 = 2.0f*F_PI * filter_frequency / frequency;

        /* Calculate filter coefficients depending on filter type */
        switch(state->bandfilter[it].type)
        {
            case LOW_SHELF:
                 alpha = sinf(w0) / 2.0f * sqrtf((gain + 1.0f / gain) *
                                                 (1.0f / 0.75f - 1.0f) + 2.0f);
                 state->bandfilter[it].b[0] = gain * ((gain + 1.0f) -
                                                      (gain - 1.0f) * cosf(w0) +
                                                      2.0f * sqrtf(gain) * alpha);
                 state->bandfilter[it].b[1] = 2.0f * gain * ((gain - 1.0f) -
                                                             (gain + 1.0f) * cosf(w0));
                 state->bandfilter[it].b[2] = gain * ((gain + 1.0f) -
                                                      (gain - 1.0f) * cosf(w0) -
                                                      2.0f * sqrtf(gain) * alpha);
                 state->bandfilter[it].a[0] = (gain + 1.0f) +
                                              (gain - 1.0f) * cosf(w0) +
                                              2.0f * sqrtf(gain) * alpha;
                 state->bandfilter[it].a[1] = -2.0f * ((gain - 1.0f) +
                                              (gain + 1.0f) * cosf(w0));
                 state->bandfilter[it].a[2] = (gain + 1.0f) +
                                              (gain - 1.0f) * cosf(w0) -
                                              2.0f * sqrtf(gain) * alpha;
                 break;
            case HIGH_SHELF:
                 alpha = sinf(w0) / 2.0f * sqrtf((gain + 1.0f / gain) *
                                                 (1.0f / 0.75f - 1.0f) + 2.0f);
                 state->bandfilter[it].b[0] = gain * ((gain + 1.0f) +
                                                      (gain - 1.0f) * cosf(w0) +
                                                      2.0f * sqrtf(gain) * alpha);
                 state->bandfilter[it].b[1] = -2.0f * gain * ((gain - 1.0f) +
                                                              (gain + 1.0f) *
                                                              cosf(w0));
                 state->bandfilter[it].b[2] = gain * ((gain + 1.0f) +
                                                      (gain - 1.0f) * cosf(w0) -
                                                      2.0f * sqrtf(gain) * alpha);
                 state->bandfilter[it].a[0] = (gain + 1.0f) -
                                              (gain - 1.0f) * cosf(w0) +
                                              2.0f * sqrtf(gain) * alpha;
                 state->bandfilter[it].a[1] = 2.0f * ((gain - 1.0f) -
                                                      (gain + 1.0f) * cosf(w0));
                 state->bandfilter[it].a[2] = (gain + 1.0f) -
                                              (gain - 1.0f) * cosf(w0) -
                                              2.0f * sqrtf(gain) * alpha;
                 break;
            case PEAKING:
                 alpha = sinf(w0) * sinhf(logf(2.0f) / 2.0f * bandwidth * w0 / sinf(w0));
                 state->bandfilter[it].b[0] =  1.0f + alpha * gain;
                 state->bandfilter[it].b[1] = -2.0f * cosf(w0);
                 state->bandfilter[it].b[2] =  1.0f - alpha * gain;
                 state->bandfilter[it].a[0] =  1.0f + alpha / gain;
                 state->bandfilter[it].a[1] = -2.0f * cosf(w0);
                 state->bandfilter[it].a[2] =  1.0f - alpha / gain;
                 break;
        }
        state->bandfilter[it].b[0] /= state->bandfilter[it].a[0];
        state->bandfilter[it].b[1] /= state->bandfilter[it].a[0];
        state->bandfilter[it].b[2] /= state->bandfilter[it].a[0];
        state->bandfilter[it].a[0] /= state->bandfilter[it].a[0];
        state->bandfilter[it].a[1] /= state->bandfilter[it].a[0];
        state->bandfilter[it].a[2] /= state->bandfilter[it].a[0];
    }
}

static ALvoid ALequalizerState_Process(ALequalizerState *state, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE])
{
    ALuint base;
    ALuint it;
    ALuint kt;
    ALuint ft;

    for(base = 0;base < SamplesToDo;)
    {
        ALfloat temps[64];
        ALuint td = minu(SamplesToDo-base, 64);

        for(it = 0;it < td;it++)
        {
            ALfloat smp = SamplesIn[base+it];

            for(ft = 0;ft < 4;ft++)
            {
                ALEQFilter *filter = &state->bandfilter[ft];
                ALfloat outsmp;

                outsmp = filter->b[0] * smp +
                         filter->b[1] * filter->x[0] +
                         filter->b[2] * filter->x[1] -
                         filter->a[1] * filter->y[0] -
                         filter->a[2] * filter->y[1];

                filter->x[1] = filter->x[0];
                filter->x[0] = smp;
                filter->y[1] = filter->y[0];
                filter->y[0] = outsmp;
                smp = outsmp;
            }

            temps[it] = smp;
        }

        for(kt = 0;kt < MaxChannels;kt++)
        {
            ALfloat gain = state->Gain[kt];
            if(!(gain > 0.00001f))
                continue;

            for(it = 0;it < td;it++)
                SamplesOut[kt][base+it] += gain * temps[it];
        }

        base += td;
    }
}

static void ALequalizerState_Delete(ALequalizerState *state)
{
    free(state);
}

DEFINE_ALEFFECTSTATE_VTABLE(ALequalizerState);


ALeffectState *ALequalizerStateFactory_create(ALequalizerStateFactory *factory)
{
    ALequalizerState *state;
    int it;
    (void)factory;

    state = malloc(sizeof(*state));
    if(!state) return NULL;
    SET_VTABLE2(ALequalizerState, ALeffectState, state);

    state->bandfilter[0].type = LOW_SHELF;
    state->bandfilter[1].type = PEAKING;
    state->bandfilter[2].type = PEAKING;
    state->bandfilter[3].type = HIGH_SHELF;

    /* Initialize sample history only on filter creation to avoid */
    /* sound clicks if filter settings were changed in runtime.   */
    for(it = 0; it < 4; it++)
    {
        state->bandfilter[it].x[0] = 0.0f;
        state->bandfilter[it].x[1] = 0.0f;
        state->bandfilter[it].y[0] = 0.0f;
        state->bandfilter[it].y[1] = 0.0f;
    }

    return STATIC_CAST(ALeffectState, state);
}

DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALequalizerStateFactory);


static void init_equalizer_factory(void)
{
    SET_VTABLE2(ALequalizerStateFactory, ALeffectStateFactory, &EqualizerFactory);
}

ALeffectStateFactory *ALequalizerStateFactory_getFactory(void)
{
    static pthread_once_t once = PTHREAD_ONCE_INIT;
    pthread_once(&once, init_equalizer_factory);
    return STATIC_CAST(ALeffectStateFactory, &EqualizerFactory);
}


void ALequalizer_SetParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
{ SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); (void)effect;(void)param;(void)val; }
void ALequalizer_SetParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
{
    ALequalizer_SetParami(effect, context, param, vals[0]);
}
void ALequalizer_SetParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
{
    ALeffectProps *props = &effect->Props;
    switch(param)
    {
        case AL_EQUALIZER_LOW_GAIN:
            if(!(val >= AL_EQUALIZER_MIN_LOW_GAIN && val <= AL_EQUALIZER_MAX_LOW_GAIN))
                SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
            props->Equalizer.LowGain = val;
            break;

        case AL_EQUALIZER_LOW_CUTOFF:
            if(!(val >= AL_EQUALIZER_MIN_LOW_CUTOFF && val <= AL_EQUALIZER_MAX_LOW_CUTOFF))
                SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
            props->Equalizer.LowCutoff = val;
            break;

        case AL_EQUALIZER_MID1_GAIN:
            if(!(val >= AL_EQUALIZER_MIN_MID1_GAIN && val <= AL_EQUALIZER_MAX_MID1_GAIN))
                SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
            props->Equalizer.Mid1Gain = val;
            break;

        case AL_EQUALIZER_MID1_CENTER:
            if(!(val >= AL_EQUALIZER_MIN_MID1_CENTER && val <= AL_EQUALIZER_MAX_MID1_CENTER))
                SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
            props->Equalizer.Mid1Center = val;
            break;

        case AL_EQUALIZER_MID1_WIDTH:
            if(!(val >= AL_EQUALIZER_MIN_MID1_WIDTH && val <= AL_EQUALIZER_MAX_MID1_WIDTH))
                SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
            props->Equalizer.Mid1Width = val;
            break;

        case AL_EQUALIZER_MID2_GAIN:
            if(!(val >= AL_EQUALIZER_MIN_MID2_GAIN && val <= AL_EQUALIZER_MAX_MID2_GAIN))
                SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
            props->Equalizer.Mid2Gain = val;
            break;

        case AL_EQUALIZER_MID2_CENTER:
            if(!(val >= AL_EQUALIZER_MIN_MID2_CENTER && val <= AL_EQUALIZER_MAX_MID2_CENTER))
                SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
            props->Equalizer.Mid2Center = val;
            break;

        case AL_EQUALIZER_MID2_WIDTH:
            if(!(val >= AL_EQUALIZER_MIN_MID2_WIDTH && val <= AL_EQUALIZER_MAX_MID2_WIDTH))
                SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
            props->Equalizer.Mid2Width = val;
            break;

        case AL_EQUALIZER_HIGH_GAIN:
            if(!(val >= AL_EQUALIZER_MIN_HIGH_GAIN && val <= AL_EQUALIZER_MAX_HIGH_GAIN))
                SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
            props->Equalizer.HighGain = val;
            break;

        case AL_EQUALIZER_HIGH_CUTOFF:
            if(!(val >= AL_EQUALIZER_MIN_HIGH_CUTOFF && val <= AL_EQUALIZER_MAX_HIGH_CUTOFF))
                SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
            props->Equalizer.HighCutoff = val;
            break;

        default:
            SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
    }
}
void ALequalizer_SetParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
{
    ALequalizer_SetParamf(effect, context, param, vals[0]);
}

void ALequalizer_GetParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
{ SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); (void)effect;(void)param;(void)val; }
void ALequalizer_GetParamiv(ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
{
    ALequalizer_GetParami(effect, context, param, vals);
}
void ALequalizer_GetParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
{
    const ALeffectProps *props = &effect->Props;
    switch(param)
    {
        case AL_EQUALIZER_LOW_GAIN:
            *val = props->Equalizer.LowGain;
            break;

        case AL_EQUALIZER_LOW_CUTOFF:
            *val = props->Equalizer.LowCutoff;
            break;

        case AL_EQUALIZER_MID1_GAIN:
            *val = props->Equalizer.Mid1Gain;
            break;

        case AL_EQUALIZER_MID1_CENTER:
            *val = props->Equalizer.Mid1Center;
            break;

        case AL_EQUALIZER_MID1_WIDTH:
            *val = props->Equalizer.Mid1Width;
            break;

        case AL_EQUALIZER_MID2_GAIN:
            *val = props->Equalizer.Mid2Gain;
            break;

        case AL_EQUALIZER_MID2_CENTER:
            *val = props->Equalizer.Mid2Center;
            break;

        case AL_EQUALIZER_MID2_WIDTH:
            *val = props->Equalizer.Mid2Width;
            break;

        case AL_EQUALIZER_HIGH_GAIN:
            *val = props->Equalizer.HighGain;
            break;

        case AL_EQUALIZER_HIGH_CUTOFF:
            *val = props->Equalizer.HighCutoff;
            break;

        default:
            SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
    }
}
void ALequalizer_GetParamfv(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
{
    ALequalizer_GetParamf(effect, context, param, vals);
}

DEFINE_ALEFFECT_VTABLE(ALequalizer);