aboutsummaryrefslogtreecommitdiffstats
path: root/al/effects/distortion.cpp
blob: f6f94775530b664e25349d0b58e55f7e02ddcce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

#include "config.h"

#include "AL/al.h"
#include "AL/efx.h"

#include "alc/effects/base.h"
#include "effects.h"

#ifdef ALSOFT_EAX
#include "alnumeric.h"
#include "al/eax/exception.h"
#include "al/eax/utils.h"
#endif // ALSOFT_EAX


namespace {

void Distortion_setParami(EffectProps*, ALenum param, int)
{ throw effect_exception{AL_INVALID_ENUM, "Invalid distortion integer property 0x%04x", param}; }
void Distortion_setParamiv(EffectProps*, ALenum param, const int*)
{
    throw effect_exception{AL_INVALID_ENUM, "Invalid distortion integer-vector property 0x%04x",
        param};
}
void Distortion_setParamf(EffectProps *props, ALenum param, float val)
{
    switch(param)
    {
    case AL_DISTORTION_EDGE:
        if(!(val >= AL_DISTORTION_MIN_EDGE && val <= AL_DISTORTION_MAX_EDGE))
            throw effect_exception{AL_INVALID_VALUE, "Distortion edge out of range"};
        props->Distortion.Edge = val;
        break;

    case AL_DISTORTION_GAIN:
        if(!(val >= AL_DISTORTION_MIN_GAIN && val <= AL_DISTORTION_MAX_GAIN))
            throw effect_exception{AL_INVALID_VALUE, "Distortion gain out of range"};
        props->Distortion.Gain = val;
        break;

    case AL_DISTORTION_LOWPASS_CUTOFF:
        if(!(val >= AL_DISTORTION_MIN_LOWPASS_CUTOFF && val <= AL_DISTORTION_MAX_LOWPASS_CUTOFF))
            throw effect_exception{AL_INVALID_VALUE, "Distortion low-pass cutoff out of range"};
        props->Distortion.LowpassCutoff = val;
        break;

    case AL_DISTORTION_EQCENTER:
        if(!(val >= AL_DISTORTION_MIN_EQCENTER && val <= AL_DISTORTION_MAX_EQCENTER))
            throw effect_exception{AL_INVALID_VALUE, "Distortion EQ center out of range"};
        props->Distortion.EQCenter = val;
        break;

    case AL_DISTORTION_EQBANDWIDTH:
        if(!(val >= AL_DISTORTION_MIN_EQBANDWIDTH && val <= AL_DISTORTION_MAX_EQBANDWIDTH))
            throw effect_exception{AL_INVALID_VALUE, "Distortion EQ bandwidth out of range"};
        props->Distortion.EQBandwidth = val;
        break;

    default:
        throw effect_exception{AL_INVALID_ENUM, "Invalid distortion float property 0x%04x", param};
    }
}
void Distortion_setParamfv(EffectProps *props, ALenum param, const float *vals)
{ Distortion_setParamf(props, param, vals[0]); }

void Distortion_getParami(const EffectProps*, ALenum param, int*)
{ throw effect_exception{AL_INVALID_ENUM, "Invalid distortion integer property 0x%04x", param}; }
void Distortion_getParamiv(const EffectProps*, ALenum param, int*)
{
    throw effect_exception{AL_INVALID_ENUM, "Invalid distortion integer-vector property 0x%04x",
        param};
}
void Distortion_getParamf(const EffectProps *props, ALenum param, float *val)
{
    switch(param)
    {
    case AL_DISTORTION_EDGE:
        *val = props->Distortion.Edge;
        break;

    case AL_DISTORTION_GAIN:
        *val = props->Distortion.Gain;
        break;

    case AL_DISTORTION_LOWPASS_CUTOFF:
        *val = props->Distortion.LowpassCutoff;
        break;

    case AL_DISTORTION_EQCENTER:
        *val = props->Distortion.EQCenter;
        break;

    case AL_DISTORTION_EQBANDWIDTH:
        *val = props->Distortion.EQBandwidth;
        break;

    default:
        throw effect_exception{AL_INVALID_ENUM, "Invalid distortion float property 0x%04x", param};
    }
}
void Distortion_getParamfv(const EffectProps *props, ALenum param, float *vals)
{ Distortion_getParamf(props, param, vals); }

EffectProps genDefaultProps() noexcept
{
    EffectProps props{};
    props.Distortion.Edge = AL_DISTORTION_DEFAULT_EDGE;
    props.Distortion.Gain = AL_DISTORTION_DEFAULT_GAIN;
    props.Distortion.LowpassCutoff = AL_DISTORTION_DEFAULT_LOWPASS_CUTOFF;
    props.Distortion.EQCenter = AL_DISTORTION_DEFAULT_EQCENTER;
    props.Distortion.EQBandwidth = AL_DISTORTION_DEFAULT_EQBANDWIDTH;
    return props;
}

} // namespace

