aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/alcChorus.c
blob: cf414bbdaa5b25b593557cff28888d38a1d5294f (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
/**
 * 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 ALchorusStateFactory {
    DERIVE_FROM_TYPE(ALeffectStateFactory);
} ALchorusStateFactory;

static ALchorusStateFactory ChorusFactory;


typedef struct ALchorusState {
    DERIVE_FROM_TYPE(ALeffectState);

    ALfloat *SampleBufferLeft;
    ALfloat *SampleBufferRight;
    ALuint BufferLength;
    ALint offset;
    ALfloat lfo_coeff;
    ALint lfo_disp;

    /* Gains for left and right sides */
    ALfloat Gain[2][MaxChannels];

    /* effect parameters */
    ALint waveform;
    ALint delay;
    ALfloat depth;
    ALfloat feedback;
} ALchorusState;

static ALvoid ALchorusState_Destruct(ALchorusState *state)
{
    free(state->SampleBufferLeft);
    state->SampleBufferLeft = NULL;

    free(state->SampleBufferRight);
    state->SampleBufferRight = NULL;
}

static ALboolean ALchorusState_DeviceUpdate(ALchorusState *state, ALCdevice *Device)
{
    ALuint maxlen;
    ALuint it;

    maxlen = fastf2u(AL_CHORUS_MAX_DELAY * 3.0f * Device->Frequency) + 1;
    maxlen = NextPowerOf2(maxlen);

    if(maxlen != state->BufferLength)
    {
        void *temp;

        temp = realloc(state->SampleBufferLeft, maxlen * sizeof(ALfloat));
        if(!temp) return AL_FALSE;
        state->SampleBufferLeft = temp;

        temp = realloc(state->SampleBufferRight, maxlen * sizeof(ALfloat));
        if(!temp) return AL_FALSE;
        state->SampleBufferRight = temp;

        state->BufferLength = maxlen;
    }

    for(it = 0;it < state->BufferLength;it++)
    {
        state->SampleBufferLeft[it] = 0.0f;
        state->SampleBufferRight[it] = 0.0f;
    }

    return AL_TRUE;
}

static ALvoid ALchorusState_Update(ALchorusState *state, ALCdevice *Device, const ALeffectslot *Slot)
{
    ALfloat frequency = Device->Frequency;
    ALfloat rate;
    ALint phase;
    ALuint it;

    for (it = 0; it < MaxChannels; it++)
    {
        state->Gain[0][it] = 0.0f;
        state->Gain[1][it] = 0.0f;
    }

    state->waveform = Slot->effect.Chorus.Waveform;
    state->depth = Slot->effect.Chorus.Depth;
    state->feedback = Slot->effect.Chorus.Feedback;
    state->delay = fastf2i(Slot->effect.Chorus.Delay * frequency);

    /* Gains for left and right sides */
    ComputeAngleGains(Device, atan2f(-1.0f, 0.0f), 0.0f, Slot->Gain, state->Gain[0]);
    ComputeAngleGains(Device, atan2f(+1.0f, 0.0f), 0.0f, Slot->Gain, state->Gain[1]);

    phase = Slot->effect.Chorus.Phase;
    rate = Slot->effect.Chorus.Rate;

    /* Calculate LFO coefficient */
    switch (state->waveform)
    {
        case AL_CHORUS_WAVEFORM_TRIANGLE:
             if(rate == 0.0f)
                 state->lfo_coeff = 0.0f;
             else
                 state->lfo_coeff = 1.0f / (frequency / rate);
             break;
        case AL_CHORUS_WAVEFORM_SINUSOID:
             if(rate == 0.0f)
                 state->lfo_coeff = 0.0f;
             else
                 state->lfo_coeff = F_PI*2.0f / (frequency / rate);
             break;
    }

    /* Calculate lfo phase displacement */
    if(phase == 0 || rate == 0.0f)
        state->lfo_disp = 0;
    else
        state->lfo_disp = fastf2i(frequency / rate / (360.0f/phase));
}

