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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: quiz for Sequential_IO Read Date: Mon, 4 Sep 2017 22:55:09 +0200 Organization: Aioe.org NNTP Server Message-ID: References: NNTP-Posting-Host: MajGvm9MbNtGBKE7r8NgYA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 Content-Language: en-US X-Notice: Filtered by postfilter v. 0.8.2 Xref: feeder.eternal-september.org comp.lang.ada:47931 Date: 2017-09-04T22:55:09+02:00 List-Id: 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