3

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?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Sammio2
  • 7,422
  • 7
  • 34
  • 49
  • Generally speaking +1.0 is not a valid sample value for floating point. This allows lossless conversion to/from float with a simple multiply or divide. – sbooth Mar 09 '11 at 16:46
  • Hi sbooth, thanks, yes I have a further subroutine that alters any +1.0 values to +0.999... before any further processing! – Sammio2 Mar 18 '11 at 20:41
  • See also: http://stackoverflow.com/questions/15087668/how-to-convert-pcm-samples-in-byte-array-as-floating-point-numbers-in-the-range – Brad Dec 27 '13 at 05:03

1 Answers1

1

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

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
  • I'm not sure if what i've done has achieved the same effect, but it seems to be working with the addition of an SInt16 variable 'sampleS' used to pass the value from sample into... (Sorry, i couldn't work out the mini-Markdown formatting to add the code) – Sammio2 Mar 07 '11 at 16:59
  • you can also subtrack 0.5 from the division result – phuclv Aug 02 '13 at 03:39