From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,fa37ee962bc4b00d X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!feeder.news-service.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!news.skynet.be!195.238.0.222.MISMATCH!newsspl501.isp.belgacom.be!tjb!not-for-mail Date: Wed, 20 May 2009 00:08:39 +0200 From: Olivier Scalbert User-Agent: Thunderbird 2.0.0.21 (X11/20090318) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Conversion from floating point to signed 16 bits References: <4a12ffa3$0$2853$ba620e4c@news.skynet.be> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <4a132ddf$0$2855$ba620e4c@news.skynet.be> Organization: -= Belgacom Usenet Service =- NNTP-Posting-Host: 410c8e21.news.skynet.be X-Trace: 1242770912 news.skynet.be 2855 87.65.144.169:58659 X-Complaints-To: usenet-abuse@skynet.be Xref: g2news2.google.com comp.lang.ada:5951 Date: 2009-05-20T00:08:39+02:00 List-Id: Ludovic Brenta wrote: > On May 19, 8:51 pm, Olivier Scalbert > 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