DEFINE_ALEFFECT_VTABLE(Distortion);

const EffectProps DistortionEffectProps{genDefaultProps()};

#ifdef ALSOFT_EAX
namespace {

class EaxDistortionEffectException : public EaxException
{
public:
    explicit EaxDistortionEffectException(const char* message)
        : EaxException{"EAX_DISTORTION_EFFECT", message}
    {}
}; // EaxDistortionEffectException

class EaxDistortionEffect final : public EaxEffect4<EaxDistortionEffectException>
{
public:
    EaxDistortionEffect(int eax_version);

private:
    struct EdgeValidator {
        void operator()(float flEdge) const
        {
            eax_validate_range<Exception>(
                "Edge",
                flEdge,
                EAXDISTORTION_MINEDGE,
                EAXDISTORTION_MAXEDGE);
        }
    }; // EdgeValidator

    struct GainValidator {
        void operator()(long lGain) const
        {
            eax_validate_range<Exception>(
                "Gain",
                lGain,
                EAXDISTORTION_MINGAIN,
                EAXDISTORTION_MAXGAIN);
        }
    }; // GainValidator

    struct LowPassCutOffValidator {
        void operator()(float flLowPassCutOff) const
        {
            eax_validate_range<Exception>(
                "Low-pass Cut-off",
                flLowPassCutOff,
                EAXDISTORTION_MINLOWPASSCUTOFF,
                EAXDISTORTION_MAXLOWPASSCUTOFF);
        }
    }; // LowPassCutOffValidator

    struct EqCenterValidator {
        void operator()(float flEQCenter) const
        {
            eax_validate_range<Exception>(
                "EQ Center",
                flEQCenter,
                EAXDISTORTION_MINEQCENTER,
                EAXDISTORTION_MAXEQCENTER);
        }
    }; // EqCenterValidator

