Don't inline the utf8 converters
[openal-soft.git] / alc / effects / vmorpher.cpp
blobd24d41d6992a310c84b30250eb31fe7ae6f87f74
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 ALfloat Sin(ALuint index)
49 constexpr ALfloat scale{al::MathDefs<float>::Tau() / ALfloat{WAVEFORM_FRACONE}};
50 return std::sin(static_cast<ALfloat>(index) * scale)*0.5f + 0.5f;
53 inline ALfloat Saw(ALuint index)
55 return static_cast<ALfloat>(index) / ALfloat{WAVEFORM_FRACONE};
58 inline ALfloat Triangle(ALuint index)
60 return std::fabs(static_cast<ALfloat>(index)*(2.0f/WAVEFORM_FRACONE) - 1.0f);
63 inline ALfloat Half(ALuint)
65 return 0.5f;
68 template<ALfloat func(ALuint)>
69 void Oscillate(ALfloat *RESTRICT dst, ALuint index, const ALuint step, size_t todo)
71 for(size_t i{0u};i < todo;i++)
73 index += step;
74 index &= WAVEFORM_FRACMASK;
75 dst[i] = func(index);
79 struct FormantFilter
81 ALfloat mCoeff{0.0f};
82 ALfloat mGain{1.0f};
83 ALfloat mS1{0.0f};
84 ALfloat mS2{0.0f};
86 FormantFilter() = default;
87 FormantFilter(ALfloat f0norm, ALfloat gain)
88 : mCoeff{std::tan(al::MathDefs<float>::Pi() * f0norm)}, mGain{gain}
89 { }
91 inline void process(const ALfloat *samplesIn, ALfloat *samplesOut, const size_t numInput)
93 /* A state variable filter from a topology-preserving transform.
94 * Based on a talk given by Ivan Cohen: https://www.youtube.com/watch?v=esjHXGPyrhg
96 const ALfloat g{mCoeff};
97 const ALfloat gain{mGain};
98 const ALfloat h{1.0f / (1.0f + (g/Q_FACTOR) + (g*g))};
99 ALfloat s1{mS1};
100 ALfloat s2{mS2};
102 for(size_t i{0u};i < numInput;i++)
104 const ALfloat H{(samplesIn[i] - (1.0f/Q_FACTOR + g)*s1 - s2)*h};
105 const ALfloat B{g*H + s1};
106 const ALfloat L{g*B + s2};
108 s1 = g*H + B;
109 s2 = g*B + L;
111 // Apply peak and accumulate samples.
112 samplesOut[i] += B * gain;
114 mS1 = s1;
115 mS2 = s2;
118 inline void clear()
120 mS1 = 0.0f;
121 mS2 = 0.0f;
126 struct VmorpherState final : public EffectState {
127 struct {
128 /* Effect parameters */
129 FormantFilter Formants[NUM_FILTERS][NUM_FORMANTS];
131 /* Effect gains for each channel */
132 ALfloat CurrentGains[MAX_OUTPUT_CHANNELS]{};
133 ALfloat TargetGains[MAX_OUTPUT_CHANNELS]{};
134 } mChans[MAX_AMBI_CHANNELS];
136 void (*mGetSamples)(ALfloat* RESTRICT, ALuint, const ALuint, size_t){};
138 ALuint mIndex{0};
139 ALuint mStep{1};
141 /* Effects buffers */
142 ALfloat mSampleBufferA[MAX_UPDATE_SAMPLES]{};
143 ALfloat mSampleBufferB[MAX_UPDATE_SAMPLES]{};
145 ALboolean deviceUpdate(const ALCdevice *device) override;
146 void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
147 void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut) override;
149 static std::array<FormantFilter,4> getFiltersByPhoneme(ALenum phoneme, ALfloat frequency, ALfloat pitch);
151 DEF_NEWDEL(VmorpherState)
154 std::array<FormantFilter,4> VmorpherState::getFiltersByPhoneme(ALenum phoneme, ALfloat frequency, ALfloat pitch)
156 /* Using soprano formant set of values to
157 * better match mid-range frequency space.
159 * See: https://www.classes.cs.uchicago.edu/archive/1999/spring/CS295/Computing_Resources/Csound/CsManual3.48b1.HTML/Appendices/table3.html
161 switch(phoneme)
163 case AL_VOCAL_MORPHER_PHONEME_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); */
170 case AL_VOCAL_MORPHER_PHONEME_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); */
177 case AL_VOCAL_MORPHER_PHONEME_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); */
184 case AL_VOCAL_MORPHER_PHONEME_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); */
191 case AL_VOCAL_MORPHER_PHONEME_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); */
199 return {};
203 ALboolean VmorpherState::deviceUpdate(const ALCdevice* /*device*/)
205 for(auto &e : mChans)
207 std::for_each(std::begin(e.Formants[VOWEL_A_INDEX]), std::end(e.Formants[VOWEL_A_INDEX]),
208 std::mem_fn(&FormantFilter::clear));
209 std::for_each(std::begin(e.Formants[VOWEL_B_INDEX]), std::end(e.Formants[VOWEL_B_INDEX]),
210 std::mem_fn(&FormantFilter::clear));
211 std::fill(std::begin(e.CurrentGains), std::end(e.CurrentGains), 0.0f);
214 return AL_TRUE;
217 void VmorpherState::update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
219 const ALCdevice *device{context->mDevice.get()};
220 const ALfloat frequency{static_cast<ALfloat>(device->Frequency)};
221 const ALfloat step{props->Vmorpher.Rate / frequency};
222 mStep = fastf2u(clampf(step*WAVEFORM_FRACONE, 0.0f, ALfloat{WAVEFORM_FRACONE-1}));
224 if(mStep == 0)
225 mGetSamples = Oscillate<Half>;
226 else if(props->Vmorpher.Waveform == AL_VOCAL_MORPHER_WAVEFORM_SINUSOID)
227 mGetSamples = Oscillate<Sin>;
228 else if(props->Vmorpher.Waveform == AL_VOCAL_MORPHER_WAVEFORM_SAWTOOTH)
229 mGetSamples = Oscillate<Saw>;
230 else /*if(props->Vmorpher.Waveform == AL_VOCAL_MORPHER_WAVEFORM_TRIANGLE)*/
231 mGetSamples = Oscillate<Triangle>;
233 const ALfloat pitchA{std::pow(2.0f,
234 static_cast<float>(props->Vmorpher.PhonemeACoarseTuning) / 12.0f)};
235 const ALfloat pitchB{std::pow(2.0f,
236 static_cast<float>(props->Vmorpher.PhonemeBCoarseTuning) / 12.0f)};
238 auto vowelA = getFiltersByPhoneme(props->Vmorpher.PhonemeA, frequency, pitchA);
239 auto vowelB = getFiltersByPhoneme(props->Vmorpher.PhonemeB, frequency, pitchB);
241 /* Copy the filter coefficients to the input channels. */
242 for(size_t i{0u};i < slot->Wet.Buffer.size();++i)
244 std::copy(vowelA.begin(), vowelA.end(), std::begin(mChans[i].Formants[VOWEL_A_INDEX]));
245 std::copy(vowelB.begin(), vowelB.end(), std::begin(mChans[i].Formants[VOWEL_B_INDEX]));
248 mOutTarget = target.Main->Buffer;
249 for(size_t i{0u};i < slot->Wet.Buffer.size();++i)
251 auto coeffs = GetAmbiIdentityRow(i);
252 ComputePanGains(target.Main, coeffs.data(), slot->Params.Gain, mChans[i].TargetGains);
256 void VmorpherState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
258 /* Following the EFX specification for a conformant implementation which describes
259 * the effect as a pair of 4-band formant filters blended together using an LFO.
261 for(size_t base{0u};base < samplesToDo;)
263 alignas(16) ALfloat lfo[MAX_UPDATE_SAMPLES];
264 const size_t td{minz(MAX_UPDATE_SAMPLES, samplesToDo-base)};
266 mGetSamples(lfo, mIndex, mStep, td);
267 mIndex += static_cast<ALuint>(mStep * td);
268 mIndex &= WAVEFORM_FRACMASK;
270 auto chandata = std::addressof(mChans[0]);
271 for(const auto &input : samplesIn)
273 std::fill_n(std::begin(mSampleBufferA), td, 0.0f);
274 std::fill_n(std::begin(mSampleBufferB), td, 0.0f);
276 auto& vowelA = chandata->Formants[VOWEL_A_INDEX];
277 auto& vowelB = chandata->Formants[VOWEL_B_INDEX];
279 /* Process first vowel. */
280 vowelA[0].process(&input[base], mSampleBufferA, td);
281 vowelA[1].process(&input[base], mSampleBufferA, td);
282 vowelA[2].process(&input[base], mSampleBufferA, td);
283 vowelA[3].process(&input[base], mSampleBufferA, td);
285 /* Process second vowel. */
286 vowelB[0].process(&input[base], mSampleBufferB, td);
287 vowelB[1].process(&input[base], mSampleBufferB, td);
288 vowelB[2].process(&input[base], mSampleBufferB, td);
289 vowelB[3].process(&input[base], mSampleBufferB, td);
291 alignas(16) ALfloat blended[MAX_UPDATE_SAMPLES];
292 for(size_t i{0u};i < td;i++)
293 blended[i] = lerp(mSampleBufferA[i], mSampleBufferB[i], lfo[i]);
295 /* Now, mix the processed sound data to the output. */
296 MixSamples({blended, td}, samplesOut, chandata->CurrentGains, chandata->TargetGains,
297 samplesToDo-base, base);
298 ++chandata;
301 base += td;
306 void Vmorpher_setParami(EffectProps* props, ALCcontext *context, ALenum param, ALint val)
308 switch(param)
310 case AL_VOCAL_MORPHER_WAVEFORM:
311 if(!(val >= AL_VOCAL_MORPHER_MIN_WAVEFORM && val <= AL_VOCAL_MORPHER_MAX_WAVEFORM))
312 SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher waveform out of range");
313 props->Vmorpher.Waveform = val;
314 break;
316 case AL_VOCAL_MORPHER_PHONEMEA:
317 if(!(val >= AL_VOCAL_MORPHER_MIN_PHONEMEA && val <= AL_VOCAL_MORPHER_MAX_PHONEMEA))
318 SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher phoneme-a out of range");
319 props->Vmorpher.PhonemeA = val;
320 break;
322 case AL_VOCAL_MORPHER_PHONEMEB:
323 if(!(val >= AL_VOCAL_MORPHER_MIN_PHONEMEB && val <= AL_VOCAL_MORPHER_MAX_PHONEMEB))
324 SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher phoneme-b out of range");
325 props->Vmorpher.PhonemeB = val;
326 break;
328 case AL_VOCAL_MORPHER_PHONEMEA_COARSE_TUNING:
329 if(!(val >= AL_VOCAL_MORPHER_MIN_PHONEMEA_COARSE_TUNING && val <= AL_VOCAL_MORPHER_MAX_PHONEMEA_COARSE_TUNING))
330 SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher phoneme-a coarse tuning out of range");
331 props->Vmorpher.PhonemeACoarseTuning = val;
332 break;
334 case AL_VOCAL_MORPHER_PHONEMEB_COARSE_TUNING:
335 if(!(val >= AL_VOCAL_MORPHER_MIN_PHONEMEB_COARSE_TUNING && val <= AL_VOCAL_MORPHER_MAX_PHONEMEB_COARSE_TUNING))
336 SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher phoneme-b coarse tuning out of range");
337 props->Vmorpher.PhonemeBCoarseTuning = val;
338 break;
340 default:
341 context->setError(AL_INVALID_ENUM, "Invalid vocal morpher integer property 0x%04x",
342 param);
345 void Vmorpher_setParamiv(EffectProps*, ALCcontext *context, ALenum param, const ALint*)
346 { context->setError(AL_INVALID_ENUM, "Invalid vocal morpher integer-vector property 0x%04x", param); }
347 void Vmorpher_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
349 switch(param)
351 case AL_VOCAL_MORPHER_RATE:
352 if(!(val >= AL_VOCAL_MORPHER_MIN_RATE && val <= AL_VOCAL_MORPHER_MAX_RATE))
353 SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher rate out of range");
354 props->Vmorpher.Rate = val;
355 break;
357 default:
358 context->setError(AL_INVALID_ENUM, "Invalid vocal morpher float property 0x%04x",
359 param);
362 void Vmorpher_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
363 { Vmorpher_setParamf(props, context, param, vals[0]); }
365 void Vmorpher_getParami(const EffectProps* props, ALCcontext *context, ALenum param, ALint* val)
367 switch(param)
369 case AL_VOCAL_MORPHER_PHONEMEA:
370 *val = props->Vmorpher.PhonemeA;
371 break;
373 case AL_VOCAL_MORPHER_PHONEMEB:
374 *val = props->Vmorpher.PhonemeB;
375 break;
377 case AL_VOCAL_MORPHER_PHONEMEA_COARSE_TUNING:
378 *val = props->Vmorpher.PhonemeACoarseTuning;
379 break;
381 case AL_VOCAL_MORPHER_PHONEMEB_COARSE_TUNING:
382 *val = props->Vmorpher.PhonemeBCoarseTuning;
383 break;
385 case AL_VOCAL_MORPHER_WAVEFORM:
386 *val = props->Vmorpher.Waveform;
387 break;
389 default:
390 context->setError(AL_INVALID_ENUM, "Invalid vocal morpher integer property 0x%04x",
391 param);
394 void Vmorpher_getParamiv(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
395 { context->setError(AL_INVALID_ENUM, "Invalid vocal morpher integer-vector property 0x%04x", param); }
396 void Vmorpher_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
398 switch(param)
400 case AL_VOCAL_MORPHER_RATE:
401 *val = props->Vmorpher.Rate;
402 break;
404 default:
405 context->setError(AL_INVALID_ENUM, "Invalid vocal morpher float property 0x%04x",
406 param);
409 void Vmorpher_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
410 { Vmorpher_getParamf(props, context, param, vals); }
412 DEFINE_ALEFFECT_VTABLE(Vmorpher);
415 struct VmorpherStateFactory final : public EffectStateFactory {
416 EffectState *create() override { return new VmorpherState{}; }
417 EffectProps getDefaultProps() const noexcept override;
418 const EffectVtable *getEffectVtable() const noexcept override { return &Vmorpher_vtable; }
421 EffectProps VmorpherStateFactory::getDefaultProps() const noexcept
423 EffectProps props{};
424 props.Vmorpher.Rate = AL_VOCAL_MORPHER_DEFAULT_RATE;
425 props.Vmorpher.PhonemeA = AL_VOCAL_MORPHER_DEFAULT_PHONEMEA;
426 props.Vmorpher.PhonemeB = AL_VOCAL_MORPHER_DEFAULT_PHONEMEB;
427 props.Vmorpher.PhonemeACoarseTuning = AL_VOCAL_MORPHER_DEFAULT_PHONEMEA_COARSE_TUNING;
428 props.Vmorpher.PhonemeBCoarseTuning = AL_VOCAL_MORPHER_DEFAULT_PHONEMEB_COARSE_TUNING;
429 props.Vmorpher.Waveform = AL_VOCAL_MORPHER_DEFAULT_WAVEFORM;
430 return props;
433 } // namespace
435 EffectStateFactory *VmorpherStateFactory_getFactory()
437 static VmorpherStateFactory VmorpherFactory{};
438 return &VmorpherFactory;