summaryrefslogtreecommitdiff
path: root/src/libsndfile.ads
blob: ad82d0cb3435300ce1fb2d2a91ba6581eb505411 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451


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


pragma Ada_2012;


with

    Interfaces;

private with

    Interfaces.C,
    System;


package Libsndfile is


    ---------------------------------
    --  Data Types and Structures  --
    ---------------------------------

    type Count_Type is new Long_Long_Integer;

    type Sound_File is tagged limited private;

    type File_Mode is (Read_Only, Write_Only, Read_Write);

    type Seek_From is (From_Start, From_Current, From_End);

    type Short_Data   is array (Positive range <>) of Interfaces.Integer_16;
    type Integer_Data is array (Positive range <>) of Interfaces.Integer_32;
    type Float_Data   is array (Positive range <>) of Interfaces.IEEE_Float_32;
    type Double_Data  is array (Positive range <>) of Interfaces.IEEE_Float_64;

    type Raw_Data is array (Positive range <>) of Interfaces.Unsigned_8;

    type Metadata is
       (Title_String,
        Copyright_String,
        Software_String,
        Artist_String,
        Comment_String,
        Date_String,
        Album_String,
        License_String,
        Track_Number_String,
        Genre_String);

    type File_Info is tagged private;

    type Major_Format is
       (Format_Unknown,
        Wav_Format,
        Aiff_Format,
        Au_Format,
        Raw_Format,
        Paf_Format,
        Svx_Format,
        Nist_Format,
        Voc_Format,
        Ircam_Format,
        W64_Format,
        Mat4_Format,
        Mat5_Format,
        Pvf_Format,
        Xi_Format,
        Htk_Format,
        Sds_Format,
        Avr_Format,
        Wavex_Format,
        Sd2_Format,
        Flac_Format,
        Caf_Format,
        Wve_Format,
        Ogg_Format,
        Mpc2k_Format,
        Rf64_Format,
        Mpeg_Format);

    type Minor_Format is
       (Encoding_Unknown,
        Pcm_S8_Encoding,
        Pcm_16_Encoding,
        Pcm_24_Encoding,
        Pcm_32_Encoding,
        Pcm_U8_Encoding,
        Float_Encoding,
        Double_Encoding,
        Ulaw_Encoding,
        Alaw_Encoding,
        Ima_Adpcm_Encoding,
        Ms_Adpcm_Encoding,
        Gsm610_Encoding,
        Vox_Adpcm_Encoding,
        Nms_Adpcm_16_Encoding,
        Nms_Adpcm_24_Encoding,
        Nms_Adpcm_32_Encoding,
        G721_32_Encoding,
        G723_24_Encoding,
        G723_40_Encoding,
        Dwvw_12_Encoding,
        Dwvw_16_Encoding,
        Dwvw_24_Encoding,
        Dwvw_N_Encoding,
        Dpcm_8_Encoding,
        Dpcm_16_Encoding,
        Vorbis_Encoding,
        Opus_Encoding,
        Alac_16_Encoding,
        Alac_20_Encoding,
        Alac_24_Encoding,
        Alac_32_Encoding,
        Mpeg_Layer_I_Encoding,
        Mpeg_Layer_II_Encoding,
        Mpeg_Layer_III_Encoding);

    type Endianness is
       (Default_Endian,
        Little_Endian,
        Big_Endian,
        Machine_Native);

    --  Use a copy of this for reading non-raw files
    Blank_Info : constant File_Info;

    --  Use this for reading raw files and writing files
    function Create
           (Rate     : in Positive;
            Channels : in Positive;
            Major    : in Major_Format;
            Minor    : in Minor_Format;
            Endian   : in Endianness := Default_Endian)
        return File_Info;

    function Frames
           (Info : in File_Info)
        return Count_Type;

    function Rate
           (Info : in File_Info)
        return Natural;

    function Channels
           (Info : in File_Info)
        return Natural;

    function Major
           (Info : in File_Info)
        return Major_Format;

    function Minor
           (Info : in File_Info)
        return Minor_Format;

    function Endian
           (Info : in File_Info)
        return Endianness;

    function Sections
           (Info : in File_Info)
        return Natural;

    function Seekable
           (Info : in File_Info)
        return Boolean;




    ------------------
    --  Exceptions  --
    ------------------

    --  May be raised by Open
    Unrecognised_Format_Error : exception;

    --  May be raised by Open
    Unsupported_Encoding_Error : exception;

    --  May be raised by Open
    Malformed_File_Error : exception;

    --  May be raised by Open, Close?
    System_Error : exception;

    --  May be raised by Seek
    Seek_Error : exception;

    --  May be raised by Current_Byterate
    Unknown_Byterate_Error : exception;

    --  May be raised by Open, Close
    General_Failure : exception;

    --  Program_Error may be raised if libsndfile in general does something out of spec
    --  Set_Meta may raise errors but it is unclear which ones




    ---------------
    --  Utility  --
    ---------------

    function Is_Open
           (File : in Sound_File)
        return Boolean;




    ---------------------
    --  API Interface  --
    ---------------------

    procedure Open
           (File : in out Sound_File;
            Name : in     String;
            Mode : in     File_Mode;
            Info : in out File_Info'Class)
    with Pre => not Is_Open (File),
        Post => Is_Open (File);

    function Format_Check
           (Info : in File_Info)
        return Boolean;

    function Seek
           (File   : in Sound_File;
            Offset : in Count_Type;
            Whence : in Seek_From;
            Mode   : in File_Mode := Read_Write)
        return Count_Type
    with Pre => Is_Open (File);

    procedure Close
           (File : in out Sound_File)
    with Pre => Is_Open (File),
        Post => not Is_Open (File);

    procedure Write_Sync
           (File : in Sound_File)
    with Pre => Is_Open (File);

    function Read_Short
           (File   : in     Sound_File;
            Data   :    out Short_Data;
            Frames : in     Count_Type)
        return Count_Type
    with Pre => Is_Open (File);

    function Read_Integer
           (File   : in     Sound_File;
            Data   :    out Integer_Data;
            Frames : in     Count_Type)
        return Count_Type
    with Pre => Is_Open (File);

    function Read_Float
           (File   : in     Sound_File;
            Data   :    out Float_Data;
            Frames : in     Count_Type)
        return Count_Type
    with Pre => Is_Open (File);

    function Read_Double
           (File   : in     Sound_File;
            Data   :    out Double_Data;
            Frames : in     Count_Type)
        return Count_Type
    with Pre => Is_Open (File);

    function Write_Short
           (File   : in Sound_File;
            Data   : in Short_Data;
            Frames : in Count_Type)
        return Count_Type
    with Pre => Is_Open (File);

    function Write_Integer
           (File   : in Sound_File;
            Data   : in Integer_Data;
            Frames : in Count_Type)
        return Count_Type
    with Pre => Is_Open (File);

    function Write_Float
           (File   : in Sound_File;
            Data   : in Float_Data;
            Frames : in Count_Type)
        return Count_Type
    with Pre => Is_Open (File);

    function Write_Double
           (File   : in Sound_File;
            Data   : in Double_Data;
            Frames : in Count_Type)
        return Count_Type
    with Pre => Is_Open (File);

    function Read_Raw
           (File  : in     Sound_File;
            Data  :    out Raw_Data;
            Bytes : in     Count_Type)
        return Count_Type
    with Pre => Is_Open (File);

    function Write_Raw
           (File  : in Sound_File;
            Data  : in Raw_Data;
            Bytes : in Count_Type)
        return Count_Type
    with Pre => Is_Open (File);

    function Get_Meta
           (File : in Sound_File;
            Kind : in Metadata)
        return String
    with Pre => Is_Open (File);

    procedure Set_Meta
           (File  : in Sound_File;
            Kind  : in Metadata;
            Value : in String)
    with Pre => Is_Open (File);

    function Version_String
        return String;

    function Current_Byterate
           (File : in Sound_File)
        return Natural
    with Pre => Is_Open (File);

    --  RIFF chunk API goes here


private


    pragma Linker_Options ("-lsndfile");

    pragma Inline (Is_Open);
    pragma Inline (Write_Sync);
    pragma Inline (Read_Raw);
    pragma Inline (Write_Raw);
    pragma Inline (Version_String);


    type Sound_File is tagged limited record
        Ptr   : System.Address   := System.Null_Address;
        FMode : File_Mode        := Read_Only;
        Chans : Interfaces.C.int := 0;
    end record;


    type C_Short_Data   is array (Positive range <>) of Interfaces.C.short;
    type C_Integer_Data is array (Positive range <>) of Interfaces.C.int;
    type C_Float_Data   is array (Positive range <>) of Interfaces.C.C_float;
    type C_Double_Data  is array (Positive range <>) of Interfaces.C.double;


    --  This cannot correspond to the C-side SF_INFO since sf_count_t can vary
    type C_File_Info is record
        My_Frames      : Interfaces.Integer_64;
        My_Sample_Rate : Interfaces.C.int;
        My_Channels    : Interfaces.C.int;
        My_Major       : Interfaces.C.int;
        My_Minor       : Interfaces.C.int;
        My_Endian      : Interfaces.C.int;
        My_Sections    : Interfaces.C.int;
        My_Seekable    : Interfaces.C.int;
    end record with Convention => C;

    type File_Info is tagged record
        Data : C_File_Info;
    end record;


    Blank_Info : constant File_Info := (Data => (My_Frames => 0, others => 0));


    procedure Raise_Error
           (Num : in Interfaces.C.int);

    function To_Major
           (Num : in Interfaces.C.int)
        return Major_Format;

    function To_Minor
           (Num : in Interfaces.C.int)
        return Minor_Format;

    function To_Endian
           (Num : in Interfaces.C.int)
        return Endianness;

    function To_Cint
           (Major : in Major_Format)
        return Interfaces.C.int;

    function To_Cint
           (Minor : in Minor_Format)
        return Interfaces.C.int;

    function To_Cint
           (Endian : in Endianness)
        return Interfaces.C.int;


    sf_false : constant Interfaces.C.int;
    pragma Import (C, sf_false, "sf_false");

    sf_true : constant Interfaces.C.int;
    pragma Import (C, sf_true, "sf_true");


    sfm_read : constant Interfaces.C.int;
    pragma Import (C, sfm_read, "sfm_read");

    sfm_write : constant Interfaces.C.int;
    pragma Import (C, sfm_write, "sfm_write");

    sfm_rdwr : constant Interfaces.C.int;
    pragma Import (C, sfm_rdwr, "sfm_rdwr");


    sf_seek_set : constant Interfaces.C.int;
    pragma Import (C, sf_seek_set, "sf_seek_set");

    sf_seek_cur : constant Interfaces.C.int;
    pragma Import (C, sf_seek_cur, "sf_seek_cur");

    sf_seek_end : constant Interfaces.C.int;
    pragma Import (C, sf_seek_end, "sf_seek_end");


    function sf_error
           (File : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, sf_error, "sf_error");


end Libsndfile;