    struct EqBandwidthValidator {
        void operator()(float flEQBandwidth) const
        {
            eax_validate_range<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

    void set_defaults(Props4& props) override;

    void set_efx_edge() noexcept;
    void set_efx_gain() noexcept;
    void set_efx_lowpass_cutoff() noexcept;
    void set_efx_eq_center() noexcept;
    void set_efx_eq_bandwidth() noexcept;
    void set_efx_defaults() override;

    void get(const EaxCall& call, const Props4& props) override;
    void set(const EaxCall& call, Props4& props) override;
    bool commit_props(const Props4& props) override;
}; // EaxDistortionEffect

EaxDistortionEffect::EaxDistortionEffect(int eax_version)
    : EaxEffect4{AL_EFFECT_DISTORTION, eax_version}
{}

void EaxDistortionEffect::set_defaults(Props4& props)
{
    props.mDistortion.flEdge = EAXDISTORTION_DEFAULTEDGE;
    props.mDistortion.lGain = EAXDISTORTION_DEFAULTGAIN;
    props.mDistortion.flLowPassCutOff = EAXDISTORTION_DEFAULTLOWPASSCUTOFF;
    props.mDistortion.flEQCenter = EAXDISTORTION_DEFAULTEQCENTER;
    props.mDistortion.flEQBandwidth = EAXDISTORTION_DEFAULTEQBANDWIDTH;
}

void EaxDistortionEffect::set_efx_edge() noexcept
{
    al_effect_props_.Distortion.Edge = clamp(
        props_.mDistortion.flEdge,
        AL_DISTORTION_MIN_EDGE,
        AL_DISTORTION_MAX_EDGE);
}

void EaxDistortionEffect::set_efx_gain() noexcept
{
    al_effect_props_.Distortion.Gain = clamp(
        level_mb_to_gain(static_cast<float>(props_.mDistortion.lGain)),
        AL_DISTORTION_MIN_GAIN,
        AL_DISTORTION_MAX_GAIN);
}

void EaxDistortionEffect::set_efx_lowpass_cutoff() noexcept
{
    al_effect_props_.Distortion.LowpassCutoff = clamp(
        props_.mDistortion.flLowPassCutOff,
        AL_DISTORTION_MIN_LOWPASS_CUTOFF,
        AL_DISTORTION_MAX_LOWPASS_CUTOFF);
}

void EaxDistortionEffect::set_efx_eq_center() noexcept
{
    al_effect_props_.Distortion.EQCenter = clamp(
        props_.mDistortion.flEQCenter,
        AL_DISTORTION_MIN_EQCENTER,
        AL_DISTORTION_MAX_EQCENTER);
}

void EaxDistortionEffect::set_efx_eq_bandwidth() noexcept
{
    al_effect_props_.Distortion.EQBandwidth = clamp(
        props_.mDistortion.flEdge,
        AL_DISTORTION_MIN_EQBANDWIDTH,
        AL_DISTORTION_MAX_EQBANDWIDTH);
}

void EaxDistortionEffect::set_efx_defaults()
{
    set_efx_edge();
    set_efx_gain();
    set_efx_lowpass_cutoff();
    set_efx_eq_center();
    set_efx_eq_bandwidth();
}

void EaxDistortionEffect::get(const EaxCall& call, const Props4& props)
{
    switch(call.get_property_id())
    {
        case EAXDISTORTION_NONE: break;
        case EAXDISTORTION_ALLPARAMETERS: call.set_value<Exception>(props.mDistortion); break;
        case EAXDISTORTION_EDGE: call.set_value<Exception>(props.mDistortion.flEdge); break;
        case EAXDISTORTION_GAIN: call.set_value<Exception>(props.mDistortion.lGain); break;
        case EAXDISTORTION_LOWPASSCUTOFF: call.set_value<Exception>(props.mDistortion.flLowPassCutOff); break;
        case EAXDISTORTION_EQCENTER: call.set_value<Exception>(props.mDistortion.flEQCenter); break;
        case EAXDISTORTION_EQBANDWIDTH: call.set_value<Exception>(props.mDistortion.flEQBandwidth); break;
        default: fail_unknown_property_id();
    }
}

void EaxDistortionEffect::set(const EaxCall& call, Props4& props)
{
    switch(call.get_property_id())
    {
        case EAXDISTORTION_NONE: break;
        case EAXDISTORTION_ALLPARAMETERS: defer<AllValidator>(call, props.mDistortion); break;
        case EAXDISTORTION_EDGE: defer<EdgeValidator>(call, props.mDistortion.flEdge); break;
        case EAXDISTORTION_GAIN: defer<GainValidator>(call, props.mDistortion.lGain); break;
        case EAXDISTORTION_LOWPASSCUTOFF: defer<LowPassCutOffValidator>(call, props.mDistortion.flLowPassCutOff); break;
        case EAXDISTORTION_EQCENTER: defer<EqCenterValidator>(call, props.mDistortion.flEQCenter); break;
        case EAXDISTORTION_EQBANDWIDTH: defer<EqBandwidthValidator>(call, props.mDistortion.flEQBandwidth); break;
        default: fail_unknown_property_id();
    }
}

bool EaxDistortionEffect::commit_props(const Props4& props)
{
    auto is_dirty = false;

    if (props_.mDistortion.flEdge != props.mDistortion.flEdge)
    {
        is_dirty = true;
        set_efx_edge();
    }

    if (props_.mDistortion.lGain != props.mDistortion.lGain)
    {
        is_dirty = true;
        set_efx_gain();
    }

    if (props_.mDistortion.flLowPassCutOff != props.mDistortion.flLowPassCutOff)
    {
        is_dirty = true;
        set_efx_lowpass_cutoff();
    }

    if (props_.mDistortion.flEQCenter != props.mDistortion.flEQCenter)
    {
        is_dirty = true;
        set_efx_eq_center();
    }

    if (props_.mDistortion.flEQBandwidth != props.mDistortion.flEQBandwidth)
    {
        is_dirty = true;
        set_efx_eq_bandwidth();
    }

    return is_dirty;
}

} // namespace

EaxEffectUPtr eax_create_eax_distortion_effect(int eax_version)
{
    return eax_create_eax4_effect<EaxDistortionEffect>(eax_version);
}

#endif // ALSOFT_EAX