static __inline void Triangle(ALint *delay_left, ALint *delay_right, ALint offset, const ALchorusState *state)
{
    ALfloat lfo_value;

    lfo_value = 2.0f - fabsf(2.0f - fmodf(state->lfo_coeff*offset*4.0f, 4.0f));
    lfo_value *= state->depth * state->delay;
    *delay_left = fastf2i(lfo_value) + state->delay;

    lfo_value = 2.0f - fabsf(2.0f - fmodf(state->lfo_coeff *
                                          (offset+state->lfo_disp)*4.0f,
                                          4.0f));
    lfo_value *= state->depth * state->delay;
    *delay_right = fastf2i(lfo_value) + state->delay;
}

static __inline void Sinusoid(ALint *delay_left, ALint *delay_right, ALint offset, const ALchorusState *state)
{
    ALfloat lfo_value;

    lfo_value = 1.0f + sinf(fmodf(state->lfo_coeff*offset, 2.0f*F_PI));
    lfo_value *= state->depth * state->delay;
    *delay_left = fastf2i(lfo_value) + state->delay;

    lfo_value = 1.0f + sinf(fmodf(state->lfo_coeff*(offset+state->lfo_disp),
                                  2.0f*F_PI));
    lfo_value *= state->depth * state->delay;
    *delay_right = fastf2i(lfo_value) + state->delay;
}

#define DECL_TEMPLATE(func)                                                    \
static void Process##func(ALchorusState *state, ALuint SamplesToDo,            \
                          const ALfloat *RESTRICT SamplesIn,                   \
                          ALfloat (*RESTRICT SamplesOut)[BUFFERSIZE])          \
{                                                                              \
    const ALint mask = state->BufferLength-1;                                  \
    ALint offset = state->offset;                                              \
    ALuint it, kt;                                                             \
    ALuint base;                                                               \
                                                                               \
    for(base = 0;base < SamplesToDo;)                                          \
    {                                                                          \
        ALfloat temps[64][2];                                                  \
        ALuint td = minu(SamplesToDo-base, 64);                                \
                                                                               \
        for(it = 0;it < td;it++,offset++)                                      \
        {                                                                      \
            ALint delay_left, delay_right;                                     \
            (func)(&delay_left, &delay_right, offset, state);                  \
                                                                               \
            temps[it][0] = state->SampleBufferLeft[(offset-delay_left)&mask];  \
            state->SampleBufferLeft[offset&mask] = (temps[it][0] +             \
                                                    SamplesIn[it+base]) *      \
                                                   state->feedback;            \
                                                                               \
            temps[it][1] = state->SampleBufferRight[(offset-delay_right)&mask];\
            state->SampleBufferRight[offset&mask] = (temps[it][1] +            \
                                                     SamplesIn[it+base]) *     \
                                                    state->feedback;           \
        }                                                                      \
                                                                               \
        for(kt = 0;kt < MaxChannels;kt++)                                      \
        {                                                                      \
            ALfloat gain = state->Gain[0][kt];                                 \
            if(gain > 0.00001f)                                                \
            {                                                                  \
                for(it = 0;it < td;it++)                                       \
                    SamplesOut[kt][it+base] += temps[it][0] * gain;            \
            }                                                                  \
                                                                               \
            gain = state->Gain[1][kt];                                         \
            if(gain > 0.00001f)                                                \
            {                                                                  \
                for(it = 0;it < td;it++)                                       \
                    SamplesOut[kt][it+base] += temps[it][1] * gain;            \
            }                                                                  \
        }                                                                      \
                                                                               \
        base += td;                                                            \
    }                                                                          \
                                                                               \
    state->offset = offset;                                                    \
}

DECL_TEMPLATE(Triangle)
DECL_TEMPLATE(Sinusoid)

#undef DECL_TEMPLATE

static ALvoid ALchorusState_Process(ALchorusState *state, ALuint SamplesToDo, const ALfloat *RESTRICT SamplesIn, ALfloat (*RESTRICT SamplesOut)[BUFFERSIZE])
{
    if(state->waveform == AL_CHORUS_WAVEFORM_TRIANGLE)
        ProcessTriangle(state, SamplesToDo, SamplesIn, SamplesOut);
    else if(state->waveform == AL_CHORUS_WAVEFORM_SINUSOID)
        ProcessSinusoid(state, SamplesToDo, SamplesIn, SamplesOut);
}

static ALeffectStateFactory *ALchorusState_getCreator(void)
{
    return STATIC_CAST(ALeffectStateFactory, &ChorusFactory);
}

DEFINE_ALEFFECTSTATE_VTABLE(ALchorusState);


