1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2009 by Chris Robinson.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
19 */
20
21 #include "config.h"
22
23 #include <cmath>
24 #include <cstdlib>
25
26 #include <cmath>
27 #include <algorithm>
28
29 #include "alcmain.h"
30 #include "alcontext.h"
31 #include "core/filters/biquad.h"
32 #include "effectslot.h"
33 #include "vecmat.h"
34
35
36 namespace {
37
38 #define MAX_UPDATE_SAMPLES 128
39
40 #define WAVEFORM_FRACBITS 24
41 #define WAVEFORM_FRACONE (1<<WAVEFORM_FRACBITS)
42 #define WAVEFORM_FRACMASK (WAVEFORM_FRACONE-1)
43
Sin(uint index)44 inline float Sin(uint index)
45 {
46 constexpr float scale{al::MathDefs<float>::Tau() / WAVEFORM_FRACONE};
47 return std::sin(static_cast<float>(index) * scale);
48 }
49
Saw(uint index)50 inline float Saw(uint index)
51 { return static_cast<float>(index)*(2.0f/WAVEFORM_FRACONE) - 1.0f; }
52
Square(uint index)53 inline float Square(uint index)
54 { return static_cast<float>(static_cast<int>((index>>(WAVEFORM_FRACBITS-2))&2) - 1); }
55
One(uint)56 inline float One(uint) { return 1.0f; }
57
58 template<float (&func)(uint)>
Modulate(float * RESTRICT dst,uint index,const uint step,size_t todo)59 void Modulate(float *RESTRICT dst, uint index, const uint step, size_t todo)
60 {
61 for(size_t i{0u};i < todo;i++)
62 {
63 index += step;
64 index &= WAVEFORM_FRACMASK;
65 dst[i] = func(index);
66 }
67 }
68
69
70 struct ModulatorState final : public EffectState {
71 void (*mGetSamples)(float*RESTRICT, uint, const uint, size_t){};
72
73 uint mIndex{0};
74 uint mStep{1};
75
76 struct {
77 BiquadFilter Filter;
78
79 float CurrentGains[MAX_OUTPUT_CHANNELS]{};
80 float TargetGains[MAX_OUTPUT_CHANNELS]{};
81 } mChans[MaxAmbiChannels];
82
83
84 void deviceUpdate(const ALCdevice *device, const Buffer &buffer) override;
85 void update(const ALCcontext *context, const EffectSlot *slot, const EffectProps *props,
86 const EffectTarget target) override;
87 void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
88 const al::span<FloatBufferLine> samplesOut) override;
89
90 DEF_NEWDEL(ModulatorState)
91 };
92
deviceUpdate(const ALCdevice *,const Buffer &)93 void ModulatorState::deviceUpdate(const ALCdevice*, const Buffer&)
94 {
95 for(auto &e : mChans)
96 {
97 e.Filter.clear();
98 std::fill(std::begin(e.CurrentGains), std::end(e.CurrentGains), 0.0f);
99 }
100 }
101
update(const ALCcontext * context,const EffectSlot * slot,const EffectProps * props,const EffectTarget target)102 void ModulatorState::update(const ALCcontext *context, const EffectSlot *slot,
103 const EffectProps *props, const EffectTarget target)
104 {
105 const ALCdevice *device{context->mDevice.get()};
106
107 const float step{props->Modulator.Frequency / static_cast<float>(device->Frequency)};
108 mStep = fastf2u(clampf(step*WAVEFORM_FRACONE, 0.0f, float{WAVEFORM_FRACONE-1}));
109
110 if(mStep == 0)
111 mGetSamples = Modulate<One>;
112 else if(props->Modulator.Waveform == ModulatorWaveform::Sinusoid)
113 mGetSamples = Modulate<Sin>;
114 else if(props->Modulator.Waveform == ModulatorWaveform::Sawtooth)
115 mGetSamples = Modulate<Saw>;
116 else /*if(props->Modulator.Waveform == ModulatorWaveform::Square)*/
117 mGetSamples = Modulate<Square>;
118
119 float f0norm{props->Modulator.HighPassCutoff / static_cast<float>(device->Frequency)};
120 f0norm = clampf(f0norm, 1.0f/512.0f, 0.49f);
121 /* Bandwidth value is constant in octaves. */
122 mChans[0].Filter.setParamsFromBandwidth(BiquadType::HighPass, f0norm, 1.0f, 0.75f);
123 for(size_t i{1u};i < slot->Wet.Buffer.size();++i)
124 mChans[i].Filter.copyParamsFrom(mChans[0].Filter);
125
126 mOutTarget = target.Main->Buffer;
127 auto set_gains = [slot,target](auto &chan, al::span<const float,MaxAmbiChannels> coeffs)
128 { ComputePanGains(target.Main, coeffs.data(), slot->Gain, chan.TargetGains); };
129 SetAmbiPanIdentity(std::begin(mChans), slot->Wet.Buffer.size(), set_gains);
130 }
131
process(const size_t samplesToDo,const al::span<const FloatBufferLine> samplesIn,const al::span<FloatBufferLine> samplesOut)132 void ModulatorState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
133 {
134 for(size_t base{0u};base < samplesToDo;)
135 {
136 alignas(16) float modsamples[MAX_UPDATE_SAMPLES];
137 const size_t td{minz(MAX_UPDATE_SAMPLES, samplesToDo-base)};
138
139 mGetSamples(modsamples, mIndex, mStep, td);
140 mIndex += static_cast<uint>(mStep * td);
141 mIndex &= WAVEFORM_FRACMASK;
142
143 auto chandata = std::begin(mChans);
144 for(const auto &input : samplesIn)
145 {
146 alignas(16) float temps[MAX_UPDATE_SAMPLES];
147
148 chandata->Filter.process({&input[base], td}, temps);
149 for(size_t i{0u};i < td;i++)
150 temps[i] *= modsamples[i];
151
152 MixSamples({temps, td}, samplesOut, chandata->CurrentGains, chandata->TargetGains,
153 samplesToDo-base, base);
154 ++chandata;
155 }
156
157 base += td;
158 }
159 }
160
161
162 struct ModulatorStateFactory final : public EffectStateFactory {
create__anon9a9e485d0111::ModulatorStateFactory163 al::intrusive_ptr<EffectState> create() override
164 { return al::intrusive_ptr<EffectState>{new ModulatorState{}}; }
165 };
166
167 } // namespace
168
ModulatorStateFactory_getFactory()169 EffectStateFactory *ModulatorStateFactory_getFactory()
170 {
171 static ModulatorStateFactory ModulatorFactory{};
172 return &ModulatorFactory;
173 }
174