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
|
2014-08-18 12:11:03 +00:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2009-04-12 23:01:10 +00:00
|
|
|
* 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
|
|
|
|
2009-05-29 20:30:50 +00:00
|
|
|
typedef struct ALechoState {
|
2013-05-20 08:32:02 +00:00
|
|
|
DERIVE_FROM_TYPE(ALeffectState);
|
2009-05-29 20:30:50 +00:00
|
|
|
|
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;
|
2011-11-01 23:55:28 +00:00
|
|
|
/* The panning gains for the two taps */
|
2014-11-07 10:18:24 +00:00
|
|
|
ALfloat Gain[2][MAX_OUTPUT_CHANNELS];
|
2009-04-12 23:01:10 +00:00
|
|
|
|
|
|
|
ALfloat FeedGain;
|
2009-04-16 12:43:09 +00:00
|
|
|
|
2013-05-28 02:55:00 +00:00
|
|
|
ALfilterState Filter;
|
2009-05-29 20:30:50 +00:00
|
|
|
} ALechoState;
|
2009-04-12 23:01:10 +00:00
|
|
|
|
2013-05-21 20:02:56 +00:00
|
|
|
static ALvoid ALechoState_Destruct(ALechoState *state)
|
2009-04-12 23:01:10 +00:00
|
|
|
{
|
2013-05-21 09:37:27 +00:00
|
|
|
free(state->SampleBuffer);
|
|
|
|
state->SampleBuffer = NULL;
|
2009-04-12 23:01:10 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 18:17:45 +00:00
|
|
|
static ALboolean ALechoState_deviceUpdate(ALechoState *state, ALCdevice *Device)
|
2009-04-12 23:01:10 +00:00
|
|
|
{
|
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
|
2011-09-29 13:55:00 +00:00
|
|
|
maxlen = fastf2u(AL_ECHO_MAX_DELAY * Device->Frequency) + 1;
|
|
|
|
maxlen += fastf2u(AL_ECHO_MAX_LRDELAY * Device->Frequency) + 1;
|
2009-11-19 18:42:41 +00:00
|
|
|
maxlen = NextPowerOf2(maxlen);
|
2009-10-19 14:46:53 +00:00
|
|
|
|
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));
|
2013-06-10 12:57:27 +00:00
|
|
|
if(!temp) return AL_FALSE;
|
2009-11-19 01:34:08 +00:00
|
|
|
state->SampleBuffer = temp;
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-25 14:11:51 +00:00
|
|
|
static ALvoid ALechoState_update(ALechoState *state, const ALCdevice *Device, const ALeffectslot *Slot)
|
2009-10-20 15:31:44 +00:00
|
|
|
{
|
2011-07-16 10:35:51 +00:00
|
|
|
ALuint frequency = Device->Frequency;
|
2016-01-25 14:11:51 +00:00
|
|
|
ALfloat coeffs[MAX_AMBI_COEFFS];
|
2015-11-01 12:43:55 +00:00
|
|
|
ALfloat gain, lrpan;
|
2009-10-20 15:31:44 +00:00
|
|
|
|
2013-05-26 05:07:31 +00:00
|
|
|
state->Tap[0].delay = fastf2u(Slot->EffectProps.Echo.Delay * frequency) + 1;
|
|
|
|
state->Tap[1].delay = fastf2u(Slot->EffectProps.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
|
|
|
|
2013-05-26 05:07:31 +00:00
|
|
|
lrpan = Slot->EffectProps.Echo.Spread;
|
2009-04-12 23:01:10 +00:00
|
|
|
|
2013-05-26 05:07:31 +00:00
|
|
|
state->FeedGain = Slot->EffectProps.Echo.Feedback;
|
2009-04-12 23:01:10 +00:00
|
|
|
|
2015-11-01 12:43:55 +00:00
|
|
|
gain = minf(1.0f - Slot->EffectProps.Echo.Damping, 0.01f);
|
2013-05-28 17:57:38 +00:00
|
|
|
ALfilterState_setParams(&state->Filter, ALfilterType_HighShelf,
|
2015-11-01 12:43:55 +00:00
|
|
|
gain, LOWPASSFREQREF/frequency,
|
|
|
|
calc_rcpQ_from_slope(gain, 0.75f));
|
|
|
|
|
|
|
|
gain = Slot->Gain;
|
2011-07-16 10:35:51 +00:00
|
|
|
|
2011-11-01 23:55:28 +00:00
|
|
|
/* First tap panning */
|
2016-01-25 14:11:51 +00:00
|
|
|
CalcXYZCoeffs(-lrpan, 0.0f, 0.0f, coeffs);
|
2016-03-10 07:43:57 +00:00
|
|
|
ComputePanningGains(Device->Dry.AmbiCoeffs, Device->Dry.NumChannels, coeffs,
|
|
|
|
gain, state->Gain[0]);
|
2011-11-01 23:55:28 +00:00
|
|
|
|
|
|
|
/* Second tap panning */
|
2016-01-25 14:11:51 +00:00
|
|
|
CalcXYZCoeffs( lrpan, 0.0f, 0.0f, coeffs);
|
2016-03-10 07:43:57 +00:00
|
|
|
ComputePanningGains(Device->Dry.AmbiCoeffs, Device->Dry.NumChannels, coeffs,
|
|
|
|
gain, state->Gain[1]);
|
2009-04-12 23:01:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 16:16:47 +00:00
|
|
|
static ALvoid ALechoState_process(ALechoState *state, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
|
2009-04-12 23:01:10 +00:00
|
|
|
{
|
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;
|
2011-11-01 23:55:28 +00:00
|
|
|
ALfloat smp;
|
2013-05-20 11:16:48 +00:00
|
|
|
ALuint base;
|
2011-11-01 23:55:28 +00:00
|
|
|
ALuint i, k;
|
2009-04-12 23:01:10 +00:00
|
|
|
|
2013-05-20 11:16:48 +00:00
|
|
|
for(base = 0;base < SamplesToDo;)
|
2009-04-12 23:01:10 +00:00
|
|
|
{
|
2014-12-18 17:23:55 +00:00
|
|
|
ALfloat temps[128][2];
|
|
|
|
ALuint td = minu(128, SamplesToDo-base);
|
2013-05-20 11:16:48 +00:00
|
|
|
|
|
|
|
for(i = 0;i < td;i++)
|
|
|
|
{
|
|
|
|
/* First tap */
|
|
|
|
temps[i][0] = state->SampleBuffer[(offset-tap1) & mask];
|
|
|
|
/* Second tap */
|
|
|
|
temps[i][1] = state->SampleBuffer[(offset-tap2) & mask];
|
|
|
|
|
|
|
|
// Apply damping and feedback gain to the second tap, and mix in the
|
|
|
|
// new sample
|
2016-01-27 16:16:47 +00:00
|
|
|
smp = ALfilterState_processSingle(&state->Filter, temps[i][1]+SamplesIn[0][i+base]);
|
2013-05-20 11:16:48 +00:00
|
|
|
state->SampleBuffer[offset&mask] = smp * state->FeedGain;
|
2013-06-10 12:57:27 +00:00
|
|
|
offset++;
|
2013-05-20 11:16:48 +00:00
|
|
|
}
|
2011-11-01 23:55:28 +00:00
|
|
|
|
2014-11-07 11:43:33 +00:00
|
|
|
for(k = 0;k < NumChannels;k++)
|
2013-05-20 11:16:48 +00:00
|
|
|
{
|
|
|
|
ALfloat gain = state->Gain[0][k];
|
2014-11-07 11:43:33 +00:00
|
|
|
if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
|
2013-05-20 11:16:48 +00:00
|
|
|
{
|
|
|
|
for(i = 0;i < td;i++)
|
|
|
|
SamplesOut[k][i+base] += temps[i][0] * gain;
|
|
|
|
}
|
|
|
|
|
|
|
|
gain = state->Gain[1][k];
|
2014-11-07 11:43:33 +00:00
|
|
|
if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
|
2013-05-20 11:16:48 +00:00
|
|
|
{
|
|
|
|
for(i = 0;i < td;i++)
|
|
|
|
SamplesOut[k][i+base] += temps[i][1] * gain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
base += td;
|
2009-04-12 23:01:10 +00:00
|
|
|
}
|
2013-05-20 11:16:48 +00:00
|
|
|
|
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
|
|
|
|
2014-03-22 06:56:18 +00:00
|
|
|
DECLARE_DEFAULT_ALLOCATORS(ALechoState)
|
2013-05-21 19:47:18 +00:00
|
|
|
|
2013-05-21 11:18:02 +00:00
|
|
|
DEFINE_ALEFFECTSTATE_VTABLE(ALechoState);
|
|
|
|
|
2013-05-21 19:47:18 +00:00
|
|
|
|
2013-10-07 21:49:36 +00:00
|
|
|
typedef struct ALechoStateFactory {
|
|
|
|
DERIVE_FROM_TYPE(ALeffectStateFactory);
|
|
|
|
} ALechoStateFactory;
|
|
|
|
|
2013-10-07 15:34:54 +00:00
|
|
|
ALeffectState *ALechoStateFactory_create(ALechoStateFactory *UNUSED(factory))
|
2009-05-29 20:30:50 +00:00
|
|
|
{
|
|
|
|
ALechoState *state;
|
|
|
|
|
2014-03-22 06:56:18 +00:00
|
|
|
state = ALechoState_New(sizeof(*state));
|
2013-05-21 11:18:02 +00:00
|
|
|
if(!state) return NULL;
|
|
|
|
SET_VTABLE2(ALechoState, ALeffectState, state);
|
2009-05-29 20:30:50 +00:00
|
|
|
|
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
|
|
|
|
2013-05-28 02:55:00 +00:00
|
|
|
ALfilterState_clear(&state->Filter);
|
2009-05-29 20:30:50 +00:00
|
|
|
|
2013-05-21 09:30:11 +00:00
|
|
|
return STATIC_CAST(ALeffectState, state);
|
2009-05-29 20:30:50 +00:00
|
|
|
}
|
2013-03-14 04:53:42 +00:00
|
|
|
|
2013-05-21 19:47:18 +00:00
|
|
|
DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALechoStateFactory);
|
|
|
|
|
|
|
|
ALeffectStateFactory *ALechoStateFactory_getFactory(void)
|
|
|
|
{
|
2013-10-07 21:49:36 +00:00
|
|
|
static ALechoStateFactory EchoFactory = { { GET_VTABLE2(ALechoStateFactory, ALeffectStateFactory) } };
|
|
|
|
|
2013-05-21 19:47:18 +00:00
|
|
|
return STATIC_CAST(ALeffectStateFactory, &EchoFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-07 15:34:54 +00:00
|
|
|
void ALecho_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
|
|
|
|
{ SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
|
2013-05-29 18:17:45 +00:00
|
|
|
void ALecho_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
|
2013-03-14 04:53:42 +00:00
|
|
|
{
|
2013-05-29 18:17:45 +00:00
|
|
|
ALecho_setParami(effect, context, param, vals[0]);
|
2013-03-14 04:53:42 +00:00
|
|
|
}
|
2013-05-29 18:17:45 +00:00
|
|
|
void ALecho_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
|
2013-03-14 04:53:42 +00:00
|
|
|
{
|
2013-05-26 05:07:31 +00:00
|
|
|
ALeffectProps *props = &effect->Props;
|
2013-03-14 04:53:42 +00:00
|
|
|
switch(param)
|
|
|
|
{
|
|
|
|
case AL_ECHO_DELAY:
|
2013-05-26 15:54:34 +00:00
|
|
|
if(!(val >= AL_ECHO_MIN_DELAY && val <= AL_ECHO_MAX_DELAY))
|
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
|
|
|
|
props->Echo.Delay = val;
|
2013-03-14 04:53:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_LRDELAY:
|
2013-05-26 15:54:34 +00:00
|
|
|
if(!(val >= AL_ECHO_MIN_LRDELAY && val <= AL_ECHO_MAX_LRDELAY))
|
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
|
|
|
|
props->Echo.LRDelay = val;
|
2013-03-14 04:53:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_DAMPING:
|
2013-05-26 15:54:34 +00:00
|
|
|
if(!(val >= AL_ECHO_MIN_DAMPING && val <= AL_ECHO_MAX_DAMPING))
|
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
|
|
|
|
props->Echo.Damping = val;
|
2013-03-14 04:53:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_FEEDBACK:
|
2013-05-26 15:54:34 +00:00
|
|
|
if(!(val >= AL_ECHO_MIN_FEEDBACK && val <= AL_ECHO_MAX_FEEDBACK))
|
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
|
|
|
|
props->Echo.Feedback = val;
|
2013-03-14 04:53:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_SPREAD:
|
2013-05-26 15:54:34 +00:00
|
|
|
if(!(val >= AL_ECHO_MIN_SPREAD && val <= AL_ECHO_MAX_SPREAD))
|
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
|
|
|
|
props->Echo.Spread = val;
|
2013-03-14 04:53:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-05-26 15:54:34 +00:00
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
|
2013-03-14 04:53:42 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-29 18:17:45 +00:00
|
|
|
void ALecho_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
|
2013-03-14 04:53:42 +00:00
|
|
|
{
|
2013-05-29 18:17:45 +00:00
|
|
|
ALecho_setParamf(effect, context, param, vals[0]);
|
2013-03-14 04:53:42 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 19:56:41 +00:00
|
|
|
void ALecho_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
|
2013-10-07 15:34:54 +00:00
|
|
|
{ SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
|
2013-10-07 19:56:41 +00:00
|
|
|
void ALecho_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
|
2013-03-14 04:53:42 +00:00
|
|
|
{
|
2013-05-29 18:17:45 +00:00
|
|
|
ALecho_getParami(effect, context, param, vals);
|
2013-03-14 04:53:42 +00:00
|
|
|
}
|
2013-10-07 19:56:41 +00:00
|
|
|
void ALecho_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
|
2013-03-14 04:53:42 +00:00
|
|
|
{
|
2013-05-26 05:07:31 +00:00
|
|
|
const ALeffectProps *props = &effect->Props;
|
2013-03-14 04:53:42 +00:00
|
|
|
switch(param)
|
|
|
|
{
|
|
|
|
case AL_ECHO_DELAY:
|
2013-05-26 05:07:31 +00:00
|
|
|
*val = props->Echo.Delay;
|
2013-03-14 04:53:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_LRDELAY:
|
2013-05-26 05:07:31 +00:00
|
|
|
*val = props->Echo.LRDelay;
|
2013-03-14 04:53:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_DAMPING:
|
2013-05-26 05:07:31 +00:00
|
|
|
*val = props->Echo.Damping;
|
2013-03-14 04:53:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_FEEDBACK:
|
2013-05-26 05:07:31 +00:00
|
|
|
*val = props->Echo.Feedback;
|
2013-03-14 04:53:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_SPREAD:
|
2013-05-26 05:07:31 +00:00
|
|
|
*val = props->Echo.Spread;
|
2013-03-14 04:53:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-05-26 15:54:34 +00:00
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
|
2013-03-14 04:53:42 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-07 19:56:41 +00:00
|
|
|
void ALecho_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
|
2013-03-14 04:53:42 +00:00
|
|
|
{
|
2013-05-29 18:17:45 +00:00
|
|
|
ALecho_getParamf(effect, context, param, vals);
|
2013-03-14 04:53:42 +00:00
|
|
|
}
|
2013-05-25 06:26:59 +00:00
|
|
|
|
|
|
|
DEFINE_ALEFFECT_VTABLE(ALecho);
|