]> git.tdb.fi Git - ext/openal.git/blob - alc/effects/vmorpher.cpp
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / alc / effects / vmorpher.cpp
1 /**
2  * This file is part of the OpenAL Soft cross platform audio library
3  *
4  * Copyright (C) 2019 by Anis A. Hireche
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice,
10  *   this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  *   this list of conditions and the following disclaimer in the documentation
14  *   and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Spherical-Harmonic-Transform nor the names of its
17  *   contributors may be used to endorse or promote products derived from
18  *   this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "config.h"
34
35 #include <algorithm>
36 #include <array>
37 #include <cstdlib>
38 #include <functional>
39 #include <iterator>
40
41 #include "alc/effects/base.h"
42 #include "almalloc.h"
43 #include "alnumbers.h"
44 #include "alnumeric.h"
45 #include "alspan.h"
46 #include "core/ambidefs.h"
47 #include "core/bufferline.h"
48 #include "core/context.h"
49 #include "core/devformat.h"
50 #include "core/device.h"
51 #include "core/effectslot.h"
52 #include "core/mixer.h"
53 #include "intrusive_ptr.h"
54
55
56 namespace {
57
58 using uint = unsigned int;
59
60 #define MAX_UPDATE_SAMPLES 256
61 #define NUM_FORMANTS       4
62 #define NUM_FILTERS        2
63 #define Q_FACTOR           5.0f
64
65 #define VOWEL_A_INDEX      0
66 #define VOWEL_B_INDEX      1
67
68 #define WAVEFORM_FRACBITS  24
69 #define WAVEFORM_FRACONE   (1<<WAVEFORM_FRACBITS)
70 #define WAVEFORM_FRACMASK  (WAVEFORM_FRACONE-1)
71
72 inline float Sin(uint index)
73 {
74     constexpr float scale{al::numbers::pi_v<float>*2.0f / WAVEFORM_FRACONE};
75     return std::sin(static_cast<float>(index) * scale)*0.5f + 0.5f;
76 }
77
78 inline float Saw(uint index)
79 { return static_cast<float>(index) / float{WAVEFORM_FRACONE}; }
80
81 inline float Triangle(uint index)
82 { return std::fabs(static_cast<float>(index)*(2.0f/WAVEFORM_FRACONE) - 1.0f); }
83
84 inline float Half(uint) { return 0.5f; }
85
86 template<float (&func)(uint)>
87 void Oscillate(float *RESTRICT dst, uint index, const uint step, size_t todo)
88 {
89     for(size_t i{0u};i < todo;i++)
90     {
91         index += step;
92         index &= WAVEFORM_FRACMASK;
93         dst[i] = func(index);
94     }
95 }
96
97 struct FormantFilter
98 {
99     float mCoeff{0.0f};
100     float mGain{1.0f};
101     float mS1{0.0f};
102     float mS2{0.0f};
103
104     FormantFilter() = default;
105     FormantFilter(float f0norm, float gain)
106       : mCoeff{std::tan(al::numbers::pi_v<float> * f0norm)}, mGain{gain}
107     { }
108
109     inline void process(const float *samplesIn, float *samplesOut, const size_t numInput)
110     {
111         /* A state variable filter from a topology-preserving transform.
112          * Based on a talk given by Ivan Cohen: https://www.youtube.com/watch?v=esjHXGPyrhg
113          */
114         const float g{mCoeff};
115         const float gain{mGain};
116         const float h{1.0f / (1.0f + (g/Q_FACTOR) + (g*g))};
117         float s1{mS1};
118         float s2{mS2};
119
120         for(size_t i{0u};i < numInput;i++)
121         {
122             const float H{(samplesIn[i] - (1.0f/Q_FACTOR + g)*s1 - s2)*h};
123             const float B{g*H + s1};
124             const float L{g*B + s2};
125
126             s1 = g*H + B;
127             s2 = g*B + L;
128
129             // Apply peak and accumulate samples.
130             samplesOut[i] += B * gain;
131         }
132         mS1 = s1;
133         mS2 = s2;
134     }
135
136     inline void clear()
137     {
138         mS1 = 0.0f;
139         mS2 = 0.0f;
140     }
141 };
142
143
144 struct VmorpherState final : public EffectState {
145     struct {
146         uint mTargetChannel{InvalidChannelIndex};
147
148         /* Effect parameters */
149         FormantFilter mFormants[NUM_FILTERS][NUM_FORMANTS];
150
151         /* Effect gains for each channel */
152         float mCurrentGain{};
153         float mTargetGain{};
154     } mChans[MaxAmbiChannels];
155
156     void (*mGetSamples)(float*RESTRICT, uint, const uint, size_t){};
157
158     uint mIndex{0};
159     uint mStep{1};
160
161     /* Effects buffers */
162     alignas(16) float mSampleBufferA[MAX_UPDATE_SAMPLES]{};
163     alignas(16) float mSampleBufferB[MAX_UPDATE_SAMPLES]{};
164     alignas(16) float mLfo[MAX_UPDATE_SAMPLES]{};
165
166     void deviceUpdate(const DeviceBase *device, const BufferStorage *buffer) override;
167     void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
168         const EffectTarget target) override;
169     void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
170         const al::span<FloatBufferLine> samplesOut) override;
171
172     static std::array<FormantFilter,4> getFiltersByPhoneme(VMorpherPhenome phoneme,
173         float frequency, float pitch);
174
175     DEF_NEWDEL(VmorpherState)
176 };
177
178 std::array<FormantFilter,4> VmorpherState::getFiltersByPhoneme(VMorpherPhenome phoneme,
179     float frequency, float pitch)
180 {
181     /* Using soprano formant set of values to
182      * better match mid-range frequency space.
183      *
184      * See: https://www.classes.cs.uchicago.edu/archive/1999/spring/CS295/Computing_Resources/Csound/CsManual3.48b1.HTML/Appendices/table3.html
185      */
186     switch(phoneme)
187     {
188     case VMorpherPhenome::A:
189         return {{
190             {( 800 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f,   0 / 20.0f); */
191             {(1150 * pitch) / frequency, 0.501187f}, /* std::pow(10.0f,  -6 / 20.0f); */
192             {(2900 * pitch) / frequency, 0.025118f}, /* std::pow(10.0f, -32 / 20.0f); */
193             {(3900 * pitch) / frequency, 0.100000f}  /* std::pow(10.0f, -20 / 20.0f); */
194         }};
195     case VMorpherPhenome::E:
196         return {{
197             {( 350 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f,   0 / 20.0f); */
198             {(2000 * pitch) / frequency, 0.100000f}, /* std::pow(10.0f, -20 / 20.0f); */
199             {(2800 * pitch) / frequency, 0.177827f}, /* std::pow(10.0f, -15 / 20.0f); */
200             {(3600 * pitch) / frequency, 0.009999f}  /* std::pow(10.0f, -40 / 20.0f); */
201         }};
202     case VMorpherPhenome::I:
203         return {{
204             {( 270 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f,   0 / 20.0f); */
205             {(2140 * pitch) / frequency, 0.251188f}, /* std::pow(10.0f, -12 / 20.0f); */
206             {(2950 * pitch) / frequency, 0.050118f}, /* std::pow(10.0f, -26 / 20.0f); */
207             {(3900 * pitch) / frequency, 0.050118f}  /* std::pow(10.0f, -26 / 20.0f); */
208         }};
209     case VMorpherPhenome::O:
210         return {{
211             {( 450 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f,   0 / 20.0f); */
212             {( 800 * pitch) / frequency, 0.281838f}, /* std::pow(10.0f, -11 / 20.0f); */
213             {(2830 * pitch) / frequency, 0.079432f}, /* std::pow(10.0f, -22 / 20.0f); */
214             {(3800 * pitch) / frequency, 0.079432f}  /* std::pow(10.0f, -22 / 20.0f); */
215         }};
216     case VMorpherPhenome::U:
217         return {{
218             {( 325 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f,   0 / 20.0f); */
219             {( 700 * pitch) / frequency, 0.158489f}, /* std::pow(10.0f, -16 / 20.0f); */
220             {(2700 * pitch) / frequency, 0.017782f}, /* std::pow(10.0f, -35 / 20.0f); */
221             {(3800 * pitch) / frequency, 0.009999f}  /* std::pow(10.0f, -40 / 20.0f); */
222         }};
223     default:
224         break;
225     }
226     return {};
227 }
228
229
230 void VmorpherState::deviceUpdate(const DeviceBase*, const BufferStorage*)
231 {
232     for(auto &e : mChans)
233     {
234         e.mTargetChannel = InvalidChannelIndex;
235         std::for_each(std::begin(e.mFormants[VOWEL_A_INDEX]), std::end(e.mFormants[VOWEL_A_INDEX]),
236             std::mem_fn(&FormantFilter::clear));
237         std::for_each(std::begin(e.mFormants[VOWEL_B_INDEX]), std::end(e.mFormants[VOWEL_B_INDEX]),
238             std::mem_fn(&FormantFilter::clear));
239         e.mCurrentGain = 0.0f;
240     }
241 }
242
243 void VmorpherState::update(const ContextBase *context, const EffectSlot *slot,
244     const EffectProps *props, const EffectTarget target)
245 {
246     const DeviceBase *device{context->mDevice};
247     const float frequency{static_cast<float>(device->Frequency)};
248     const float step{props->Vmorpher.Rate / frequency};
249     mStep = fastf2u(clampf(step*WAVEFORM_FRACONE, 0.0f, float{WAVEFORM_FRACONE-1}));
250
251     if(mStep == 0)
252         mGetSamples = Oscillate<Half>;
253     else if(props->Vmorpher.Waveform == VMorpherWaveform::Sinusoid)
254         mGetSamples = Oscillate<Sin>;
255     else if(props->Vmorpher.Waveform == VMorpherWaveform::Triangle)
256         mGetSamples = Oscillate<Triangle>;
257     else /*if(props->Vmorpher.Waveform == VMorpherWaveform::Sawtooth)*/
258         mGetSamples = Oscillate<Saw>;
259
260     const float pitchA{std::pow(2.0f,
261         static_cast<float>(props->Vmorpher.PhonemeACoarseTuning) / 12.0f)};
262     const float pitchB{std::pow(2.0f,
263         static_cast<float>(props->Vmorpher.PhonemeBCoarseTuning) / 12.0f)};
264
265     auto vowelA = getFiltersByPhoneme(props->Vmorpher.PhonemeA, frequency, pitchA);
266     auto vowelB = getFiltersByPhoneme(props->Vmorpher.PhonemeB, frequency, pitchB);
267
268     /* Copy the filter coefficients to the input channels. */
269     for(size_t i{0u};i < slot->Wet.Buffer.size();++i)
270     {
271         std::copy(vowelA.begin(), vowelA.end(), std::begin(mChans[i].mFormants[VOWEL_A_INDEX]));
272         std::copy(vowelB.begin(), vowelB.end(), std::begin(mChans[i].mFormants[VOWEL_B_INDEX]));
273     }
274
275     mOutTarget = target.Main->Buffer;
276     auto set_channel = [this](size_t idx, uint outchan, float outgain)
277     {
278         mChans[idx].mTargetChannel = outchan;
279         mChans[idx].mTargetGain = outgain;
280     };
281     target.Main->setAmbiMixParams(slot->Wet, slot->Gain, set_channel);
282 }
283
284 void VmorpherState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
285 {
286     /* Following the EFX specification for a conformant implementation which describes
287      * the effect as a pair of 4-band formant filters blended together using an LFO.
288      */
289     for(size_t base{0u};base < samplesToDo;)
290     {
291         const size_t td{minz(MAX_UPDATE_SAMPLES, samplesToDo-base)};
292
293         mGetSamples(mLfo, mIndex, mStep, td);
294         mIndex += static_cast<uint>(mStep * td);
295         mIndex &= WAVEFORM_FRACMASK;
296
297         auto chandata = std::begin(mChans);
298         for(const auto &input : samplesIn)
299         {
300             const size_t outidx{chandata->mTargetChannel};
301             if(outidx == InvalidChannelIndex)
302             {
303                 ++chandata;
304                 continue;
305             }
306
307             auto& vowelA = chandata->mFormants[VOWEL_A_INDEX];
308             auto& vowelB = chandata->mFormants[VOWEL_B_INDEX];
309
310             /* Process first vowel. */
311             std::fill_n(std::begin(mSampleBufferA), td, 0.0f);
312             vowelA[0].process(&input[base], mSampleBufferA, td);
313             vowelA[1].process(&input[base], mSampleBufferA, td);
314             vowelA[2].process(&input[base], mSampleBufferA, td);
315             vowelA[3].process(&input[base], mSampleBufferA, td);
316
317             /* Process second vowel. */
318             std::fill_n(std::begin(mSampleBufferB), td, 0.0f);
319             vowelB[0].process(&input[base], mSampleBufferB, td);
320             vowelB[1].process(&input[base], mSampleBufferB, td);
321             vowelB[2].process(&input[base], mSampleBufferB, td);
322             vowelB[3].process(&input[base], mSampleBufferB, td);
323
324             alignas(16) float blended[MAX_UPDATE_SAMPLES];
325             for(size_t i{0u};i < td;i++)
326                 blended[i] = lerpf(mSampleBufferA[i], mSampleBufferB[i], mLfo[i]);
327
328             /* Now, mix the processed sound data to the output. */
329             MixSamples({blended, td}, samplesOut[outidx].data()+base, chandata->mCurrentGain,
330                 chandata->mTargetGain, samplesToDo-base);
331             ++chandata;
332         }
333
334         base += td;
335     }
336 }
337
338
339 struct VmorpherStateFactory final : public EffectStateFactory {
340     al::intrusive_ptr<EffectState> create() override
341     { return al::intrusive_ptr<EffectState>{new VmorpherState{}}; }
342 };
343
344 } // namespace
345
346 EffectStateFactory *VmorpherStateFactory_getFactory()
347 {
348     static VmorpherStateFactory VmorpherFactory{};
349     return &VmorpherFactory;
350 }