4#ifndef GREM_AUDIO_FILTERS_HPP
5#define GREM_AUDIO_FILTERS_HPP
29struct ExampleFilterOptions {
56 explicit ExampleFilter(
const ExampleFilterOptions& options)
57 : parameters{.gain = options.gain} {}
60 void filter(
Span<float> samples,
size_t channelCount,
size_t sampleCountPerChannel,
float sampleRate,
double time)
const {
70 (void)sampleCountPerChannel;
75 for (
float&
sample : samples) {
83enum class FilterParameterType : uint32_t {
94struct FilterParameterDescription {
95 FilterParameterType type;
99struct FilterParameterInfo {
105template <
typename FilterParameterStruct>
106[[nodiscard]]
constexpr auto getFilterParameterDescriptions() {
107 static_assert(aggregate<FilterParameterStruct>);
108 static_assert(standard_layout<FilterParameterStruct>);
110 size_t byteOffset = 0;
112 FilterParameterType type{};
113 if constexpr (same_as<T, bool>) {
114 type = FilterParameterType::BOOL;
115 }
else if constexpr (enumeration<T>) {
116 if constexpr (
sizeof(
T) == 1) {
117 type = FilterParameterType::UINT8;
118 }
else if constexpr (
sizeof(
T) == 2) {
119 type = FilterParameterType::UINT16;
120 }
else if constexpr (
sizeof(
T) == 4) {
121 type = FilterParameterType::UINT32;
125 }
else if constexpr (integral<T>) {
126 if constexpr (signed_integral<T>) {
127 if constexpr (
sizeof(
T) == 1) {
128 type = FilterParameterType::INT8;
129 }
else if constexpr (
sizeof(
T) == 2) {
130 type = FilterParameterType::INT16;
131 }
else if constexpr (
sizeof(
T) == 4) {
132 type = FilterParameterType::INT32;
137 if constexpr (
sizeof(
T) == 1) {
138 type = FilterParameterType::UINT8;
139 }
else if constexpr (
sizeof(
T) == 2) {
140 type = FilterParameterType::UINT16;
141 }
else if constexpr (
sizeof(
T) == 4) {
142 type = FilterParameterType::UINT32;
147 }
else if constexpr (floating_point<T>) {
148 if constexpr (
sizeof(
T) == 4) {
149 type = FilterParameterType::FLOAT32;
163 .byteOffset =
static_cast<uint32_t
>(byteOffset),
165 byteOffset +=
sizeof(
T);
170template <
typename FilterParameterStruct>
171[[nodiscard]]
constexpr auto getFilterParameterInfos() {
172 static_assert(aggregate<FilterParameterStruct>);
173 static_assert(standard_layout<FilterParameterStruct>);
176 float minValue = 0.0f;
177 float maxValue = 0.0f;
178 if constexpr (same_as<T, bool>) {
181 }
else if constexpr (enumeration<T>) {
182 if constexpr (
sizeof(
T) == 1) {
185 }
else if constexpr (
sizeof(
T) == 2) {
188 }
else if constexpr (
sizeof(
T) == 4) {
194 }
else if constexpr (floating_point<T>) {
197 }
else if constexpr (integral<T>) {
214 .minValue = minValue,
215 .maxValue = maxValue,
221template <
typename FilterParameterStruct>
222inline constexpr auto FILTER_PARAMETER_DESCRIPTIONS = getFilterParameterDescriptions<FilterParameterStruct>();
224template <
typename FilterParameterStruct>
225inline constexpr auto FILTER_PARAMETER_INFOS = getFilterParameterInfos<FilterParameterStruct>();
227using FilterFunction = void (*)(
byte* data,
Span<float> samples,
size_t channelCount,
size_t sampleCountPerChannel,
float sampleRate,
double time);
229struct FilterDeleter {
230 GREM_API(audio)
void operator()(
void* handle)
const noexcept;
233using Filter = UniqueHandle<void*, FilterDeleter>;
247 T::GREM_private_createBuiltinFilter(data);
248}) || (
requires(T t,
const Span<float> samples,
const size_t channelCount,
const size_t sampleCountPerChannel,
const float sampleRate,
const double time) {
249 t.filter(samples, channelCount, sampleCountPerChannel, sampleRate, time);
252static_assert(filter<detail::ExampleFilter>);
297 [[nodiscard]] GREM_API(
audio)
static void* GREM_private_createBuiltinFilter(
const void* data);
315 .cutoffFrequency = options.cutoffFrequency,
316 .resonance = options.resonance,
318 , type(options.type) {}
356 [[nodiscard]] GREM_API(
audio)
static void* GREM_private_createBuiltinFilter(
const void* data);
364 : options(options) {}
395 [[nodiscard]] GREM_API(
audio)
static void* GREM_private_createBuiltinFilter(
const void* data);
413 .sampleRate = options.sampleRate,
414 .bitDepth = options.bitDepth,
444 [[nodiscard]] GREM_API(
audio)
static void* GREM_private_createBuiltinFilter(
const void* data);
462 .delay = options.delay,
463 .frequency = options.frequency,
488 [[nodiscard]] GREM_API(
audio)
static void* GREM_private_createBuiltinFilter(
const void* data);
496 : options(options) {}
540 [[nodiscard]] GREM_API(
audio)
static void* GREM_private_createBuiltinFilter(
const void* data);
560 .freeze = options.freeze,
561 .roomSize = options.roomSize,
562 .damping = options.damping,
563 .width = options.width,
634 [[nodiscard]] GREM_API(
audio)
static void* GREM_private_createBuiltinFilter(
const void* data);
655 .band1Volume = options.band1Volume,
656 .band2Volume = options.band2Volume,
657 .band3Volume = options.band3Volume,
658 .band4Volume = options.band4Volume,
659 .band5Volume = options.band5Volume,
660 .band6Volume = options.band6Volume,
661 .band7Volume = options.band7Volume,
662 .band8Volume = options.band8Volume,
698 [[nodiscard]] GREM_API(
audio)
static void* GREM_private_createBuiltinFilter(
const void* data);
708 : soundStage(&soundStage)
710 , options(options) {}
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
BiquadResonantFilter(const BiquadResonantFilterOptions &options)
Construct a biquadratic resonant filter.
Definition filters.hpp:313
struct grem::audio::BiquadResonantFilter::Parameters parameters
DCRemovalFilter(const DCRemovalFilterOptions &options)
Construct a DC removal filter.
Definition filters.hpp:495
DuckingFilter(SoundStage &soundStage, SoundInstanceID listenTo, const DuckingFilterOptions &options)
Construct a ducking filter.
Definition filters.hpp:707
EchoFilter(const EchoFilterOptions &options)
Construct an echo filter.
Definition filters.hpp:363
struct grem::audio::EqualizationFilter::Parameters parameters
EqualizationFilter(const EqualizationFilterOptions &options)
Construct an equalization filter.
Definition filters.hpp:653
FlangerFilter(const FlangerFilterOptions &options)
Construct a flanger filter.
Definition filters.hpp:460
struct grem::audio::FlangerFilter::Parameters parameters
FreeverbFilter(const FreeverbFilterOptions &options)
Construct a Freeverb filter.
Definition filters.hpp:558
struct grem::audio::FreeverbFilter::Parameters parameters
LoFiFilter(const LoFiFilterOptions &options)
Construct a lo-fi filter.
Definition filters.hpp:411
struct grem::audio::LoFiFilter::Parameters parameters
Persistent system for playing sound in a simulated 3D arena to the default audio device.
Definition SoundStage.hpp:122
Container for a particular sound wave that can be played in a SoundStage.
Definition Sound.hpp:233
Concept that checks if a type is a valid sound filter type.
Definition filters.hpp:246
Definition concepts.hpp:187
BiquadResonantFilterType
Filter type of a BiquadResonantFilter.
Definition filters.hpp:257
@ BANDPASS
Preserve the sound frequencies near the cutoff frequency, filter those above and below.
Definition filters.hpp:260
@ HIGHPASS
Preserve the sound frequencies above the cutoff frequency, filter those below.
Definition filters.hpp:259
@ LOWPASS
Preserve the sound frequencies below the cutoff frequency, filter those above.
Definition filters.hpp:258
@ T
't'
Definition keyboard.hpp:90
Array(T, Us...) -> Array< T, 1+sizeof...(Us)>
GREM_ALWAYS_INLINE constexpr UnsignedInteger roundUpToMultiple(UnsignedInteger value, UnsignedInteger n)
Get the smallest multiple of n that is not greater than a given value.
Definition fundamentals.hpp:822
Span(R &&range) -> Span< std::remove_reference_t< decltype(*std::data(range))> >
void unreachable()
Definition assertions.hpp:125
auto sample(forward_range auto &&range, auto outputIterator, auto n, auto &&uniformRandomBitGenerator)
Definition algorithms.hpp:629
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
static constexpr T MAX
Definition fundamentals.hpp:29
static constexpr T MIN
Definition fundamentals.hpp:28
Configuration options for a BiquadResonantFilter.
Definition filters.hpp:266
float resonance
Resonance/sharpness parameter of the filter.
Definition filters.hpp:288
BiquadResonantFilterType type
Filter type that determines which sound frequencies to preserve/filter.
Definition filters.hpp:270
float cutoffFrequency
Cutoff frequency of the filter, in Hertz.
Definition filters.hpp:279
Dynamic filter parameters that can be set/faded on-the-fly.
Definition filters.hpp:302
float resonance
See BiquadResonantFilterOptions::resonance.
Definition filters.hpp:305
float cutoffFrequency
See BiquadResonantFilterOptions::cutoffFrequency.
Definition filters.hpp:304
float wet
Filter active amount (0 for unfiltered, 1 for fully filtered).
Definition filters.hpp:303
Configuration options for a DCRemovalFilter.
Definition filters.hpp:470
float length
Length of the averaging buffer, in seconds.
Definition filters.hpp:476
Configuration options for a DuckingFilter.
Definition filters.hpp:669
float onRamp
On-ramp parameter.
Definition filters.hpp:675
float level
Level parameter.
Definition filters.hpp:689
float offRamp
Off-ramp parameter.
Definition filters.hpp:682
Configuration options for an EchoFilter.
Definition filters.hpp:327
float decay
Echo decay multiplier.
Definition filters.hpp:340
float delay
Echo delay in seconds.
Definition filters.hpp:333
Configuration options for an EqualizationFilter.
Definition filters.hpp:570
float band5Volume
Volume/gain of frequency band 5 of 8.
Definition filters.hpp:604
float band2Volume
Volume/gain of frequency band 2 of 8.
Definition filters.hpp:583
float band3Volume
Volume/gain of frequency band 3 of 8.
Definition filters.hpp:590
float band1Volume
Volume/gain of frequency band 1 of 8.
Definition filters.hpp:576
float band4Volume
Volume/gain of frequency band 4 of 8.
Definition filters.hpp:597
float band8Volume
Volume/gain of frequency band 8 of 8.
Definition filters.hpp:625
float band6Volume
Volume/gain of frequency band 6 of 8.
Definition filters.hpp:611
float band7Volume
Volume/gain of frequency band 7 of 8.
Definition filters.hpp:618
Definition filters.hpp:636
float band2Volume
See EqualizationFilterOptions::band2Volume.
Definition filters.hpp:639
float band6Volume
See EqualizationFilterOptions::band6Volume.
Definition filters.hpp:643
float band1Volume
See EqualizationFilterOptions::band1Volume.
Definition filters.hpp:638
float wet
Filter active amount (0 for unfiltered, 1 for fully filtered).
Definition filters.hpp:637
float band5Volume
See EqualizationFilterOptions::band5Volume.
Definition filters.hpp:642
float band7Volume
See EqualizationFilterOptions::band7Volume.
Definition filters.hpp:644
float band3Volume
See EqualizationFilterOptions::band3Volume.
Definition filters.hpp:640
float band8Volume
See EqualizationFilterOptions::band8Volume.
Definition filters.hpp:645
float band4Volume
See EqualizationFilterOptions::band4Volume.
Definition filters.hpp:641
Configuration options for a FlangerFilter.
Definition filters.hpp:421
float frequency
Frequency of the flanger effect, in Hertz.
Definition filters.hpp:434
float delay
Delay of the flanger effect, in seconds.
Definition filters.hpp:427
Dynamic filter parameters that can be set/faded on-the-fly.
Definition filters.hpp:449
float wet
Filter active amount (0 for unfiltered, 1 for fully filtered).
Definition filters.hpp:450
float delay
See FlangerFilterOptions::delay.
Definition filters.hpp:451
float frequency
See FlangerFilterOptions::frequency.
Definition filters.hpp:452
Configuration options for a FreeverbFilter.
Definition filters.hpp:505
float damping
Freeverb damping parameter.
Definition filters.hpp:525
float width
Freeverb width parameter.
Definition filters.hpp:532
float roomSize
Freeverb room size parameter.
Definition filters.hpp:518
bool freeze
Freeverb mode parameter.
Definition filters.hpp:511
Dynamic filter parameters that can be set/faded on-the-fly.
Definition filters.hpp:545
bool freeze
See FreeverbFilterOptions::freeze.
Definition filters.hpp:547
float damping
See FreeverbFilterOptions::damping.
Definition filters.hpp:549
float wet
Filter active amount (0 for unfiltered, 1 for fully filtered).
Definition filters.hpp:546
float width
See FreeverbFilterOptions::width.
Definition filters.hpp:550
float roomSize
See FreeverbFilterOptions::roomSize.
Definition filters.hpp:548
Configuration options for a LoFiFilter.
Definition filters.hpp:373
float sampleRate
Sample rate of the filter, in Hertz.
Definition filters.hpp:379
float bitDepth
Bit depth of the filter.
Definition filters.hpp:386
Dynamic filter parameters that can be set/faded on-the-fly.
Definition filters.hpp:400
float sampleRate
See LoFiFilterOptions::sampleRate.
Definition filters.hpp:402
float wet
Filter active amount (0 for unfiltered, 1 for fully filtered).
Definition filters.hpp:401
float bitDepth
See LoFiFilterOptions::bitDepth.
Definition filters.hpp:403
Opaque handle to a specific instance of a sound or sound mix playing in a sound stage or sound mix.
Definition SoundInstanceID.hpp:23