From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: quiz for Sequential_IO Read
Date: Mon, 4 Sep 2017 22:55:09 +0200
Date: 2017-09-04T22:55:09+02:00 [thread overview]
Message-ID: <ookejb$1j11$1@gioia.aioe.org> (raw)
In-Reply-To: ookb93$feb$1@newsreader4.netcologne.de
On 2017-09-04 21:58, Frank Buss wrote:
> On 09/03/2017 03:11 PM, Dmitry A. Kazakov wrote:
>>
>> In practice Stream I/O is a better choice. You also should not use
>> compiler-generated serialization attributes (see T'Read, T'Write,
>> T'Input, T'Output attributes). These are not portable either. You should
>> always replace them with your own.
>
> Ok, I wrote my program with Stream_IO. It will be reading from a FIFO
> file for receiving MIDI signals, and then write to a sound device
> continuously. I found a convenient package which defines low-level types
> of fixed size, same as stdint.h in C. See below for the code. Would this
> be portable on all systems (assuming they provide FIFO files) ?
You probably mean a pipe here?
> At least
> it works on my Debian system so far.
>
>
> with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
> with Ada.Text_IO; use Ada.Text_IO;
> with Interfaces; use Interfaces;
>
> procedure Hello is
>
> Data : Unsigned_8;
> Input_File : Ada.Streams.Stream_IO.File_Type;
> Input_Stream : Ada.Streams.Stream_IO.Stream_Access;
>
> task Main_Task is
> entry Data_Received(Data : in Unsigned_8);
> end Main_Task;
>
> task body Main_Task is
> begin
> loop
> select
> accept Data_Received(Data : in Unsigned_8) do
> Put("test: " & Unsigned_8'Image(Data));
> New_Line;
> end Data_Received;
> else
> delay 0.1;
To avoid biased time error you should use "delay until" instead:
delay until Last_Time + 0.1;
Last_Time := Last_Time + 0.1;
And it is not a good idea to pass each octet to the task. A better
design is to have a FIFO (ring buffer) to be filled by one task and read
by another. It can be the same task as well.
> -- TODO: create continuous sound output here
> end select;
> end loop;
> end Main_Task;
>
> begin
> Open(Input_File, In_File, "/home/frank/midi");
> Input_Stream := Stream(Input_File);
> loop
> Unsigned_8'Read(Input_Stream, Data);
Then you can read stream by pieces rather element by element. E.g.
Buffer : Stream_Element_Array (1..Buffer_Size);
Last : Stream_Element_OFfset;
...
Input_Stream.Read (Buffer, Last); -- Read available data
-- Buffer (1..Last) is
-- the data read
If you have FIFO to communicate with the task. It also can server as
the input buffer. And you will simply read into the space between
In_Index of the FIFO and either the FIFO's end index or Out_Index - 1,
assuming FIFO is a ring buffer. You can use slice to do this:
FIFO : Stream_Element_Array (1..Buffer_Size);
In_Index : Stream_Element_Offset := 1; -- The index to write at
Out_Index : Stream_Element_Offset := 1; -- The index to get data at
...
if In_Index < Out_Index then
Input_Stream.Read (FIFO (In_Index..Out_Index - 1), Last);
In_Index := Last + 1;
else
Input_Stream.Read (FIFO (In_Index..FIFO'Last), Last);
if Last = FIFO'Last then -- wrap
In_Index := FIFO'First;
else
In_Index := Last + 1;
end if;
end if;
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
next prev parent reply other threads:[~2017-09-04 20:55 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-03 11:01 quiz for Sequential_IO Read Frank Buss
2017-09-03 11:40 ` Jeffrey R. Carter
2017-09-03 12:47 ` Frank Buss
2017-09-03 13:44 ` Jeffrey R. Carter
2017-09-03 15:38 ` AdaMagica
2017-09-03 12:26 ` Dmitry A. Kazakov
2017-09-03 12:43 ` Frank Buss
2017-09-03 13:11 ` Dmitry A. Kazakov
2017-09-04 19:58 ` Frank Buss
2017-09-04 20:55 ` Dmitry A. Kazakov [this message]
2017-09-04 21:51 ` Frank Buss
2017-09-05 1:54 ` Dennis Lee Bieber
2017-09-03 17:28 ` J-P. Rosen
2017-09-03 20:10 ` Frank Buss
2017-09-04 5:12 ` J-P. Rosen
2017-09-04 7:37 ` Dmitry A. Kazakov
2017-09-03 20:17 ` Dennis Lee Bieber
2017-09-04 5:14 ` J-P. Rosen
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox