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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
|
-- Programmed by Jedidiah Barber
-- Released into the public domain
pragma Ada_2012;
with
Interfaces.C.Strings,
System;
use type
Interfaces.C.int,
Interfaces.C.Strings.chars_ptr,
System.Address;
package body Libao is
------------------------
-- Constants From C --
------------------------
type_live : constant Interfaces.C.int;
pragma Import (C, type_live, "type_live");
type_file : constant Interfaces.C.int;
pragma Import (C, type_file, "type_file");
sample_little_endian : constant Interfaces.C.int;
pragma Import (C, sample_little_endian, "sample_little_endian");
sample_big_endian : constant Interfaces.C.int;
pragma Import (C, sample_big_endian, "sample_big_endian");
sample_native_endian : constant Interfaces.C.int;
pragma Import (C, sample_native_endian, "sample_native_endian");
error_no_driver : constant Interfaces.C.int;
pragma Import (C, error_no_driver, "error_no_driver");
error_not_file : constant Interfaces.C.int;
pragma Import (C, error_not_file, "error_not_file");
error_not_live : constant Interfaces.C.int;
pragma Import (C, error_not_live, "error_not_live");
error_bad_option : constant Interfaces.C.int;
pragma Import (C, error_bad_option, "error_bad_option");
error_open_device : constant Interfaces.C.int;
pragma Import (C, error_open_device, "error_open_device");
error_open_file : constant Interfaces.C.int;
pragma Import (C, error_open_file, "error_open_file");
error_file_exists : constant Interfaces.C.int;
pragma Import (C, error_file_exists, "error_file_exists");
error_bad_format : constant Interfaces.C.int;
pragma Import (C, error_bad_format, "error_bad_format");
error_fail : constant Interfaces.C.int;
pragma Import (C, error_fail, "error_fail");
------------------------
-- Functions From C --
------------------------
procedure ao_initialize;
pragma Import (C, ao_initialize, "ao_initialize");
procedure ao_shutdown;
pragma Import (C, ao_shutdown, "ao_shutdown");
function ao_append_option
(Options : in out System.Address;
Key : in Interfaces.C.char_array;
Value : in Interfaces.C.char_array)
return Interfaces.C.int;
pragma Import (C, ao_append_option, "ao_append_option");
function ao_append_global_option
(Key : in Interfaces.C.char_array;
Value : in Interfaces.C.char_array)
return Interfaces.C.int;
pragma Import (C, ao_append_global_option, "ao_append_global_option");
procedure ao_free_options
(Options : in System.Address);
pragma Import (C, ao_free_options, "ao_free_options");
function ao_open_live
(Driver_ID : in Interfaces.C.int;
Format : in System.Address;
Options : in System.Address)
return System.Address;
pragma Import (C, ao_open_live, "ao_open_live");
function ao_open_file
(Driver_ID : in Interfaces.C.int;
Filename : in Interfaces.C.char_array;
Overwrite : in Interfaces.C.int;
Format : in System.Address;
Options : in System.Address)
return System.Address;
pragma Import (C, ao_open_file, "ao_open_file");
function ao_play
(Output_Device : in System.Address;
Samples : in Interfaces.C.char_array;
Num_Bytes : in Interfaces.Unsigned_32)
return Interfaces.C.int;
pragma Import (C, ao_play, "ao_play");
function ao_close
(Output_Device : in System.Address)
return Interfaces.C.int;
pragma Import (C, ao_close, "ao_close");
function ao_driver_id
(Short_Name : in Interfaces.C.char_array)
return Interfaces.C.int;
pragma Import (C, ao_driver_id, "ao_driver_id");
function ao_default_driver_id
return Interfaces.C.int;
pragma Import (C, ao_default_driver_id, "ao_default_driver_id");
function ao_driver_info
(Ident : in Interfaces.C.int)
return System.Address;
pragma Import (C, ao_driver_info, "ao_driver_info");
function ao_driver_info_list
(Count : out Interfaces.C.int)
return System.Address;
pragma Import (C, ao_driver_info_list, "ao_driver_info_list");
function ao_file_extension
(Ident : in Interfaces.C.int)
return Interfaces.C.Strings.chars_ptr;
pragma Import (C, ao_file_extension, "ao_file_extension");
function ao_is_big_endian
return Interfaces.C.int;
pragma Import (C, ao_is_big_endian, "ao_is_big_endian");
function info_item_get
(Infos : in System.Address;
Index : in Interfaces.C.int)
return System.Address;
pragma Import (C, info_item_get, "info_item_get");
pragma Inline (info_item_get);
function info_kind_get
(Item : in System.Address)
return Interfaces.C.int;
pragma Import (C, info_kind_get, "info_kind_get");
pragma Inline (info_kind_get);
function info_name_get
(Item : in System.Address)
return Interfaces.C.Strings.chars_ptr;
pragma Import (C, info_name_get, "info_name_get");
pragma Inline (info_name_get);
function info_short_name_get
(Item : in System.Address)
return Interfaces.C.Strings.chars_ptr;
pragma Import (C, info_short_name_get, "info_short_name_get");
pragma Inline (info_short_name_get);
function info_preferred_byte_format_get
(Item : in System.Address)
return Interfaces.C.int;
pragma Import (C, info_preferred_byte_format_get, "info_preferred_byte_format_get");
pragma Inline (info_preferred_byte_format_get);
function info_priority_get
(Item : in System.Address)
return Interfaces.C.int;
pragma Import (C, info_priority_get, "info_priority_get");
pragma Inline (info_priority_get);
function info_comment_get
(Item : in System.Address)
return Interfaces.C.Strings.chars_ptr;
pragma Import (C, info_comment_get, "info_comment_get");
pragma Inline (info_comment_get);
function info_option_count_get
(Item : in System.Address)
return Interfaces.C.int;
pragma Import (C, info_option_count_get, "info_option_count_get");
pragma Inline (info_option_count_get);
function info_option_key_get
(Item : in System.Address;
Index : in Interfaces.C.int)
return Interfaces.C.Strings.chars_ptr;
pragma Import (C, info_option_key_get, "info_option_key_get");
pragma Inline (info_option_key_get);
function get_errno
return Interfaces.C.int;
pragma Import (C, get_errno, "get_errno");
pragma Inline (get_errno);
function option_key
(Item : in System.Address)
return Interfaces.C.Strings.chars_ptr;
pragma Import (C, option_key, "option_key");
pragma Inline (option_key);
function option_value
(Item : in System.Address)
return Interfaces.C.Strings.chars_ptr;
pragma Import (C, option_value, "option_value");
pragma Inline (option_value);
function option_next
(Item : in System.Address)
return System.Address;
pragma Import (C, option_next, "option_next");
pragma Inline (option_next);
------------------------
-- Internal Utility --
------------------------
procedure Do_Append
(Ptr : in out System.Address;
Key : in Interfaces.C.char_array;
Value : in Interfaces.C.char_array)
is
Result : Interfaces.C.int;
begin
Result := ao_append_option (Ptr, Key, Value);
if Result = 0 then
raise Storage_Error;
elsif Result /= 1 then
raise Program_Error;
end if;
end Do_Append;
procedure Do_Close
(Ptr : in System.Address)
is
Result : Interfaces.C.int := ao_close (Ptr);
begin
if Result = 0 then
raise Close_Device_Error;
elsif Result /= 1 then
raise Program_Error;
end if;
end Do_Close;
-----------------------------------
-- Controlled Type Subprograms --
-----------------------------------
procedure Adjust
(This : in out Option_List)
is
Old : System.Address := This.Ptr;
begin
This.Ptr := System.Null_Address;
while Old /= System.Null_Address loop
Do_Append
(This.Ptr,
Interfaces.C.Strings.Value (option_key (This.Ptr)),
Interfaces.C.Strings.Value (option_value (This.Ptr)));
Old := option_next (Old);
end loop;
end Adjust;
procedure Finalize
(This : in out Option_List) is
begin
ao_free_options (This.Ptr);
end Finalize;
procedure Adjust
(This : in out Sample_Format) is
begin
This.C_Struct.Matrix := Interfaces.C.Strings.New_String
(Interfaces.C.Strings.Value (This.C_Struct.Matrix));
end Adjust;
procedure Finalize
(This : in out Sample_Format) is
begin
Interfaces.C.Strings.Free (This.C_Struct.Matrix);
end Finalize;
procedure Finalize
(This : in out Final_Controller) is
begin
for Addy of Device_List loop
Do_Close (Addy);
end loop;
ao_shutdown;
end Finalize;
---------------------------------
-- Data Types and Structures --
---------------------------------
function Kind
(Attributes : in Info)
return Output_Kind
is
Value : Interfaces.C.int := info_kind_get (Attributes.Ptr);
begin
if Value = type_live then
return Live_Output;
elsif Value = type_file then
return File_Output;
else
raise Program_Error;
end if;
end Kind;
function Name
(Attributes : in Info)
return String is
begin
return Interfaces.C.Strings.Value (info_name_get (Attributes.Ptr));
end Name;
function Short_Name
(Attributes : in Info)
return String is
begin
return Interfaces.C.Strings.Value (info_short_name_get (Attributes.Ptr));
end Short_Name;
function Preferred_Byte_Format
(Attributes : in Info)
return Endianness
is
Value : Interfaces.C.int := info_preferred_byte_format_get (Attributes.Ptr);
begin
if Value = sample_little_endian then
return Little_Endian;
elsif Value = sample_big_endian then
return Big_Endian;
elsif Value = sample_native_endian then
return Machine_Native;
else
raise Program_Error; -- libao would be doing weird shit to get here
end if;
end Preferred_Byte_Format;
function Priority_Level
(Attributes : in Info)
return Positive is
begin
return Positive (info_priority_get (Attributes.Ptr));
end Priority_Level;
function Comment
(Attributes : in Info)
return String is
begin
return Interfaces.C.Strings.Value (info_comment_get (Attributes.Ptr));
end Comment;
function Option_Count
(Attributes : in Info)
return Natural is
begin
return Natural (info_option_count_get (Attributes.Ptr));
end Option_Count;
function Option_Key
(Attributes : in Info;
Index : in Positive)
return String is
begin
return Interfaces.C.Strings.Value
(info_option_key_get (Attributes.Ptr, Interfaces.C.int (Index)));
end Option_Key;
function Image_Length
(Channel : in Channel_Mnemonic)
return Positive is
begin
case Channel is
when L | R | C | M | X =>
return 1;
when CL | CR | BL | BR | BC | SL | SR | A1 | A2 | A3 | A4 =>
return 2;
when LFE =>
return 3;
end case;
end Image_Length;
function Image_Length
(Channel_Matrix : in Mnemonic_Array)
return Natural
is
Result : Integer := Channel_Matrix'Length - 1;
begin
if Channel_Matrix'Length = 0 then
return 0;
end if;
for Channel of Channel_Matrix loop
Result := Result + Image_Length (Channel);
end loop;
return Result;
end Image_Length;
function Image
(Channel_Matrix : in Mnemonic_Array)
return String
is
Result : String (1 .. Image_Length (Channel_Matrix));
Position : Integer := 1;
begin
for Index in Integer range Channel_Matrix'First .. Channel_Matrix'Last - 1 loop
Result (Position .. Position + Image_Length (Channel_Matrix (Index))) :=
Channel_Matrix (Index)'Image & ",";
Position := Position + Image_Length (Channel_Matrix (Index)) + 1;
end loop;
Result (Position .. Result'Last) := Channel_Matrix (Channel_Matrix'Last)'Image;
return Result;
end Image;
function Create
(Bits, Rate, Channels : in Positive;
Byte_Format : in Endianness;
Channel_Matrix : in Mnemonic_Array)
return Sample_Format is
begin
return This : Sample_Format := (Ada.Finalization.Controlled with
C_Struct =>
(Bits => Interfaces.C.int (Bits),
Rate => Interfaces.C.int (Rate),
Channels => Interfaces.C.int (Channels),
Byte_Format => (case Byte_Format is
when Little_Endian => sample_little_endian,
when Big_Endian => sample_big_endian,
when Machine_Native => sample_native_endian),
Matrix => Interfaces.C.Strings.New_String (Image (Channel_Matrix))));
end Create;
--------------------------------------
-- Device Setup/Playback/Teardown --
--------------------------------------
procedure Append
(This : in out Option_List;
Key : in String;
Value : in String) is
begin
Do_Append (This.Ptr, Interfaces.C.To_C (Key), Interfaces.C.To_C (Value));
end Append;
procedure Append_Global_Option
(Key : in String;
Value : in String)
is
Result : Interfaces.C.int;
begin
Result := ao_append_global_option
(Interfaces.C.To_C (Key),
Interfaces.C.To_C (Value));
if Result = 0 then
raise Storage_Error;
elsif Result /= 1 then
raise Program_Error;
end if;
end Append_Global_Option;
procedure Open_Live
(Output : in out Device;
Driver_ID : in Driver_ID_Number;
Format : in Sample_Format'Class;
Options : in Option_List'Class)
is
Result : System.Address := ao_open_live
(Driver_ID => Interfaces.C.int (Driver_ID),
Format => Format.C_Struct'Address,
Options => Options.Ptr);
My_Errno : Interfaces.C.int;
begin
if Result = System.Null_Address then
My_Errno := get_errno;
if My_Errno = error_no_driver then
raise No_Driver_Error;
elsif My_Errno = error_not_live then
raise Not_Live_Error;
elsif My_Errno = error_bad_option then
raise Bad_Option_Error;
elsif My_Errno = error_open_device then
raise Open_Device_Error;
elsif My_Errno = error_bad_format then
raise Bad_Format_Error;
elsif My_Errno = error_fail then
raise General_Failure;
else
raise Program_Error;
end if;
else
Output.Close;
Device_List.Append (Result);
Output.Ptr := Result;
end if;
end Open_Live;
procedure Open_File
(Output : in out Device;
Driver_ID : in Driver_ID_Number;
Filename : in String;
Format : in Sample_Format'Class;
Options : in Option_List'Class;
Overwrite : in Boolean := False)
is
Result : System.Address := ao_open_file
(Driver_ID => Interfaces.C.int (Driver_ID),
Filename => Interfaces.C.To_C (Filename),
Overwrite => Boolean'Pos (Overwrite),
Format => Format.C_Struct'Address,
Options => Options.Ptr);
My_Errno : Interfaces.C.int;
begin
if Result = System.Null_Address then
My_Errno := get_errno;
if My_Errno = error_no_driver then
raise No_Driver_Error;
elsif My_Errno = error_not_file then
raise Not_File_Error;
elsif My_Errno = error_bad_option then
raise Bad_Option_Error;
elsif My_Errno = error_open_file then
raise Open_File_Error;
elsif My_Errno = error_file_exists then
raise File_Exists_Error;
elsif My_Errno = error_bad_format then
raise Bad_Format_Error;
elsif My_Errno = error_fail then
raise General_Failure;
else
raise Program_Error;
end if;
else
Output.Close;
Device_List.Append (Result);
Output.Ptr := Result;
end if;
end Open_File;
procedure Play
(Output : in Device;
Samples : in Data_Buffer)
is
Result : Interfaces.C.int := ao_play
(Output_Device => Output.Ptr,
Samples => Interfaces.C.To_C (Item => String (Samples), Append_Nul => False),
Num_Bytes => Interfaces.Unsigned_32 (Samples'Length));
begin
if Result = 0 then
raise General_Failure;
end if;
end Play;
procedure Close
(Output : in out Device)
is
Found : Boolean := False;
begin
for Index in reverse Integer range Device_List.First_Index .. Device_List.Last_Index loop
if Device_List.Element (Index) = Output.Ptr then
Device_List.Delete (Index);
Found := True;
end if;
end loop;
if Found then
Do_Close (Output.Ptr);
end if;
Output.Ptr := System.Null_Address;
end Close;
--------------------------
-- Driver Information --
--------------------------
function Driver_ID
(Short_Name : in String)
return Driver_ID_Number
is
Result : Interfaces.C.int;
begin
Result := ao_driver_id (Interfaces.C.To_C (Short_Name));
if Result = -1 then
raise No_Driver_Error;
elsif Result < 0 then
raise Program_Error;
end if;
return Driver_ID_Number (Result);
end Driver_ID;
function Default_Driver_ID
return Driver_ID_Number
is
Result : Interfaces.C.int;
begin
Result := ao_default_driver_id;
if Result = -1 then
raise No_Device_Error;
elsif Result < 0 then
raise Program_Error;
end if;
return Driver_ID_Number (Result);
end Default_Driver_ID;
function Driver_Info
(Ident : in Driver_ID_Number)
return Info
is
Result : System.Address;
begin
Result := ao_driver_info (Interfaces.C.int (Ident));
if Result = System.Null_Address then
raise No_Driver_Error;
else
return (Ptr => Result);
end if;
end Driver_Info;
function Driver_Info_List
return Info_Array
is
Count : Interfaces.C.int;
Carr : System.Address;
begin
Carr := ao_driver_info_list (Count);
return Actual : Info_Array (1 .. Positive (Count)) do
for N in Integer range Actual'First .. Actual'Last loop
Actual (N) := (Ptr => info_item_get (Carr, Interfaces.C.int (N)));
end loop;
end return;
end Driver_Info_List;
function File_Extension
(Ident : in Driver_ID_Number)
return String
is
Result : Interfaces.C.Strings.chars_ptr := ao_file_extension (Interfaces.C.int (Ident));
begin
if Result = Interfaces.C.Strings.Null_Ptr then
return "";
else
return Interfaces.C.Strings.Value (Result);
end if;
end File_Extension;
---------------------
-- Miscellaneous --
---------------------
function Is_Big_Endian
return Boolean is
begin
case ao_is_big_endian is
when 1 => return True;
when 0 => return False;
when others => raise Program_Error;
end case;
end Is_Big_Endian;
begin
ao_initialize;
end Libao;
|