static ALeffectState *ALchorusStateFactory_create(void)
{
    ALchorusState *state;

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

    state->BufferLength = 0;
    state->SampleBufferLeft = NULL;
    state->SampleBufferRight = NULL;
    state->offset = 0;

    return STATIC_CAST(ALeffectState, state);
}

static ALvoid ALchorusStateFactory_destroy(ALeffectState *effect)
{
    ALchorusState *state = STATIC_UPCAST(ALchorusState, ALeffectState, effect);
    ALchorusState_Destruct(state);
    free(state);
}

DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALchorusStateFactory);


static void init_chorus_factory(void)
{
    SET_VTABLE2(ALchorusStateFactory, ALeffectStateFactory, &ChorusFactory);
}

ALeffectStateFactory *ALchorusStateFactory_getFactory(void)
{
    static pthread_once_t once = PTHREAD_ONCE_INIT;
    pthread_once(&once, init_chorus_factory);
    return STATIC_CAST(ALeffectStateFactory, &ChorusFactory);
}


void chorus_SetParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
{
    switch(param)
    {
        case AL_CHORUS_WAVEFORM:
            if(val >= AL_CHORUS_MIN_WAVEFORM && val <= AL_CHORUS_MAX_WAVEFORM)
                effect->Chorus.Waveform = val;
            else
                alSetError(context, AL_INVALID_VALUE);
            break;

        case AL_CHORUS_PHASE:
            if(val >= AL_CHORUS_MIN_PHASE && val <= AL_CHORUS_MAX_PHASE)
                effect->Chorus.Phase = val;
            else
                alSetError(context, AL_INVALID_VALUE);
            break;

        default:
            alSetError(context, AL_INVALID_ENUM);
            break;
    }
}
void chorus_SetParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
{
    chorus_SetParami(effect, context, param, vals[0]);
}
void chorus_SetParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
{
    switch(param)
    {
        case AL_CHORUS_RATE:
            if(val >= AL_CHORUS_MIN_RATE && val <= AL_CHORUS_MAX_RATE)
                effect->Chorus.Rate = val;
            else
                alSetError(context, AL_INVALID_VALUE);
            break;

        case AL_CHORUS_DEPTH:
            if(val >= AL_CHORUS_MIN_DEPTH && val <= AL_CHORUS_MAX_DEPTH)
                effect->Chorus.Depth = val;
            else
                alSetError(context, AL_INVALID_VALUE);
            break;

        case AL_CHORUS_FEEDBACK:
            if(val >= AL_CHORUS_MIN_FEEDBACK && val <= AL_CHORUS_MAX_FEEDBACK)
                effect->Chorus.Feedback = val;
            else
                alSetError(context, AL_INVALID_VALUE);
            break;

        case AL_CHORUS_DELAY:
            if(val >= AL_CHORUS_MIN_DELAY && val <= AL_CHORUS_MAX_DELAY)
                effect->Chorus.Delay = val;
            else
                alSetError(context, AL_INVALID_VALUE);
            break;

        default:
            alSetError(context, AL_INVALID_ENUM);
            break;
    }
}
void chorus_SetParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
{
    chorus_SetParamf(effect, context, param, vals[0]);
}

void chorus_GetParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
{
    switch(param)
    {
        case AL_CHORUS_WAVEFORM:
            *val = effect->Chorus.Waveform;
            break;

        case AL_CHORUS_PHASE:
            *val = effect->Chorus.Phase;
            break;

        default:
            alSetError(context, AL_INVALID_ENUM);
            break;
    }
}
void chorus_GetParamiv(ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
{
    chorus_GetParami(effect, context, param, vals);
}
void chorus_GetParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
{
    switch(param)
    {
        case AL_CHORUS_RATE:
            *val = effect->Chorus.Rate;
            break;

        case AL_CHORUS_DEPTH:
            *val = effect->Chorus.Depth;
            break;

        case AL_CHORUS_FEEDBACK:
            *val = effect->Chorus.Feedback;
            break;

        case AL_CHORUS_DELAY:
            *val = effect->Chorus.Delay;
            break;

        default:
            alSetError(context, AL_INVALID_ENUM);
            break;
    }
}
void chorus_GetParamfv(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
{
    chorus_GetParamf(effect, context, param, vals);
}