Hi all, I am playing an audio file. I read it as a byte[] and then I need to normalize the audio by putting values into range of [-1,1]. I want to then put each float value into a byte[i] array and then put that byte[] back into the playing audio player.
I’ve tried this:
byte[] data = ar.ReadData();
byte[] temp=new byte[data.Length];
float biggest= 0; ;
for (int i = 0; i < data.Length; i++)
{
if (data[i] > biggest)
{
biggest= data[i];
}
}
This part of code should put for example 0.43 int byte[] if that is even possible I tried this but it’s not working:
for (int i = 0; i < data.Length; i++)
{
temp = BitConverter.GetBytes(data[i] * (1 / biggest));
}
In a comment, you stated “I am playing audio file… I read it as byte[] and then I need to normalize audio by putting values into range of [-1,1] and then I need to put that byte[] back into playing audio player“
I am making a big assumption here, but I’m guessing the the data you receive from
ar.ReadData()is a byte array of 2-channel 16-bit/44.1kHz PCM data. (side note: are you using the Alvas.Audio library?) If that is the case, here is how to do what you want.Background
First, a little background. A 2-channel, 16-bit PCM data stream looks like this:
It’s important here to take note of a few things:
short(2 bytes), not anint(4 bytes), with a value in the range -32768 to 32767.BitConverterclass for the conversion.Helper Functions
Before we jump into the actual normalization, let’s make this easier on ourselves by writing a couple of helper functions to get a
shortfrom abyte[]and vice-versa:Normalization
An important distinction should be made here: audio normalization is not the same as statistical normalization. Here we are going to perform peak normalization on our audio data, amplifying the signal by a constant amount so that its peak is at the upper limit. To peak normalize audio data, we first find the largest value, subtract it from the upper limit (for 16-bit PCM data, this is 32767) to get an offset, and then increase each value by this offset.
So, to normalize our audio data, first scan through it to find the peak magnitude:
At this point,
biggestcontains the largest value from our audio data. Now to perform the actual normalization, we subtractbiggestfrom 32767 to get a value which corresponds to the offset from peak of the loudest sample in our audio data. Next we add this offset to each audio sample, effectively increasing the volume of each sample until our loudest sample is at the peak value.The last step is to convert the samples from floating-point to integer values, and store them as little-endian
shorts.And we’re done! Now you can send the
outputbyte array, which contains the normalized PCM data, to your audio player.As a final note, keep in mind that this code isn’t the most efficient; you could combine several of these loops, and you could probably use
Buffer.BlockCopy()for the array copying, as well as modifying yourshorttobyte[]helper function to take a byte array as a parameter and copy the value directly into the array.I didn’t do any of this so as to make it easier to see what’s going on.
And as I mentioned before, you should absolutely read up on dithering, as it will vastly improve the quality of your audio output.
I’ve been working on an audio project myself, so I figured all this out through some trial-and-error; I hope it helps somebody somewhere.