Subject: Re: [linux-audio-dev] CDDA interface - How to convert to float? Any library?
From: Erik de Castro Lopo (erikd-lad_AT_mega-nerd.com)
Date: Sun Aug 11 2002 - 02:35:13 EEST
On Fri, 09 Aug 2002 18:18:44 -0700
Martin Wolters <martinw_AT_atc.creative.com> wrote:
> I tried using something like the following to convert the data:
>
> float samples[samplesInBuffer];
> for(i=0;i<samplesInBuffer;i++) {
> int value = (buf[1] << 8) + (buf[0] << 16);
> sample[i] = ((float)(value/256))*scalefactor;
> buf += 2;
> }
Thats not going to work.
The data in buf are unsigned chars but I am almost certain that the actual
data in the buffer are signed short values. This means that this:
(buf[1] << 8) + (buf[0] << 16)
gives an unsigned value which is definitely wrong.
Try this:
float samples[samplesInBuffer];
for(i=0;i<samplesInBuffer;i++) {
short value = (short) (buf[1] | (buf[0] << 8));
sample[i] = (value / ((float) 0x8000) ;
buf += 2;
}
This should give you floats in the range [-1.0, 1.0].
The bitwise or means that the combination of buf [0] and buf [1] will not
try and do anything remotely arithmetic but instead paste the two values
together.
Hope this helps,
Erik
-- +-----------------------------------------------------------+ Erik de Castro Lopo nospam_AT_mega-nerd.com (Yes it's valid) +-----------------------------------------------------------+ "Therapists typically base the nuttiness of a patient on the strength of their convictions, on which basis this 43,000 word opus alone stands as a kind of testament to Bill's (Gates) madness." - The Register
This archive was generated by hypermail 2b28 : Sun Aug 11 2002 - 02:35:47 EEST