2009-04-12 23:01:10 +00:00
|
|
|
/**
|
|
|
|
* OpenAL cross platform audio library
|
|
|
|
* Copyright (C) 2009 by Chris Robinson.
|
|
|
|
* 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>
|
|
|
|
|
2009-05-29 20:30:50 +00:00
|
|
|
#include "alMain.h"
|
2009-04-12 23:01:10 +00:00
|
|
|
#include "alFilter.h"
|
|
|
|
#include "alAuxEffectSlot.h"
|
2009-05-31 18:54:49 +00:00
|
|
|
#include "alError.h"
|
2009-05-17 06:26:39 +00:00
|
|
|
#include "alu.h"
|
2009-04-12 23:01:10 +00:00
|
|
|
|
2009-10-19 14:46:53 +00:00
|
|
|
// Just a soft maximum. Being higher will cause EchoUpdate to reallocate the
|
|
|
|
// sample buffer which may cause an abort if realloc fails
|
|
|
|
#define MAX_ECHO_FREQ 192000
|
|
|
|
|
2009-05-29 20:30:50 +00:00
|
|
|
typedef struct ALechoState {
|
|
|
|
// Must be first in all effects!
|
|
|
|
ALeffectState state;
|
|
|
|
|
2009-04-12 23:01:10 +00:00
|
|
|
ALfloat *SampleBuffer;
|
|
|
|
ALuint BufferLength;
|
|
|
|
|
2009-10-19 14:46:53 +00:00
|
|
|
// The echo is two tap. The delay is the number of samples from before the
|
|
|
|
// current offset
|
2009-04-12 23:01:10 +00:00
|
|
|
struct {
|
2009-10-19 14:46:53 +00:00
|
|
|
ALuint delay;
|
|
|
|
} Tap[2];
|
|
|
|
ALuint Offset;
|
2009-04-12 23:01:10 +00:00
|
|
|
// The LR gains for the first tap. The second tap uses the reverse
|
|
|
|
ALfloat GainL;
|
|
|
|
ALfloat GainR;
|
|
|
|
|
|
|
|
ALfloat FeedGain;
|
2009-04-16 12:43:09 +00:00
|
|
|
|
2009-04-12 23:01:10 +00:00
|
|
|
FILTER iirFilter;
|
2009-04-16 12:43:09 +00:00
|
|
|
ALfloat history[2];
|
2009-05-29 20:30:50 +00:00
|
|
|
} ALechoState;
|
2009-04-12 23:01:10 +00:00
|
|
|
|
|
|
|
// Find the next power of 2. Actually, this will return the input value if
|
|
|
|
// it is already a power of 2.
|
|
|
|
static ALuint NextPowerOf2(ALuint value)
|
|
|
|
{
|
|
|
|
ALuint powerOf2 = 1;
|
|
|
|
|
|
|
|
if(value)
|
|
|
|
{
|
|
|
|
value--;
|
|
|
|
while(value)
|
|
|
|
{
|
|
|
|
value >>= 1;
|
|
|
|
powerOf2 <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return powerOf2;
|
|
|
|
}
|
|
|
|
|
2009-05-29 20:30:50 +00:00
|
|
|
ALvoid EchoDestroy(ALeffectState *effect)
|
2009-04-12 23:01:10 +00:00
|
|
|
{
|
2009-05-29 20:30:50 +00:00
|
|
|
ALechoState *state = (ALechoState*)effect;
|
2009-04-12 23:01:10 +00:00
|
|
|
if(state)
|
|
|
|
{
|
|
|
|
free(state->SampleBuffer);
|
|
|
|
state->SampleBuffer = NULL;
|
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-20 15:31:44 +00:00
|
|
|
ALboolean EchoDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
|
2009-04-12 23:01:10 +00:00
|
|
|
{
|
2009-05-29 20:30:50 +00:00
|
|
|
ALechoState *state = (ALechoState*)effect;
|
2009-10-21 09:03:33 +00:00
|
|
|
ALuint maxlen, i;
|
2009-04-12 23:01:10 +00:00
|
|
|
|
2009-10-20 15:31:44 +00:00
|
|
|
// Use the next power of 2 for the buffer length, so the tap offsets can be
|
|
|
|
// wrapped using a mask instead of a modulo
|
|
|
|
maxlen = (ALuint)(AL_ECHO_MAX_DELAY * Device->Frequency);
|
|
|
|
maxlen += (ALuint)(AL_ECHO_MAX_LRDELAY * Device->Frequency);
|
2009-10-19 14:46:53 +00:00
|
|
|
maxlen = NextPowerOf2(maxlen+1);
|
|
|
|
|
2009-10-20 15:31:44 +00:00
|
|
|
if(maxlen != state->BufferLength)
|
2009-10-19 14:46:53 +00:00
|
|
|
{
|
|
|
|
void *temp;
|
|
|
|
|
2009-10-20 15:31:44 +00:00
|
|
|
temp = realloc(state->SampleBuffer, maxlen * sizeof(ALfloat));
|
2009-10-19 14:46:53 +00:00
|
|
|
if(!temp)
|
|
|
|
{
|
2009-10-20 15:31:44 +00:00
|
|
|
alSetError(AL_OUT_OF_MEMORY);
|
|
|
|
return AL_FALSE;
|
2009-10-19 14:46:53 +00:00
|
|
|
}
|
2009-10-20 15:31:44 +00:00
|
|
|
state->BufferLength = maxlen;
|
2009-10-19 14:46:53 +00:00
|
|
|
}
|
2009-10-21 09:03:33 +00:00
|
|
|
for(i = 0;i < state->BufferLength;i++)
|
|
|
|
state->SampleBuffer[i] = 0.0f;
|
2009-04-12 23:01:10 +00:00
|
|
|
|
2009-10-20 15:31:44 +00:00
|
|
|
return AL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALvoid EchoUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
|
|
|
|
{
|
|
|
|
ALechoState *state = (ALechoState*)effect;
|
2009-10-20 20:47:55 +00:00
|
|
|
ALuint frequency = Context->Device->Frequency;
|
2009-10-20 15:31:44 +00:00
|
|
|
ALfloat lrpan, cw, a, g;
|
|
|
|
|
2009-10-20 20:47:55 +00:00
|
|
|
state->Tap[0].delay = (ALuint)(Effect->Echo.Delay * frequency);
|
|
|
|
state->Tap[1].delay = (ALuint)(Effect->Echo.LRDelay * frequency);
|
2009-10-19 14:46:53 +00:00
|
|
|
state->Tap[1].delay += state->Tap[0].delay;
|
2009-04-12 23:01:10 +00:00
|
|
|
|
|
|
|
lrpan = Effect->Echo.Spread*0.5f + 0.5f;
|
|
|
|
state->GainL = aluSqrt( lrpan);
|
|
|
|
state->GainR = aluSqrt(1.0f-lrpan);
|
|
|
|
|
|
|
|
state->FeedGain = Effect->Echo.Feedback;
|
|
|
|
|
2009-10-20 20:47:55 +00:00
|
|
|
cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / frequency);
|
2009-04-12 23:01:10 +00:00
|
|
|
g = 1.0f - Effect->Echo.Damping;
|
|
|
|
a = 0.0f;
|
|
|
|
if(g < 0.9999f) // 1-epsilon
|
|
|
|
a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
|
|
|
|
state->iirFilter.coeff = a;
|
|
|
|
}
|
|
|
|
|
2009-05-29 23:51:00 +00:00
|
|
|
ALvoid EchoProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
|
2009-04-12 23:01:10 +00:00
|
|
|
{
|
2009-05-29 20:30:50 +00:00
|
|
|
ALechoState *state = (ALechoState*)effect;
|
2009-10-19 14:46:53 +00:00
|
|
|
const ALuint mask = state->BufferLength-1;
|
|
|
|
const ALuint tap1 = state->Tap[0].delay;
|
|
|
|
const ALuint tap2 = state->Tap[1].delay;
|
|
|
|
ALuint offset = state->Offset;
|
|
|
|
const ALfloat gain = Slot->Gain;
|
|
|
|
ALfloat samp[2], smp;
|
2009-04-12 23:01:10 +00:00
|
|
|
ALuint i;
|
|
|
|
|
2009-10-19 14:46:53 +00:00
|
|
|
for(i = 0;i < SamplesToDo;i++,offset++)
|
2009-04-12 23:01:10 +00:00
|
|
|
{
|
|
|
|
// Sample first tap
|
2009-10-19 14:46:53 +00:00
|
|
|
smp = state->SampleBuffer[(offset-tap1) & mask];
|
|
|
|
samp[0] = smp * state->GainL;
|
|
|
|
samp[1] = smp * state->GainR;
|
2009-04-12 23:01:10 +00:00
|
|
|
// Sample second tap. Reverse LR panning
|
2009-10-19 14:46:53 +00:00
|
|
|
smp = state->SampleBuffer[(offset-tap2) & mask];
|
|
|
|
samp[0] += smp * state->GainR;
|
|
|
|
samp[1] += smp * state->GainL;
|
|
|
|
|
|
|
|
// Apply damping and feedback gain to the second tap, and mix in the
|
|
|
|
// new sample
|
|
|
|
smp = lpFilter2P(&state->iirFilter, 0, smp+SamplesIn[i]);
|
|
|
|
state->SampleBuffer[offset&mask] = smp * state->FeedGain;
|
|
|
|
|
2009-05-29 23:51:00 +00:00
|
|
|
// Apply slot gain
|
|
|
|
samp[0] *= gain;
|
|
|
|
samp[1] *= gain;
|
2009-04-12 23:01:10 +00:00
|
|
|
|
|
|
|
SamplesOut[i][FRONT_LEFT] += samp[0];
|
|
|
|
SamplesOut[i][FRONT_RIGHT] += samp[1];
|
|
|
|
SamplesOut[i][SIDE_LEFT] += samp[0];
|
|
|
|
SamplesOut[i][SIDE_RIGHT] += samp[1];
|
|
|
|
SamplesOut[i][BACK_LEFT] += samp[0];
|
|
|
|
SamplesOut[i][BACK_RIGHT] += samp[1];
|
|
|
|
}
|
2009-10-19 14:46:53 +00:00
|
|
|
state->Offset = offset;
|
2009-04-12 23:01:10 +00:00
|
|
|
}
|
2009-05-29 20:30:50 +00:00
|
|
|
|
2009-10-19 14:46:53 +00:00
|
|
|
ALeffectState *EchoCreate(void)
|
2009-05-29 20:30:50 +00:00
|
|
|
{
|
|
|
|
ALechoState *state;
|
|
|
|
|
|
|
|
state = malloc(sizeof(*state));
|
|
|
|
if(!state)
|
2009-05-31 18:54:49 +00:00
|
|
|
{
|
|
|
|
alSetError(AL_OUT_OF_MEMORY);
|
2009-05-29 20:30:50 +00:00
|
|
|
return NULL;
|
2009-05-31 18:54:49 +00:00
|
|
|
}
|
2009-05-29 20:30:50 +00:00
|
|
|
|
|
|
|
state->state.Destroy = EchoDestroy;
|
2009-10-20 15:31:44 +00:00
|
|
|
state->state.DeviceUpdate = EchoDeviceUpdate;
|
2009-05-29 20:30:50 +00:00
|
|
|
state->state.Update = EchoUpdate;
|
|
|
|
state->state.Process = EchoProcess;
|
|
|
|
|
2009-10-20 15:31:44 +00:00
|
|
|
state->BufferLength = 0;
|
|
|
|
state->SampleBuffer = NULL;
|
2009-05-29 20:30:50 +00:00
|
|
|
|
2009-10-19 14:46:53 +00:00
|
|
|
state->Tap[0].delay = 0;
|
|
|
|
state->Tap[1].delay = 0;
|
|
|
|
state->Offset = 0;
|
2009-05-29 20:30:50 +00:00
|
|
|
state->GainL = 0.0f;
|
|
|
|
state->GainR = 0.0f;
|
|
|
|
|
|
|
|
state->iirFilter.coeff = 0.0f;
|
2009-10-20 15:31:44 +00:00
|
|
|
state->iirFilter.history[0] = 0.0f;
|
|
|
|
state->iirFilter.history[1] = 0.0f;
|
2009-05-29 20:30:50 +00:00
|
|
|
|
|
|
|
return &state->state;
|
|
|
|
}
|