2009-04-12 16:01:10 -07: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 14:11:03 +02:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2009-04-12 16:01:10 -07:00
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2018-11-19 19:57:30 -08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2009-05-29 13:30:50 -07:00
|
|
|
#include "alMain.h"
|
2018-11-17 23:02:27 -08:00
|
|
|
#include "alcontext.h"
|
2009-04-12 16:01:10 -07:00
|
|
|
#include "alFilter.h"
|
|
|
|
#include "alAuxEffectSlot.h"
|
2009-05-31 11:54:49 -07:00
|
|
|
#include "alError.h"
|
2009-05-16 23:26:39 -07:00
|
|
|
#include "alu.h"
|
2018-03-22 07:05:40 -07:00
|
|
|
#include "filters/defs.h"
|
2018-11-19 20:15:40 -08:00
|
|
|
#include "vector.h"
|
2009-04-12 16:01:10 -07:00
|
|
|
|
2009-10-19 07:46:53 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
struct ALechoState final : public EffectState {
|
2018-11-19 20:15:40 -08:00
|
|
|
al::vector<ALfloat,16> mSampleBuffer;
|
2009-04-12 16:01:10 -07:00
|
|
|
|
2009-10-19 07:46:53 -07:00
|
|
|
// The echo is two tap. The delay is the number of samples from before the
|
|
|
|
// current offset
|
2009-04-12 16:01:10 -07:00
|
|
|
struct {
|
2018-11-19 19:57:30 -08:00
|
|
|
ALsizei delay{0};
|
|
|
|
} mTap[2];
|
|
|
|
ALsizei mOffset{0};
|
2017-12-19 15:13:11 -08:00
|
|
|
|
2011-11-01 16:55:28 -07:00
|
|
|
/* The panning gains for the two taps */
|
2017-12-19 15:13:11 -08:00
|
|
|
struct {
|
2018-11-19 19:57:30 -08:00
|
|
|
ALfloat Current[MAX_OUTPUT_CHANNELS]{};
|
|
|
|
ALfloat Target[MAX_OUTPUT_CHANNELS]{};
|
|
|
|
} mGains[2];
|
2009-04-12 16:01:10 -07:00
|
|
|
|
2018-11-19 19:57:30 -08:00
|
|
|
ALfloat mFeedGain{0.0f};
|
2009-04-16 05:43:09 -07:00
|
|
|
|
2018-11-19 19:57:30 -08:00
|
|
|
BiquadFilter mFilter;
|
2016-08-25 03:42:43 -07:00
|
|
|
|
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
ALboolean deviceUpdate(ALCdevice *device) override;
|
|
|
|
void update(const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props) override;
|
|
|
|
void process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], ALsizei numChannels) override;
|
2016-08-25 03:42:43 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
DEF_NEWDEL(ALechoState)
|
|
|
|
};
|
2009-04-12 16:01:10 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
ALboolean ALechoState::deviceUpdate(ALCdevice *Device)
|
2009-04-12 16:01:10 -07:00
|
|
|
{
|
2018-11-19 20:15:40 -08:00
|
|
|
ALuint maxlen;
|
2009-04-12 16:01:10 -07:00
|
|
|
|
2009-10-20 08:31:44 -07: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
|
2018-05-03 21:43:53 -07:00
|
|
|
maxlen = float2int(AL_ECHO_MAX_DELAY*Device->Frequency + 0.5f) +
|
|
|
|
float2int(AL_ECHO_MAX_LRDELAY*Device->Frequency + 0.5f);
|
2018-01-11 09:53:52 -08:00
|
|
|
maxlen = NextPowerOf2(maxlen);
|
|
|
|
if(maxlen <= 0) return AL_FALSE;
|
2009-10-19 07:46:53 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
if(maxlen != mSampleBuffer.size())
|
2009-10-19 07:46:53 -07:00
|
|
|
{
|
2018-11-19 22:34:26 -08:00
|
|
|
mSampleBuffer.resize(maxlen);
|
|
|
|
mSampleBuffer.shrink_to_fit();
|
2009-10-19 07:46:53 -07:00
|
|
|
}
|
2017-12-19 15:13:11 -08:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
std::fill(mSampleBuffer.begin(), mSampleBuffer.end(), 0.0f);
|
|
|
|
for(auto &e : mGains)
|
2018-11-19 19:57:30 -08:00
|
|
|
{
|
|
|
|
std::fill(std::begin(e.Current), std::end(e.Current), 0.0f);
|
|
|
|
std::fill(std::begin(e.Target), std::end(e.Target), 0.0f);
|
|
|
|
}
|
2009-04-12 16:01:10 -07:00
|
|
|
|
2009-10-20 08:31:44 -07:00
|
|
|
return AL_TRUE;
|
|
|
|
}
|
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
void ALechoState::update(const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props)
|
2009-10-20 08:31:44 -07:00
|
|
|
{
|
2017-09-21 05:42:35 -07:00
|
|
|
const ALCdevice *device = context->Device;
|
|
|
|
ALuint frequency = device->Frequency;
|
2016-01-25 06:11:51 -08:00
|
|
|
ALfloat coeffs[MAX_AMBI_COEFFS];
|
2017-12-19 15:13:11 -08:00
|
|
|
ALfloat gainhf, lrpan, spread;
|
2009-10-20 08:31:44 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
mTap[0].delay = maxi(float2int(props->Echo.Delay*frequency + 0.5f), 1);
|
|
|
|
mTap[1].delay = float2int(props->Echo.LRDelay*frequency + 0.5f);
|
|
|
|
mTap[1].delay += mTap[0].delay;
|
2009-04-12 16:01:10 -07:00
|
|
|
|
2016-05-13 18:28:01 -07:00
|
|
|
spread = props->Echo.Spread;
|
2016-04-24 21:42:59 -07:00
|
|
|
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;
|
2009-04-12 16:01:10 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
mFeedGain = props->Echo.Feedback;
|
2009-04-12 16:01:10 -07:00
|
|
|
|
2017-12-19 15:13:11 -08:00
|
|
|
gainhf = maxf(1.0f - props->Echo.Damping, 0.0625f); /* Limit -24dB */
|
2018-11-19 22:34:26 -08:00
|
|
|
BiquadFilter_setParams(&mFilter, BiquadType::HighShelf,
|
2018-04-04 18:07:46 -07:00
|
|
|
gainhf, LOWPASSFREQREF/frequency, calc_rcpQ_from_slope(gainhf, 1.0f)
|
|
|
|
);
|
2011-07-16 03:35:51 -07:00
|
|
|
|
2011-11-01 16:55:28 -07:00
|
|
|
/* First tap panning */
|
2017-02-23 16:44:59 -08:00
|
|
|
CalcAngleCoeffs(-F_PI_2*lrpan, 0.0f, spread, coeffs);
|
2018-11-19 22:34:26 -08:00
|
|
|
ComputePanGains(&device->Dry, coeffs, slot->Params.Gain, mGains[0].Target);
|
2011-11-01 16:55:28 -07:00
|
|
|
|
|
|
|
/* Second tap panning */
|
2017-02-23 16:44:59 -08:00
|
|
|
CalcAngleCoeffs( F_PI_2*lrpan, 0.0f, spread, coeffs);
|
2018-11-19 22:34:26 -08:00
|
|
|
ComputePanGains(&device->Dry, coeffs, slot->Params.Gain, mGains[1].Target);
|
2009-04-12 16:01:10 -07:00
|
|
|
}
|
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
void ALechoState::process(ALsizei SamplesToDo, const ALfloat (*RESTRICT SamplesIn)[BUFFERSIZE], ALfloat (*RESTRICT SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
|
2009-04-12 16:01:10 -07:00
|
|
|
{
|
2018-11-19 22:34:26 -08:00
|
|
|
const ALsizei mask = mSampleBuffer.size()-1;
|
|
|
|
const ALsizei tap1 = mTap[0].delay;
|
|
|
|
const ALsizei tap2 = mTap[1].delay;
|
|
|
|
ALfloat *RESTRICT delaybuf = mSampleBuffer.data();
|
|
|
|
ALsizei offset = mOffset;
|
2018-04-03 10:15:35 -07:00
|
|
|
ALfloat z1, z2, in, out;
|
2017-12-19 15:13:11 -08:00
|
|
|
ALsizei base;
|
|
|
|
ALsizei c, i;
|
2009-04-12 16:01:10 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
z1 = mFilter.z1;
|
|
|
|
z2 = mFilter.z2;
|
2013-05-20 04:16:48 -07:00
|
|
|
for(base = 0;base < SamplesToDo;)
|
2009-04-12 16:01:10 -07:00
|
|
|
{
|
2018-03-24 13:43:56 +01:00
|
|
|
alignas(16) ALfloat temps[2][128];
|
2017-03-24 14:35:25 -07:00
|
|
|
ALsizei td = mini(128, SamplesToDo-base);
|
2013-05-20 04:16:48 -07:00
|
|
|
|
|
|
|
for(i = 0;i < td;i++)
|
|
|
|
{
|
2017-12-19 14:11:37 -08:00
|
|
|
/* Feed the delay buffer's input first. */
|
|
|
|
delaybuf[offset&mask] = SamplesIn[0][i+base];
|
|
|
|
|
2013-05-20 04:16:48 -07:00
|
|
|
/* First tap */
|
2017-12-19 14:11:37 -08:00
|
|
|
temps[0][i] = delaybuf[(offset-tap1) & mask];
|
2013-05-20 04:16:48 -07:00
|
|
|
/* Second tap */
|
2017-12-19 14:11:37 -08:00
|
|
|
temps[1][i] = delaybuf[(offset-tap2) & mask];
|
2013-05-20 04:16:48 -07:00
|
|
|
|
2017-12-19 14:11:37 -08:00
|
|
|
/* Apply damping to the second tap, then add it to the buffer with
|
|
|
|
* feedback attenuation.
|
|
|
|
*/
|
|
|
|
in = temps[1][i];
|
2018-11-19 22:34:26 -08:00
|
|
|
out = in*mFilter.b0 + z1;
|
|
|
|
z1 = in*mFilter.b1 - out*mFilter.a1 + z2;
|
|
|
|
z2 = in*mFilter.b2 - out*mFilter.a2;
|
2016-07-26 04:09:01 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
delaybuf[offset&mask] += out * mFeedGain;
|
2013-06-10 05:57:27 -07:00
|
|
|
offset++;
|
2013-05-20 04:16:48 -07:00
|
|
|
}
|
2011-11-01 16:55:28 -07:00
|
|
|
|
2017-12-19 15:13:11 -08:00
|
|
|
for(c = 0;c < 2;c++)
|
2018-11-19 22:34:26 -08:00
|
|
|
MixSamples(temps[c], NumChannels, SamplesOut, mGains[c].Current,
|
|
|
|
mGains[c].Target, SamplesToDo-base, base, td);
|
2013-05-20 04:16:48 -07:00
|
|
|
|
|
|
|
base += td;
|
2009-04-12 16:01:10 -07:00
|
|
|
}
|
2018-11-19 22:34:26 -08:00
|
|
|
mFilter.z1 = z1;
|
|
|
|
mFilter.z2 = z2;
|
2013-05-20 04:16:48 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
mOffset = offset;
|
2009-04-12 16:01:10 -07:00
|
|
|
}
|
2009-05-29 13:30:50 -07:00
|
|
|
|
2013-05-21 12:47:18 -07:00
|
|
|
|
2018-11-17 06:53:20 -08:00
|
|
|
struct EchoStateFactory final : public EffectStateFactory {
|
2018-11-19 22:34:26 -08:00
|
|
|
EffectState *create() override;
|
2018-11-17 06:53:20 -08:00
|
|
|
};
|
2013-10-07 14:49:36 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
EffectState *EchoStateFactory::create()
|
|
|
|
{ return new ALechoState{}; }
|
2013-10-07 14:49:36 -07:00
|
|
|
|
2018-11-17 06:53:20 -08:00
|
|
|
EffectStateFactory *EchoStateFactory_getFactory(void)
|
|
|
|
{
|
|
|
|
static EchoStateFactory EchoFactory{};
|
2018-11-19 06:43:37 -08:00
|
|
|
return &EchoFactory;
|
2013-05-21 12:47:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-25 15:59:59 -08: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); }
|
2013-05-29 11:17:45 -07:00
|
|
|
void ALecho_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
|
2013-03-13 21:53:42 -07:00
|
|
|
{
|
2013-05-25 22:07:31 -07:00
|
|
|
ALeffectProps *props = &effect->Props;
|
2013-03-13 21:53:42 -07:00
|
|
|
switch(param)
|
|
|
|
{
|
|
|
|
case AL_ECHO_DELAY:
|
2013-05-26 08:54:34 -07:00
|
|
|
if(!(val >= AL_ECHO_MIN_DELAY && val <= AL_ECHO_MAX_DELAY))
|
2018-01-25 15:59:59 -08:00
|
|
|
SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo delay out of range");
|
2013-05-26 08:54:34 -07:00
|
|
|
props->Echo.Delay = val;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_LRDELAY:
|
2013-05-26 08:54:34 -07:00
|
|
|
if(!(val >= AL_ECHO_MIN_LRDELAY && val <= AL_ECHO_MAX_LRDELAY))
|
2018-01-25 15:59:59 -08:00
|
|
|
SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo LR delay out of range");
|
2013-05-26 08:54:34 -07:00
|
|
|
props->Echo.LRDelay = val;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_DAMPING:
|
2013-05-26 08:54:34 -07:00
|
|
|
if(!(val >= AL_ECHO_MIN_DAMPING && val <= AL_ECHO_MAX_DAMPING))
|
2018-01-25 15:59:59 -08:00
|
|
|
SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo damping out of range");
|
2013-05-26 08:54:34 -07:00
|
|
|
props->Echo.Damping = val;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_FEEDBACK:
|
2013-05-26 08:54:34 -07:00
|
|
|
if(!(val >= AL_ECHO_MIN_FEEDBACK && val <= AL_ECHO_MAX_FEEDBACK))
|
2018-01-25 15:59:59 -08:00
|
|
|
SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo feedback out of range");
|
2013-05-26 08:54:34 -07:00
|
|
|
props->Echo.Feedback = val;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_SPREAD:
|
2013-05-26 08:54:34 -07:00
|
|
|
if(!(val >= AL_ECHO_MIN_SPREAD && val <= AL_ECHO_MAX_SPREAD))
|
2018-01-25 15:59:59 -08:00
|
|
|
SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo spread out of range");
|
2013-05-26 08:54:34 -07:00
|
|
|
props->Echo.Spread = val;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-01-25 15:59:59 -08:00
|
|
|
alSetError(context, AL_INVALID_ENUM, "Invalid echo float property 0x%04x", param);
|
2013-03-13 21:53:42 -07:00
|
|
|
}
|
|
|
|
}
|
2013-05-29 11:17:45 -07:00
|
|
|
void ALecho_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
|
2018-01-24 17:07:01 -08:00
|
|
|
{ ALecho_setParamf(effect, context, param, vals[0]); }
|
2013-03-13 21:53:42 -07:00
|
|
|
|
2018-01-25 15:59:59 -08: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); }
|
2013-10-07 12:56:41 -07:00
|
|
|
void ALecho_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
|
2013-03-13 21:53:42 -07:00
|
|
|
{
|
2013-05-25 22:07:31 -07:00
|
|
|
const ALeffectProps *props = &effect->Props;
|
2013-03-13 21:53:42 -07:00
|
|
|
switch(param)
|
|
|
|
{
|
|
|
|
case AL_ECHO_DELAY:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Echo.Delay;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_LRDELAY:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Echo.LRDelay;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_DAMPING:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Echo.Damping;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_FEEDBACK:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Echo.Feedback;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_ECHO_SPREAD:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Echo.Spread;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-01-25 15:59:59 -08:00
|
|
|
alSetError(context, AL_INVALID_ENUM, "Invalid echo float property 0x%04x", param);
|
2013-03-13 21:53:42 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-07 12:56:41 -07:00
|
|
|
void ALecho_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
|
2018-01-24 17:07:01 -08:00
|
|
|
{ ALecho_getParamf(effect, context, param, vals); }
|
2013-05-24 23:26:59 -07:00
|
|
|
|
|
|
|
DEFINE_ALEFFECT_VTABLE(ALecho);
|