Csound Csound-dev Csound-tekno Search About

[Csnd] integer to float to integer conversions

Date2009-12-05 20:02
FromMatt Barber
Subject[Csnd] integer to float to integer conversions
Hello,

I'm trying to find out how csound goes about converting from integers
to floats and floats to integers -- for instance if I read a 16-bit
sound file in with diskin, does it just divide by 32768 to convert to
float? This seems to be the case, since -32768 to +32767 seems to map
to -1.0 to +0.999969 when 0dbfs is set to 1.0 or when floats are
written to a new file.

Also, when csound writes a 16-bit integer, how is the conversion from
float back to int handled?  It doesn't seem to be a simple
multiplication by 32767 or 32768 since -1.0 to +1.0 w/ 0dbfs=1.0 seems
to map back to the asymmetrical -32768 to +32767.  I suppose it could
just be a multiply by 32768 with the positive value just clipped to
+32767?  Are the values rounded to the nearest integer, or truncated,
or floored?

And, will the same reasoning apply to 24-bit samples in/out?

Thanks,

Matt


Send bugs reports to this list.
To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"

Date2009-12-05 23:39
FromErik de Castro Lopo
Subject[Csnd] Re: integer to float to integer conversions
Matt Barber wrote:

> I'm trying to find out how csound goes about converting from integers
> to floats and floats to integers -- for instance if I read a 16-bit
> sound file in with diskin, does it just divide by 32768 to convert to
> float?

I'm  pretty sure the conversion from integer to float when reading 
sound files is actually handled by libsndfile:

    http://www.mega-nerd.com/libsndfile/

and if that is correct, then yes, for 16 bit sound files it just
divides by 32768.

> Also, when csound writes a 16-bit integer, how is the conversion from
> float back to int handled?  It doesn't seem to be a simple
> multiplication by 32767 or 32768 since -1.0 to +1.0 w/ 0dbfs=1.0 seems
> to map back to the asymmetrical -32768 to +32767.

If libsndfile is being used for this, then it is actually just a multiply
by 32768. This is explained more fully here:

   http://www.mega-nerd.com/libsndfile/FAQ.html#Q010

> I suppose it could
> just be a multiply by 32768 with the positive value just clipped to
> +32767?

libsndfile does have a mode where values that would exceed the range 
allowed by the output file are clipped instead of being allowed to
wrap around. I'm not sure if Csound uses this mode or not.

>  Are the values rounded to the nearest integer, or truncated,
> or floored?

Round to nearest integer.

> And, will the same reasoning apply to 24-bit samples in/out?

Yes, but with different divisors and multipliers.

HTH,
Erik