Check for unsigned underflow
[openal-soft.git] / al / effects / compressor.cpp
blob3579d82ff18d065bad202865269a6e047d4a31ae
2 #include "config.h"
4 #include "AL/al.h"
5 #include "AL/efx.h"
7 #include "alc/effects/base.h"
8 #include "effects.h"
10 #if ALSOFT_EAX
11 #include "al/eax/effect.h"
12 #include "al/eax/exception.h"
13 #include "al/eax/utils.h"
14 #endif // ALSOFT_EAX
17 namespace {
19 constexpr EffectProps genDefaultProps() noexcept
21 CompressorProps props{};
22 props.OnOff = AL_COMPRESSOR_DEFAULT_ONOFF;
23 return props;
26 } // namespace
28 const EffectProps CompressorEffectProps{genDefaultProps()};
30 void CompressorEffectHandler::SetParami(CompressorProps &props, ALenum param, int val)
32 switch(param)
34 case AL_COMPRESSOR_ONOFF:
35 if(!(val >= AL_COMPRESSOR_MIN_ONOFF && val <= AL_COMPRESSOR_MAX_ONOFF))
36 throw effect_exception{AL_INVALID_VALUE, "Compressor state out of range"};
37 props.OnOff = (val != AL_FALSE);
38 break;
40 default:
41 throw effect_exception{AL_INVALID_ENUM, "Invalid compressor integer property 0x%04x",
42 param};
45 void CompressorEffectHandler::SetParamiv(CompressorProps &props, ALenum param, const int *vals)
46 { SetParami(props, param, *vals); }
47 void CompressorEffectHandler::SetParamf(CompressorProps&, ALenum param, float)
48 { throw effect_exception{AL_INVALID_ENUM, "Invalid compressor float property 0x%04x", param}; }
49 void CompressorEffectHandler::SetParamfv(CompressorProps&, ALenum param, const float*)
51 throw effect_exception{AL_INVALID_ENUM, "Invalid compressor float-vector property 0x%04x",
52 param};
55 void CompressorEffectHandler::GetParami(const CompressorProps &props, ALenum param, int *val)
57 switch(param)
59 case AL_COMPRESSOR_ONOFF: *val = props.OnOff; break;
60 default:
61 throw effect_exception{AL_INVALID_ENUM, "Invalid compressor integer property 0x%04x",
62 param};
65 void CompressorEffectHandler::GetParamiv(const CompressorProps &props, ALenum param, int *vals)
66 { GetParami(props, param, vals); }
67 void CompressorEffectHandler::GetParamf(const CompressorProps&, ALenum param, float*)
68 { throw effect_exception{AL_INVALID_ENUM, "Invalid compressor float property 0x%04x", param}; }
69 void CompressorEffectHandler::GetParamfv(const CompressorProps&, ALenum param, float*)
71 throw effect_exception{AL_INVALID_ENUM, "Invalid compressor float-vector property 0x%04x",
72 param};
76 #if ALSOFT_EAX
77 namespace {
79 using CompressorCommitter = EaxCommitter<EaxCompressorCommitter>;
81 struct OnOffValidator {
82 void operator()(unsigned long ulOnOff) const
84 eax_validate_range<CompressorCommitter::Exception>(
85 "On-Off",
86 ulOnOff,
87 EAXAGCCOMPRESSOR_MINONOFF,
88 EAXAGCCOMPRESSOR_MAXONOFF);
90 }; // OnOffValidator
92 struct AllValidator {
93 void operator()(const EAXAGCCOMPRESSORPROPERTIES& all) const
95 OnOffValidator{}(all.ulOnOff);
97 }; // AllValidator
99 } // namespace
101 template<>
102 struct CompressorCommitter::Exception : public EaxException
104 explicit Exception(const char *message) : EaxException{"EAX_CHORUS_EFFECT", message}
108 template<>
109 [[noreturn]] void CompressorCommitter::fail(const char *message)
111 throw Exception{message};
114 bool EaxCompressorCommitter::commit(const EAXAGCCOMPRESSORPROPERTIES &props)
116 if(auto *cur = std::get_if<EAXAGCCOMPRESSORPROPERTIES>(&mEaxProps); cur && *cur == props)
117 return false;
119 mEaxProps = props;
120 mAlProps = CompressorProps{props.ulOnOff != 0};
122 return true;
125 void EaxCompressorCommitter::SetDefaults(EaxEffectProps &props)
127 props = EAXAGCCOMPRESSORPROPERTIES{EAXAGCCOMPRESSOR_DEFAULTONOFF};
130 void EaxCompressorCommitter::Get(const EaxCall &call, const EAXAGCCOMPRESSORPROPERTIES &props)
132 switch(call.get_property_id())
134 case EAXAGCCOMPRESSOR_NONE: break;
135 case EAXAGCCOMPRESSOR_ALLPARAMETERS: call.set_value<Exception>(props); break;
136 case EAXAGCCOMPRESSOR_ONOFF: call.set_value<Exception>(props.ulOnOff); break;
137 default: fail_unknown_property_id();
141 void EaxCompressorCommitter::Set(const EaxCall &call, EAXAGCCOMPRESSORPROPERTIES &props)
143 switch(call.get_property_id())
145 case EAXAGCCOMPRESSOR_NONE: break;
146 case EAXAGCCOMPRESSOR_ALLPARAMETERS: defer<AllValidator>(call, props); break;
147 case EAXAGCCOMPRESSOR_ONOFF: defer<OnOffValidator>(call, props.ulOnOff); break;
148 default: fail_unknown_property_id();
152 #endif // ALSOFT_EAX