aboutsummaryrefslogtreecommitdiffstats
path: root/al/eax/effect.h
blob: 14506846a3942a01c32a80231fc09e3fba29cd26 (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#ifndef EAX_EFFECT_INCLUDED
#define EAX_EFFECT_INCLUDED


#include <cassert>
#include <memory>
#include <variant>

#include "alnumeric.h"
#include "AL/al.h"
#include "core/effects/base.h"
#include "call.h"

struct EaxEffectErrorMessages {
    static constexpr auto unknown_property_id() noexcept { return "Unknown property id."; }
    static constexpr auto unknown_version() noexcept { return "Unknown version."; }
}; // EaxEffectErrorMessages

using EaxEffectProps = std::variant<std::monostate,
    EAXREVERBPROPERTIES,
    EAXCHORUSPROPERTIES,
    EAXAUTOWAHPROPERTIES,
    EAXAGCCOMPRESSORPROPERTIES,
    EAXDISTORTIONPROPERTIES,
    EAXECHOPROPERTIES,
    EAXEQUALIZERPROPERTIES,
    EAXFLANGERPROPERTIES,
    EAXFREQUENCYSHIFTERPROPERTIES,
    EAXRINGMODULATORPROPERTIES,
    EAXPITCHSHIFTERPROPERTIES,
    EAXVOCALMORPHERPROPERTIES>;

template<typename... Ts>
struct overloaded : Ts... { using Ts::operator()...; };

template<typename... Ts>
overloaded(Ts...) -> overloaded<Ts...>;

constexpr ALenum EnumFromEaxEffectType(const EaxEffectProps &props) noexcept
{
    return std::visit(overloaded{
        [](const std::monostate&) noexcept { return AL_EFFECT_NULL; },
        [](const EAXREVERBPROPERTIES&) noexcept { return AL_EFFECT_EAXREVERB; },
        [](const EAXCHORUSPROPERTIES&) noexcept { return AL_EFFECT_CHORUS; },
        [](const EAXAUTOWAHPROPERTIES&) noexcept { return AL_EFFECT_AUTOWAH; },
        [](const EAXAGCCOMPRESSORPROPERTIES&) noexcept { return AL_EFFECT_COMPRESSOR; },
        [](const EAXDISTORTIONPROPERTIES&) noexcept { return AL_EFFECT_DISTORTION; },
        [](const EAXECHOPROPERTIES&) noexcept { return AL_EFFECT_ECHO; },
        [](const EAXEQUALIZERPROPERTIES&) noexcept { return AL_EFFECT_EQUALIZER; },
        [](const EAXFLANGERPROPERTIES&) noexcept { return AL_EFFECT_FLANGER; },
        [](const EAXFREQUENCYSHIFTERPROPERTIES&) noexcept { return AL_EFFECT_FREQUENCY_SHIFTER; },
        [](const EAXRINGMODULATORPROPERTIES&) noexcept { return AL_EFFECT_RING_MODULATOR; },
        [](const EAXPITCHSHIFTERPROPERTIES&) noexcept { return AL_EFFECT_PITCH_SHIFTER; },
        [](const EAXVOCALMORPHERPROPERTIES&) noexcept { return AL_EFFECT_VOCAL_MORPHER; }
    }, props);
}

struct EaxReverbCommitter {
    struct Exception;

    EaxReverbCommitter(EaxEffectProps &eaxprops, EffectProps &alprops)
        : mEaxProps{eaxprops}, mAlProps{alprops}
    { }

    EaxEffectProps &mEaxProps;
    EffectProps &mAlProps;

    [[noreturn]] static void fail(const char* message);
    [[noreturn]] static void fail_unknown_property_id()
    { fail(EaxEffectErrorMessages::unknown_property_id()); }

    template<typename TValidator, typename TProperty>
    static void defer(const EaxCall& call, TProperty& property)
    {
        const auto& value = call.get_value<Exception, const TProperty>();
        TValidator{}(value);
        property = value;
    }

    template<typename TValidator, typename TDeferrer, typename TProperties, typename TProperty>
    static void defer(const EaxCall& call, TProperties& properties, TProperty&)
    {
        const auto& value = call.get_value<Exception, const TProperty>();
        TValidator{}(value);
        TDeferrer{}(properties, value);
    }

    template<typename TValidator, typename TProperty>
    static void defer3(const EaxCall& call, EAXREVERBPROPERTIES& properties, TProperty& property)
    {
        const auto& value = call.get_value<Exception, const TProperty>();
        TValidator{}(value);
        if (value == property)
            return;
        property = value;
        properties.ulEnvironment = EAX_ENVIRONMENT_UNDEFINED;
    }


    bool commit(const EAX_REVERBPROPERTIES &props);
    bool commit(const EAX20LISTENERPROPERTIES &props);
    bool commit(const EAXREVERBPROPERTIES &props);
    bool commit(const EaxEffectProps &props);

    static void SetDefaults(EAX_REVERBPROPERTIES &props);
    static void SetDefaults(EAX20LISTENERPROPERTIES &props);
    static void SetDefaults(EAXREVERBPROPERTIES &props);
    static void SetDefaults(EaxEffectProps &props);

    static void Get(const EaxCall &call, const EAX_REVERBPROPERTIES &props);
    static void Get(const EaxCall &call, const EAX20LISTENERPROPERTIES &props);
    static void Get(const EaxCall &call, const EAXREVERBPROPERTIES &props);
    static void Get(const EaxCall &call, const EaxEffectProps &props);

    static void Set(const EaxCall &call, EAX_REVERBPROPERTIES &props);
    static void Set(const EaxCall &call, EAX20LISTENERPROPERTIES &props);
    static void Set(const EaxCall &call, EAXREVERBPROPERTIES &props);
    static void Set(const EaxCall &call, EaxEffectProps &props);

    static void translate(const EAX_REVERBPROPERTIES& src, EaxEffectProps& dst) noexcept;
    static void translate(const EAX20LISTENERPROPERTIES& src, EaxEffectProps& dst) noexcept;
    static void translate(const EAXREVERBPROPERTIES& src, EaxEffectProps& dst) noexcept;
};

template<typename T>
struct EaxCommitter {
    struct Exception;

    EaxCommitter(EaxEffectProps &eaxprops, EffectProps &alprops)
        : mEaxProps{eaxprops}, mAlProps{alprops}
    { }

    EaxEffectProps &mEaxProps;
    EffectProps &mAlProps;

    template<typename TValidator, typename TProperty>
    static void defer(const EaxCall& call, TProperty& property)
    {
        const auto& value = call.get_value<Exception, const TProperty>();
        TValidator{}(value);
        property = value;
    }

    [[noreturn]] static void fail(const char *message);
    [[noreturn]] static void fail_unknown_property_id()
    { fail(EaxEffectErrorMessages::unknown_property_id()); }

    bool commit(const EaxEffectProps &props);

    static void SetDefaults(EaxEffectProps &props);
    static void Get(const EaxCall &call, const EaxEffectProps &props);
    static void Set(const EaxCall &call, EaxEffectProps &props);
};

struct EaxAutowahCommitter : public EaxCommitter<EaxAutowahCommitter> {
    using EaxCommitter<EaxAutowahCommitter>::EaxCommitter;
};
struct EaxChorusCommitter : public EaxCommitter<EaxChorusCommitter> {
    using EaxCommitter<EaxChorusCommitter>::EaxCommitter;
};
struct EaxCompressorCommitter : public EaxCommitter<EaxCompressorCommitter> {
    using EaxCommitter<EaxCompressorCommitter>::EaxCommitter;
};
struct EaxDistortionCommitter : public EaxCommitter<EaxDistortionCommitter> {
    using EaxCommitter<EaxDistortionCommitter>::EaxCommitter;
};
struct EaxEchoCommitter : public EaxCommitter<EaxEchoCommitter> {
    using EaxCommitter<EaxEchoCommitter>::EaxCommitter;
};
struct EaxEqualizerCommitter : public EaxCommitter<EaxEqualizerCommitter> {
    using EaxCommitter<EaxEqualizerCommitter>::EaxCommitter;
};
struct EaxFlangerCommitter : public EaxCommitter<EaxFlangerCommitter> {
    using EaxCommitter<EaxFlangerCommitter>::EaxCommitter;
};
struct EaxFrequencyShifterCommitter : public EaxCommitter<EaxFrequencyShifterCommitter> {
    using EaxCommitter<EaxFrequencyShifterCommitter>::EaxCommitter;
};
struct EaxModulatorCommitter : public EaxCommitter<EaxModulatorCommitter> {
    using EaxCommitter<EaxModulatorCommitter>::EaxCommitter;
};
struct EaxPitchShifterCommitter : public EaxCommitter<EaxPitchShifterCommitter> {
    using EaxCommitter<EaxPitchShifterCommitter>::EaxCommitter;
};
struct EaxVocalMorpherCommitter : public EaxCommitter<EaxVocalMorpherCommitter> {
    using EaxCommitter<EaxVocalMorpherCommitter>::EaxCommitter;
};
struct EaxNullCommitter : public EaxCommitter<EaxNullCommitter> {
    using EaxCommitter<EaxNullCommitter>::EaxCommitter;
};


class EaxEffect {
public:
    EaxEffect() noexcept = default;
    ~EaxEffect() = default;

    ALenum al_effect_type_{AL_EFFECT_NULL};
    EffectProps al_effect_props_{};

    using Props1 = EAX_REVERBPROPERTIES;
    using Props2 = EAX20LISTENERPROPERTIES;
    using Props3 = EAXREVERBPROPERTIES;
    using Props4 = EaxEffectProps;

    struct State1 {
        Props1 i; // Immediate.
        Props1 d; // Deferred.
    };

    struct State2 {
        Props2 i; // Immediate.
        Props2 d; // Deferred.
    };

    struct State3 {
        Props3 i; // Immediate.
        Props3 d; // Deferred.
    };

    struct State4 {
        Props4 i; // Immediate.
        Props4 d; // Deferred.
    };

    int version_{};
    bool changed_{};
    Props4 props_{};
    State1 state1_{};
    State2 state2_{};
    State3 state3_{};
    State4 state4_{};
    State4 state5_{};


    template<typename T, typename ...Args>
    void call_set_defaults(Args&& ...args)
    { return T::SetDefaults(std::forward<Args>(args)...); }

    void call_set_defaults(const ALenum altype, EaxEffectProps &props)
    {
        if(altype == AL_EFFECT_EAXREVERB)
            return call_set_defaults<EaxReverbCommitter>(props);
        if(altype == AL_EFFECT_CHORUS)
            return call_set_defaults<EaxChorusCommitter>(props);
        if(altype == AL_EFFECT_AUTOWAH)
            return call_set_defaults<EaxAutowahCommitter>(props);
        if(altype == AL_EFFECT_COMPRESSOR)
            return call_set_defaults<EaxCompressorCommitter>(props);
        if(altype == AL_EFFECT_DISTORTION)
            return call_set_defaults<EaxDistortionCommitter>(props);
        if(altype == AL_EFFECT_ECHO)
            return call_set_defaults<EaxEchoCommitter>(props);
        if(altype == AL_EFFECT_EQUALIZER)
            return call_set_defaults<EaxEqualizerCommitter>(props);
        if(altype == AL_EFFECT_FLANGER)
            return call_set_defaults<EaxFlangerCommitter>(props);
        if(altype == AL_EFFECT_FREQUENCY_SHIFTER)
            return call_set_defaults<EaxFrequencyShifterCommitter>(props);
        if(altype == AL_EFFECT_RING_MODULATOR)
            return call_set_defaults<EaxModulatorCommitter>(props);
        if(altype == AL_EFFECT_PITCH_SHIFTER)
            return call_set_defaults<EaxPitchShifterCommitter>(props);
        if(altype == AL_EFFECT_VOCAL_MORPHER)
            return call_set_defaults<EaxVocalMorpherCommitter>(props);
        return call_set_defaults<EaxNullCommitter>(props);
    }

    template<typename T>
    void init()
    {
        call_set_defaults<EaxReverbCommitter>(state1_.d);
        state1_.i = state1_.d;
        call_set_defaults<EaxReverbCommitter>(state2_.d);
        state2_.i = state2_.d;
        call_set_defaults<EaxReverbCommitter>(state3_.d);
        state3_.i = state3_.d;
        call_set_defaults<T>(state4_.d);
        state4_.i = state4_.d;
        call_set_defaults<T>(state5_.d);
        state5_.i = state5_.d;
    }

    void set_defaults(int eax_version, ALenum altype)
    {
        switch(eax_version)
        {
        case 1: call_set_defaults<EaxReverbCommitter>(state1_.d); break;
        case 2: call_set_defaults<EaxReverbCommitter>(state2_.d); break;
        case 3: call_set_defaults<EaxReverbCommitter>(state3_.d); break;
        case 4: call_set_defaults(altype, state4_.d); break;
        case 5: call_set_defaults(altype, state5_.d); break;
        }
        changed_ = true;
    }


#define EAXCALL(Props, Callable, ...)                                         \
    return std::visit(overloaded{                                             \
        [&](const std::monostate&) { return Callable<EaxNullCommitter>(__VA_ARGS__); }, \
        [&](const EAXREVERBPROPERTIES&) { return Callable<EaxReverbCommitter>(__VA_ARGS__); }, \
        [&](const EAXCHORUSPROPERTIES&) { return Callable<EaxChorusCommitter>(__VA_ARGS__); }, \
        [&](const EAXAUTOWAHPROPERTIES&) { return Callable<EaxAutowahCommitter>(__VA_ARGS__); }, \
        [&](const EAXAGCCOMPRESSORPROPERTIES&) { return Callable<EaxCompressorCommitter>(__VA_ARGS__); }, \
        [&](const EAXDISTORTIONPROPERTIES&) { return Callable<EaxDistortionCommitter>(__VA_ARGS__); }, \
        [&](const EAXECHOPROPERTIES&) { return Callable<EaxEchoCommitter>(__VA_ARGS__); }, \
        [&](const EAXEQUALIZERPROPERTIES&) { return Callable<EaxEqualizerCommitter>(__VA_ARGS__); }, \
        [&](const EAXFLANGERPROPERTIES&) { return Callable<EaxFlangerCommitter>(__VA_ARGS__); }, \
        [&](const EAXFREQUENCYSHIFTERPROPERTIES&) { return Callable<EaxFrequencyShifterCommitter>(__VA_ARGS__); }, \
        [&](const EAXRINGMODULATORPROPERTIES&) { return Callable<EaxModulatorCommitter>(__VA_ARGS__); }, \
        [&](const EAXPITCHSHIFTERPROPERTIES&) { return Callable<EaxPitchShifterCommitter>(__VA_ARGS__); }, \
        [&](const EAXVOCALMORPHERPROPERTIES&) { return Callable<EaxVocalMorpherCommitter>(__VA_ARGS__); } \
    }, Props)

    template<typename T, typename ...Args>
    static void call_set(Args&& ...args)
    { return T::Set(std::forward<Args>(args)...); }

    static void call_set(const EaxCall &call, EaxEffectProps &props)
    { EAXCALL(props, call_set, call, props); }

    void set(const EaxCall &call)
    {
        switch(call.get_version())
        {
        case 1: call_set<EaxReverbCommitter>(call, state1_.d); break;
        case 2: call_set<EaxReverbCommitter>(call, state2_.d); break;
        case 3: call_set<EaxReverbCommitter>(call, state3_.d); break;
        case 4: call_set(call, state4_.d); break;
        case 5: call_set(call, state5_.d); break;
        }
        changed_ = true;
    }


    template<typename T, typename ...Args>
    static void call_get(Args&& ...args)
    { return T::Get(std::forward<Args>(args)...); }

    static void call_get(const EaxCall &call, const EaxEffectProps &props)
    { EAXCALL(props, call_get, call, props); }

    void get(const EaxCall &call)
    {
        switch(call.get_version())
        {
        case 1: call_get<EaxReverbCommitter>(call, state1_.d); break;
        case 2: call_get<EaxReverbCommitter>(call, state2_.d); break;
        case 3: call_get<EaxReverbCommitter>(call, state3_.d); break;
        case 4: call_get(call, state4_.d); break;
        case 5: call_get(call, state5_.d); break;
        }
    }


    template<typename T, typename ...Args>
    bool call_commit(Args&& ...args)
    { return T{props_, al_effect_props_}.commit(std::forward<Args>(args)...); }

    bool call_commit(const EaxEffectProps &props)
    { EAXCALL(props, call_commit, props); }

    bool commit(int eax_version)
    {
        changed_ |= version_ != eax_version;
        if(!changed_) return false;

        bool ret{version_ != eax_version};
        version_ = eax_version;
        changed_ = false;

        switch(eax_version)
        {
        case 1:
            state1_.i = state1_.d;
            ret |= call_commit<EaxReverbCommitter>(state1_.d);
            break;
        case 2:
            state2_.i = state2_.d;
            ret |= call_commit<EaxReverbCommitter>(state2_.d);
            break;
        case 3:
            state3_.i = state3_.d;
            ret |= call_commit<EaxReverbCommitter>(state3_.d);
            break;
        case 4:
            state4_.i = state4_.d;
            ret |= call_commit(state4_.d);
            break;
        case 5:
            state5_.i = state5_.d;
            ret |= call_commit(state5_.d);
            break;
        }
        al_effect_type_ = EnumFromEaxEffectType(props_);
        return ret;
    }
#undef EAXCALL
}; // EaxEffect

using EaxEffectUPtr = std::unique_ptr<EaxEffect>;

#endif // !EAX_EFFECT_INCLUDED