How to normalize audio¶
One channel¶
Input/Output data: See how to pass data to KFR
univector<float> audio;
Code
audio /= absmaxof(audio);
absmaxof
function calculates the absolute maximum of the given array (max(abs(x[i])...)
)operator/=
performs inplace division for array
Equivalent pure C++ code
float absmax = 0;
for (size_t i = 0; i < audio.size(); ++i)
{
absmax = std::max(absmax, std::abs(audio[i]));
}
for (size_t i = 0; i < audio.size(); ++i)
{
audio[i] = audio[i] / absmax;
}
Stereo audio¶
Input/Output data:
univector<univector<float>, 2> stereo;
Code
const float peak = absmaxof(concatenate(stereo[0], stereo[1]));
unpack(stereo[0], stereo[1]) = pack(stereo[0], stereo[1]) / pack(peak, peak);
concatenate
function concatenates two arrays as if they were written sequentially in memory (but without any actual memory copies)absmaxof
function calculates the absolute maximum of the concatenated arrayspack
function combines two arrays as if it were a single array containing pairs of valuesoperator/
divides pairs by the peak valueunpack
function unpacks the pairs to two target arrays