comp.lang.ada
 help / color / mirror / Atom feed
From: Martin <martin.dowie@btopenworld.com>
Subject: Re: Allocation questions
Date: Thu, 28 May 2009 03:02:49 -0700 (PDT)
Date: 2009-05-28T03:02:49-07:00	[thread overview]
Message-ID: <f7ca968f-93d1-4a64-988c-dfd4005c7e2a@s20g2000vbp.googlegroups.com> (raw)
In-Reply-To: 4a1e5d9e$0$2868$ba620e4c@news.skynet.be

On May 28, 10:46 am, Olivier Scalbert <olivier.scalb...@algosyn.com>
wrote:
> Hello,
>
> In the context of my small audio project, I need to create an object to
> introduce a delay in a signal. With some of these objects and other
> stuff it is possible to simulate nice reverberation.
>
> Here is the code (As delay is a reserved word in Ada, I use Delay_T as I
> found nothing better !):
>
> package audio.effect is
>
>      type Sample is new Float;
>      type Time   is new Float;
>
>      type Samples_Array is array (Positive range <>) of Sample;
>
>      type Delay_T is tagged record
>          Delay_Time: Time;
>          Output    : Sample;
>          Samples   : access Samples_Array;
>      end record;
>
>      function Create_Delay (Time_Value: Time) return Delay_T;
>
>      procedure Input (D: Delay_T; Input: Sample);
>      function Output (D: Delay_T) return Sample;
>      procedure  Next (D: Delay_T); -- compute next output
>
> end audio.effect;
>
> package body audio.effect is
>
>      function Create_Delay(Time_Value: Time) return Delay_T is
>          Result:      Delay_T;
>          Nb_Samples:  Integer;
>      begin
>          Result.Delay_Time := Time_Value;
>          Nb_Samples := Integer(Time_Value * 44_100.0);
>          Result.Samples := new Samples_Array(1..Nb_Samples);
>          Result.Output  := 0.0;
>          return Result;
>      end Create_Delay;
>
>      procedure Input (D: Delay_T; Input: Sample) is
>      begin
>          null; --TBD
>      end Input;
>
>      function Output(D: Delay_T) return Sample is
>      begin
>          return D.Output;
>      end Output;
>
>      procedure Next(D: Delay_T) is
>      begin
>          null; --TBD
>      end Next;
>
> end audio.effect;
> -- --------------------------------
> Here are my questions:
>
> 1) Is there a way to avoid the dynamic array allocation, as I do not
> know the Time_Value at compile time ?

You could look at Ada.Container.Vectors and the Reserve_Capacity
subprogram. The container libraries should offer perfectly acceptable
performance, esp if you only need to reserve once.

Or you could use a discriminant:

   type Samples (No_Of_Samples : Natural := 0) is
      record
         S : array (1 .. No_Of_Samples) of Sample;
      end record;


> 2) Is it possible to release the array automatically when the Delay
> object died ?

Checkout package Ada.Finalization.


> 3) I have read that there is a program call gnatmem to detect memory
> leaks. Is there somewhere a package for Linux (Debian) ?

Sorry, no idea.

Cheers
-- Martin



  reply	other threads:[~2009-05-28 10:02 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-28  9:46 Allocation questions Olivier Scalbert
2009-05-28 10:02 ` Martin [this message]
2009-05-28 11:55   ` xavier grave
2009-05-28 12:19     ` Ludovic Brenta
2009-05-28 13:00       ` Georg Bauhaus
2009-05-28 23:13       ` Robert A Duff
2009-05-28 10:05 ` Dmitry A. Kazakov
2009-05-28 10:40   ` Martin
2009-05-28 12:32     ` Olivier Scalbert
2009-05-28 13:39       ` Martin
2009-05-28 13:57         ` Olivier Scalbert
2009-05-28 14:51         ` Adam Beneschan
2009-05-28 17:58           ` Randy Brukardt
2009-05-28 14:04       ` Dmitry A. Kazakov
2009-05-28 16:59 ` Jeffrey R. Carter
2009-05-28 17:26   ` Olivier Scalbert
replies disabled

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