etlegacy-libs/openal/Alc/effects/echo.c

311 lines
10 KiB
C
Raw Normal View History

2015-12-12 21:07:33 +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.,
* 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 "alFilter.h"
#include "alAuxEffectSlot.h"
#include "alError.h"
#include "alu.h"
2019-01-03 15:00:15 +00:00
#include "filters/defs.h"
2015-12-12 21:07:33 +00:00
typedef struct ALechoState {
DERIVE_FROM_TYPE(ALeffectState);
ALfloat *SampleBuffer;
2017-07-01 14:18:03 +00:00
ALsizei BufferLength;
2015-12-12 21:07:33 +00:00
// The echo is two tap. The delay is the number of samples from before the
// current offset
struct {
2017-07-01 14:18:03 +00:00
ALsizei delay;
2015-12-12 21:07:33 +00:00
} Tap[2];
2017-07-01 14:18:03 +00:00
ALsizei Offset;
2019-01-03 15:00:15 +00:00
2015-12-12 21:07:33 +00:00
/* The panning gains for the two taps */
2019-01-03 15:00:15 +00:00
struct {
ALfloat Current[MAX_OUTPUT_CHANNELS];
ALfloat Target[MAX_OUTPUT_CHANNELS];
} Gains[2];
2015-12-12 21:07:33 +00:00
ALfloat FeedGain;
2019-01-03 15:00:15 +00:00
BiquadFilter Filter;
2015-12-12 21:07:33 +00:00
} ALechoState;
2017-07-01 14:18:03 +00:00
static ALvoid ALechoState_Destruct(ALechoState *state);
static ALboolean ALechoState_deviceUpdate(ALechoState *state, ALCdevice *Device);
2019-01-03 15:00:15 +00:00
static ALvoid ALechoState_update(ALechoState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props);
2017-07-01 14:18:03 +00:00
static ALvoid ALechoState_process(ALechoState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels);
DECLARE_DEFAULT_ALLOCATORS(ALechoState)
DEFINE_ALEFFECTSTATE_VTABLE(ALechoState);
static void ALechoState_Construct(ALechoState *state)
{
ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
SET_VTABLE2(ALechoState, ALeffectState, state);
state->BufferLength = 0;
state->SampleBuffer = NULL;
state->Tap[0].delay = 0;
state->Tap[1].delay = 0;
state->Offset = 0;
2019-01-03 15:00:15 +00:00
BiquadFilter_clear(&state->Filter);
2017-07-01 14:18:03 +00:00
}
2015-12-12 21:07:33 +00:00
static ALvoid ALechoState_Destruct(ALechoState *state)
{
2017-07-01 14:18:03 +00:00
al_free(state->SampleBuffer);
2015-12-12 21:07:33 +00:00
state->SampleBuffer = NULL;
2017-07-01 14:18:03 +00:00
ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
2015-12-12 21:07:33 +00:00
}
static ALboolean ALechoState_deviceUpdate(ALechoState *state, ALCdevice *Device)
{
2019-01-03 15:00:15 +00:00
ALsizei maxlen;
2015-12-12 21:07:33 +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
2019-01-03 15:00:15 +00:00
maxlen = float2int(AL_ECHO_MAX_DELAY*Device->Frequency + 0.5f) +
float2int(AL_ECHO_MAX_LRDELAY*Device->Frequency + 0.5f);
maxlen = NextPowerOf2(maxlen);
if(maxlen <= 0) return AL_FALSE;
2015-12-12 21:07:33 +00:00
if(maxlen != state->BufferLength)
{
2017-07-01 14:18:03 +00:00
void *temp = al_calloc(16, maxlen * sizeof(ALfloat));
2015-12-12 21:07:33 +00:00
if(!temp) return AL_FALSE;
2017-07-01 14:18:03 +00:00
al_free(state->SampleBuffer);
2015-12-12 21:07:33 +00:00
state->SampleBuffer = temp;
state->BufferLength = maxlen;
}
2019-01-03 15:00:15 +00:00
memset(state->SampleBuffer, 0, state->BufferLength*sizeof(ALfloat));
memset(state->Gains, 0, sizeof(state->Gains));
2015-12-12 21:07:33 +00:00
return AL_TRUE;
}
2019-01-03 15:00:15 +00:00
static ALvoid ALechoState_update(ALechoState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props)
2015-12-12 21:07:33 +00:00
{
2019-01-03 15:00:15 +00:00
const ALCdevice *device = context->Device;
ALuint frequency = device->Frequency;
2017-07-01 14:18:03 +00:00
ALfloat coeffs[MAX_AMBI_COEFFS];
2019-01-03 15:00:15 +00:00
ALfloat gainhf, lrpan, spread;
2015-12-12 21:07:33 +00:00
2019-01-03 15:00:15 +00:00
state->Tap[0].delay = maxi(float2int(props->Echo.Delay*frequency + 0.5f), 1);
state->Tap[1].delay = float2int(props->Echo.LRDelay*frequency + 0.5f);
2015-12-12 21:07:33 +00:00
state->Tap[1].delay += state->Tap[0].delay;
2017-07-01 14:18:03 +00:00
spread = props->Echo.Spread;
if(spread < 0.0f) lrpan = -1.0f;
else lrpan = 1.0f;
/* Convert echo spread (where 0 = omni, +/-1 = directional) to coverage
* spread (where 0 = point, tau = omni).
*/
spread = asinf(1.0f - fabsf(spread))*4.0f;
2015-12-12 21:07:33 +00:00
2017-07-01 14:18:03 +00:00
state->FeedGain = props->Echo.Feedback;
2015-12-12 21:07:33 +00:00
2019-01-03 15:00:15 +00:00
gainhf = maxf(1.0f - props->Echo.Damping, 0.0625f); /* Limit -24dB */
BiquadFilter_setParams(&state->Filter, BiquadType_HighShelf,
gainhf, LOWPASSFREQREF/frequency, calc_rcpQ_from_slope(gainhf, 1.0f)
);
2015-12-12 21:07:33 +00:00
/* First tap panning */
2017-07-01 14:18:03 +00:00
CalcAngleCoeffs(-F_PI_2*lrpan, 0.0f, spread, coeffs);
2019-01-03 15:00:15 +00:00
ComputePanGains(&device->Dry, coeffs, slot->Params.Gain, state->Gains[0].Target);
2015-12-12 21:07:33 +00:00
/* Second tap panning */
2017-07-01 14:18:03 +00:00
CalcAngleCoeffs( F_PI_2*lrpan, 0.0f, spread, coeffs);
2019-01-03 15:00:15 +00:00
ComputePanGains(&device->Dry, coeffs, slot->Params.Gain, state->Gains[1].Target);
2015-12-12 21:07:33 +00:00
}
2017-07-01 14:18:03 +00:00
static ALvoid ALechoState_process(ALechoState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
2015-12-12 21:07:33 +00:00
{
2017-07-01 14:18:03 +00:00
const ALsizei mask = state->BufferLength-1;
const ALsizei tap1 = state->Tap[0].delay;
const ALsizei tap2 = state->Tap[1].delay;
2019-01-03 15:00:15 +00:00
ALfloat *restrict delaybuf = state->SampleBuffer;
2017-07-01 14:18:03 +00:00
ALsizei offset = state->Offset;
2019-01-03 15:00:15 +00:00
ALfloat z1, z2, in, out;
ALsizei base;
ALsizei c, i;
z1 = state->Filter.z1;
z2 = state->Filter.z2;
2015-12-12 21:07:33 +00:00
for(base = 0;base < SamplesToDo;)
{
2019-01-03 15:00:15 +00:00
alignas(16) ALfloat temps[2][128];
2017-07-01 14:18:03 +00:00
ALsizei td = mini(128, SamplesToDo-base);
2015-12-12 21:07:33 +00:00
for(i = 0;i < td;i++)
{
2019-01-03 15:00:15 +00:00
/* Feed the delay buffer's input first. */
delaybuf[offset&mask] = SamplesIn[0][i+base];
2015-12-12 21:07:33 +00:00
/* First tap */
2019-01-03 15:00:15 +00:00
temps[0][i] = delaybuf[(offset-tap1) & mask];
2015-12-12 21:07:33 +00:00
/* Second tap */
2019-01-03 15:00:15 +00:00
temps[1][i] = delaybuf[(offset-tap2) & mask];
/* Apply damping to the second tap, then add it to the buffer with
* feedback attenuation.
*/
in = temps[1][i];
out = in*state->Filter.b0 + z1;
z1 = in*state->Filter.b1 - out*state->Filter.a1 + z2;
z2 = in*state->Filter.b2 - out*state->Filter.a2;
delaybuf[offset&mask] += out * state->FeedGain;
2015-12-12 21:07:33 +00:00
offset++;
}
2019-01-03 15:00:15 +00:00
for(c = 0;c < 2;c++)
MixSamples(temps[c], NumChannels, SamplesOut, state->Gains[c].Current,
state->Gains[c].Target, SamplesToDo-base, base, td);
2015-12-12 21:07:33 +00:00
base += td;
}
2019-01-03 15:00:15 +00:00
state->Filter.z1 = z1;
state->Filter.z2 = z2;
2015-12-12 21:07:33 +00:00
state->Offset = offset;
}
2019-01-03 15:00:15 +00:00
typedef struct EchoStateFactory {
DERIVE_FROM_TYPE(EffectStateFactory);
} EchoStateFactory;
2015-12-12 21:07:33 +00:00
2019-01-03 15:00:15 +00:00
ALeffectState *EchoStateFactory_create(EchoStateFactory *UNUSED(factory))
2015-12-12 21:07:33 +00:00
{
ALechoState *state;
2017-07-01 14:18:03 +00:00
NEW_OBJ0(state, ALechoState)();
2015-12-12 21:07:33 +00:00
if(!state) return NULL;
return STATIC_CAST(ALeffectState, state);
}
2019-01-03 15:00:15 +00:00
DEFINE_EFFECTSTATEFACTORY_VTABLE(EchoStateFactory);
2015-12-12 21:07:33 +00:00
2019-01-03 15:00:15 +00:00
EffectStateFactory *EchoStateFactory_getFactory(void)
2015-12-12 21:07:33 +00:00
{
2019-01-03 15:00:15 +00:00
static EchoStateFactory EchoFactory = { { GET_VTABLE2(EchoStateFactory, EffectStateFactory) } };
2015-12-12 21:07:33 +00:00
2019-01-03 15:00:15 +00:00
return STATIC_CAST(EffectStateFactory, &EchoFactory);
2015-12-12 21:07:33 +00:00
}
2019-01-03 15:00:15 +00:00
void ALecho_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALint UNUSED(val))
{ alSetError(context, AL_INVALID_ENUM, "Invalid echo integer property 0x%04x", param); }
void ALecho_setParamiv(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, const ALint *UNUSED(vals))
{ alSetError(context, AL_INVALID_ENUM, "Invalid echo integer-vector property 0x%04x", param); }
2015-12-12 21:07:33 +00:00
void ALecho_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
{
ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_ECHO_DELAY:
if(!(val >= AL_ECHO_MIN_DELAY && val <= AL_ECHO_MAX_DELAY))
2019-01-03 15:00:15 +00:00
SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo delay out of range");
2015-12-12 21:07:33 +00:00
props->Echo.Delay = val;
break;
case AL_ECHO_LRDELAY:
if(!(val >= AL_ECHO_MIN_LRDELAY && val <= AL_ECHO_MAX_LRDELAY))
2019-01-03 15:00:15 +00:00
SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo LR delay out of range");
2015-12-12 21:07:33 +00:00
props->Echo.LRDelay = val;
break;
case AL_ECHO_DAMPING:
if(!(val >= AL_ECHO_MIN_DAMPING && val <= AL_ECHO_MAX_DAMPING))
2019-01-03 15:00:15 +00:00
SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo damping out of range");
2015-12-12 21:07:33 +00:00
props->Echo.Damping = val;
break;
case AL_ECHO_FEEDBACK:
if(!(val >= AL_ECHO_MIN_FEEDBACK && val <= AL_ECHO_MAX_FEEDBACK))
2019-01-03 15:00:15 +00:00
SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo feedback out of range");
2015-12-12 21:07:33 +00:00
props->Echo.Feedback = val;
break;
case AL_ECHO_SPREAD:
if(!(val >= AL_ECHO_MIN_SPREAD && val <= AL_ECHO_MAX_SPREAD))
2019-01-03 15:00:15 +00:00
SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo spread out of range");
2015-12-12 21:07:33 +00:00
props->Echo.Spread = val;
break;
default:
2019-01-03 15:00:15 +00:00
alSetError(context, AL_INVALID_ENUM, "Invalid echo float property 0x%04x", param);
2015-12-12 21:07:33 +00:00
}
}
void ALecho_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
2019-01-03 15:00:15 +00:00
{ ALecho_setParamf(effect, context, param, vals[0]); }
2015-12-12 21:07:33 +00:00
2019-01-03 15:00:15 +00:00
void ALecho_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALint *UNUSED(val))
{ alSetError(context, AL_INVALID_ENUM, "Invalid echo integer property 0x%04x", param); }
void ALecho_getParamiv(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALint *UNUSED(vals))
{ alSetError(context, AL_INVALID_ENUM, "Invalid echo integer-vector property 0x%04x", param); }
2015-12-12 21:07:33 +00:00
void ALecho_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
{
const ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_ECHO_DELAY:
*val = props->Echo.Delay;
break;
case AL_ECHO_LRDELAY:
*val = props->Echo.LRDelay;
break;
case AL_ECHO_DAMPING:
*val = props->Echo.Damping;
break;
case AL_ECHO_FEEDBACK:
*val = props->Echo.Feedback;
break;
case AL_ECHO_SPREAD:
*val = props->Echo.Spread;
break;
default:
2019-01-03 15:00:15 +00:00
alSetError(context, AL_INVALID_ENUM, "Invalid echo float property 0x%04x", param);
2015-12-12 21:07:33 +00:00
}
}
void ALecho_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
2019-01-03 15:00:15 +00:00
{ ALecho_getParamf(effect, context, param, vals); }
2015-12-12 21:07:33 +00:00
DEFINE_ALEFFECT_VTABLE(ALecho);