summaryrefslogtreecommitdiff
path: root/example/virtual_io_example.adb
blob: 0561655b466149033c183e20e0394d60ba58d928 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158


--  Programmed by Jedidiah Barber
--  Released into the public domain

--  This program opens the file supplied as a command line argument. Except
--  rather than the usual libsndfile open procedure, it uses the standard Ada
--  library and libsndfile's virtual open. Then the program outputs the file
--  info and closes the file. No modifications are made.


with

    Ada.Command_Line,
    Ada.Directories,
    Ada.Direct_IO,
    Ada.Text_IO,
    Interfaces,
    Libsndfile.Virtual;

use type

    Libsndfile.Count_Type;


procedure Virtual_IO_Example is


    package ACom renames Ada.Command_Line;
    package ADir renames Ada.Directories;
    package DIO is new Ada.Direct_IO (Element_Type => Interfaces.Unsigned_8);
    package TIO renames Ada.Text_IO;


    Virtual_File   : DIO.File_Type;
    Current_Offset : Libsndfile.Count_Type := 0;


    function My_Length
        return Libsndfile.Count_Type is
    begin
        return Libsndfile.Count_Type (DIO.Size (Virtual_File));
    end My_Length;

    function My_Seek
           (Offset : in Libsndfile.Count_Type;
            Whence : in Libsndfile.Seek_From)
        return Libsndfile.Count_Type is
    begin
        case Whence is
            when Libsndfile.From_Start =>
                Current_Offset := Offset;
            when Libsndfile.From_Current =>
                Current_Offset := Current_Offset + Offset;
            when Libsndfile.From_End =>
                Current_Offset := My_Length - 1 + Offset;
        end case;
        Current_Offset := Current_Offset mod My_Length;
        return Current_Offset;
    end My_Seek;

    function My_Read
           (Data  :    out Libsndfile.Raw_Data;
            Bytes : in     Libsndfile.Count_Type)
        return Libsndfile.Count_Type
    is
        Item : Interfaces.Unsigned_8;
        Data_Position : Integer := 1;
    begin
        while Data_Position <= Integer (Bytes) loop
            DIO.Read (Virtual_File, Item, DIO.Count (1 + Current_Offset));
            Data (Data_Position) := Item;
            Data_Position := Data_Position + 1;
            exit when Current_Offset + 1 > My_Length;
            Current_Offset := Current_Offset + 1;
        end loop;
        return Libsndfile.Count_Type (Data_Position - 1);
    end My_Read;

    function My_Write
           (Data  : in Libsndfile.Raw_Data;
            Bytes : in Libsndfile.Count_Type)
        return Libsndfile.Count_Type
    is
        Item : Interfaces.Unsigned_8;
        Data_Position : Integer := 1;
    begin
        while Data_Position <= Integer (Bytes) loop
            Item := Data (Data_Position);
            DIO.Write (Virtual_File, Item, DIO.Count (1 + Current_Offset));
            Data_Position := Data_Position + 1;
            exit when Current_Offset + 1 > My_Length;
            Current_Offset := Current_Offset + 1;
        end loop;
        return Libsndfile.Count_Type (Data_Position - 1);
    end My_Write;

    function My_Tell
        return Libsndfile.Count_Type is
    begin
        return Current_Offset + 1;
    end My_Tell;


    My_Info : Libsndfile.File_Info := Libsndfile.Blank_Info;
    Test_File : Libsndfile.Virtual.Virtual_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));
    DIO.Open (Virtual_File, DIO.In_File, ACom.Argument (1));
    Test_File.Open
       (Libsndfile.Read_Only,
        My_Info,
        My_Length'Unrestricted_Access,
        My_Seek'Unrestricted_Access,
        My_Read'Unrestricted_Access,
        My_Write'Unrestricted_Access,
        My_Tell'Unrestricted_Access);
    TIO.New_Line;

    TIO.Put_Line ("Open frame count:" &
        Libsndfile.Count_Type'Image (Libsndfile.Frames (My_Info)));
    TIO.Put_Line ("Open sample rate:" & Natural'Image (Libsndfile.Rate (My_Info)));
    TIO.Put_Line ("Open channel count:" & Natural'Image (Libsndfile.Channels (My_Info)));
    TIO.Put_Line ("Open major format: " &
        Libsndfile.Major_Format'Image (Libsndfile.Major (My_Info)));
    TIO.Put_Line ("Open minor format: " &
        Libsndfile.Minor_Format'Image (Libsndfile.Minor (My_Info)));
    TIO.Put_Line ("Open endianness: " &
        Libsndfile.Endianness'Image (Libsndfile.Endian (My_Info)));
    TIO.Put_Line ("Open section count:" & Natural'Image (Libsndfile.Sections (My_Info)));
    TIO.Put_Line ("Open seekable: " & Boolean'Image (Libsndfile.Seekable (My_Info)));

    TIO.New_Line;
    Test_File.Close;
    DIO.Close (Virtual_File);
    TIO.Put_Line ("File closed");


end Virtual_IO_Example;