| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #include "config.h"
- #include "AL/al.h"
- #include "AL/efx.h"
- #include "alc/context.h"
- #include "alnumeric.h"
- #include "effects.h"
- #if ALSOFT_EAX
- #include "al/eax/effect.h"
- #include "al/eax/exception.h"
- #include "al/eax/utils.h"
- #endif // ALSOFT_EAX
- namespace {
- constexpr EffectProps genDefaultProps() noexcept
- {
- DistortionProps props{};
- props.Edge = AL_DISTORTION_DEFAULT_EDGE;
- props.Gain = AL_DISTORTION_DEFAULT_GAIN;
- props.LowpassCutoff = AL_DISTORTION_DEFAULT_LOWPASS_CUTOFF;
- props.EQCenter = AL_DISTORTION_DEFAULT_EQCENTER;
- props.EQBandwidth = AL_DISTORTION_DEFAULT_EQBANDWIDTH;
- return props;
- }
- } // namespace
- const EffectProps DistortionEffectProps{genDefaultProps()};
- void DistortionEffectHandler::SetParami(ALCcontext *context, DistortionProps&, ALenum param, int)
- { context->throw_error(AL_INVALID_ENUM, "Invalid distortion integer property {:#04x}", as_unsigned(param)); }
- void DistortionEffectHandler::SetParamiv(ALCcontext *context, DistortionProps&, ALenum param, const int*)
- { context->throw_error(AL_INVALID_ENUM, "Invalid distortion integer-vector property {:#04x}", as_unsigned(param)); }
- void DistortionEffectHandler::SetParamf(ALCcontext *context, DistortionProps &props, ALenum param, float val)
- {
- switch(param)
- {
- case AL_DISTORTION_EDGE:
- if(!(val >= AL_DISTORTION_MIN_EDGE && val <= AL_DISTORTION_MAX_EDGE))
- context->throw_error(AL_INVALID_VALUE, "Distortion edge out of range");
- props.Edge = val;
- return;
- case AL_DISTORTION_GAIN:
- if(!(val >= AL_DISTORTION_MIN_GAIN && val <= AL_DISTORTION_MAX_GAIN))
- context->throw_error(AL_INVALID_VALUE, "Distortion gain out of range");
- props.Gain = val;
- return;
- case AL_DISTORTION_LOWPASS_CUTOFF:
- if(!(val >= AL_DISTORTION_MIN_LOWPASS_CUTOFF && val <= AL_DISTORTION_MAX_LOWPASS_CUTOFF))
- context->throw_error(AL_INVALID_VALUE, "Distortion low-pass cutoff out of range");
- props.LowpassCutoff = val;
- return;
- case AL_DISTORTION_EQCENTER:
- if(!(val >= AL_DISTORTION_MIN_EQCENTER && val <= AL_DISTORTION_MAX_EQCENTER))
- context->throw_error(AL_INVALID_VALUE, "Distortion EQ center out of range");
- props.EQCenter = val;
- return;
- case AL_DISTORTION_EQBANDWIDTH:
- if(!(val >= AL_DISTORTION_MIN_EQBANDWIDTH && val <= AL_DISTORTION_MAX_EQBANDWIDTH))
- context->throw_error(AL_INVALID_VALUE, "Distortion EQ bandwidth out of range");
- props.EQBandwidth = val;
- return;
- }
- context->throw_error(AL_INVALID_ENUM, "Invalid distortion float property {:#04x}",
- as_unsigned(param));
- }
- void DistortionEffectHandler::SetParamfv(ALCcontext *context, DistortionProps &props, ALenum param, const float *vals)
- { SetParamf(context, props, param, *vals); }
- void DistortionEffectHandler::GetParami(ALCcontext *context, const DistortionProps&, ALenum param, int*)
- { context->throw_error(AL_INVALID_ENUM, "Invalid distortion integer property {:#04x}", as_unsigned(param)); }
- void DistortionEffectHandler::GetParamiv(ALCcontext *context, const DistortionProps&, ALenum param, int*)
- { context->throw_error(AL_INVALID_ENUM, "Invalid distortion integer-vector property {:#04x}", as_unsigned(param)); }
- void DistortionEffectHandler::GetParamf(ALCcontext *context, const DistortionProps &props, ALenum param, float *val)
- {
- switch(param)
- {
- case AL_DISTORTION_EDGE: *val = props.Edge; return;
- case AL_DISTORTION_GAIN: *val = props.Gain; return;
- case AL_DISTORTION_LOWPASS_CUTOFF: *val = props.LowpassCutoff; return;
- case AL_DISTORTION_EQCENTER: *val = props.EQCenter; return;
- case AL_DISTORTION_EQBANDWIDTH: *val = props.EQBandwidth; return;
- }
- context->throw_error(AL_INVALID_ENUM, "Invalid distortion float property {:#04x}",
- as_unsigned(param));
- }
- void DistortionEffectHandler::GetParamfv(ALCcontext *context, const DistortionProps &props, ALenum param, float *vals)
- { GetParamf(context, props, param, vals); }
- #if ALSOFT_EAX
- namespace {
- using DistortionCommitter = EaxCommitter<EaxDistortionCommitter>;
- struct EdgeValidator {
- void operator()(float flEdge) const
- {
- eax_validate_range<DistortionCommitter::Exception>(
- "Edge",
- flEdge,
- EAXDISTORTION_MINEDGE,
- EAXDISTORTION_MAXEDGE);
- }
- }; // EdgeValidator
- struct GainValidator {
- void operator()(long lGain) const
- {
- eax_validate_range<DistortionCommitter::Exception>(
- "Gain",
- lGain,
- EAXDISTORTION_MINGAIN,
- EAXDISTORTION_MAXGAIN);
- }
- }; // GainValidator
- struct LowPassCutOffValidator {
- void operator()(float flLowPassCutOff) const
- {
- eax_validate_range<DistortionCommitter::Exception>(
- "Low-pass Cut-off",
- flLowPassCutOff,
- EAXDISTORTION_MINLOWPASSCUTOFF,
- EAXDISTORTION_MAXLOWPASSCUTOFF);
- }
- }; // LowPassCutOffValidator
- struct EqCenterValidator {
- void operator()(float flEQCenter) const
- {
- eax_validate_range<DistortionCommitter::Exception>(
- "EQ Center",
- flEQCenter,
- EAXDISTORTION_MINEQCENTER,
- EAXDISTORTION_MAXEQCENTER);
- }
- }; // EqCenterValidator
- struct EqBandwidthValidator {
- void operator()(float flEQBandwidth) const
- {
- eax_validate_range<DistortionCommitter::Exception>(
- "EQ Bandwidth",
- flEQBandwidth,
- EAXDISTORTION_MINEQBANDWIDTH,
- EAXDISTORTION_MAXEQBANDWIDTH);
- }
- }; // EqBandwidthValidator
- struct AllValidator {
- void operator()(const EAXDISTORTIONPROPERTIES& all) const
- {
- EdgeValidator{}(all.flEdge);
- GainValidator{}(all.lGain);
- LowPassCutOffValidator{}(all.flLowPassCutOff);
- EqCenterValidator{}(all.flEQCenter);
- EqBandwidthValidator{}(all.flEQBandwidth);
- }
- }; // AllValidator
- } // namespace
- template<>
- struct DistortionCommitter::Exception : public EaxException {
- explicit Exception(const char *message) : EaxException{"EAX_DISTORTION_EFFECT", message}
- { }
- };
- template<>
- [[noreturn]] void DistortionCommitter::fail(const char *message)
- {
- throw Exception{message};
- }
- bool EaxDistortionCommitter::commit(const EAXDISTORTIONPROPERTIES &props)
- {
- if(auto *cur = std::get_if<EAXDISTORTIONPROPERTIES>(&mEaxProps); cur && *cur == props)
- return false;
- mEaxProps = props;
- mAlProps = [&]{
- DistortionProps ret{};
- ret.Edge = props.flEdge;
- ret.Gain = level_mb_to_gain(static_cast<float>(props.lGain));
- ret.LowpassCutoff = props.flLowPassCutOff;
- ret.EQCenter = props.flEQCenter;
- ret.EQBandwidth = props.flEdge;
- return ret;
- }();
- return true;
- }
- void EaxDistortionCommitter::SetDefaults(EaxEffectProps &props)
- {
- static constexpr EAXDISTORTIONPROPERTIES defprops{[]
- {
- EAXDISTORTIONPROPERTIES ret{};
- ret.flEdge = EAXDISTORTION_DEFAULTEDGE;
- ret.lGain = EAXDISTORTION_DEFAULTGAIN;
- ret.flLowPassCutOff = EAXDISTORTION_DEFAULTLOWPASSCUTOFF;
- ret.flEQCenter = EAXDISTORTION_DEFAULTEQCENTER;
- ret.flEQBandwidth = EAXDISTORTION_DEFAULTEQBANDWIDTH;
- return ret;
- }()};
- props = defprops;
- }
- void EaxDistortionCommitter::Get(const EaxCall &call, const EAXDISTORTIONPROPERTIES &props)
- {
- switch(call.get_property_id())
- {
- case EAXDISTORTION_NONE: break;
- case EAXDISTORTION_ALLPARAMETERS: call.set_value<Exception>(props); break;
- case EAXDISTORTION_EDGE: call.set_value<Exception>(props.flEdge); break;
- case EAXDISTORTION_GAIN: call.set_value<Exception>(props.lGain); break;
- case EAXDISTORTION_LOWPASSCUTOFF: call.set_value<Exception>(props.flLowPassCutOff); break;
- case EAXDISTORTION_EQCENTER: call.set_value<Exception>(props.flEQCenter); break;
- case EAXDISTORTION_EQBANDWIDTH: call.set_value<Exception>(props.flEQBandwidth); break;
- default: fail_unknown_property_id();
- }
- }
- void EaxDistortionCommitter::Set(const EaxCall &call, EAXDISTORTIONPROPERTIES &props)
- {
- switch(call.get_property_id())
- {
- case EAXDISTORTION_NONE: break;
- case EAXDISTORTION_ALLPARAMETERS: defer<AllValidator>(call, props); break;
- case EAXDISTORTION_EDGE: defer<EdgeValidator>(call, props.flEdge); break;
- case EAXDISTORTION_GAIN: defer<GainValidator>(call, props.lGain); break;
- case EAXDISTORTION_LOWPASSCUTOFF: defer<LowPassCutOffValidator>(call, props.flLowPassCutOff); break;
- case EAXDISTORTION_EQCENTER: defer<EqCenterValidator>(call, props.flEQCenter); break;
- case EAXDISTORTION_EQBANDWIDTH: defer<EqBandwidthValidator>(call, props.flEQBandwidth); break;
- default: fail_unknown_property_id();
- }
- }
- #endif // ALSOFT_EAX
|