summaryrefslogtreecommitdiff
path: root/example/read_example.adb
blob: 719e7ecf68a06764377d4ec780be168e750df299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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 := My_Info.Channels;
        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;