From 46396d324ec5494bfe5fb203904796ba21ac9650 Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Thu, 20 Jul 2023 23:11:35 +1200 Subject: Added mini example program for reading files --- example/read_example.adb | 117 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 example/read_example.adb (limited to 'example/read_example.adb') diff --git a/example/read_example.adb b/example/read_example.adb new file mode 100644 index 0000000..33b2d7a --- /dev/null +++ b/example/read_example.adb @@ -0,0 +1,117 @@ + + +-- Programmed by Jedidiah Barber +-- Released into the public domain + + +-- This program opens the file supplied as a command line argument, then reads +-- up to 5 frames from it in Short, Integer, Float, Double formats, seeking to +-- the file beginning between each read. The data is printed to stdout, then +-- the file is closed. No modifications are made. + + +with + + Ada.Command_Line, + Ada.Directories, + Ada.Text_IO, + Interfaces, + Libsndfile; + + +procedure Read_Example is + + + package ACom renames Ada.Command_Line; + package ADir renames Ada.Directories; + package TIO renames Ada.Text_IO; + + + My_Info : Libsndfile.File_Info := Libsndfile.Blank_Info; + My_Sound_File : Libsndfile.Sound_File; + + +begin + + + if ACom.Argument_Count < 1 then + TIO.Put_Line ("No filename provided"); + ACom.Set_Exit_Status (ACom.Failure); + return; + end if; + + if not ADir.Exists (ACom.Argument (1)) then + TIO.Put_Line ("File does not exist"); + ACom.Set_Exit_Status (ACom.Failure); + return; + end if; + + + TIO.Put_Line ("Load file " & ACom.Argument (1)); + My_Sound_File.Open (ACom.Argument (1), Libsndfile.Read_Only, My_Info); + TIO.New_Line; + + declare + Channel_Count : Natural := Libsndfile.Channels (My_Info); + Test_Frames : Libsndfile.Count_Type := 5; + + My_Shorts : Libsndfile.Short_Data (1 .. Natural (Test_Frames) * Channel_Count); + My_Integers : Libsndfile.Integer_Data (1 .. Natural (Test_Frames) * Channel_Count); + My_Floats : Libsndfile.Float_Data (1 .. Natural (Test_Frames) * Channel_Count); + My_Doubles : Libsndfile.Double_Data (1 .. Natural (Test_Frames) * Channel_Count); + + Read_Count, Position : Libsndfile.Count_Type; + begin + TIO.Put_Line ("Reading" & Libsndfile.Count_Type'Image (Test_Frames) & + " frames of Short data"); + Read_Count := My_Sound_File.Read_Short (My_Shorts, Test_Frames); + TIO.Put_Line ("Obtained" & Libsndfile.Count_Type'Image (Read_Count) & " frames"); + for Index in Integer range 1 .. Natural (Read_Count) * Channel_Count loop + TIO.Put_Line (Interfaces.Integer_16'Image (My_Shorts (Index))); + end loop; + TIO.Put_Line ("Seeking to start again"); + Position := My_Sound_File.Seek (0, Libsndfile.From_Start); + TIO.Put_Line ("Reached position" & Libsndfile.Count_Type'Image (Position)); + TIO.New_Line; + + TIO.Put_Line ("Reading" & Libsndfile.Count_Type'Image (Test_Frames) & + " frames of Integer data"); + Read_Count := My_Sound_File.Read_Integer (My_Integers, Test_Frames); + TIO.Put_Line ("Obtained" & Libsndfile.Count_Type'Image (Read_Count) & " frames"); + for Index in Integer range 1 .. Natural (Read_Count) * Channel_Count loop + TIO.Put_Line (Interfaces.Integer_32'Image (My_Integers (Index))); + end loop; + TIO.Put_Line ("Seeking to start again"); + Position := My_Sound_File.Seek (0, Libsndfile.From_Start); + TIO.Put_Line ("Reached position" & Libsndfile.Count_Type'Image (Position)); + TIO.New_Line; + + TIO.Put_Line ("Reading" & Libsndfile.Count_Type'Image (Test_Frames) & + " frames of Float data"); + Read_Count := My_Sound_File.Read_Float (My_Floats, Test_Frames); + TIO.Put_Line ("Obtained" & Libsndfile.Count_Type'Image (Read_Count) & " frames"); + for Index in Integer range 1 .. Natural (Read_Count) * Channel_Count loop + TIO.Put_Line (Interfaces.IEEE_Float_32'Image (My_Floats (Index))); + end loop; + TIO.Put_Line ("Seeking to start again"); + Position := My_Sound_File.Seek (0, Libsndfile.From_Start); + TIO.Put_Line ("Reached position" & Libsndfile.Count_Type'Image (Position)); + TIO.New_Line; + + TIO.Put_Line ("Reading" & Libsndfile.Count_Type'Image (Test_Frames) & + " frames of Double data"); + Read_Count := My_Sound_File.Read_Double (My_Doubles, Test_Frames); + TIO.Put_Line ("Obtained" & Libsndfile.Count_Type'Image (Read_Count) & " frames"); + for Index in Integer range 1 .. Natural (Read_Count) * Channel_Count loop + TIO.Put_Line (Interfaces.IEEE_Float_64'Image (My_Doubles (Index))); + end loop; + TIO.New_Line; + end; + + My_Sound_File.Close; + TIO.Put_Line ("File closed"); + + +end Read_Example; + + -- cgit