AuroraOpenALSoft/Alc/alcEcho.c

185 lines
5.2 KiB
C
Raw Normal View History

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>
#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
typedef struct ALechoState {
// Must be first in all effects!
ALeffectState state;
2009-04-12 23:01:10 +00:00
ALfloat *SampleBuffer;
ALuint BufferLength;
// 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 {
ALuint delay;
} Tap[2];
ALuint Offset;
/* The panning gains for the two taps */
ALfloat Gain[2][MAXCHANNELS];
2009-04-12 23:01:10 +00:00
ALfloat FeedGain;
2009-04-12 23:01:10 +00:00
FILTER iirFilter;
ALfloat history[2];
} ALechoState;
2009-04-12 23:01:10 +00:00
2010-04-08 19:14:18 +00:00
static ALvoid EchoDestroy(ALeffectState *effect)
2009-04-12 23:01:10 +00:00
{
ALechoState *state = (ALechoState*)effect;
2009-04-12 23:01:10 +00:00
if(state)
{
free(state->SampleBuffer);
state->SampleBuffer = NULL;
free(state);
}
}
2010-04-08 19:14:18 +00:00
static ALboolean EchoDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
2009-04-12 23:01:10 +00:00
{
ALechoState *state = (ALechoState*)effect;
ALuint maxlen, i;
2009-04-12 23:01:10 +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 = fastf2u(AL_ECHO_MAX_DELAY * Device->Frequency) + 1;
maxlen += fastf2u(AL_ECHO_MAX_LRDELAY * Device->Frequency) + 1;
maxlen = NextPowerOf2(maxlen);
if(maxlen != state->BufferLength)
{
void *temp;
temp = realloc(state->SampleBuffer, maxlen * sizeof(ALfloat));
if(!temp)
return AL_FALSE;
state->SampleBuffer = temp;
state->BufferLength = maxlen;
}
for(i = 0;i < state->BufferLength;i++)
state->SampleBuffer[i] = 0.0f;
2009-04-12 23:01:10 +00:00
return AL_TRUE;
}
static ALvoid EchoUpdate(ALeffectState *effect, ALCdevice *Device, const ALeffectslot *Slot)
{
ALechoState *state = (ALechoState*)effect;
ALuint frequency = Device->Frequency;
ALfloat lrpan, cw, g, gain;
ALfloat dirGain;
ALuint i;
state->Tap[0].delay = fastf2u(Slot->effect.Echo.Delay * frequency) + 1;
state->Tap[1].delay = fastf2u(Slot->effect.Echo.LRDelay * frequency);
state->Tap[1].delay += state->Tap[0].delay;
2009-04-12 23:01:10 +00:00
lrpan = Slot->effect.Echo.Spread;
2009-04-12 23:01:10 +00:00
state->FeedGain = Slot->effect.Echo.Feedback;
2009-04-12 23:01:10 +00:00
cw = aluCos(F_PI*2.0f * LOWPASSFREQREF / frequency);
g = 1.0f - Slot->effect.Echo.Damping;
2011-05-19 01:49:41 +00:00
state->iirFilter.coeff = lpCoeffCalc(g, cw);
gain = Slot->Gain;
for(i = 0;i < MAXCHANNELS;i++)
{
state->Gain[0][i] = 0.0f;
state->Gain[1][i] = 0.0f;
}
dirGain = aluFabs(lrpan);
/* First tap panning */
ComputeAngleGains(Device, aluAtan2(-lrpan, 0.0f), (1.0f-dirGain)*F_PI, gain, state->Gain[0]);
/* Second tap panning */
ComputeAngleGains(Device, aluAtan2(+lrpan, 0.0f), (1.0f-dirGain)*F_PI, gain, state->Gain[1]);
2009-04-12 23:01:10 +00:00
}
static ALvoid EchoProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
2009-04-12 23:01:10 +00:00
{
ALechoState *state = (ALechoState*)effect;
const ALuint mask = state->BufferLength-1;
const ALuint tap1 = state->Tap[0].delay;
const ALuint tap2 = state->Tap[1].delay;
ALuint offset = state->Offset;
ALfloat smp;
ALuint i, k;
2009-04-12 23:01:10 +00:00
for(i = 0;i < SamplesToDo;i++,offset++)
2009-04-12 23:01:10 +00:00
{
/* First tap */
smp = state->SampleBuffer[(offset-tap1) & mask];
for(k = 0;k < MAXCHANNELS;k++)
SamplesOut[i][k] += smp * state->Gain[0][k];
/* Second tap */
smp = state->SampleBuffer[(offset-tap2) & mask];
for(k = 0;k < MAXCHANNELS;k++)
SamplesOut[i][k] += smp * state->Gain[1][k];
// 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-04-12 23:01:10 +00:00
}
state->Offset = offset;
2009-04-12 23:01:10 +00:00
}
ALeffectState *EchoCreate(void)
{
ALechoState *state;
state = malloc(sizeof(*state));
if(!state)
return NULL;
state->state.Destroy = EchoDestroy;
state->state.DeviceUpdate = EchoDeviceUpdate;
state->state.Update = EchoUpdate;
state->state.Process = EchoProcess;
state->BufferLength = 0;
state->SampleBuffer = NULL;
state->Tap[0].delay = 0;
state->Tap[1].delay = 0;
state->Offset = 0;
state->iirFilter.coeff = 0.0f;
state->iirFilter.history[0] = 0.0f;
state->iirFilter.history[1] = 0.0f;
return &state->state;
}