vmorpher.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #include "config.h"
  21. #include <cmath>
  22. #include <cstdlib>
  23. #include <algorithm>
  24. #include <functional>
  25. #include "al/auxeffectslot.h"
  26. #include "alcmain.h"
  27. #include "alcontext.h"
  28. #include "alu.h"
  29. namespace {
  30. #define MAX_UPDATE_SAMPLES 128
  31. #define NUM_FORMANTS 4
  32. #define NUM_FILTERS 2
  33. #define Q_FACTOR 5.0f
  34. #define VOWEL_A_INDEX 0
  35. #define VOWEL_B_INDEX 1
  36. #define WAVEFORM_FRACBITS 24
  37. #define WAVEFORM_FRACONE (1<<WAVEFORM_FRACBITS)
  38. #define WAVEFORM_FRACMASK (WAVEFORM_FRACONE-1)
  39. inline float Sin(ALuint index)
  40. {
  41. constexpr float scale{al::MathDefs<float>::Tau() / WAVEFORM_FRACONE};
  42. return std::sin(static_cast<float>(index) * scale)*0.5f + 0.5f;
  43. }
  44. inline float Saw(ALuint index)
  45. { return static_cast<float>(index) / float{WAVEFORM_FRACONE}; }
  46. inline float Triangle(ALuint index)
  47. { return std::fabs(static_cast<float>(index)*(2.0f/WAVEFORM_FRACONE) - 1.0f); }
  48. inline float Half(ALuint) { return 0.5f; }
  49. template<float (&func)(ALuint)>
  50. void Oscillate(float *RESTRICT dst, ALuint index, const ALuint step, size_t todo)
  51. {
  52. for(size_t i{0u};i < todo;i++)
  53. {
  54. index += step;
  55. index &= WAVEFORM_FRACMASK;
  56. dst[i] = func(index);
  57. }
  58. }
  59. struct FormantFilter
  60. {
  61. ALfloat mCoeff{0.0f};
  62. ALfloat mGain{1.0f};
  63. ALfloat mS1{0.0f};
  64. ALfloat mS2{0.0f};
  65. FormantFilter() = default;
  66. FormantFilter(ALfloat f0norm, ALfloat gain)
  67. : mCoeff{std::tan(al::MathDefs<float>::Pi() * f0norm)}, mGain{gain}
  68. { }
  69. inline void process(const ALfloat *samplesIn, ALfloat *samplesOut, const size_t numInput)
  70. {
  71. /* A state variable filter from a topology-preserving transform.
  72. * Based on a talk given by Ivan Cohen: https://www.youtube.com/watch?v=esjHXGPyrhg
  73. */
  74. const ALfloat g{mCoeff};
  75. const ALfloat gain{mGain};
  76. const ALfloat h{1.0f / (1.0f + (g/Q_FACTOR) + (g*g))};
  77. ALfloat s1{mS1};
  78. ALfloat s2{mS2};
  79. for(size_t i{0u};i < numInput;i++)
  80. {
  81. const ALfloat H{(samplesIn[i] - (1.0f/Q_FACTOR + g)*s1 - s2)*h};
  82. const ALfloat B{g*H + s1};
  83. const ALfloat L{g*B + s2};
  84. s1 = g*H + B;
  85. s2 = g*B + L;
  86. // Apply peak and accumulate samples.
  87. samplesOut[i] += B * gain;
  88. }
  89. mS1 = s1;
  90. mS2 = s2;
  91. }
  92. inline void clear()
  93. {
  94. mS1 = 0.0f;
  95. mS2 = 0.0f;
  96. }
  97. };
  98. struct VmorpherState final : public EffectState {
  99. struct {
  100. /* Effect parameters */
  101. FormantFilter Formants[NUM_FILTERS][NUM_FORMANTS];
  102. /* Effect gains for each channel */
  103. ALfloat CurrentGains[MAX_OUTPUT_CHANNELS]{};
  104. ALfloat TargetGains[MAX_OUTPUT_CHANNELS]{};
  105. } mChans[MAX_AMBI_CHANNELS];
  106. void (*mGetSamples)(float*RESTRICT, ALuint, const ALuint, size_t){};
  107. ALuint mIndex{0};
  108. ALuint mStep{1};
  109. /* Effects buffers */
  110. ALfloat mSampleBufferA[MAX_UPDATE_SAMPLES]{};
  111. ALfloat mSampleBufferB[MAX_UPDATE_SAMPLES]{};
  112. ALboolean deviceUpdate(const ALCdevice *device) override;
  113. void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
  114. void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut) override;
  115. static std::array<FormantFilter,4> getFiltersByPhoneme(ALenum phoneme, ALfloat frequency, ALfloat pitch);
  116. DEF_NEWDEL(VmorpherState)
  117. };
  118. std::array<FormantFilter,4> VmorpherState::getFiltersByPhoneme(ALenum phoneme, ALfloat frequency, ALfloat pitch)
  119. {
  120. /* Using soprano formant set of values to
  121. * better match mid-range frequency space.
  122. *
  123. * See: https://www.classes.cs.uchicago.edu/archive/1999/spring/CS295/Computing_Resources/Csound/CsManual3.48b1.HTML/Appendices/table3.html
  124. */
  125. switch(phoneme)
  126. {
  127. case AL_VOCAL_MORPHER_PHONEME_A:
  128. return {{
  129. {( 800 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f, 0 / 20.0f); */
  130. {(1150 * pitch) / frequency, 0.501187f}, /* std::pow(10.0f, -6 / 20.0f); */
  131. {(2900 * pitch) / frequency, 0.025118f}, /* std::pow(10.0f, -32 / 20.0f); */
  132. {(3900 * pitch) / frequency, 0.100000f} /* std::pow(10.0f, -20 / 20.0f); */
  133. }};
  134. case AL_VOCAL_MORPHER_PHONEME_E:
  135. return {{
  136. {( 350 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f, 0 / 20.0f); */
  137. {(2000 * pitch) / frequency, 0.100000f}, /* std::pow(10.0f, -20 / 20.0f); */
  138. {(2800 * pitch) / frequency, 0.177827f}, /* std::pow(10.0f, -15 / 20.0f); */
  139. {(3600 * pitch) / frequency, 0.009999f} /* std::pow(10.0f, -40 / 20.0f); */
  140. }};
  141. case AL_VOCAL_MORPHER_PHONEME_I:
  142. return {{
  143. {( 270 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f, 0 / 20.0f); */
  144. {(2140 * pitch) / frequency, 0.251188f}, /* std::pow(10.0f, -12 / 20.0f); */
  145. {(2950 * pitch) / frequency, 0.050118f}, /* std::pow(10.0f, -26 / 20.0f); */
  146. {(3900 * pitch) / frequency, 0.050118f} /* std::pow(10.0f, -26 / 20.0f); */
  147. }};
  148. case AL_VOCAL_MORPHER_PHONEME_O:
  149. return {{
  150. {( 450 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f, 0 / 20.0f); */
  151. {( 800 * pitch) / frequency, 0.281838f}, /* std::pow(10.0f, -11 / 20.0f); */
  152. {(2830 * pitch) / frequency, 0.079432f}, /* std::pow(10.0f, -22 / 20.0f); */
  153. {(3800 * pitch) / frequency, 0.079432f} /* std::pow(10.0f, -22 / 20.0f); */
  154. }};
  155. case AL_VOCAL_MORPHER_PHONEME_U:
  156. return {{
  157. {( 325 * pitch) / frequency, 1.000000f}, /* std::pow(10.0f, 0 / 20.0f); */
  158. {( 700 * pitch) / frequency, 0.158489f}, /* std::pow(10.0f, -16 / 20.0f); */
  159. {(2700 * pitch) / frequency, 0.017782f}, /* std::pow(10.0f, -35 / 20.0f); */
  160. {(3800 * pitch) / frequency, 0.009999f} /* std::pow(10.0f, -40 / 20.0f); */
  161. }};
  162. }
  163. return {};
  164. }
  165. ALboolean VmorpherState::deviceUpdate(const ALCdevice* /*device*/)
  166. {
  167. for(auto &e : mChans)
  168. {
  169. std::for_each(std::begin(e.Formants[VOWEL_A_INDEX]), std::end(e.Formants[VOWEL_A_INDEX]),
  170. std::mem_fn(&FormantFilter::clear));
  171. std::for_each(std::begin(e.Formants[VOWEL_B_INDEX]), std::end(e.Formants[VOWEL_B_INDEX]),
  172. std::mem_fn(&FormantFilter::clear));
  173. std::fill(std::begin(e.CurrentGains), std::end(e.CurrentGains), 0.0f);
  174. }
  175. return AL_TRUE;
  176. }
  177. void VmorpherState::update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
  178. {
  179. const ALCdevice *device{context->mDevice.get()};
  180. const ALfloat frequency{static_cast<ALfloat>(device->Frequency)};
  181. const ALfloat step{props->Vmorpher.Rate / frequency};
  182. mStep = fastf2u(clampf(step*WAVEFORM_FRACONE, 0.0f, ALfloat{WAVEFORM_FRACONE-1}));
  183. if(mStep == 0)
  184. mGetSamples = Oscillate<Half>;
  185. else if(props->Vmorpher.Waveform == AL_VOCAL_MORPHER_WAVEFORM_SINUSOID)
  186. mGetSamples = Oscillate<Sin>;
  187. else if(props->Vmorpher.Waveform == AL_VOCAL_MORPHER_WAVEFORM_SAWTOOTH)
  188. mGetSamples = Oscillate<Saw>;
  189. else /*if(props->Vmorpher.Waveform == AL_VOCAL_MORPHER_WAVEFORM_TRIANGLE)*/
  190. mGetSamples = Oscillate<Triangle>;
  191. const ALfloat pitchA{std::pow(2.0f,
  192. static_cast<float>(props->Vmorpher.PhonemeACoarseTuning) / 12.0f)};
  193. const ALfloat pitchB{std::pow(2.0f,
  194. static_cast<float>(props->Vmorpher.PhonemeBCoarseTuning) / 12.0f)};
  195. auto vowelA = getFiltersByPhoneme(props->Vmorpher.PhonemeA, frequency, pitchA);
  196. auto vowelB = getFiltersByPhoneme(props->Vmorpher.PhonemeB, frequency, pitchB);
  197. /* Copy the filter coefficients to the input channels. */
  198. for(size_t i{0u};i < slot->Wet.Buffer.size();++i)
  199. {
  200. std::copy(vowelA.begin(), vowelA.end(), std::begin(mChans[i].Formants[VOWEL_A_INDEX]));
  201. std::copy(vowelB.begin(), vowelB.end(), std::begin(mChans[i].Formants[VOWEL_B_INDEX]));
  202. }
  203. mOutTarget = target.Main->Buffer;
  204. for(size_t i{0u};i < slot->Wet.Buffer.size();++i)
  205. {
  206. auto coeffs = GetAmbiIdentityRow(i);
  207. ComputePanGains(target.Main, coeffs.data(), slot->Params.Gain, mChans[i].TargetGains);
  208. }
  209. }
  210. void VmorpherState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  211. {
  212. /* Following the EFX specification for a conformant implementation which describes
  213. * the effect as a pair of 4-band formant filters blended together using an LFO.
  214. */
  215. for(size_t base{0u};base < samplesToDo;)
  216. {
  217. alignas(16) ALfloat lfo[MAX_UPDATE_SAMPLES];
  218. const size_t td{minz(MAX_UPDATE_SAMPLES, samplesToDo-base)};
  219. mGetSamples(lfo, mIndex, mStep, td);
  220. mIndex += static_cast<ALuint>(mStep * td);
  221. mIndex &= WAVEFORM_FRACMASK;
  222. auto chandata = std::addressof(mChans[0]);
  223. for(const auto &input : samplesIn)
  224. {
  225. std::fill_n(std::begin(mSampleBufferA), td, 0.0f);
  226. std::fill_n(std::begin(mSampleBufferB), td, 0.0f);
  227. auto& vowelA = chandata->Formants[VOWEL_A_INDEX];
  228. auto& vowelB = chandata->Formants[VOWEL_B_INDEX];
  229. /* Process first vowel. */
  230. vowelA[0].process(&input[base], mSampleBufferA, td);
  231. vowelA[1].process(&input[base], mSampleBufferA, td);
  232. vowelA[2].process(&input[base], mSampleBufferA, td);
  233. vowelA[3].process(&input[base], mSampleBufferA, td);
  234. /* Process second vowel. */
  235. vowelB[0].process(&input[base], mSampleBufferB, td);
  236. vowelB[1].process(&input[base], mSampleBufferB, td);
  237. vowelB[2].process(&input[base], mSampleBufferB, td);
  238. vowelB[3].process(&input[base], mSampleBufferB, td);
  239. alignas(16) ALfloat blended[MAX_UPDATE_SAMPLES];
  240. for(size_t i{0u};i < td;i++)
  241. blended[i] = lerp(mSampleBufferA[i], mSampleBufferB[i], lfo[i]);
  242. /* Now, mix the processed sound data to the output. */
  243. MixSamples({blended, td}, samplesOut, chandata->CurrentGains, chandata->TargetGains,
  244. samplesToDo-base, base);
  245. ++chandata;
  246. }
  247. base += td;
  248. }
  249. }
  250. void Vmorpher_setParami(EffectProps* props, ALCcontext *context, ALenum param, ALint val)
  251. {
  252. switch(param)
  253. {
  254. case AL_VOCAL_MORPHER_WAVEFORM:
  255. if(!(val >= AL_VOCAL_MORPHER_MIN_WAVEFORM && val <= AL_VOCAL_MORPHER_MAX_WAVEFORM))
  256. SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher waveform out of range");
  257. props->Vmorpher.Waveform = val;
  258. break;
  259. case AL_VOCAL_MORPHER_PHONEMEA:
  260. if(!(val >= AL_VOCAL_MORPHER_MIN_PHONEMEA && val <= AL_VOCAL_MORPHER_MAX_PHONEMEA))
  261. SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher phoneme-a out of range");
  262. props->Vmorpher.PhonemeA = val;
  263. break;
  264. case AL_VOCAL_MORPHER_PHONEMEB:
  265. if(!(val >= AL_VOCAL_MORPHER_MIN_PHONEMEB && val <= AL_VOCAL_MORPHER_MAX_PHONEMEB))
  266. SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher phoneme-b out of range");
  267. props->Vmorpher.PhonemeB = val;
  268. break;
  269. case AL_VOCAL_MORPHER_PHONEMEA_COARSE_TUNING:
  270. if(!(val >= AL_VOCAL_MORPHER_MIN_PHONEMEA_COARSE_TUNING && val <= AL_VOCAL_MORPHER_MAX_PHONEMEA_COARSE_TUNING))
  271. SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher phoneme-a coarse tuning out of range");
  272. props->Vmorpher.PhonemeACoarseTuning = val;
  273. break;
  274. case AL_VOCAL_MORPHER_PHONEMEB_COARSE_TUNING:
  275. if(!(val >= AL_VOCAL_MORPHER_MIN_PHONEMEB_COARSE_TUNING && val <= AL_VOCAL_MORPHER_MAX_PHONEMEB_COARSE_TUNING))
  276. SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher phoneme-b coarse tuning out of range");
  277. props->Vmorpher.PhonemeBCoarseTuning = val;
  278. break;
  279. default:
  280. context->setError(AL_INVALID_ENUM, "Invalid vocal morpher integer property 0x%04x",
  281. param);
  282. }
  283. }
  284. void Vmorpher_setParamiv(EffectProps*, ALCcontext *context, ALenum param, const ALint*)
  285. { context->setError(AL_INVALID_ENUM, "Invalid vocal morpher integer-vector property 0x%04x", param); }
  286. void Vmorpher_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
  287. {
  288. switch(param)
  289. {
  290. case AL_VOCAL_MORPHER_RATE:
  291. if(!(val >= AL_VOCAL_MORPHER_MIN_RATE && val <= AL_VOCAL_MORPHER_MAX_RATE))
  292. SETERR_RETURN(context, AL_INVALID_VALUE,, "Vocal morpher rate out of range");
  293. props->Vmorpher.Rate = val;
  294. break;
  295. default:
  296. context->setError(AL_INVALID_ENUM, "Invalid vocal morpher float property 0x%04x",
  297. param);
  298. }
  299. }
  300. void Vmorpher_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
  301. { Vmorpher_setParamf(props, context, param, vals[0]); }
  302. void Vmorpher_getParami(const EffectProps* props, ALCcontext *context, ALenum param, ALint* val)
  303. {
  304. switch(param)
  305. {
  306. case AL_VOCAL_MORPHER_PHONEMEA:
  307. *val = props->Vmorpher.PhonemeA;
  308. break;
  309. case AL_VOCAL_MORPHER_PHONEMEB:
  310. *val = props->Vmorpher.PhonemeB;
  311. break;
  312. case AL_VOCAL_MORPHER_PHONEMEA_COARSE_TUNING:
  313. *val = props->Vmorpher.PhonemeACoarseTuning;
  314. break;
  315. case AL_VOCAL_MORPHER_PHONEMEB_COARSE_TUNING:
  316. *val = props->Vmorpher.PhonemeBCoarseTuning;
  317. break;
  318. case AL_VOCAL_MORPHER_WAVEFORM:
  319. *val = props->Vmorpher.Waveform;
  320. break;
  321. default:
  322. context->setError(AL_INVALID_ENUM, "Invalid vocal morpher integer property 0x%04x",
  323. param);
  324. }
  325. }
  326. void Vmorpher_getParamiv(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
  327. { context->setError(AL_INVALID_ENUM, "Invalid vocal morpher integer-vector property 0x%04x", param); }
  328. void Vmorpher_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
  329. {
  330. switch(param)
  331. {
  332. case AL_VOCAL_MORPHER_RATE:
  333. *val = props->Vmorpher.Rate;
  334. break;
  335. default:
  336. context->setError(AL_INVALID_ENUM, "Invalid vocal morpher float property 0x%04x",
  337. param);
  338. }
  339. }
  340. void Vmorpher_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
  341. { Vmorpher_getParamf(props, context, param, vals); }
  342. DEFINE_ALEFFECT_VTABLE(Vmorpher);
  343. struct VmorpherStateFactory final : public EffectStateFactory {
  344. EffectState *create() override { return new VmorpherState{}; }
  345. EffectProps getDefaultProps() const noexcept override;
  346. const EffectVtable *getEffectVtable() const noexcept override { return &Vmorpher_vtable; }
  347. };
  348. EffectProps VmorpherStateFactory::getDefaultProps() const noexcept
  349. {
  350. EffectProps props{};
  351. props.Vmorpher.Rate = AL_VOCAL_MORPHER_DEFAULT_RATE;
  352. props.Vmorpher.PhonemeA = AL_VOCAL_MORPHER_DEFAULT_PHONEMEA;
  353. props.Vmorpher.PhonemeB = AL_VOCAL_MORPHER_DEFAULT_PHONEMEB;
  354. props.Vmorpher.PhonemeACoarseTuning = AL_VOCAL_MORPHER_DEFAULT_PHONEMEA_COARSE_TUNING;
  355. props.Vmorpher.PhonemeBCoarseTuning = AL_VOCAL_MORPHER_DEFAULT_PHONEMEB_COARSE_TUNING;
  356. props.Vmorpher.Waveform = AL_VOCAL_MORPHER_DEFAULT_WAVEFORM;
  357. return props;
  358. }
  359. } // namespace
  360. EffectStateFactory *VmorpherStateFactory_getFactory()
  361. {
  362. static VmorpherStateFactory VmorpherFactory{};
  363. return &VmorpherFactory;
  364. }