comp.lang.ada
 help / color / mirror / Atom feed
From: Olivier Scalbert <olivier.scalbert@algosyn.com>
Subject: Re: Conversion from floating point to signed 16 bits
Date: Wed, 20 May 2009 00:08:39 +0200
Date: 2009-05-20T00:08:39+02:00	[thread overview]
Message-ID: <4a132ddf$0$2855$ba620e4c@news.skynet.be> (raw)
In-Reply-To: <ce890d73-6671-4f96-b1ce-bd15d1852114@s31g2000vbp.googlegroups.com>

Ludovic Brenta wrote:
> On May 19, 8:51 pm, Olivier Scalbert <olivier.scalb...@algosyn.com>
> wrote:
> Your requirements seem a bit strange to me.  Could you please explaun
> why you need the integer to be signed?  Ada has direct support for
> fixed-point types (RM 3.5.9).  They would work like this:
> 
> 0.0            => 2#0000_0000_0000_0000#
> 0.25           => 2#0100_0000_0000_0000#
> 0.5            => 2#1000_0000_0000_0000#
> 0.75           => 2#1100_0000_0000_0000#
> 1.0-2.0**(-16) => 2#1111_1111_1111_1111#
> 
> If you interpret the 16 bits as an unsigned integer, you get
> 
>     0
> 16384
> 32768
> 49152
> 65535
> 
> If you interpret these same 16 bits as a signed integer, due to two's
> complement you get
> 
>  0
>  16384
> -32768
> -16384
> -1
> 
> Here is my program using this feature:
> 
> with Ada.Unchecked_Conversion;
> with Interfaces;
> with Ada.Text_IO;
> procedure Conversion is
>    type Analog_Value is delta 2.0 ** (-16) range 0.0 .. 1.0; -- fixed-
> point
>    for Analog_Value'Size use 16;
> 
>    function "+" is
>       new Ada.Unchecked_Conversion (Source => Analog_Value,
>                                     Target => Interfaces.Integer_16);
> 
>    procedure Put (S16 : in Interfaces.Integer_16) is
>    begin
>       Ada.Text_IO.Put_Line (Interfaces.Integer_16'Image (S16));
>    end Put;
> 
> begin
>    Put (+0.0);
>    Put (+0.25);
>    Put (+0.5);
>    Put (+0.75);
>    Put (+(1.0 - Analog_Value'Small));
> end Conversion;
> 
> I am aware that my program does not address the exact requirement you
> expressed but I'm curious to know what the high-level requirement is,
> i.e. why you need the conversion in your example.
> 
> --
> Ludovic Brenta.

Hi Ludovic,

The purpose of this conversion is in fact to convert a set of 
Analog_Value, representing an sound wave (normalized to [0.0, 1.0]) to a 
snd (or au) audio sound file. This file format is very simple to manage.
Specs can be found there:
http://www.opengroup.org/public/pubs/external/auformat.html
Inside the specs, you can find:
"All of the linear formats are signed integers, centered at zero."
That is why, I need this strange requirement !

The sound wave is generated by mathematical functions and saved as an 
als file (see a previous post - I do not know how to link to it!).
Then the file is normalized after fetching the min/max and converted 
with the maximum of dynamic to a snd file.

So, no rocket science here!
;-)

Olivier



  reply	other threads:[~2009-05-19 22:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-19 18:51 Conversion from floating point to signed 16 bits Olivier Scalbert
2009-05-19 19:37 ` Adam Beneschan
2009-05-19 19:57   ` Jeffrey R. Carter
2009-05-19 22:36     ` Adam Beneschan
2009-05-19 19:55 ` Jeffrey R. Carter
2009-05-19 20:06 ` Ludovic Brenta
2009-05-19 22:08   ` Olivier Scalbert [this message]
2009-05-20  8:29 ` Stuart
2009-05-20  8:48   ` Stuart
2009-05-20 11:11 ` Georg Bauhaus
2009-05-20 12:06   ` Martin
2009-05-20 15:18     ` Jeffrey R. Carter
2009-05-20 15:23       ` Martin
2009-05-20 15:47         ` Adam Beneschan
2009-05-20 16:13           ` Martin
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox