Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting 16Bit PCM Values into -1 to 1 values

This is my first post, so I hope someone can help!

I am reading in audio data (in CoreAudio) using the AudioFileReadPackets function, this code is working correctly and loads the 16 bit PCM values into a buffer.

The first sample value is this: '65491' (There is silence at the beginning of this audio). I understand that this is an Unsigned integer, so my question is, how to convert this value to a range of -1 to 1.

Currently I am dividing the sample value by 32768.0 into a float variable, like so...

    for (UInt32 i = 0; i < packetCount; i++){ 
            
            sample = *(audioData + i);
    
            
            //turn it into the range -1.0 - 1.0
            monoFloatDataLeft[i] = (float)sample / 32768.0;
            
        }

However, for the sample given above (as example) this results in an output of '1.998626709' which is not zero (as it should be for silence)?

Saying this, when I look at a sample much later on in the file, the value of which i know to be around the '0.3' mark, the result of the algorithm comes out at '0.311584473' which i believe to be correct?

So why are the first samples not being read as zero, as i know them to be?

like image 991
Sammio2 Avatar asked Nov 29 '25 03:11

Sammio2


1 Answers

You need to subtract 32768 from your unsigned data first, so it's 0 centered.

like image 74
Tyler Eaves Avatar answered Dec 02 '25 02:12

Tyler Eaves



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!