Re: [Audacity-nyquist] read-int in XLisp
A free multi-track audio editor and recorder
Brought to you by:
aosiniao
|
From: Sami J. <sam...@gm...> - 2005-06-30 06:21:56
|
You don't need to handle bits when converting from signed / unsigned. Assuming 2's complement signed values (I won't explain this now!), just add/subtract conditionally. Adding an uneven value doesn't sound good, it must be 32768, for example: if x < 0 then x =3D x + 32768 Note that this simple adding doesn't work if the signed temp/destination is also 16-bit wide, because it will only hold values between -32768 .. 32767. What if... x2 =3D x // intermediate 32-bit variable if x2 < 0 then x2 =3D x2 + 32768 // move the "negative" values above the "positive" x2 =3D x2 - 32768 // restore the original signed range -32768 .. 32767 x =3D x2 // convert to 16-bit format (how?) The example is just a guess, I'm lousy on maths (and Lisp especially), but I've doe something like this a long time ago in BASIC on Amiga. I'd suggest just trying different things. The reason why signed numbers are (sometimes) stored as 2's complement is that they're mode efficient to operate with the binary logic artithmetics. --=20 Sami "Some-E" Jumppanen sam...@gm... http://netti.nic.fi/~some-e/ |