diff options
author | Chris Robinson <[email protected]> | 2023-05-23 05:49:09 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2023-05-23 05:49:09 -0700 |
commit | 55fb5c9c008216ba7ce18b4e43252ac1d1cedabe (patch) | |
tree | 41675136a482c95782c4de4f50fa9a93eea0f91d /al | |
parent | 7f72f83fcb3d463d4e5db5d393fff8606f0e2763 (diff) |
Use a variant to call the proper filter handler function
Diffstat (limited to 'al')
-rw-r--r-- | al/filter.cpp | 473 | ||||
-rw-r--r-- | al/filter.h | 44 |
2 files changed, 259 insertions, 258 deletions
diff --git a/al/filter.cpp b/al/filter.cpp index 4b927a9b..c42589da 100644 --- a/al/filter.cpp +++ b/al/filter.cpp @@ -74,20 +74,155 @@ filter_exception::filter_exception(ALenum code, const char* msg, ...) : mErrorCo filter_exception::~filter_exception() = default; -#define DEFINE_ALFILTER_VTABLE(T) \ -const ALfilter::Vtable T##_vtable = { \ - T##_setParami, T##_setParamiv, T##_setParamf, T##_setParamfv, \ - T##_getParami, T##_getParamiv, T##_getParamf, T##_getParamfv, \ +void InitFilterParams(ALfilter *filter, ALenum type) +{ + if(type == AL_FILTER_LOWPASS) + { + filter->Gain = AL_LOWPASS_DEFAULT_GAIN; + filter->GainHF = AL_LOWPASS_DEFAULT_GAINHF; + filter->HFReference = LOWPASSFREQREF; + filter->GainLF = 1.0f; + filter->LFReference = HIGHPASSFREQREF; + filter->mTypeVariant.emplace<LowpassFilterTable>(); + } + else if(type == AL_FILTER_HIGHPASS) + { + filter->Gain = AL_HIGHPASS_DEFAULT_GAIN; + filter->GainHF = 1.0f; + filter->HFReference = LOWPASSFREQREF; + filter->GainLF = AL_HIGHPASS_DEFAULT_GAINLF; + filter->LFReference = HIGHPASSFREQREF; + filter->mTypeVariant.emplace<HighpassFilterTable>(); + } + else if(type == AL_FILTER_BANDPASS) + { + filter->Gain = AL_BANDPASS_DEFAULT_GAIN; + filter->GainHF = AL_BANDPASS_DEFAULT_GAINHF; + filter->HFReference = LOWPASSFREQREF; + filter->GainLF = AL_BANDPASS_DEFAULT_GAINLF; + filter->LFReference = HIGHPASSFREQREF; + filter->mTypeVariant.emplace<BandpassFilterTable>(); + } + else + { + filter->Gain = 1.0f; + filter->GainHF = 1.0f; + filter->HFReference = LOWPASSFREQREF; + filter->GainLF = 1.0f; + filter->LFReference = HIGHPASSFREQREF; + filter->mTypeVariant.emplace<NullFilterTable>(); + } + filter->type = type; } -void ALlowpass_setParami(ALfilter*, ALenum param, int) -{ throw filter_exception{AL_INVALID_ENUM, "Invalid low-pass integer property 0x%04x", param}; } -void ALlowpass_setParamiv(ALfilter*, ALenum param, const int*) +bool EnsureFilters(ALCdevice *device, size_t needed) +{ + size_t count{std::accumulate(device->FilterList.cbegin(), device->FilterList.cend(), size_t{0}, + [](size_t cur, const FilterSubList &sublist) noexcept -> size_t + { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })}; + + while(needed > count) + { + if(device->FilterList.size() >= 1<<25) UNLIKELY + return false; + + device->FilterList.emplace_back(); + auto sublist = device->FilterList.end() - 1; + sublist->FreeMask = ~0_u64; + sublist->Filters = static_cast<ALfilter*>(al_calloc(alignof(ALfilter), sizeof(ALfilter)*64)); + if(!sublist->Filters) UNLIKELY + { + device->FilterList.pop_back(); + return false; + } + count += 64; + } + return true; +} + + +ALfilter *AllocFilter(ALCdevice *device) +{ + auto sublist = std::find_if(device->FilterList.begin(), device->FilterList.end(), + [](const FilterSubList &entry) noexcept -> bool + { return entry.FreeMask != 0; }); + auto lidx = static_cast<ALuint>(std::distance(device->FilterList.begin(), sublist)); + auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask)); + ASSUME(slidx < 64); + + ALfilter *filter{al::construct_at(sublist->Filters + slidx)}; + InitFilterParams(filter, AL_FILTER_NULL); + + /* Add 1 to avoid filter ID 0. */ + filter->id = ((lidx<<6) | slidx) + 1; + + sublist->FreeMask &= ~(1_u64 << slidx); + + return filter; +} + +void FreeFilter(ALCdevice *device, ALfilter *filter) { - throw filter_exception{AL_INVALID_ENUM, "Invalid low-pass integer-vector property 0x%04x", - param}; + const ALuint id{filter->id - 1}; + const size_t lidx{id >> 6}; + const ALuint slidx{id & 0x3f}; + + std::destroy_at(filter); + + device->FilterList[lidx].FreeMask |= 1_u64 << slidx; } -void ALlowpass_setParamf(ALfilter *filter, ALenum param, float val) + + +inline ALfilter *LookupFilter(ALCdevice *device, ALuint id) +{ + const size_t lidx{(id-1) >> 6}; + const ALuint slidx{(id-1) & 0x3f}; + + if(lidx >= device->FilterList.size()) UNLIKELY + return nullptr; + FilterSubList &sublist = device->FilterList[lidx]; + if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY + return nullptr; + return sublist.Filters + slidx; +} + +} // namespace + +/* Null filter parameter handlers */ +template<> +void FilterTable<NullFilterTable>::setParami(ALfilter*, ALenum param, int) +{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } +template<> +void FilterTable<NullFilterTable>::setParamiv(ALfilter*, ALenum param, const int*) +{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } +template<> +void FilterTable<NullFilterTable>::setParamf(ALfilter*, ALenum param, float) +{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } +template<> +void FilterTable<NullFilterTable>::setParamfv(ALfilter*, ALenum param, const float*) +{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } +template<> +void FilterTable<NullFilterTable>::getParami(const ALfilter*, ALenum param, int*) +{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } +template<> +void FilterTable<NullFilterTable>::getParamiv(const ALfilter*, ALenum param, int*) +{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } +template<> +void FilterTable<NullFilterTable>::getParamf(const ALfilter*, ALenum param, float*) +{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } +template<> +void FilterTable<NullFilterTable>::getParamfv(const ALfilter*, ALenum param, float*) +{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } + +/* Lowpass parameter handlers */ +template<> +void FilterTable<LowpassFilterTable>::setParami(ALfilter*, ALenum param, int) +{ throw filter_exception{AL_INVALID_ENUM, "Invalid low-pass integer property 0x%04x", param}; } +template<> +void FilterTable<LowpassFilterTable>::setParamiv(ALfilter *filter, ALenum param, const int *values) +{ setParami(filter, param, values[0]); } +template<> +void FilterTable<LowpassFilterTable>::setParamf(ALfilter *filter, ALenum param, float val) { switch(param) { @@ -107,17 +242,17 @@ void ALlowpass_setParamf(ALfilter *filter, ALenum param, float val) throw filter_exception{AL_INVALID_ENUM, "Invalid low-pass float property 0x%04x", param}; } } -void ALlowpass_setParamfv(ALfilter *filter, ALenum param, const float *vals) -{ ALlowpass_setParamf(filter, param, vals[0]); } - -void ALlowpass_getParami(const ALfilter*, ALenum param, int*) +template<> +void FilterTable<LowpassFilterTable>::setParamfv(ALfilter *filter, ALenum param, const float *vals) +{ setParamf(filter, param, vals[0]); } +template<> +void FilterTable<LowpassFilterTable>::getParami(const ALfilter*, ALenum param, int*) { throw filter_exception{AL_INVALID_ENUM, "Invalid low-pass integer property 0x%04x", param}; } -void ALlowpass_getParamiv(const ALfilter*, ALenum param, int*) -{ - throw filter_exception{AL_INVALID_ENUM, "Invalid low-pass integer-vector property 0x%04x", - param}; -} -void ALlowpass_getParamf(const ALfilter *filter, ALenum param, float *val) +template<> +void FilterTable<LowpassFilterTable>::getParamiv(const ALfilter *filter, ALenum param, int *values) +{ getParami(filter, param, values); } +template<> +void FilterTable<LowpassFilterTable>::getParamf(const ALfilter *filter, ALenum param, float *val) { switch(param) { @@ -133,20 +268,19 @@ void ALlowpass_getParamf(const ALfilter *filter, ALenum param, float *val) throw filter_exception{AL_INVALID_ENUM, "Invalid low-pass float property 0x%04x", param}; } } -void ALlowpass_getParamfv(const ALfilter *filter, ALenum param, float *vals) -{ ALlowpass_getParamf(filter, param, vals); } - -DEFINE_ALFILTER_VTABLE(ALlowpass); +template<> +void FilterTable<LowpassFilterTable>::getParamfv(const ALfilter *filter, ALenum param, float *vals) +{ getParamf(filter, param, vals); } - -void ALhighpass_setParami(ALfilter*, ALenum param, int) +/* Highpass parameter handlers */ +template<> +void FilterTable<HighpassFilterTable>::setParami(ALfilter*, ALenum param, int) { throw filter_exception{AL_INVALID_ENUM, "Invalid high-pass integer property 0x%04x", param}; } -void ALhighpass_setParamiv(ALfilter*, ALenum param, const int*) -{ - throw filter_exception{AL_INVALID_ENUM, "Invalid high-pass integer-vector property 0x%04x", - param}; -} -void ALhighpass_setParamf(ALfilter *filter, ALenum param, float val) +template<> +void FilterTable<HighpassFilterTable>::setParamiv(ALfilter *filter, ALenum param, const int *values) +{ setParami(filter, param, values[0]); } +template<> +void FilterTable<HighpassFilterTable>::setParamf(ALfilter *filter, ALenum param, float val) { switch(param) { @@ -166,17 +300,17 @@ void ALhighpass_setParamf(ALfilter *filter, ALenum param, float val) throw filter_exception{AL_INVALID_ENUM, "Invalid high-pass float property 0x%04x", param}; } } -void ALhighpass_setParamfv(ALfilter *filter, ALenum param, const float *vals) -{ ALhighpass_setParamf(filter, param, vals[0]); } - -void ALhighpass_getParami(const ALfilter*, ALenum param, int*) +template<> +void FilterTable<HighpassFilterTable>::setParamfv(ALfilter *filter, ALenum param, const float *vals) +{ setParamf(filter, param, vals[0]); } +template<> +void FilterTable<HighpassFilterTable>::getParami(const ALfilter*, ALenum param, int*) { throw filter_exception{AL_INVALID_ENUM, "Invalid high-pass integer property 0x%04x", param}; } -void ALhighpass_getParamiv(const ALfilter*, ALenum param, int*) -{ - throw filter_exception{AL_INVALID_ENUM, "Invalid high-pass integer-vector property 0x%04x", - param}; -} -void ALhighpass_getParamf(const ALfilter *filter, ALenum param, float *val) +template<> +void FilterTable<HighpassFilterTable>::getParamiv(const ALfilter *filter, ALenum param, int *values) +{ getParami(filter, param, values); } +template<> +void FilterTable<HighpassFilterTable>::getParamf(const ALfilter *filter, ALenum param, float *val) { switch(param) { @@ -192,20 +326,19 @@ void ALhighpass_getParamf(const ALfilter *filter, ALenum param, float *val) throw filter_exception{AL_INVALID_ENUM, "Invalid high-pass float property 0x%04x", param}; } } -void ALhighpass_getParamfv(const ALfilter *filter, ALenum param, float *vals) -{ ALhighpass_getParamf(filter, param, vals); } - -DEFINE_ALFILTER_VTABLE(ALhighpass); +template<> +void FilterTable<HighpassFilterTable>::getParamfv(const ALfilter *filter, ALenum param, float *vals) +{ getParamf(filter, param, vals); } - -void ALbandpass_setParami(ALfilter*, ALenum param, int) +/* Bandpass parameter handlers */ +template<> +void FilterTable<BandpassFilterTable>::setParami(ALfilter*, ALenum param, int) { throw filter_exception{AL_INVALID_ENUM, "Invalid band-pass integer property 0x%04x", param}; } -void ALbandpass_setParamiv(ALfilter*, ALenum param, const int*) -{ - throw filter_exception{AL_INVALID_ENUM, "Invalid band-pass integer-vector property 0x%04x", - param}; -} -void ALbandpass_setParamf(ALfilter *filter, ALenum param, float val) +template<> +void FilterTable<BandpassFilterTable>::setParamiv(ALfilter *filter, ALenum param, const int *values) +{ setParami(filter, param, values[0]); } +template<> +void FilterTable<BandpassFilterTable>::setParamf(ALfilter *filter, ALenum param, float val) { switch(param) { @@ -231,17 +364,17 @@ void ALbandpass_setParamf(ALfilter *filter, ALenum param, float val) throw filter_exception{AL_INVALID_ENUM, "Invalid band-pass float property 0x%04x", param}; } } -void ALbandpass_setParamfv(ALfilter *filter, ALenum param, const float *vals) -{ ALbandpass_setParamf(filter, param, vals[0]); } - -void ALbandpass_getParami(const ALfilter*, ALenum param, int*) +template<> +void FilterTable<BandpassFilterTable>::setParamfv(ALfilter *filter, ALenum param, const float *vals) +{ setParamf(filter, param, vals[0]); } +template<> +void FilterTable<BandpassFilterTable>::getParami(const ALfilter*, ALenum param, int*) { throw filter_exception{AL_INVALID_ENUM, "Invalid band-pass integer property 0x%04x", param}; } -void ALbandpass_getParamiv(const ALfilter*, ALenum param, int*) -{ - throw filter_exception{AL_INVALID_ENUM, "Invalid band-pass integer-vector property 0x%04x", - param}; -} -void ALbandpass_getParamf(const ALfilter *filter, ALenum param, float *val) +template<> +void FilterTable<BandpassFilterTable>::getParamiv(const ALfilter *filter, ALenum param, int *values) +{ getParami(filter, param, values); } +template<> +void FilterTable<BandpassFilterTable>::getParamf(const ALfilter *filter, ALenum param, float *val) { switch(param) { @@ -261,146 +394,10 @@ void ALbandpass_getParamf(const ALfilter *filter, ALenum param, float *val) throw filter_exception{AL_INVALID_ENUM, "Invalid band-pass float property 0x%04x", param}; } } -void ALbandpass_getParamfv(const ALfilter *filter, ALenum param, float *vals) -{ ALbandpass_getParamf(filter, param, vals); } - -DEFINE_ALFILTER_VTABLE(ALbandpass); - +template<> +void FilterTable<BandpassFilterTable>::getParamfv(const ALfilter *filter, ALenum param, float *vals) +{ getParamf(filter, param, vals); } -void ALnullfilter_setParami(ALfilter*, ALenum param, int) -{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } -void ALnullfilter_setParamiv(ALfilter*, ALenum param, const int*) -{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } -void ALnullfilter_setParamf(ALfilter*, ALenum param, float) -{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } -void ALnullfilter_setParamfv(ALfilter*, ALenum param, const float*) -{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } - -void ALnullfilter_getParami(const ALfilter*, ALenum param, int*) -{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } -void ALnullfilter_getParamiv(const ALfilter*, ALenum param, int*) -{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } -void ALnullfilter_getParamf(const ALfilter*, ALenum param, float*) -{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } -void ALnullfilter_getParamfv(const ALfilter*, ALenum param, float*) -{ throw filter_exception{AL_INVALID_ENUM, "Invalid null filter property 0x%04x", param}; } - -DEFINE_ALFILTER_VTABLE(ALnullfilter); - - -void InitFilterParams(ALfilter *filter, ALenum type) -{ - if(type == AL_FILTER_LOWPASS) - { - filter->Gain = AL_LOWPASS_DEFAULT_GAIN; - filter->GainHF = AL_LOWPASS_DEFAULT_GAINHF; - filter->HFReference = LOWPASSFREQREF; - filter->GainLF = 1.0f; - filter->LFReference = HIGHPASSFREQREF; - filter->vtab = &ALlowpass_vtable; - } - else if(type == AL_FILTER_HIGHPASS) - { - filter->Gain = AL_HIGHPASS_DEFAULT_GAIN; - filter->GainHF = 1.0f; - filter->HFReference = LOWPASSFREQREF; - filter->GainLF = AL_HIGHPASS_DEFAULT_GAINLF; - filter->LFReference = HIGHPASSFREQREF; - filter->vtab = &ALhighpass_vtable; - } - else if(type == AL_FILTER_BANDPASS) - { - filter->Gain = AL_BANDPASS_DEFAULT_GAIN; - filter->GainHF = AL_BANDPASS_DEFAULT_GAINHF; - filter->HFReference = LOWPASSFREQREF; - filter->GainLF = AL_BANDPASS_DEFAULT_GAINLF; - filter->LFReference = HIGHPASSFREQREF; - filter->vtab = &ALbandpass_vtable; - } - else - { - filter->Gain = 1.0f; - filter->GainHF = 1.0f; - filter->HFReference = LOWPASSFREQREF; - filter->GainLF = 1.0f; - filter->LFReference = HIGHPASSFREQREF; - filter->vtab = &ALnullfilter_vtable; - } - filter->type = type; -} - -bool EnsureFilters(ALCdevice *device, size_t needed) -{ - size_t count{std::accumulate(device->FilterList.cbegin(), device->FilterList.cend(), size_t{0}, - [](size_t cur, const FilterSubList &sublist) noexcept -> size_t - { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })}; - - while(needed > count) - { - if(device->FilterList.size() >= 1<<25) UNLIKELY - return false; - - device->FilterList.emplace_back(); - auto sublist = device->FilterList.end() - 1; - sublist->FreeMask = ~0_u64; - sublist->Filters = static_cast<ALfilter*>(al_calloc(alignof(ALfilter), sizeof(ALfilter)*64)); - if(!sublist->Filters) UNLIKELY - { - device->FilterList.pop_back(); - return false; - } - count += 64; - } - return true; -} - - -ALfilter *AllocFilter(ALCdevice *device) -{ - auto sublist = std::find_if(device->FilterList.begin(), device->FilterList.end(), - [](const FilterSubList &entry) noexcept -> bool - { return entry.FreeMask != 0; }); - auto lidx = static_cast<ALuint>(std::distance(device->FilterList.begin(), sublist)); - auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask)); - ASSUME(slidx < 64); - - ALfilter *filter{al::construct_at(sublist->Filters + slidx)}; - InitFilterParams(filter, AL_FILTER_NULL); - - /* Add 1 to avoid filter ID 0. */ - filter->id = ((lidx<<6) | slidx) + 1; - - sublist->FreeMask &= ~(1_u64 << slidx); - - return filter; -} - -void FreeFilter(ALCdevice *device, ALfilter *filter) -{ - const ALuint id{filter->id - 1}; - const size_t lidx{id >> 6}; - const ALuint slidx{id & 0x3f}; - - std::destroy_at(filter); - - device->FilterList[lidx].FreeMask |= 1_u64 << slidx; -} - - -inline ALfilter *LookupFilter(ALCdevice *device, ALuint id) -{ - const size_t lidx{(id-1) >> 6}; - const ALuint slidx{(id-1) & 0x3f}; - - if(lidx >= device->FilterList.size()) UNLIKELY - return nullptr; - FilterSubList &sublist = device->FilterList[lidx]; - if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY - return nullptr; - return sublist.Filters + slidx; -} - -} // namespace FORCE_ALIGN void AL_APIENTRY alGenFiltersDirect(ALCcontext *context, ALsizei n, ALuint *filters) noexcept { @@ -487,24 +484,22 @@ FORCE_ALIGN void AL_APIENTRY alFilteriDirect(ALCcontext *context, ALuint filter, ALfilter *alfilt{LookupFilter(device, filter)}; if(!alfilt) UNLIKELY context->setError(AL_INVALID_NAME, "Invalid filter ID %u", filter); - else + else if(param == AL_FILTER_TYPE) { - if(param == AL_FILTER_TYPE) - { - if(value == AL_FILTER_NULL || value == AL_FILTER_LOWPASS - || value == AL_FILTER_HIGHPASS || value == AL_FILTER_BANDPASS) - InitFilterParams(alfilt, value); - else - context->setError(AL_INVALID_VALUE, "Invalid filter type 0x%04x", value); - } - else try - { - /* Call the appropriate handler */ - alfilt->setParami(param, value); - } - catch(filter_exception &e) { - context->setError(e.errorCode(), "%s", e.what()); - } + if(value == AL_FILTER_NULL || value == AL_FILTER_LOWPASS + || value == AL_FILTER_HIGHPASS || value == AL_FILTER_BANDPASS) + InitFilterParams(alfilt, value); + else + context->setError(AL_INVALID_VALUE, "Invalid filter type 0x%04x", value); + } + else try + { + /* Call the appropriate handler */ + std::visit([alfilt,param,value](auto&& thunk){thunk.setParami(alfilt, param, value);}, + alfilt->mTypeVariant); + } + catch(filter_exception &e) { + context->setError(e.errorCode(), "%s", e.what()); } } @@ -527,7 +522,8 @@ FORCE_ALIGN void AL_APIENTRY alFilterivDirect(ALCcontext *context, ALuint filter else try { /* Call the appropriate handler */ - alfilt->setParamiv(param, values); + std::visit([alfilt,param,values](auto&& thunk){thunk.setParamiv(alfilt, param, values);}, + alfilt->mTypeVariant); } catch(filter_exception &e) { context->setError(e.errorCode(), "%s", e.what()); @@ -546,7 +542,8 @@ FORCE_ALIGN void AL_APIENTRY alFilterfDirect(ALCcontext *context, ALuint filter, else try { /* Call the appropriate handler */ - alfilt->setParamf(param, value); + std::visit([alfilt,param,value](auto&& thunk){thunk.setParamf(alfilt, param, value);}, + alfilt->mTypeVariant); } catch(filter_exception &e) { context->setError(e.errorCode(), "%s", e.what()); @@ -565,7 +562,8 @@ FORCE_ALIGN void AL_APIENTRY alFilterfvDirect(ALCcontext *context, ALuint filter else try { /* Call the appropriate handler */ - alfilt->setParamfv(param, values); + std::visit([alfilt,param,values](auto&& thunk){thunk.setParamfv(alfilt, param, values);}, + alfilt->mTypeVariant); } catch(filter_exception &e) { context->setError(e.errorCode(), "%s", e.what()); @@ -581,18 +579,16 @@ FORCE_ALIGN void AL_APIENTRY alGetFilteriDirect(ALCcontext *context, ALuint filt const ALfilter *alfilt{LookupFilter(device, filter)}; if(!alfilt) UNLIKELY context->setError(AL_INVALID_NAME, "Invalid filter ID %u", filter); - else + else if(param == AL_FILTER_TYPE) + *value = alfilt->type; + else try { - if(param == AL_FILTER_TYPE) - *value = alfilt->type; - else try - { - /* Call the appropriate handler */ - alfilt->getParami(param, value); - } - catch(filter_exception &e) { - context->setError(e.errorCode(), "%s", e.what()); - } + /* Call the appropriate handler */ + std::visit([alfilt,param,value](auto&& thunk){thunk.getParami(alfilt, param, value);}, + alfilt->mTypeVariant); + } + catch(filter_exception &e) { + context->setError(e.errorCode(), "%s", e.what()); } } @@ -615,7 +611,8 @@ FORCE_ALIGN void AL_APIENTRY alGetFilterivDirect(ALCcontext *context, ALuint fil else try { /* Call the appropriate handler */ - alfilt->getParamiv(param, values); + std::visit([alfilt,param,values](auto&& thunk){thunk.getParamiv(alfilt, param, values);}, + alfilt->mTypeVariant); } catch(filter_exception &e) { context->setError(e.errorCode(), "%s", e.what()); @@ -634,7 +631,8 @@ FORCE_ALIGN void AL_APIENTRY alGetFilterfDirect(ALCcontext *context, ALuint filt else try { /* Call the appropriate handler */ - alfilt->getParamf(param, value); + std::visit([alfilt,param,value](auto&& thunk){thunk.getParamf(alfilt, param, value);}, + alfilt->mTypeVariant); } catch(filter_exception &e) { context->setError(e.errorCode(), "%s", e.what()); @@ -653,7 +651,8 @@ FORCE_ALIGN void AL_APIENTRY alGetFilterfvDirect(ALCcontext *context, ALuint fil else try { /* Call the appropriate handler */ - alfilt->getParamfv(param, values); + std::visit([alfilt,param,values](auto&& thunk){thunk.getParamfv(alfilt, param, values);}, + alfilt->mTypeVariant); } catch(filter_exception &e) { context->setError(e.errorCode(), "%s", e.what()); diff --git a/al/filter.h b/al/filter.h index 65a9e30f..2ed483cc 100644 --- a/al/filter.h +++ b/al/filter.h @@ -1,6 +1,7 @@ #ifndef AL_FILTER_H #define AL_FILTER_H +#include <variant> #include "AL/al.h" #include "AL/alc.h" @@ -12,6 +13,25 @@ #define HIGHPASSFREQREF 250.0f +template<typename T> +struct FilterTable { + static void setParami(struct ALfilter*, ALenum, int); + static void setParamiv(struct ALfilter*, ALenum, const int*); + static void setParamf(struct ALfilter*, ALenum, float); + static void setParamfv(struct ALfilter*, ALenum, const float*); + + static void getParami(const struct ALfilter*, ALenum, int*); + static void getParamiv(const struct ALfilter*, ALenum, int*); + static void getParamf(const struct ALfilter*, ALenum, float*); + static void getParamfv(const struct ALfilter*, ALenum, float*); +}; + +struct NullFilterTable : public FilterTable<NullFilterTable> { }; +struct LowpassFilterTable : public FilterTable<LowpassFilterTable> { }; +struct HighpassFilterTable : public FilterTable<HighpassFilterTable> { }; +struct BandpassFilterTable : public FilterTable<BandpassFilterTable> { }; + + struct ALfilter { ALenum type{AL_FILTER_NULL}; @@ -21,31 +41,13 @@ struct ALfilter { float GainLF{1.0f}; float LFReference{HIGHPASSFREQREF}; - struct Vtable { - void (*const setParami )(ALfilter *filter, ALenum param, int val); - void (*const setParamiv)(ALfilter *filter, ALenum param, const int *vals); - void (*const setParamf )(ALfilter *filter, ALenum param, float val); - void (*const setParamfv)(ALfilter *filter, ALenum param, const float *vals); - - void (*const getParami )(const ALfilter *filter, ALenum param, int *val); - void (*const getParamiv)(const ALfilter *filter, ALenum param, int *vals); - void (*const getParamf )(const ALfilter *filter, ALenum param, float *val); - void (*const getParamfv)(const ALfilter *filter, ALenum param, float *vals); - }; - const Vtable *vtab{nullptr}; + using TableTypes = std::variant<NullFilterTable,LowpassFilterTable,HighpassFilterTable, + BandpassFilterTable>; + TableTypes mTypeVariant; /* Self ID */ ALuint id{0}; - void setParami(ALenum param, int value) { vtab->setParami(this, param, value); } - void setParamiv(ALenum param, const int *values) { vtab->setParamiv(this, param, values); } - void setParamf(ALenum param, float value) { vtab->setParamf(this, param, value); } - void setParamfv(ALenum param, const float *values) { vtab->setParamfv(this, param, values); } - void getParami(ALenum param, int *value) const { vtab->getParami(this, param, value); } - void getParamiv(ALenum param, int *values) const { vtab->getParamiv(this, param, values); } - void getParamf(ALenum param, float *value) const { vtab->getParamf(this, param, value); } - void getParamfv(ALenum param, float *values) const { vtab->getParamfv(this, param, values); } - DISABLE_ALLOC() }; |