openal-soft/alc/effects/dedicated.cpp

159 lines
5.7 KiB
C++
Raw Normal View History

2011-03-12 20:11:25 -08:00
/**
* OpenAL cross platform audio library
* Copyright (C) 2011 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.
2011-03-12 20:11:25 -08:00
* Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#include "config.h"
2019-01-09 19:42:40 +01:00
#include <cstdlib>
2018-11-19 09:51:29 -08:00
#include <cmath>
2018-11-19 19:57:30 -08:00
#include <algorithm>
2011-03-12 20:11:25 -08:00
#include "al/auxeffectslot.h"
#include "alcmain.h"
#include "alcontext.h"
2011-03-12 20:11:25 -08:00
#include "alu.h"
namespace {
struct DedicatedState final : public EffectState {
2018-11-19 19:57:30 -08:00
ALfloat mCurrentGains[MAX_OUTPUT_CHANNELS];
ALfloat mTargetGains[MAX_OUTPUT_CHANNELS];
2018-12-22 18:43:34 -08:00
ALboolean deviceUpdate(const ALCdevice *device) override;
2019-03-22 22:48:12 -07:00
void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
void process(const ALsizei samplesToDo, const FloatBufferLine *RESTRICT samplesIn, const ALsizei numInput, const al::span<FloatBufferLine> samplesOut) override;
2011-03-12 20:11:25 -08:00
DEF_NEWDEL(DedicatedState)
};
2011-03-12 20:11:25 -08:00
2019-07-28 17:15:34 -07:00
ALboolean DedicatedState::deviceUpdate(const ALCdevice*)
2011-03-12 20:11:25 -08:00
{
std::fill(std::begin(mCurrentGains), std::end(mCurrentGains), 0.0f);
2011-03-12 20:11:25 -08:00
return AL_TRUE;
}
2019-07-28 17:15:34 -07:00
void DedicatedState::update(const ALCcontext*, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
2011-03-12 20:11:25 -08:00
{
std::fill(std::begin(mTargetGains), std::end(mTargetGains), 0.0f);
2011-03-12 20:11:25 -08:00
const ALfloat Gain{slot->Params.Gain * props->Dedicated.Gain};
if(slot->Params.EffectType == AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT)
{
const int idx{!target.RealOut ? -1 : GetChannelIdxByName(*target.RealOut, LFE)};
if(idx != -1)
{
2019-07-03 23:26:33 -07:00
mOutTarget = target.RealOut->Buffer;
mTargetGains[idx] = Gain;
}
}
else if(slot->Params.EffectType == AL_EFFECT_DEDICATED_DIALOGUE)
{
/* Dialog goes to the front-center speaker if it exists, otherwise it
* plays from the front-center location. */
const int idx{!target.RealOut ? -1 : GetChannelIdxByName(*target.RealOut, FrontCenter)};
2018-11-19 19:57:30 -08:00
if(idx != -1)
{
2019-07-03 23:26:33 -07:00
mOutTarget = target.RealOut->Buffer;
mTargetGains[idx] = Gain;
}
else
{
ALfloat coeffs[MAX_AMBI_CHANNELS];
CalcDirectionCoeffs({0.0f, 0.0f, -1.0f}, 0.0f, coeffs);
2019-07-04 15:02:12 -07:00
mOutTarget = target.Main->Buffer;
ComputePanGains(target.Main, coeffs, Gain, mTargetGains);
}
}
2011-03-12 20:11:25 -08:00
}
void DedicatedState::process(const ALsizei samplesToDo, const FloatBufferLine *RESTRICT samplesIn, const ALsizei /*numInput*/, const al::span<FloatBufferLine> samplesOut)
2011-03-12 20:11:25 -08:00
{
MixSamples(samplesIn[0].data(), samplesOut, mCurrentGains, mTargetGains, samplesToDo, 0,
samplesToDo);
2011-03-12 20:11:25 -08:00
}
2019-03-22 22:48:12 -07:00
void Dedicated_setParami(EffectProps*, ALCcontext *context, ALenum param, ALint)
{ context->setError(AL_INVALID_ENUM, "Invalid dedicated integer property 0x%04x", param); }
2019-03-22 22:48:12 -07:00
void Dedicated_setParamiv(EffectProps*, ALCcontext *context, ALenum param, const ALint*)
{ context->setError(AL_INVALID_ENUM, "Invalid dedicated integer-vector property 0x%04x", param); }
2019-03-22 22:48:12 -07:00
void Dedicated_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
{
switch(param)
{
case AL_DEDICATED_GAIN:
2018-11-19 09:51:29 -08:00
if(!(val >= 0.0f && std::isfinite(val)))
SETERR_RETURN(context, AL_INVALID_VALUE,, "Dedicated gain out of range");
props->Dedicated.Gain = val;
break;
default:
context->setError(AL_INVALID_ENUM, "Invalid dedicated float property 0x%04x", param);
}
}
2019-03-22 22:48:12 -07:00
void Dedicated_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
{ Dedicated_setParamf(props, context, param, vals[0]); }
2019-03-22 22:48:12 -07:00
void Dedicated_getParami(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
{ context->setError(AL_INVALID_ENUM, "Invalid dedicated integer property 0x%04x", param); }
2019-03-22 22:48:12 -07:00
void Dedicated_getParamiv(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
{ context->setError(AL_INVALID_ENUM, "Invalid dedicated integer-vector property 0x%04x", param); }
2019-03-22 22:48:12 -07:00
void Dedicated_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
{
switch(param)
{
case AL_DEDICATED_GAIN:
*val = props->Dedicated.Gain;
break;
default:
context->setError(AL_INVALID_ENUM, "Invalid dedicated float property 0x%04x", param);
}
}
2019-03-22 22:48:12 -07:00
void Dedicated_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
{ Dedicated_getParamf(props, context, param, vals); }
DEFINE_ALEFFECT_VTABLE(Dedicated);
struct DedicatedStateFactory final : public EffectStateFactory {
EffectState *create() override { return new DedicatedState{}; }
2019-03-22 22:48:12 -07:00
EffectProps getDefaultProps() const noexcept override;
const EffectVtable *getEffectVtable() const noexcept override { return &Dedicated_vtable; }
};
2019-03-22 22:48:12 -07:00
EffectProps DedicatedStateFactory::getDefaultProps() const noexcept
{
2019-03-22 22:48:12 -07:00
EffectProps props{};
props.Dedicated.Gain = 1.0f;
return props;
}
} // namespace
EffectStateFactory *DedicatedStateFactory_getFactory()
{
static DedicatedStateFactory DedicatedFactory{};
return &DedicatedFactory;
}