2 * This file is part of the OpenAL Soft cross platform audio library
4 * Copyright (C) 2019 by Anis A. Hireche
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
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.
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.
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.
41 #include "alc/effects/base.h"
43 #include "alnumbers.h"
44 #include "alnumeric.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"
58 using uint
= unsigned int;
60 #define MAX_UPDATE_SAMPLES 256
61 #define NUM_FORMANTS 4
65 #define VOWEL_A_INDEX 0
66 #define VOWEL_B_INDEX 1
68 #define WAVEFORM_FRACBITS 24
69 #define WAVEFORM_FRACONE (1<<WAVEFORM_FRACBITS)
70 #define WAVEFORM_FRACMASK (WAVEFORM_FRACONE-1)
72 inline float Sin(uint index
)
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
;
78 inline float Saw(uint index
)
79 { return static_cast<float>(index
) / float{WAVEFORM_FRACONE
}; }
81 inline float Triangle(uint index
)
82 { return std::fabs(static_cast<float>(index
)*(2.0f
/WAVEFORM_FRACONE
) - 1.0f
); }
84 inline float Half(uint
) { return 0.5f
; }
86 template<float (&func
)(uint
)>
87 void Oscillate(float *RESTRICT dst
, uint index
, const uint step
, size_t todo
)
89 for(size_t i
{0u};i
< todo
;i
++)
92 index
&= WAVEFORM_FRACMASK
;
104 FormantFilter() = default;
105 FormantFilter(float f0norm
, float gain
)
106 : mCoeff
{std::tan(al::numbers::pi_v
<float> * f0norm
)}, mGain
{gain
}
109 inline void process(const float *samplesIn
, float *samplesOut
, const size_t numInput
)
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
114 const float g
{mCoeff
};
115 const float gain
{mGain
};
116 const float h
{1.0f
/ (1.0f
+ (g
/Q_FACTOR
) + (g
*g
))};
120 for(size_t i
{0u};i
< numInput
;i
++)
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
};
129 // Apply peak and accumulate samples.
130 samplesOut
[i
] += B
* gain
;
144 struct VmorpherState final
: public EffectState
{
146 /* Effect parameters */
147 FormantFilter Formants
[NUM_FILTERS
][NUM_FORMANTS
];
149 /* Effect gains for each channel */
150 float CurrentGains
[MAX_OUTPUT_CHANNELS
]{};
151 float TargetGains
[MAX_OUTPUT_CHANNELS
]{};
152 } mChans
[MaxAmbiChannels
];
154 void (*mGetSamples
)(float*RESTRICT
, uint
, const uint
, size_t){};
159 /* Effects buffers */
160 alignas(16) float mSampleBufferA
[MAX_UPDATE_SAMPLES
]{};
161 alignas(16) float mSampleBufferB
[MAX_UPDATE_SAMPLES
]{};
162 alignas(16) float mLfo
[MAX_UPDATE_SAMPLES
]{};
164 void deviceUpdate(const DeviceBase
*device
, const Buffer
&buffer
) override
;
165 void update(const ContextBase
*context
, const EffectSlot
*slot
, const EffectProps
*props
,
166 const EffectTarget target
) override
;
167 void process(const size_t samplesToDo
, const al::span
<const FloatBufferLine
> samplesIn
,
168 const al::span
<FloatBufferLine
> samplesOut
) override
;
170 static std::array
<FormantFilter
,4> getFiltersByPhoneme(VMorpherPhenome phoneme
,
171 float frequency
, float pitch
);
173 DEF_NEWDEL(VmorpherState
)
176 std::array
<FormantFilter
,4> VmorpherState::getFiltersByPhoneme(VMorpherPhenome phoneme
,
177 float frequency
, float pitch
)
179 /* Using soprano formant set of values to
180 * better match mid-range frequency space.
182 * See: https://www.classes.cs.uchicago.edu/archive/1999/spring/CS295/Computing_Resources/Csound/CsManual3.48b1.HTML/Appendices/table3.html
186 case VMorpherPhenome::A
:
188 {( 800 * pitch
) / frequency
, 1.000000f
}, /* std::pow(10.0f, 0 / 20.0f); */
189 {(1150 * pitch
) / frequency
, 0.501187f
}, /* std::pow(10.0f, -6 / 20.0f); */
190 {(2900 * pitch
) / frequency
, 0.025118f
}, /* std::pow(10.0f, -32 / 20.0f); */
191 {(3900 * pitch
) / frequency
, 0.100000f
} /* std::pow(10.0f, -20 / 20.0f); */
193 case VMorpherPhenome::E
:
195 {( 350 * pitch
) / frequency
, 1.000000f
}, /* std::pow(10.0f, 0 / 20.0f); */
196 {(2000 * pitch
) / frequency
, 0.100000f
}, /* std::pow(10.0f, -20 / 20.0f); */
197 {(2800 * pitch
) / frequency
, 0.177827f
}, /* std::pow(10.0f, -15 / 20.0f); */
198 {(3600 * pitch
) / frequency
, 0.009999f
} /* std::pow(10.0f, -40 / 20.0f); */
200 case VMorpherPhenome::I
:
202 {( 270 * pitch
) / frequency
, 1.000000f
}, /* std::pow(10.0f, 0 / 20.0f); */
203 {(2140 * pitch
) / frequency
, 0.251188f
}, /* std::pow(10.0f, -12 / 20.0f); */
204 {(2950 * pitch
) / frequency
, 0.050118f
}, /* std::pow(10.0f, -26 / 20.0f); */
205 {(3900 * pitch
) / frequency
, 0.050118f
} /* std::pow(10.0f, -26 / 20.0f); */
207 case VMorpherPhenome::O
:
209 {( 450 * pitch
) / frequency
, 1.000000f
}, /* std::pow(10.0f, 0 / 20.0f); */
210 {( 800 * pitch
) / frequency
, 0.281838f
}, /* std::pow(10.0f, -11 / 20.0f); */
211 {(2830 * pitch
) / frequency
, 0.079432f
}, /* std::pow(10.0f, -22 / 20.0f); */
212 {(3800 * pitch
) / frequency
, 0.079432f
} /* std::pow(10.0f, -22 / 20.0f); */
214 case VMorpherPhenome::U
:
216 {( 325 * pitch
) / frequency
, 1.000000f
}, /* std::pow(10.0f, 0 / 20.0f); */
217 {( 700 * pitch
) / frequency
, 0.158489f
}, /* std::pow(10.0f, -16 / 20.0f); */
218 {(2700 * pitch
) / frequency
, 0.017782f
}, /* std::pow(10.0f, -35 / 20.0f); */
219 {(3800 * pitch
) / frequency
, 0.009999f
} /* std::pow(10.0f, -40 / 20.0f); */
228 void VmorpherState::deviceUpdate(const DeviceBase
*, const Buffer
&)
230 for(auto &e
: mChans
)
232 std::for_each(std::begin(e
.Formants
[VOWEL_A_INDEX
]), std::end(e
.Formants
[VOWEL_A_INDEX
]),
233 std::mem_fn(&FormantFilter::clear
));
234 std::for_each(std::begin(e
.Formants
[VOWEL_B_INDEX
]), std::end(e
.Formants
[VOWEL_B_INDEX
]),
235 std::mem_fn(&FormantFilter::clear
));
236 std::fill(std::begin(e
.CurrentGains
), std::end(e
.CurrentGains
), 0.0f
);
240 void VmorpherState::update(const ContextBase
*context
, const EffectSlot
*slot
,
241 const EffectProps
*props
, const EffectTarget target
)
243 const DeviceBase
*device
{context
->mDevice
};
244 const float frequency
{static_cast<float>(device
->Frequency
)};
245 const float step
{props
->Vmorpher
.Rate
/ frequency
};
246 mStep
= fastf2u(clampf(step
*WAVEFORM_FRACONE
, 0.0f
, float{WAVEFORM_FRACONE
-1}));
249 mGetSamples
= Oscillate
<Half
>;
250 else if(props
->Vmorpher
.Waveform
== VMorpherWaveform::Sinusoid
)
251 mGetSamples
= Oscillate
<Sin
>;
252 else if(props
->Vmorpher
.Waveform
== VMorpherWaveform::Triangle
)
253 mGetSamples
= Oscillate
<Triangle
>;
254 else /*if(props->Vmorpher.Waveform == VMorpherWaveform::Sawtooth)*/
255 mGetSamples
= Oscillate
<Saw
>;
257 const float pitchA
{std::pow(2.0f
,
258 static_cast<float>(props
->Vmorpher
.PhonemeACoarseTuning
) / 12.0f
)};
259 const float pitchB
{std::pow(2.0f
,
260 static_cast<float>(props
->Vmorpher
.PhonemeBCoarseTuning
) / 12.0f
)};
262 auto vowelA
= getFiltersByPhoneme(props
->Vmorpher
.PhonemeA
, frequency
, pitchA
);
263 auto vowelB
= getFiltersByPhoneme(props
->Vmorpher
.PhonemeB
, frequency
, pitchB
);
265 /* Copy the filter coefficients to the input channels. */
266 for(size_t i
{0u};i
< slot
->Wet
.Buffer
.size();++i
)
268 std::copy(vowelA
.begin(), vowelA
.end(), std::begin(mChans
[i
].Formants
[VOWEL_A_INDEX
]));
269 std::copy(vowelB
.begin(), vowelB
.end(), std::begin(mChans
[i
].Formants
[VOWEL_B_INDEX
]));
272 mOutTarget
= target
.Main
->Buffer
;
273 auto set_gains
= [slot
,target
](auto &chan
, al::span
<const float,MaxAmbiChannels
> coeffs
)
274 { ComputePanGains(target
.Main
, coeffs
.data(), slot
->Gain
, chan
.TargetGains
); };
275 SetAmbiPanIdentity(std::begin(mChans
), slot
->Wet
.Buffer
.size(), set_gains
);
278 void VmorpherState::process(const size_t samplesToDo
, const al::span
<const FloatBufferLine
> samplesIn
, const al::span
<FloatBufferLine
> samplesOut
)
280 /* Following the EFX specification for a conformant implementation which describes
281 * the effect as a pair of 4-band formant filters blended together using an LFO.
283 for(size_t base
{0u};base
< samplesToDo
;)
285 const size_t td
{minz(MAX_UPDATE_SAMPLES
, samplesToDo
-base
)};
287 mGetSamples(mLfo
, mIndex
, mStep
, td
);
288 mIndex
+= static_cast<uint
>(mStep
* td
);
289 mIndex
&= WAVEFORM_FRACMASK
;
291 auto chandata
= std::begin(mChans
);
292 for(const auto &input
: samplesIn
)
294 auto& vowelA
= chandata
->Formants
[VOWEL_A_INDEX
];
295 auto& vowelB
= chandata
->Formants
[VOWEL_B_INDEX
];
297 /* Process first vowel. */
298 std::fill_n(std::begin(mSampleBufferA
), td
, 0.0f
);
299 vowelA
[0].process(&input
[base
], mSampleBufferA
, td
);
300 vowelA
[1].process(&input
[base
], mSampleBufferA
, td
);
301 vowelA
[2].process(&input
[base
], mSampleBufferA
, td
);
302 vowelA
[3].process(&input
[base
], mSampleBufferA
, td
);
304 /* Process second vowel. */
305 std::fill_n(std::begin(mSampleBufferB
), td
, 0.0f
);
306 vowelB
[0].process(&input
[base
], mSampleBufferB
, td
);
307 vowelB
[1].process(&input
[base
], mSampleBufferB
, td
);
308 vowelB
[2].process(&input
[base
], mSampleBufferB
, td
);
309 vowelB
[3].process(&input
[base
], mSampleBufferB
, td
);
311 alignas(16) float blended
[MAX_UPDATE_SAMPLES
];
312 for(size_t i
{0u};i
< td
;i
++)
313 blended
[i
] = lerp(mSampleBufferA
[i
], mSampleBufferB
[i
], mLfo
[i
]);
315 /* Now, mix the processed sound data to the output. */
316 MixSamples({blended
, td
}, samplesOut
, chandata
->CurrentGains
, chandata
->TargetGains
,
317 samplesToDo
-base
, base
);
326 struct VmorpherStateFactory final
: public EffectStateFactory
{
327 al::intrusive_ptr
<EffectState
> create() override
328 { return al::intrusive_ptr
<EffectState
>{new VmorpherState
{}}; }
333 EffectStateFactory
*VmorpherStateFactory_getFactory()
335 static VmorpherStateFactory VmorpherFactory
{};
336 return &VmorpherFactory
;