axmol/thirdparty/openal/al/effects/modulator.cpp

320 lines
9.7 KiB
C++
Raw Normal View History

#include "config.h"
2021-05-14 10:15:42 +08:00
#include <stdexcept>
#include "AL/al.h"
#include "AL/efx.h"
2021-05-14 10:15:42 +08:00
#include "alc/effects/base.h"
#include "aloptional.h"
#include "effects.h"
2022-04-25 12:02:45 +08:00
#ifdef ALSOFT_EAX
#include <cassert>
#include "alnumeric.h"
#include "al/eax/exception.h"
#include "al/eax/utils.h"
2022-04-25 12:02:45 +08:00
#endif // ALSOFT_EAX
namespace {
al::optional<ModulatorWaveform> WaveformFromEmum(ALenum value)
{
switch(value)
{
2023-02-04 15:03:54 +08:00
case AL_RING_MODULATOR_SINUSOID: return ModulatorWaveform::Sinusoid;
case AL_RING_MODULATOR_SAWTOOTH: return ModulatorWaveform::Sawtooth;
case AL_RING_MODULATOR_SQUARE: return ModulatorWaveform::Square;
}
return al::nullopt;
}
ALenum EnumFromWaveform(ModulatorWaveform type)
{
switch(type)
{
case ModulatorWaveform::Sinusoid: return AL_RING_MODULATOR_SINUSOID;
case ModulatorWaveform::Sawtooth: return AL_RING_MODULATOR_SAWTOOTH;
case ModulatorWaveform::Square: return AL_RING_MODULATOR_SQUARE;
}
throw std::runtime_error{"Invalid modulator waveform: " +
std::to_string(static_cast<int>(type))};
}
void Modulator_setParamf(EffectProps *props, ALenum param, float val)
{
switch(param)
{
case AL_RING_MODULATOR_FREQUENCY:
if(!(val >= AL_RING_MODULATOR_MIN_FREQUENCY && val <= AL_RING_MODULATOR_MAX_FREQUENCY))
throw effect_exception{AL_INVALID_VALUE, "Modulator frequency out of range: %f", val};
props->Modulator.Frequency = val;
break;
case AL_RING_MODULATOR_HIGHPASS_CUTOFF:
if(!(val >= AL_RING_MODULATOR_MIN_HIGHPASS_CUTOFF && val <= AL_RING_MODULATOR_MAX_HIGHPASS_CUTOFF))
throw effect_exception{AL_INVALID_VALUE, "Modulator high-pass cutoff out of range: %f", val};
props->Modulator.HighPassCutoff = val;
break;
default:
throw effect_exception{AL_INVALID_ENUM, "Invalid modulator float property 0x%04x", param};
}
}
void Modulator_setParamfv(EffectProps *props, ALenum param, const float *vals)
{ Modulator_setParamf(props, param, vals[0]); }
void Modulator_setParami(EffectProps *props, ALenum param, int val)
{
switch(param)
{
case AL_RING_MODULATOR_FREQUENCY:
case AL_RING_MODULATOR_HIGHPASS_CUTOFF:
Modulator_setParamf(props, param, static_cast<float>(val));
break;
case AL_RING_MODULATOR_WAVEFORM:
if(auto formopt = WaveformFromEmum(val))
props->Modulator.Waveform = *formopt;
else
throw effect_exception{AL_INVALID_VALUE, "Invalid modulator waveform: 0x%04x", val};
break;
default:
throw effect_exception{AL_INVALID_ENUM, "Invalid modulator integer property 0x%04x",
param};
}
}
void Modulator_setParamiv(EffectProps *props, ALenum param, const int *vals)
{ Modulator_setParami(props, param, vals[0]); }
void Modulator_getParami(const EffectProps *props, ALenum param, int *val)
{
switch(param)
{
case AL_RING_MODULATOR_FREQUENCY:
*val = static_cast<int>(props->Modulator.Frequency);
break;
case AL_RING_MODULATOR_HIGHPASS_CUTOFF:
*val = static_cast<int>(props->Modulator.HighPassCutoff);
break;
case AL_RING_MODULATOR_WAVEFORM:
*val = EnumFromWaveform(props->Modulator.Waveform);
break;
default:
throw effect_exception{AL_INVALID_ENUM, "Invalid modulator integer property 0x%04x",
param};
}
}
void Modulator_getParamiv(const EffectProps *props, ALenum param, int *vals)
{ Modulator_getParami(props, param, vals); }
void Modulator_getParamf(const EffectProps *props, ALenum param, float *val)
{
switch(param)
{
case AL_RING_MODULATOR_FREQUENCY:
*val = props->Modulator.Frequency;
break;
case AL_RING_MODULATOR_HIGHPASS_CUTOFF:
*val = props->Modulator.HighPassCutoff;
break;
default:
throw effect_exception{AL_INVALID_ENUM, "Invalid modulator float property 0x%04x", param};
}
}
void Modulator_getParamfv(const EffectProps *props, ALenum param, float *vals)
{ Modulator_getParamf(props, param, vals); }
EffectProps genDefaultProps() noexcept
{
EffectProps props{};
props.Modulator.Frequency = AL_RING_MODULATOR_DEFAULT_FREQUENCY;
props.Modulator.HighPassCutoff = AL_RING_MODULATOR_DEFAULT_HIGHPASS_CUTOFF;
props.Modulator.Waveform = *WaveformFromEmum(AL_RING_MODULATOR_DEFAULT_WAVEFORM);
return props;
}
} // namespace
DEFINE_ALEFFECT_VTABLE(Modulator);
const EffectProps ModulatorEffectProps{genDefaultProps()};
2022-04-25 12:02:45 +08:00
#ifdef ALSOFT_EAX
namespace {
class EaxRingModulatorEffectException : public EaxException
2022-04-25 12:02:45 +08:00
{
public:
explicit EaxRingModulatorEffectException(const char* message)
: EaxException{"EAX_RING_MODULATOR_EFFECT", message}
{}
}; // EaxRingModulatorEffectException
2022-04-25 12:02:45 +08:00
class EaxRingModulatorEffect final : public EaxEffect4<EaxRingModulatorEffectException, EAXRINGMODULATORPROPERTIES>
2022-04-25 12:02:45 +08:00
{
public:
2023-02-04 15:03:54 +08:00
EaxRingModulatorEffect(int eax_version);
2022-04-25 12:02:45 +08:00
private:
struct FrequencyValidator {
void operator()(float flFrequency) const
{
eax_validate_range<EaxRingModulatorEffectException>(
"Frequency",
flFrequency,
EAXRINGMODULATOR_MINFREQUENCY,
EAXRINGMODULATOR_MAXFREQUENCY);
}
}; // FrequencyValidator
struct HighPassCutOffValidator {
void operator()(float flHighPassCutOff) const
{
eax_validate_range<EaxRingModulatorEffectException>(
"High-Pass Cutoff",
flHighPassCutOff,
EAXRINGMODULATOR_MINHIGHPASSCUTOFF,
EAXRINGMODULATOR_MAXHIGHPASSCUTOFF);
}
}; // HighPassCutOffValidator
struct WaveformValidator {
void operator()(unsigned long ulWaveform) const
{
eax_validate_range<EaxRingModulatorEffectException>(
"Waveform",
ulWaveform,
EAXRINGMODULATOR_MINWAVEFORM,
EAXRINGMODULATOR_MAXWAVEFORM);
}
}; // WaveformValidator
struct AllValidator {
void operator()(const Props& all) const
{
FrequencyValidator{}(all.flFrequency);
HighPassCutOffValidator{}(all.flHighPassCutOff);
WaveformValidator{}(all.ulWaveform);
}
}; // AllValidator
void set_defaults(Props& props) override;
void set_efx_frequency() noexcept;
void set_efx_high_pass_cutoff() noexcept;
2022-04-25 12:02:45 +08:00
void set_efx_waveform();
void set_efx_defaults() override;
2022-04-25 12:02:45 +08:00
void get(const EaxCall& call, const Props& props) override;
void set(const EaxCall& call, Props& props) override;
bool commit_props(const Props& props) override;
2022-04-25 12:02:45 +08:00
}; // EaxRingModulatorEffect
2023-02-04 15:03:54 +08:00
EaxRingModulatorEffect::EaxRingModulatorEffect(int eax_version)
: EaxEffect4{AL_EFFECT_RING_MODULATOR, eax_version}
{}
2022-04-25 12:02:45 +08:00
void EaxRingModulatorEffect::set_defaults(Props& props)
2022-04-25 12:02:45 +08:00
{
props.flFrequency = EAXRINGMODULATOR_DEFAULTFREQUENCY;
props.flHighPassCutOff = EAXRINGMODULATOR_DEFAULTHIGHPASSCUTOFF;
props.ulWaveform = EAXRINGMODULATOR_DEFAULTWAVEFORM;
2022-04-25 12:02:45 +08:00
}
void EaxRingModulatorEffect::set_efx_frequency() noexcept
2022-04-25 12:02:45 +08:00
{
al_effect_props_.Modulator.Frequency = clamp(
props_.flFrequency,
2022-04-25 12:02:45 +08:00
AL_RING_MODULATOR_MIN_FREQUENCY,
AL_RING_MODULATOR_MAX_FREQUENCY);
}
void EaxRingModulatorEffect::set_efx_high_pass_cutoff() noexcept
2022-04-25 12:02:45 +08:00
{
al_effect_props_.Modulator.HighPassCutoff = clamp(
props_.flHighPassCutOff,
2022-04-25 12:02:45 +08:00
AL_RING_MODULATOR_MIN_HIGHPASS_CUTOFF,
AL_RING_MODULATOR_MAX_HIGHPASS_CUTOFF);
}
void EaxRingModulatorEffect::set_efx_waveform()
{
const auto waveform = clamp(
static_cast<ALint>(props_.ulWaveform),
2022-04-25 12:02:45 +08:00
AL_RING_MODULATOR_MIN_WAVEFORM,
AL_RING_MODULATOR_MAX_WAVEFORM);
const auto efx_waveform = WaveformFromEmum(waveform);
assert(efx_waveform.has_value());
al_effect_props_.Modulator.Waveform = *efx_waveform;
}
void EaxRingModulatorEffect::set_efx_defaults()
{
set_efx_frequency();
set_efx_high_pass_cutoff();
set_efx_waveform();
}
void EaxRingModulatorEffect::get(const EaxCall& call, const Props& props)
2022-04-25 12:02:45 +08:00
{
switch(call.get_property_id())
2022-04-25 12:02:45 +08:00
{
case EAXRINGMODULATOR_NONE: break;
case EAXRINGMODULATOR_ALLPARAMETERS: call.set_value<Exception>(props); break;
case EAXRINGMODULATOR_FREQUENCY: call.set_value<Exception>(props.flFrequency); break;
case EAXRINGMODULATOR_HIGHPASSCUTOFF: call.set_value<Exception>(props.flHighPassCutOff); break;
case EAXRINGMODULATOR_WAVEFORM: call.set_value<Exception>(props.ulWaveform); break;
default: fail_unknown_property_id();
2022-04-25 12:02:45 +08:00
}
}
void EaxRingModulatorEffect::set(const EaxCall& call, Props& props)
2022-04-25 12:02:45 +08:00
{
switch (call.get_property_id())
2022-04-25 12:02:45 +08:00
{
case EAXRINGMODULATOR_NONE: break;
case EAXRINGMODULATOR_ALLPARAMETERS: defer<AllValidator>(call, props); break;
case EAXRINGMODULATOR_FREQUENCY: defer<FrequencyValidator>(call, props.flFrequency); break;
case EAXRINGMODULATOR_HIGHPASSCUTOFF: defer<HighPassCutOffValidator>(call, props.flHighPassCutOff); break;
case EAXRINGMODULATOR_WAVEFORM: defer<WaveformValidator>(call, props.ulWaveform); break;
default: fail_unknown_property_id();
2022-04-25 12:02:45 +08:00
}
}
2022-04-25 12:02:45 +08:00
bool EaxRingModulatorEffect::commit_props(const Props& props)
{
auto is_dirty = false;
2022-04-25 12:02:45 +08:00
if (props_.flFrequency != props.flFrequency)
2022-04-25 12:02:45 +08:00
{
is_dirty = true;
2022-04-25 12:02:45 +08:00
set_efx_frequency();
}
if (props_.flHighPassCutOff != props.flHighPassCutOff)
2022-04-25 12:02:45 +08:00
{
is_dirty = true;
2022-04-25 12:02:45 +08:00
set_efx_high_pass_cutoff();
}
if (props_.ulWaveform != props.ulWaveform)
2022-04-25 12:02:45 +08:00
{
is_dirty = true;
2022-04-25 12:02:45 +08:00
set_efx_waveform();
}
return is_dirty;
2022-04-25 12:02:45 +08:00
}
} // namespace
2023-02-04 15:03:54 +08:00
EaxEffectUPtr eax_create_eax_ring_modulator_effect(int eax_version)
2022-04-25 12:02:45 +08:00
{
2023-02-04 15:03:54 +08:00
return eax_create_eax4_effect<EaxRingModulatorEffect>(eax_version);
2022-04-25 12:02:45 +08:00
}
#endif // ALSOFT_EAX