Avoid static constexpr for arrays iterated over at run-time
[openal-soft.git] / alc / effects / vmorpher.cpp
blobb1b7cc060b23cc22268e8f1de6b84b4345b956da
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.
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.
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
21 #include "config.h"
23 #include <cmath>
24 #include <cstdlib>
25 #include <algorithm>
26 #include <functional>
28 #include "al/auxeffectslot.h"
29 #include "alcmain.h"
30 #include "alcontext.h"
31 #include "alu.h"
33 namespace {
35 #define MAX_UPDATE_SAMPLES 128
36 #define NUM_FORMANTS 4
37 #define NUM_FILTERS 2
38 #define Q_FACTOR 5.0f
40 #define VOWEL_A_INDEX 0
41 #define VOWEL_B_INDEX 1
43 #define WAVEFORM_FRACBITS 24
44 #define WAVEFORM_FRACONE (1<<WAVEFORM_FRACBITS)
45 #define WAVEFORM_FRACMASK (WAVEFORM_FRACONE-1)
47 inline float Sin(ALuint index)
49 constexpr float scale{al::MathDefs<float>::Tau() / WAVEFORM_FRACONE};
50 return std::sin(static_cast<float>(index) * scale)*0.5f + 0.5f;
53 inline float Saw(ALuint index)
54 { return static_cast<float>(index) / float{WAVEFORM_FRACONE}; }
56 inline float Triangle(ALuint index)
57 { return std::fabs(static_cast<float>(index)*(2.0f/WAVEFORM_FRACONE) - 1.0f); }
59 inline float Half(ALuint) { return 0.5f; }
61 template<float (&func)(ALuint)>
62 void Oscillate(float *RESTRICT dst, ALuint index, const ALuint step, size_t todo)
64 for(size_t i{0u};i < todo;i++)
66 index += step;
67 index &= WAVEFORM_FRACMASK;
68 dst[i] = func(index);
72 struct FormantFilter
74 ALfloat mCoeff{0.0f};
75 ALfloat mGain{1.0f};
76 ALfloat mS1{0.0f};
77 ALfloat mS2{0.0f};
79 FormantFilter() = default;
80 FormantFilter(ALfloat f0norm, ALfloat gain)
81 : mCoeff{std::tan(al::MathDefs<float>::Pi() * f0norm)}, mGain{gain}
82 { }
84 inline void process(const ALfloat *samplesIn, ALfloat *samplesOut, const size_t numInput)
86 /* A state variable filter from a topology-preserving transform.
87 * Based on a talk given by Ivan Cohen: https://www.youtube.com/watch?v=esjHXGPyrhg
89 const ALfloat g{mCoeff};
90 const ALfloat gain{mGain};
91 const ALfloat h{1.0f / (1.0f + (g/Q_FACTOR) + (g*g))};
92 ALfloat s1{mS1};
93 ALfloat s2{mS2};
95 for(size_t i{0u};i < numInput;i++)
97 const ALfloat H{(samplesIn[i] - (1.0f/Q_FACTOR + g)*s1 - s2)*h};
98 const ALfloat B{g*H + s1};
99 const ALfloat L{g*B + s2};
101 s1 = g*H + B;
102 s2 = g*B + L;
104 // Apply peak and accumulate samples.
105 samplesOut[i] += B * gain;
107 mS1 = s1;
108 mS2 = s2;
111 inline void clear()
113 mS1 = 0.0f;
114 mS2 = 0.0f;
119 struct VmorpherState final : public EffectState {
120 struct {
121 /* Effect parameters */
122 FormantFilter Formants[NUM_FILTERS][NUM_FORMANTS];
124 /* Effect gains for each channel */
125 ALfloat CurrentGains[MAX_OUTPUT_CHANNELS]{};
126 ALfloat TargetGains[MAX_OUTPUT_CHANNELS]{};
127 } mChans[MAX_AMBI_CHANNELS];
129 void (*mGetSamples)(float*RESTRICT, ALuint, const ALuint, size_t){};
131 ALuint mIndex{0};
132 ALuint mStep{1};
134 /* Effects buffers */
135 ALfloat mSampleBufferA[MAX_UPDATE_SAMPLES]{};
136 ALfloat mSampleBufferB[MAX_UPDATE_SAMPLES]{};
138 ALboolean deviceUpdate(const ALCdevice *device) override;
139 void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
140 void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut) override;
142 static std::array<FormantFilter,4> getFiltersByPhoneme(ALenum phoneme, ALfloat frequency, ALfloat pitch);
144 DEF_NEWDEL(VmorpherState)
147 std::array<FormantFilter,4> VmorpherState::getFiltersByPhoneme(ALenum phoneme, ALfloat frequency, ALfloat pitch)
149 /* Using soprano formant set of values to
150 * better match mid-range frequency space.
152 * See: https://www.classes.cs.uchicago.edu/archive/1999/spring/CS295/Computing_Resources/Csound/CsManual3.48b1.HTML/Appendices/table3.html
154 switch(phoneme)
156 case AL_VOCAL_MORPHER_PHONEME_A:
157 return {{
158 {( 800 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f, 0 / 20.0f); */
159 {(1150 * pitch) / frequency, 0.501187f}, /* std::pow(10.0f, -6 / 20.0f); */
160 {(2900 * pitch) / frequency, 0.025118f}, /* std::pow(10.0f, -32 / 20.0f); */
161 {(3900 * pitch) / frequency, 0.100000f} /* std::pow(10.0f, -20 / 20.0f); */
163 case AL_VOCAL_MORPHER_PHONEME_E:
164 return {{
165 {( 350 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f, 0 / 20.0f); */
166 {(2000 * pitch) / frequency, 0.100000f}, /* std::pow(10.0f, -20 / 20.0f); */
167 {(2800 * pitch) / frequency, 0.177827f}, /* std::pow(10.0f, -15 / 20.0f); */
168 {(3600 * pitch) / frequency, 0.009999f} /* std::pow(10.0f, -40 / 20.0f); */
170 case AL_VOCAL_MORPHER_PHONEME_I:
171 return {{
172 {( 270 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f, 0 / 20.0f); */
173 {(2140 * pitch) / frequency, 0.251188f}, /* std::pow(10.0f, -12 / 20.0f); */
174 {(2950 * pitch) / frequency, 0.050118f}, /* std::pow(10.0f, -26 / 20.0f); */
175 {(3900 * pitch) / frequency, 0.050118f} /* std::pow(10.0f, -26 / 20.0f); */
177 case AL_VOCAL_MORPHER_PHONEME_O:
178 return {{
179 {( 450 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f, 0 / 20.0f); */
180 {( 800 * pitch) / frequency, 0.281838f}, /* std::pow(10.0f, -11 / 20.0f); */
181 {(2830 * pitch) / frequency, 0.079432f}, /* std::pow(10.0f, -22 / 20.0f); */
182 {(3800 * pitch) / frequency, 0.079432f} /* std::pow(10.0f, -22 / 20.0f); */
184 case AL_VOCAL_MORPHER_PHONEME_U:
185 return {{
186 {( 325 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f, 0 / 20.0f); */
187 {( 700 * pitch) / frequency, 0.158489f}, /* std::pow(10.0f, -16 / 20.0f); */
188 {(2700 * pitch) / frequency, 0.017782f}, /* std::pow(10.0f, -35 / 20.0f); */
189 {(3800 * pitch) / frequency, 0.009999f} /* std::pow(10.0f, -40 / 20.0f); */
192 return {};
196 ALboolean VmorpherState::deviceUpdate(const ALCdevice* /*device*/)
198 for(auto &e : mChans)
200 std::for_each(std::begin(e.Formants[VOWEL_A_INDEX]), std::end(e.Formants[VOWEL_A_INDEX]),
201 std::mem_fn(&FormantFilter::clear));
202 std::for_each(std::begin(e.Formants[VOWEL_B_INDEX]), std::end(e.Formants[VOWEL_B_INDEX]),
203 std::mem_fn(&FormantFilter::clear));
204 std::fill(std::begin(e.CurrentGains), std::end(e.CurrentGains), 0.0f);
207 return AL_TRUE;
210 void VmorpherState::update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
212 const ALCdevice *device{context->mDevice.get()};
213 const ALfloat frequency{static_cast<ALfloat>(device->Frequency)};
214 const ALfloat step{props->Vmorpher.Rate / frequency};
215 mStep = fastf2u(clampf(step*WAVEFORM_FRACONE, 0.0f, ALfloat{WAVEFORM_FRACONE-1}));
217 if(mStep == 0)
218 mGetSamples = Oscillate<Half>;
219 else if(props->Vmorpher.Waveform == AL_VOCAL_MORPHER_WAVEFORM_SINUSOID)
220 mGetSamples = Oscillate<Sin>;
221 else if(props->Vmorpher.Waveform == AL_VOCAL_MORPHER_WAVEFORM_SAWTOOTH)
222 mGetSamples = Oscillate<Saw>;
223 else /*if(props->Vmorpher.Waveform == AL_VOCAL_MORPHER_WAVEFORM_TRIANGLE)*/
224 mGetSamples = Oscillate<Triangle>;
226 const ALfloat pitchA{std::pow(2.0f,
227 static_cast<float>(props->Vmorpher.PhonemeACoarseTuning) / 12.0f)};
228 const ALfloat pitchB{std::pow(2.0f,
229 static_cast<float>(props->Vmorpher.PhonemeBCoarseTuning) / 12.0f)};
231 auto vowelA = getFiltersByPhoneme(props->Vmorpher.PhonemeA, frequency, pitchA);
232 auto vowelB = getFiltersByPhoneme(props->Vmorpher.PhonemeB, frequency, pitchB);
234 /* Copy the filter coefficients to the input channels. */
235 for(size_t i{0u};i < slot->Wet.Buffer.size();++i)
237 std::copy(vowelA.begin(), vowelA.end(), std::begin(mChans[i].Formants[VOWEL_A_INDEX]));
238 std::copy(vowelB.begin(), vowelB.end(), std::begin(mChans[i].Formants[VOWEL_B_INDEX]));
241 mOutTarget = target.Main->Buffer;
242 for(size_t i{0u};i < slot->Wet.Buffer.size();++i)
244 auto coeffs = GetAmbiIdentityRow(i);
245 ComputePanGains(target.Main, coeffs.data(), slot->Params.Gain, mChans[i].TargetGains);
249 void VmorpherState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
251 /* Following the EFX specification for a conformant implementation which describes
252 * the effect as a pair of 4-band formant filters blended together using an LFO.
254 for(size_t base{0u};base < samplesToDo;)
256 alignas(16) ALfloat lfo[MAX_UPDATE_SAMPLES];
257 const size_t td{minz(MAX_UPDATE_SAMPLES, samplesToDo-base)};
259 mGetSamples(lfo, mIndex, mStep, td);
260 mIndex += static_cast<ALuint>(mStep * td);
261 mIndex &= WAVEFORM_FRACMASK;
263 auto chandata = std::addressof(mChans[0]);
264 for(const auto &input : samplesIn)
266 std::fill_n(std::begin(mSampleBufferA), td, 0.0f);
267 std::fill_n(std::begin(mSampleBufferB), td, 0.0f);
269 auto& vowelA = chandata->Formants[VOWEL_A_INDEX];
270 auto& vowelB = chandata->Formants[VOWEL_B_INDEX];
272 /* Process first vowel. */
273 vowelA[0].process(&input[base], mSampleBufferA, td);
274 vowelA[1].process(&input[base], mSampleBufferA, td);
275 vowelA[2].process(&input[base], mSampleBufferA, td);
276 vowelA[3].process(&input[base], mSampleBufferA, td);
278 /* Process second vowel. */
279 vowelB[0].process(&input[base], mSampleBufferB, td);
280 vowelB[1].process(&input[base], mSampleBufferB, td);
281 vowelB[2].process(&input[base], mSampleBufferB, td);
282 vowelB[3].process(&input[base], mSampleBufferB, td);
284 alignas(16) ALfloat blended[MAX_UPDATE_SAMPLES];
285 for(size_t i{0u};i < td;i++)
286 blended[i] = lerp(mSampleBufferA[i], mSampleBufferB[i], lfo[i]);
288 /* Now, mix the processed sound data to the output. */
289 MixSamples({blended, td}, samplesOut, chandata->CurrentGains, chandata->TargetGains,
290 samplesToDo-base, base);
291 ++chandata;
294 base += td;
299 void Vmorpher_setParami(EffectProps* props, ALCcontext *context, ALenum param, ALint val)
301 switch(param)
303 case AL_VOCAL_MORPHER_WAVEFORM:
304 if(!(val >= AL_VOCAL_MORPHER_MIN_WAVEFORM && val <= AL_VOCAL_MORPHER_MAX_WAVEFORM))
305 SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher waveform out of range");
306 props->Vmorpher.Waveform = val;
307 break;
309 case AL_VOCAL_MORPHER_PHONEMEA:
310 if(!(val >= AL_VOCAL_MORPHER_MIN_PHONEMEA && val <= AL_VOCAL_MORPHER_MAX_PHONEMEA))
311 SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher phoneme-a out of range");
312 props->Vmorpher.PhonemeA = val;
313 break;
315 case AL_VOCAL_MORPHER_PHONEMEB:
316 if(!(val >= AL_VOCAL_MORPHER_MIN_PHONEMEB && val <= AL_VOCAL_MORPHER_MAX_PHONEMEB))
317 SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher phoneme-b out of range");
318 props->Vmorpher.PhonemeB = val;
319 break;
321 case AL_VOCAL_MORPHER_PHONEMEA_COARSE_TUNING:
322 if(!(val >= AL_VOCAL_MORPHER_MIN_PHONEMEA_COARSE_TUNING && val <= AL_VOCAL_MORPHER_MAX_PHONEMEA_COARSE_TUNING))
323 SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher phoneme-a coarse tuning out of range");
324 props->Vmorpher.PhonemeACoarseTuning = val;
325 break;
327 case AL_VOCAL_MORPHER_PHONEMEB_COARSE_TUNING:
328 if(!(val >= AL_VOCAL_MORPHER_MIN_PHONEMEB_COARSE_TUNING && val <= AL_VOCAL_MORPHER_MAX_PHONEMEB_COARSE_TUNING))
329 SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher phoneme-b coarse tuning out of range");
330 props->Vmorpher.PhonemeBCoarseTuning = val;
331 break;
333 default:
334 context->setError(AL_INVALID_ENUM, "Invalid vocal morpher integer property 0x%04x",
335 param);
338 void Vmorpher_setParamiv(EffectProps*, ALCcontext *context, ALenum param, const ALint*)
339 { context->setError(AL_INVALID_ENUM, "Invalid vocal morpher integer-vector property 0x%04x", param); }
340 void Vmorpher_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
342 switch(param)
344 case AL_VOCAL_MORPHER_RATE:
345 if(!(val >= AL_VOCAL_MORPHER_MIN_RATE && val <= AL_VOCAL_MORPHER_MAX_RATE))
346 SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher rate out of range");
347 props->Vmorpher.Rate = val;
348 break;
350 default:
351 context->setError(AL_INVALID_ENUM, "Invalid vocal morpher float property 0x%04x",
352 param);
355 void Vmorpher_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
356 { Vmorpher_setParamf(props, context, param, vals[0]); }
358 void Vmorpher_getParami(const EffectProps* props, ALCcontext *context, ALenum param, ALint* val)
360 switch(param)
362 case AL_VOCAL_MORPHER_PHONEMEA:
363 *val = props->Vmorpher.PhonemeA;
364 break;
366 case AL_VOCAL_MORPHER_PHONEMEB:
367 *val = props->Vmorpher.PhonemeB;
368 break;
370 case AL_VOCAL_MORPHER_PHONEMEA_COARSE_TUNING:
371 *val = props->Vmorpher.PhonemeACoarseTuning;
372 break;
374 case AL_VOCAL_MORPHER_PHONEMEB_COARSE_TUNING:
375 *val = props->Vmorpher.PhonemeBCoarseTuning;
376 break;
378 case AL_VOCAL_MORPHER_WAVEFORM:
379 *val = props->Vmorpher.Waveform;
380 break;
382 default:
383 context->setError(AL_INVALID_ENUM, "Invalid vocal morpher integer property 0x%04x",
384 param);
387 void Vmorpher_getParamiv(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
388 { context->setError(AL_INVALID_ENUM, "Invalid vocal morpher integer-vector property 0x%04x", param); }
389 void Vmorpher_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
391 switch(param)
393 case AL_VOCAL_MORPHER_RATE:
394 *val = props->Vmorpher.Rate;
395 break;
397 default:
398 context->setError(AL_INVALID_ENUM, "Invalid vocal morpher float property 0x%04x",
399 param);
402 void Vmorpher_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
403 { Vmorpher_getParamf(props, context, param, vals); }
405 DEFINE_ALEFFECT_VTABLE(Vmorpher);
408 struct VmorpherStateFactory final : public EffectStateFactory {
409 EffectState *create() override { return new VmorpherState{}; }
410 EffectProps getDefaultProps() const noexcept override;
411 const EffectVtable *getEffectVtable() const noexcept override { return &Vmorpher_vtable; }
414 EffectProps VmorpherStateFactory::getDefaultProps() const noexcept
416 EffectProps props{};
417 props.Vmorpher.Rate = AL_VOCAL_MORPHER_DEFAULT_RATE;
418 props.Vmorpher.PhonemeA = AL_VOCAL_MORPHER_DEFAULT_PHONEMEA;
419 props.Vmorpher.PhonemeB = AL_VOCAL_MORPHER_DEFAULT_PHONEMEB;
420 props.Vmorpher.PhonemeACoarseTuning = AL_VOCAL_MORPHER_DEFAULT_PHONEMEA_COARSE_TUNING;
421 props.Vmorpher.PhonemeBCoarseTuning = AL_VOCAL_MORPHER_DEFAULT_PHONEMEB_COARSE_TUNING;
422 props.Vmorpher.Waveform = AL_VOCAL_MORPHER_DEFAULT_WAVEFORM;
423 return props;
426 } // namespace
428 EffectStateFactory *VmorpherStateFactory_getFactory()
430 static VmorpherStateFactory VmorpherFactory{};
431 return &VmorpherFactory;