How to normalize audio¶
One channel¶
Input/Output data: See how to pass data to KFR
univector<float> audio;
Code
audio /= absmaxof(audio);
- absmaxoffunction 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);
- concatenatefunction concatenates two arrays as if they were written sequentially in memory (but without any actual memory copies)
- absmaxoffunction calculates the absolute maximum of the concatenated arrays
- packfunction combines two arrays as if it were a single array containing pairs of values
- operator/divides pairs by the peak value
- unpackfunction unpacks the pairs to two target arrays