diff options
author | Jedidiah Barber <contact@jedbarber.id.au> | 2023-07-20 23:11:35 +1200 |
---|---|---|
committer | Jedidiah Barber <contact@jedbarber.id.au> | 2023-07-20 23:11:35 +1200 |
commit | 46396d324ec5494bfe5fb203904796ba21ac9650 (patch) | |
tree | 20382433d70417af7d7070d3ee2c7dc76948359b | |
parent | 59a2fd979cfb79e9f6cd0699b0ad05814279e2a7 (diff) |
Added mini example program for reading files
-rw-r--r-- | example.gpr | 3 | ||||
-rw-r--r-- | example/read_example.adb | 117 |
2 files changed, 119 insertions, 1 deletions
diff --git a/example.gpr b/example.gpr index 5170e1f..9f8479e 100644 --- a/example.gpr +++ b/example.gpr @@ -12,11 +12,12 @@ project Example is for Source_Dirs use ("example"); for Object_Dir use "obj"; for Exec_Dir use "bin"; - for Main use ("file_info_example.adb", "virtual_io_example.adb"); + for Main use ("file_info_example.adb", "read_example.adb", "virtual_io_example.adb"); package Builder is for Executable("file_info_example.adb") use "info_example"; + for Executable("read_example.adb") use "read_example"; for Executable("virtual_io_example.adb") use "virtual_example"; end Builder; 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; + + |