]> git.tdb.fi Git - ext/openal.git/blob - alc/effects/modulator.cpp
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / alc / effects / modulator.cpp
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 <algorithm>
24 #include <array>
25 #include <cstdlib>
26 #include <iterator>
27
28 #include "alc/effects/base.h"
29 #include "almalloc.h"
30 #include "alnumbers.h"
31 #include "alnumeric.h"
32 #include "alspan.h"
33 #include "core/ambidefs.h"
34 #include "core/bufferline.h"
35 #include "core/context.h"
36 #include "core/devformat.h"
37 #include "core/device.h"
38 #include "core/effectslot.h"
39 #include "core/filters/biquad.h"
40 #include "core/mixer.h"
41 #include "intrusive_ptr.h"
42
43
44 namespace {
45
46 using uint = unsigned int;
47
48 #define MAX_UPDATE_SAMPLES 128
49
50 #define WAVEFORM_FRACBITS  24
51 #define WAVEFORM_FRACONE   (1<<WAVEFORM_FRACBITS)
52 #define WAVEFORM_FRACMASK  (WAVEFORM_FRACONE-1)
53
54 inline float Sin(uint index)
55 {
56     constexpr float scale{al::numbers::pi_v<float>*2.0f / WAVEFORM_FRACONE};
57     return std::sin(static_cast<float>(index) * scale);
58 }
59
60 inline float Saw(uint index)
61 { return static_cast<float>(index)*(2.0f/WAVEFORM_FRACONE) - 1.0f; }
62
63 inline float Square(uint index)
64 { return static_cast<float>(static_cast<int>((index>>(WAVEFORM_FRACBITS-2))&2) - 1); }
65
66 inline float One(uint) { return 1.0f; }
67
68 template<float (&func)(uint)>
69 void Modulate(float *RESTRICT dst, uint index, const uint step, size_t todo)
70 {
71     for(size_t i{0u};i < todo;i++)
72     {
73         index += step;
74         index &= WAVEFORM_FRACMASK;
75         dst[i] = func(index);
76     }
77 }
78
79
80 struct ModulatorState final : public EffectState {
81     void (*mGetSamples)(float*RESTRICT, uint, const uint, size_t){};
82
83     uint mIndex{0};
84     uint mStep{1};
85
86     struct {
87         uint mTargetChannel{InvalidChannelIndex};
88
89         BiquadFilter mFilter;
90
91         float mCurrentGain{};
92         float mTargetGain{};
93     } mChans[MaxAmbiChannels];
94
95
96     void deviceUpdate(const DeviceBase *device, const BufferStorage *buffer) override;
97     void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
98         const EffectTarget target) override;
99     void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
100         const al::span<FloatBufferLine> samplesOut) override;
101
102     DEF_NEWDEL(ModulatorState)
103 };
104
105 void ModulatorState::deviceUpdate(const DeviceBase*, const BufferStorage*)
106 {
107     for(auto &e : mChans)
108     {
109         e.mTargetChannel = InvalidChannelIndex;
110         e.mFilter.clear();
111         e.mCurrentGain = 0.0f;
112     }
113 }
114
115 void ModulatorState::update(const ContextBase *context, const EffectSlot *slot,
116     const EffectProps *props, const EffectTarget target)
117 {
118     const DeviceBase *device{context->mDevice};
119
120     const float step{props->Modulator.Frequency / static_cast<float>(device->Frequency)};
121     mStep = fastf2u(clampf(step*WAVEFORM_FRACONE, 0.0f, float{WAVEFORM_FRACONE-1}));
122
123     if(mStep == 0)
124         mGetSamples = Modulate<One>;
125     else if(props->Modulator.Waveform == ModulatorWaveform::Sinusoid)
126         mGetSamples = Modulate<Sin>;
127     else if(props->Modulator.Waveform == ModulatorWaveform::Sawtooth)
128         mGetSamples = Modulate<Saw>;
129     else /*if(props->Modulator.Waveform == ModulatorWaveform::Square)*/
130         mGetSamples = Modulate<Square>;
131
132     float f0norm{props->Modulator.HighPassCutoff / static_cast<float>(device->Frequency)};
133     f0norm = clampf(f0norm, 1.0f/512.0f, 0.49f);
134     /* Bandwidth value is constant in octaves. */
135     mChans[0].mFilter.setParamsFromBandwidth(BiquadType::HighPass, f0norm, 1.0f, 0.75f);
136     for(size_t i{1u};i < slot->Wet.Buffer.size();++i)
137         mChans[i].mFilter.copyParamsFrom(mChans[0].mFilter);
138
139     mOutTarget = target.Main->Buffer;
140     auto set_channel = [this](size_t idx, uint outchan, float outgain)
141     {
142         mChans[idx].mTargetChannel = outchan;
143         mChans[idx].mTargetGain = outgain;
144     };
145     target.Main->setAmbiMixParams(slot->Wet, slot->Gain, set_channel);
146 }
147
148 void ModulatorState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
149 {
150     for(size_t base{0u};base < samplesToDo;)
151     {
152         alignas(16) float modsamples[MAX_UPDATE_SAMPLES];
153         const size_t td{minz(MAX_UPDATE_SAMPLES, samplesToDo-base)};
154
155         mGetSamples(modsamples, mIndex, mStep, td);
156         mIndex += static_cast<uint>(mStep * td);
157         mIndex &= WAVEFORM_FRACMASK;
158
159         auto chandata = std::begin(mChans);
160         for(const auto &input : samplesIn)
161         {
162             const size_t outidx{chandata->mTargetChannel};
163             if(outidx != InvalidChannelIndex)
164             {
165                 alignas(16) float temps[MAX_UPDATE_SAMPLES];
166
167                 chandata->mFilter.process({&input[base], td}, temps);
168                 for(size_t i{0u};i < td;i++)
169                     temps[i] *= modsamples[i];
170
171                 MixSamples({temps, td}, samplesOut[outidx].data()+base, chandata->mCurrentGain,
172                     chandata->mTargetGain, samplesToDo-base);
173             }
174             ++chandata;
175         }
176
177         base += td;
178     }
179 }
180
181
182 struct ModulatorStateFactory final : public EffectStateFactory {
183     al::intrusive_ptr<EffectState> create() override
184     { return al::intrusive_ptr<EffectState>{new ModulatorState{}}; }
185 };
186
187 } // namespace
188
189 EffectStateFactory *ModulatorStateFactory_getFactory()
190 {
191     static ModulatorStateFactory ModulatorFactory{};
192     return &ModulatorFactory;
193 }