aboutsummaryrefslogtreecommitdiffstats
path: root/al/auxeffectslot.h
blob: fb6a2e1e7c1c97376dc1e84278c26342f18921eb (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
#ifndef AL_AUXEFFECTSLOT_H
#define AL_AUXEFFECTSLOT_H

#include <atomic>
#include <cstddef>

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

#include "alc/device.h"
#include "alc/effects/base.h"
#include "almalloc.h"
#include "atomic.h"
#include "core/effectslot.h"
#include "intrusive_ptr.h"
#include "vector.h"

#ifdef ALSOFT_EAX
#include <memory>
#include "eax/call.h"
#include "eax/effect.h"
#include "eax/exception.h"
#include "eax/fx_slot_index.h"
#include "eax/utils.h"
#endif // ALSOFT_EAX

struct ALbuffer;
struct ALeffect;
struct WetBuffer;

#ifdef ALSOFT_EAX
class EaxFxSlotException : public EaxException {
public:
	explicit EaxFxSlotException(const char* message)
		: EaxException{"EAX_FX_SLOT", message}
	{}
};
#endif // ALSOFT_EAX

enum class SlotState : ALenum {
    Initial = AL_INITIAL,
    Playing = AL_PLAYING,
    Stopped = AL_STOPPED,
};

struct ALeffectslot {
    float Gain{1.0f};
    bool  AuxSendAuto{true};
    ALeffectslot *Target{nullptr};
    ALbuffer *Buffer{nullptr};

    struct {
        EffectSlotType Type{EffectSlotType::None};
        EffectProps Props{};

        al::intrusive_ptr<EffectState> State;
    } Effect;

    bool mPropsDirty{true};

    SlotState mState{SlotState::Initial};

    RefCount ref{0u};

    EffectSlot mSlot;

    /* Self ID */
    ALuint id{};

    ALeffectslot();
    ALeffectslot(const ALeffectslot&) = delete;
    ALeffectslot& operator=(const ALeffectslot&) = delete;
    ~ALeffectslot();

    ALenum initEffect(ALenum effectType, const EffectProps &effectProps, ALCcontext *context);
    void updateProps(ALCcontext *context);

    /* This can be new'd for the context's default effect slot. */
    DEF_NEWDEL(ALeffectslot)


#ifdef ALSOFT_EAX
public:
    void eax_initialize(
        const EaxCall& call,
        ALCcontext& al_context,
        EaxFxSlotIndexValue index);

    const EAX50FXSLOTPROPERTIES& eax_get_eax_fx_slot() const noexcept;

    // Returns `true` if all sources should be updated, or `false` otherwise.
    bool eax_dispatch(const EaxCall& call)
    { return call.is_get() ? eax_get(call) : eax_set(call); }

    void eax_commit();

private:
    static constexpr auto eax_load_effect_dirty_bit = EaxDirtyFlags{1} << 0;
    static constexpr auto eax_volume_dirty_bit = EaxDirtyFlags{1} << 1;
    static constexpr auto eax_lock_dirty_bit = EaxDirtyFlags{1} << 2;
    static constexpr auto eax_flags_dirty_bit = EaxDirtyFlags{1} << 3;
    static constexpr auto eax_occlusion_dirty_bit = EaxDirtyFlags{1} << 4;
    static constexpr auto eax_occlusion_lf_ratio_dirty_bit = EaxDirtyFlags{1} << 5;

    using Exception = EaxFxSlotException;

    using Eax4Props = EAX40FXSLOTPROPERTIES;

    struct Eax4State {
        Eax4Props i; // Immediate.
        EaxDirtyFlags df; // Dirty flags.
    };

    using Eax5Props = EAX50FXSLOTPROPERTIES;

    struct Eax5State {
        Eax5Props i; // Immediate.
        EaxDirtyFlags df; // Dirty flags.
    };

    struct EaxRangeValidator {
        template<typename TValue>
        void operator()(
            const char* name,
            const TValue& value,
            const TValue& min_value,
            const TValue& max_value) const
        {
            eax_validate_range<Exception>(name, value, min_value, max_value);
        }
    };

    struct Eax4GuidLoadEffectValidator {
        void operator()(const GUID& guidLoadEffect) const
        {
            if (guidLoadEffect != EAX_NULL_GUID &&
                guidLoadEffect != EAX_REVERB_EFFECT &&
                guidLoadEffect != EAX_AGCCOMPRESSOR_EFFECT &&
                guidLoadEffect != EAX_AUTOWAH_EFFECT &&
                guidLoadEffect != EAX_CHORUS_EFFECT &&
                guidLoadEffect != EAX_DISTORTION_EFFECT &&
                guidLoadEffect != EAX_ECHO_EFFECT &&
                guidLoadEffect != EAX_EQUALIZER_EFFECT &&
                guidLoadEffect != EAX_FLANGER_EFFECT &&
                guidLoadEffect != EAX_FREQUENCYSHIFTER_EFFECT &&
                guidLoadEffect != EAX_VOCALMORPHER_EFFECT &&
                guidLoadEffect != EAX_PITCHSHIFTER_EFFECT &&
                guidLoadEffect != EAX_RINGMODULATOR_EFFECT)
            {
                eax_fail_unknown_effect_id();
            }
        }
    };

    struct Eax4VolumeValidator {
        void operator()(long lVolume) const
        {
            EaxRangeValidator{}(
                "Volume",
                lVolume,
                EAXFXSLOT_MINVOLUME,
                EAXFXSLOT_MAXVOLUME);
        }
    };

    struct Eax4LockValidator {
        void operator()(long lLock) const
        {
            EaxRangeValidator{}(
                "Lock",
                lLock,
                EAXFXSLOT_MINLOCK,
                EAXFXSLOT_MAXLOCK);
        }
    };

    struct Eax4FlagsValidator {
        void operator()(unsigned long ulFlags) const
        {
            EaxRangeValidator{}(
                "Flags",
                ulFlags,
                0UL,
                ~EAX40FXSLOTFLAGS_RESERVED);
        }
    };

    struct Eax4AllValidator {
        void operator()(const EAX40FXSLOTPROPERTIES& all) const
        {
            Eax4GuidLoadEffectValidator{}(all.guidLoadEffect);
            Eax4VolumeValidator{}(all.lVolume);
            Eax4LockValidator{}(all.lLock);
            Eax4FlagsValidator{}(all.ulFlags);
        }
    };

    struct Eax5OcclusionValidator {
        void operator()(long lOcclusion) const
        {
            EaxRangeValidator{}(
                "Occlusion",
                lOcclusion,
                EAXFXSLOT_MINOCCLUSION,
                EAXFXSLOT_MAXOCCLUSION);
        }
    };

    struct Eax5OcclusionLfRatioValidator {
        void operator()(float flOcclusionLFRatio) const
        {
            EaxRangeValidator{}(
                "Occlusion LF Ratio",
                flOcclusionLFRatio,
                EAXFXSLOT_MINOCCLUSIONLFRATIO,
                EAXFXSLOT_MAXOCCLUSIONLFRATIO);
        }
    };

    struct Eax5FlagsValidator {
        void operator()(unsigned long ulFlags) const
        {
            EaxRangeValidator{}(
                "Flags",
                ulFlags,
                0UL,
                ~EAX50FXSLOTFLAGS_RESERVED);
        }
    };

    struct Eax5AllValidator {
        void operator()(const EAX50FXSLOTPROPERTIES& all) const
        {
            Eax4AllValidator{}(static_cast<const EAX40FXSLOTPROPERTIES&>(all));
            Eax5OcclusionValidator{}(all.lOcclusion);
            Eax5OcclusionLfRatioValidator{}(all.flOcclusionLFRatio);
        }
    };

    ALCcontext* eax_al_context_{};
    EaxFxSlotIndexValue eax_fx_slot_index_{};
    int eax_version_{}; // Current EAX version.
    EaxEffectUPtr eax_effect_{};
    Eax5State eax123_{}; // EAX1/EAX2/EAX3 state.
    Eax4State eax4_{}; // EAX4 state.
    Eax5State eax5_{}; // EAX5 state.
    Eax5Props eax_{}; // Current EAX state.

    [[noreturn]] static void eax_fail(const char* message);
    [[noreturn]] static void eax_fail_unknown_effect_id();
    [[noreturn]] static void eax_fail_unknown_property_id();
    [[noreturn]] static void eax_fail_unknown_version();

    // Gets a new value from EAX call,
    // validates it,
    // sets a dirty flag only if the new value differs form the old one,
    // and assigns the new value.
    template<typename TValidator, EaxDirtyFlags TDirtyBit, typename TProperties>
    void eax_fx_slot_set(const EaxCall& call, TProperties& dst, EaxDirtyFlags& dirty_flags)
    {
        const auto& src = call.get_value<Exception, const TProperties>();
        TValidator{}(src);
        dirty_flags |= (dst != src ? TDirtyBit : EaxDirtyFlags{});
        dst = src;
    }

    // Gets a new value from EAX call,
    // validates it,
    // sets a dirty flag without comparing the values,
    // and assigns the new value.
    template<typename TValidator, EaxDirtyFlags TDirtyBit, typename TProperties>
    void eax_fx_slot_set_dirty(const EaxCall& call, TProperties& dst, EaxDirtyFlags& dirty_flags)
    {
        const auto& src = call.get_value<Exception, const TProperties>();
        TValidator{}(src);
        dirty_flags |= TDirtyBit;
        dst = src;
    }

    constexpr bool eax4_fx_slot_is_legacy() const noexcept
    { return eax_fx_slot_index_ < 2; }

    void eax4_fx_slot_ensure_unlocked() const;

    static ALenum eax_get_efx_effect_type(const GUID& guid);
    const GUID& eax_get_eax_default_effect_guid() const noexcept;
    long eax_get_eax_default_lock() const noexcept;

    void eax4_fx_slot_set_defaults(Eax4Props& props);
    void eax4_fx_slot_set_defaults();
    void eax5_fx_slot_set_defaults(Eax5Props& props);
    void eax5_fx_slot_set_defaults();
    void eax_fx_slot_set_defaults();

    void eax4_fx_slot_get(const EaxCall& call, const Eax4Props& props) const;
    void eax5_fx_slot_get(const EaxCall& call, const Eax5Props& props) const;
    void eax_fx_slot_get(const EaxCall& call) const;
    // Returns `true` if all sources should be updated, or `false` otherwise.
    bool eax_get(const EaxCall& call);

    void eax_fx_slot_load_effect();
    void eax_fx_slot_set_volume();
    void eax_fx_slot_set_environment_flag();
    void eax_fx_slot_set_flags();

    void eax4_fx_slot_set_all(const EaxCall& call);
    void eax5_fx_slot_set_all(const EaxCall& call);

    // Returns `true` if all sources should be updated, or `false` otherwise.
    bool eax4_fx_slot_set(const EaxCall& call);
    // Returns `true` if all sources should be updated, or `false` otherwise.
    bool eax5_fx_slot_set(const EaxCall& call);
    // Returns `true` if all sources should be updated, or `false` otherwise.
    bool eax_fx_slot_set(const EaxCall& call);
    // Returns `true` if all sources should be updated, or `false` otherwise.
    bool eax_set(const EaxCall& call);

    template<
        EaxDirtyFlags TDirtyBit,
        typename TMemberResult,
        typename TProps,
        typename TState>
    void eax_fx_slot_commit_property(
        TState& state,
        EaxDirtyFlags& dst_df,
        TMemberResult TProps::*member) noexcept
    {
        auto& src_i = state.i;
        auto& src_df = state.df;
        auto& dst_i = eax_;

        if ((src_df & TDirtyBit) != EaxDirtyFlags{}) {
            dst_df |= TDirtyBit;
            dst_i.*member = src_i.*member;
        }
    }

    void eax4_fx_slot_commit(EaxDirtyFlags& dst_df);
    void eax5_fx_slot_commit(Eax5State& state, EaxDirtyFlags& dst_df);

    void eax_dispatch_effect(const EaxCall& call);

    // `alAuxiliaryEffectSloti(effect_slot, AL_EFFECTSLOT_EFFECT, effect)`
    void eax_set_efx_slot_effect(EaxEffect &effect);

    // `alAuxiliaryEffectSloti(effect_slot, AL_EFFECTSLOT_AUXILIARY_SEND_AUTO, value)`
    void eax_set_efx_slot_send_auto(bool is_send_auto);

    // `alAuxiliaryEffectSlotf(effect_slot, AL_EFFECTSLOT_GAIN, gain)`
    void eax_set_efx_slot_gain(ALfloat gain);

public:
    class EaxDeleter {
    public:
        void operator()(ALeffectslot *effect_slot);
    };
#endif // ALSOFT_EAX
};

void UpdateAllEffectSlotProps(ALCcontext *context);

#ifdef ALSOFT_EAX
using EaxAlEffectSlotUPtr = std::unique_ptr<ALeffectslot, ALeffectslot::EaxDeleter>;

EaxAlEffectSlotUPtr eax_create_al_effect_slot(ALCcontext& context);
void eax_delete_al_effect_slot(ALCcontext& context, ALeffectslot& effect_slot);
#endif // ALSOFT_EAX

#endif