3
votes

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));
}
5
"it's not working" is not a good description of the problem - and your sample code is looping without taking any account of the changes to temp... It's not clear what you're trying to achieve, either. Sometimes you're using each byte as a separate value, and sometimes not...Jon Skeet
I mean it is working but it is giving me all the 0 into an temp arrayuser123_456
That's why I asked..I need to have at the end array of bytes beetween [-1,1] if that is possibleuser123_456
Bytes are unsigned, and only integer values. You're really not explaining clearly what you want.Jon Skeet
You're still not being clear about what you want in the byte array. Do you understand that if you're trying to store float values, each float will take 4 bytes? And that in the code you've given for reading from data, biggest will always be positive, and at most 255? It feels like you're fundamentally missing how bytes and floats work...Jon Skeet

5 Answers

17
votes

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:

   byte | 01 02 | 03 04 | 05 06 | 07 08 | 09 10 | 11 12 | ...
channel |  Left | Right | Left  | Right | Left |  Right | ...
  frame |     First     |    Second     |     Third     | ...
 sample | 1st L | 1st R | 2nd L | 2nd R | 3rd L | 3rd R | ... etc.

It's important here to take note of a few things:

  1. Since the audio data is 16-bit, a single sample from a single channel is a short (2 bytes), not an int (4 bytes), with a value in the range -32768 to 32767.
  2. This data is in little-endian representation, and unless your architecture is also little-endian, you can't use the .NET BitConverter class for the conversion.
  3. We don't have to split the data into per-channel streams, because we are normalizing both channels based on the single highest value from either channel.
  4. Converting a floating-point value to an integer value will result in quantization errors, so you probably want to use some sort of dithering (which is an entire topic in its own right).

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 short from a byte[] and vice-versa:

short GetShortFromLittleEndianBytes(byte[] data, int startIndex)
{
    return (short)((data[startIndex + 1] << 8)
         | data[startIndex]);
}

byte[] GetLittleEndianBytesFromShort(short data)
{
    byte[] b = new byte[2];
    b[0] = (byte)data;
    b[1] = (byte)(data >> 8 & 0xFF);
    return b;
}

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:

byte[] input = ar.ReadData();  // the function you used above
float biggest = -32768F;
float sample;
for (int i = 0; i < input.Length; i += 2)
{
    sample = (float)GetShortFromLittleEndianBytes(input, i);
    if (sample > biggest) biggest = sample;
}

At this point, biggest contains the largest value from our audio data. Now to perform the actual normalization, we subtract biggest from 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.

float offset = 32767 - biggest;

float[] data = new float[input.length / 2];
for (int i = 0; i < input.Length; i += 2)
{
    data[i / 2] = (float)GetShortFromLittleEndianBytes(input, i) + offset;
}

The last step is to convert the samples from floating-point to integer values, and store them as little-endian shorts.

byte[] output = new byte[input.Length];
for (int i = 0; i < output.Length; i += 2)
{
    byte[] tmp = GetLittleEndianBytesFromShort(Convert.ToInt16(data[i / 2]));
    output[i] = tmp[0];
    output[i + 1] = tmp[1];
}

And we're done! Now you can send the output byte 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 your short to byte[] 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.

2
votes

This works:

float number = 0.43f;
byte[] array = BitConverter.GetBytes(number);

What does not work for you?

1
votes
if (Math.Abs(sample) > biggest) biggest = sample;

I would change this to:

if (Math.Abs(sample) > biggest) biggest = Math.Abs(sample);

Because if the biggest value is negative you will multiply all values with a negative.

0
votes

You can use Buffer.BlockCopy like this:

float[] floats = new float[] { 0.43f, 0.45f, 0.47f };
byte[] result = new byte[sizeof(float) * floats.Length];
Buffer.BlockCopy(floats, 0, result, 0, result.Length);
0
votes

You can change temp to a list of byte arrays to avoid overwriting it all the time.

    byte[] data = new byte[] { 1, 3, 5, 7, 9 };  // sample data
    IList<byte[]> temp = new List<byte[]>(data.Length);
    float biggest = 0; ;

    for (int i = 0; i < data.Length; i++)
    {
        if (data[i] > biggest)
            biggest = data[i];
    }

    for (int i = 0; i < data.Length; i++)
    {
        temp.Add(BitConverter.GetBytes(data[i] * (1 / biggest)));
    }