1 /**
2  * OpenAL cross platform audio library
3  * Copyright (C) 2019 by Anis A. Hireche
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 #include <algorithm>
26 #include <functional>
27 
28 #include "alcmain.h"
29 #include "alcontext.h"
30 #include "alu.h"
31 #include "effectslot.h"
32 #include "math_defs.h"
33 
34 
35 namespace {
36 
37 #define MAX_UPDATE_SAMPLES 256
38 #define NUM_FORMANTS       4
39 #define NUM_FILTERS        2
40 #define Q_FACTOR           5.0f
41 
42 #define VOWEL_A_INDEX      0
43 #define VOWEL_B_INDEX      1
44 
45 #define WAVEFORM_FRACBITS  24
46 #define WAVEFORM_FRACONE   (1<<WAVEFORM_FRACBITS)
47 #define WAVEFORM_FRACMASK  (WAVEFORM_FRACONE-1)
48 
Sin(uint index)49 inline float Sin(uint index)
50 {
51     constexpr float scale{al::MathDefs<float>::Tau() / WAVEFORM_FRACONE};
52     return std::sin(static_cast<float>(index) * scale)*0.5f + 0.5f;
53 }
54 
Saw(uint index)55 inline float Saw(uint index)
56 { return static_cast<float>(index) / float{WAVEFORM_FRACONE}; }
57 
Triangle(uint index)58 inline float Triangle(uint index)
59 { return std::fabs(static_cast<float>(index)*(2.0f/WAVEFORM_FRACONE) - 1.0f); }
60 
Half(uint)61 inline float Half(uint) { return 0.5f; }
62 
63 template<float (&func)(uint)>
Oscillate(float * RESTRICT dst,uint index,const uint step,size_t todo)64 void Oscillate(float *RESTRICT dst, uint index, const uint step, size_t todo)
65 {
66     for(size_t i{0u};i < todo;i++)
67     {
68         index += step;
69         index &= WAVEFORM_FRACMASK;
70         dst[i] = func(index);
71     }
72 }
73 
74 struct FormantFilter
75 {
76     float mCoeff{0.0f};
77     float mGain{1.0f};
78     float mS1{0.0f};
79     float mS2{0.0f};
80 
81     FormantFilter() = default;
FormantFilter__anona695c3d90111::FormantFilter82     FormantFilter(float f0norm, float gain)
83       : mCoeff{std::tan(al::MathDefs<float>::Pi() * f0norm)}, mGain{gain}
84     { }
85 
process__anona695c3d90111::FormantFilter86     inline void process(const float *samplesIn, float *samplesOut, const size_t numInput)
87     {
88         /* A state variable filter from a topology-preserving transform.
89          * Based on a talk given by Ivan Cohen: https://www.youtube.com/watch?v=esjHXGPyrhg
90          */
91         const float g{mCoeff};
92         const float gain{mGain};
93         const float h{1.0f / (1.0f + (g/Q_FACTOR) + (g*g))};
94         float s1{mS1};
95         float s2{mS2};
96 
97         for(size_t i{0u};i < numInput;i++)
98         {
99             const float H{(samplesIn[i] - (1.0f/Q_FACTOR + g)*s1 - s2)*h};
100             const float B{g*H + s1};
101             const float L{g*B + s2};
102 
103             s1 = g*H + B;
104             s2 = g*B + L;
105 
106             // Apply peak and accumulate samples.
107             samplesOut[i] += B * gain;
108         }
109         mS1 = s1;
110         mS2 = s2;
111     }
112 
clear__anona695c3d90111::FormantFilter113     inline void clear()
114     {
115         mS1 = 0.0f;
116         mS2 = 0.0f;
117     }
118 };
119 
120 
121 struct VmorpherState final : public EffectState {
122     struct {
123         /* Effect parameters */
124         FormantFilter Formants[NUM_FILTERS][NUM_FORMANTS];
125 
126         /* Effect gains for each channel */
127         float CurrentGains[MAX_OUTPUT_CHANNELS]{};
128         float TargetGains[MAX_OUTPUT_CHANNELS]{};
129     } mChans[MaxAmbiChannels];
130 
131     void (*mGetSamples)(float*RESTRICT, uint, const uint, size_t){};
132 
133     uint mIndex{0};
134     uint mStep{1};
135 
136     /* Effects buffers */
137     alignas(16) float mSampleBufferA[MAX_UPDATE_SAMPLES]{};
138     alignas(16) float mSampleBufferB[MAX_UPDATE_SAMPLES]{};
139     alignas(16) float mLfo[MAX_UPDATE_SAMPLES]{};
140 
141     void deviceUpdate(const ALCdevice *device, const Buffer &buffer) override;
142     void update(const ALCcontext *context, const EffectSlot *slot, const EffectProps *props,
143         const EffectTarget target) override;
144     void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
145         const al::span<FloatBufferLine> samplesOut) override;
146 
147     static std::array<FormantFilter,4> getFiltersByPhoneme(VMorpherPhenome phoneme,
148         float frequency, float pitch);
149 
150     DEF_NEWDEL(VmorpherState)
151 };
152 
getFiltersByPhoneme(VMorpherPhenome phoneme,float frequency,float pitch)153 std::array<FormantFilter,4> VmorpherState::getFiltersByPhoneme(VMorpherPhenome phoneme,
154     float frequency, float pitch)
155 {
156     /* Using soprano formant set of values to
157      * better match mid-range frequency space.
158      *
159      * See: https://www.classes.cs.uchicago.edu/archive/1999/spring/CS295/Computing_Resources/Csound/CsManual3.48b1.HTML/Appendices/table3.html
160      */
161     switch(phoneme)
162     {
163     case VMorpherPhenome::A:
164         return {{
165             {( 800 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f,   0 / 20.0f); */
166             {(1150 * pitch) / frequency, 0.501187f}, /* std::pow(10.0f,  -6 / 20.0f); */
167             {(2900 * pitch) / frequency, 0.025118f}, /* std::pow(10.0f, -32 / 20.0f); */
168             {(3900 * pitch) / frequency, 0.100000f}  /* std::pow(10.0f, -20 / 20.0f); */
169         }};
170     case VMorpherPhenome::E:
171         return {{
172             {( 350 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f,   0 / 20.0f); */
173             {(2000 * pitch) / frequency, 0.100000f}, /* std::pow(10.0f, -20 / 20.0f); */
174             {(2800 * pitch) / frequency, 0.177827f}, /* std::pow(10.0f, -15 / 20.0f); */
175             {(3600 * pitch) / frequency, 0.009999f}  /* std::pow(10.0f, -40 / 20.0f); */
176         }};
177     case VMorpherPhenome::I:
178         return {{
179             {( 270 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f,   0 / 20.0f); */
180             {(2140 * pitch) / frequency, 0.251188f}, /* std::pow(10.0f, -12 / 20.0f); */
181             {(2950 * pitch) / frequency, 0.050118f}, /* std::pow(10.0f, -26 / 20.0f); */
182             {(3900 * pitch) / frequency, 0.050118f}  /* std::pow(10.0f, -26 / 20.0f); */
183         }};
184     case VMorpherPhenome::O:
185         return {{
186             {( 450 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f,   0 / 20.0f); */
187             {( 800 * pitch) / frequency, 0.281838f}, /* std::pow(10.0f, -11 / 20.0f); */
188             {(2830 * pitch) / frequency, 0.079432f}, /* std::pow(10.0f, -22 / 20.0f); */
189             {(3800 * pitch) / frequency, 0.079432f}  /* std::pow(10.0f, -22 / 20.0f); */
190         }};
191     case VMorpherPhenome::U:
192         return {{
193             {( 325 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f,   0 / 20.0f); */
194             {( 700 * pitch) / frequency, 0.158489f}, /* std::pow(10.0f, -16 / 20.0f); */
195             {(2700 * pitch) / frequency, 0.017782f}, /* std::pow(10.0f, -35 / 20.0f); */
196             {(3800 * pitch) / frequency, 0.009999f}  /* std::pow(10.0f, -40 / 20.0f); */
197         }};
198     default:
199         break;
200     }
201     return {};
202 }
203 
204 
deviceUpdate(const ALCdevice *,const Buffer &)205 void VmorpherState::deviceUpdate(const ALCdevice*, const Buffer&)
206 {
207     for(auto &e : mChans)
208     {
209         std::for_each(std::begin(e.Formants[VOWEL_A_INDEX]), std::end(e.Formants[VOWEL_A_INDEX]),
210             std::mem_fn(&FormantFilter::clear));
211         std::for_each(std::begin(e.Formants[VOWEL_B_INDEX]), std::end(e.Formants[VOWEL_B_INDEX]),
212             std::mem_fn(&FormantFilter::clear));
213         std::fill(std::begin(e.CurrentGains), std::end(e.CurrentGains), 0.0f);
214     }
215 }
216 
update(const ALCcontext * context,const EffectSlot * slot,const EffectProps * props,const EffectTarget target)217 void VmorpherState::update(const ALCcontext *context, const EffectSlot *slot,
218     const EffectProps *props, const EffectTarget target)
219 {
220     const ALCdevice *device{context->mDevice.get()};
221     const float frequency{static_cast<float>(device->Frequency)};
222     const float step{props->Vmorpher.Rate / frequency};
223     mStep = fastf2u(clampf(step*WAVEFORM_FRACONE, 0.0f, float{WAVEFORM_FRACONE-1}));
224 
225     if(mStep == 0)
226         mGetSamples = Oscillate<Half>;
227     else if(props->Vmorpher.Waveform == VMorpherWaveform::Sinusoid)
228         mGetSamples = Oscillate<Sin>;
229     else if(props->Vmorpher.Waveform == VMorpherWaveform::Triangle)
230         mGetSamples = Oscillate<Triangle>;
231     else /*if(props->Vmorpher.Waveform == VMorpherWaveform::Sawtooth)*/
232         mGetSamples = Oscillate<Saw>;
233 
234     const float pitchA{std::pow(2.0f,
235         static_cast<float>(props->Vmorpher.PhonemeACoarseTuning) / 12.0f)};
236     const float pitchB{std::pow(2.0f,
237         static_cast<float>(props->Vmorpher.PhonemeBCoarseTuning) / 12.0f)};
238 
239     auto vowelA = getFiltersByPhoneme(props->Vmorpher.PhonemeA, frequency, pitchA);
240     auto vowelB = getFiltersByPhoneme(props->Vmorpher.PhonemeB, frequency, pitchB);
241 
242     /* Copy the filter coefficients to the input channels. */
243     for(size_t i{0u};i < slot->Wet.Buffer.size();++i)
244     {
245         std::copy(vowelA.begin(), vowelA.end(), std::begin(mChans[i].Formants[VOWEL_A_INDEX]));
246         std::copy(vowelB.begin(), vowelB.end(), std::begin(mChans[i].Formants[VOWEL_B_INDEX]));
247     }
248 
249     mOutTarget = target.Main->Buffer;
250     auto set_gains = [slot,target](auto &chan, al::span<const float,MaxAmbiChannels> coeffs)
251     { ComputePanGains(target.Main, coeffs.data(), slot->Gain, chan.TargetGains); };
252     SetAmbiPanIdentity(std::begin(mChans), slot->Wet.Buffer.size(), set_gains);
253 }
254 
process(const size_t samplesToDo,const al::span<const FloatBufferLine> samplesIn,const al::span<FloatBufferLine> samplesOut)255 void VmorpherState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
256 {
257     /* Following the EFX specification for a conformant implementation which describes
258      * the effect as a pair of 4-band formant filters blended together using an LFO.
259      */
260     for(size_t base{0u};base < samplesToDo;)
261     {
262         const size_t td{minz(MAX_UPDATE_SAMPLES, samplesToDo-base)};
263 
264         mGetSamples(mLfo, mIndex, mStep, td);
265         mIndex += static_cast<uint>(mStep * td);
266         mIndex &= WAVEFORM_FRACMASK;
267 
268         auto chandata = std::begin(mChans);
269         for(const auto &input : samplesIn)
270         {
271             auto& vowelA = chandata->Formants[VOWEL_A_INDEX];
272             auto& vowelB = chandata->Formants[VOWEL_B_INDEX];
273 
274             /* Process first vowel. */
275             std::fill_n(std::begin(mSampleBufferA), td, 0.0f);
276             vowelA[0].process(&input[base], mSampleBufferA, td);
277             vowelA[1].process(&input[base], mSampleBufferA, td);
278             vowelA[2].process(&input[base], mSampleBufferA, td);
279             vowelA[3].process(&input[base], mSampleBufferA, td);
280 
281             /* Process second vowel. */
282             std::fill_n(std::begin(mSampleBufferB), td, 0.0f);
283             vowelB[0].process(&input[base], mSampleBufferB, td);
284             vowelB[1].process(&input[base], mSampleBufferB, td);
285             vowelB[2].process(&input[base], mSampleBufferB, td);
286             vowelB[3].process(&input[base], mSampleBufferB, td);
287 
288             alignas(16) float blended[MAX_UPDATE_SAMPLES];
289             for(size_t i{0u};i < td;i++)
290                 blended[i] = lerp(mSampleBufferA[i], mSampleBufferB[i], mLfo[i]);
291 
292             /* Now, mix the processed sound data to the output. */
293             MixSamples({blended, td}, samplesOut, chandata->CurrentGains, chandata->TargetGains,
294                 samplesToDo-base, base);
295             ++chandata;
296         }
297 
298         base += td;
299     }
300 }
301 
302 
303 struct VmorpherStateFactory final : public EffectStateFactory {
create__anona695c3d90111::VmorpherStateFactory304     al::intrusive_ptr<EffectState> create() override
305     { return al::intrusive_ptr<EffectState>{new VmorpherState{}}; }
306 };
307 
308 } // namespace
309 
VmorpherStateFactory_getFactory()310 EffectStateFactory *VmorpherStateFactory_getFactory()
311 {
312     static VmorpherStateFactory VmorpherFactory{};
313     return &VmorpherFactory;
314 }
315