summaryrefslogtreecommitdiff
path: root/src/portaudio-streams.adb
blob: 6b05008a78acf745cfa42757700de571c0f247e6 (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
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079


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


pragma Ada_2012;


with

    Interfaces.C,
    System.Address_To_Access_Conversions;

use type

    Interfaces.C.double,
    Interfaces.C.int,
    Interfaces.C.long,
    Interfaces.C.unsigned_long,
    System.Address;


package body Portaudio.Streams is


    ------------------------
    --  Constants From C  --
    ------------------------

    pa_float_32 : constant Interfaces.C.unsigned_long;
    pragma Import (C, pa_float_32, "pa_float_32");

    pa_int_32 : constant Interfaces.C.unsigned_long;
    pragma Import (C, pa_int_32, "pa_int_32");

    pa_int_24 : constant Interfaces.C.unsigned_long;
    pragma Import (C, pa_int_24, "pa_int_24");

    pa_int_16 : constant Interfaces.C.unsigned_long;
    pragma Import (C, pa_int_16, "pa_int_16");

    pa_int_8 : constant Interfaces.C.unsigned_long;
    pragma Import (C, pa_int_8, "pa_int_8");

    pa_uint_8 : constant Interfaces.C.unsigned_long;
    pragma Import (C, pa_uint_8, "pa_uint_8");


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


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

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

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


    pa_input_underflow : constant Interfaces.C.unsigned_long;
    pragma Import (C, pa_input_underflow, "pa_input_underflow");

    pa_input_overflow : constant Interfaces.C.unsigned_long;
    pragma Import (C, pa_input_overflow, "pa_input_overflow");

    pa_output_underflow : constant Interfaces.C.unsigned_long;
    pragma Import (C, pa_output_underflow, "pa_output_underflow");

    pa_output_overflow : constant Interfaces.C.unsigned_long;
    pragma Import (C, pa_output_overflow, "pa_output_overflow");

    pa_priming_output : constant Interfaces.C.unsigned_long;
    pragma Import (C, pa_priming_output, "pa_priming_output");




    ------------------------
    --  Functions From C  --
    ------------------------

    function pa_is_format_supported
           (Input  : in System.Address;
            Output : in System.Address;
            Rate   : in Interfaces.C.double)
        return Interfaces.C.int;
    pragma Import (C, pa_is_format_supported, "Pa_IsFormatSupported");

    function pa_open_stream
           (Stream     : in out System.Address;
            In_Params  : in     System.Address;
            Out_Params : in     System.Address;
            Rate       : in     Interfaces.C.double;
            Frames     : in     Interfaces.C.unsigned_long;
            Flags      : in     Interfaces.C.unsigned_long;
            Callback   : in     System.Address;
            Userdata   : in     System.Address)
        return Interfaces.C.int;
    pragma Import (C, pa_open_stream, "Pa_OpenStream");

    function pa_open_default_stream
           (Stream    : in out System.Address;
            In_Chans  : in     Interfaces.C.int;
            Out_Chans : in     Interfaces.C.int;
            Format    : in     Interfaces.C.unsigned_long;
            Rate      : in     Interfaces.C.double;
            Frames    : in     Interfaces.C.unsigned_long;
            Callback  : in     System.Address;
            Userdata  : in     System.Address)
        return Interfaces.C.int;
    pragma Import (C, pa_open_default_stream, "Pa_OpenDefaultStream");

    function pa_close_stream
           (Stream : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, pa_close_stream, "Pa_CloseStream");

    function pa_set_stream_finished_callback
           (Stream   : in System.Address;
            Callback : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, pa_set_stream_finished_callback, "Pa_SetStreamFinishedCallback");

    function pa_start_stream
           (Stream : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, pa_start_stream, "Pa_StartStream");

    function pa_stop_stream
           (Stream : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, pa_stop_stream, "Pa_StopStream");

    function pa_abort_stream
           (Stream : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, pa_abort_stream, "Pa_AbortStream");

    function pa_is_stream_stopped
           (Stream : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, pa_is_stream_stopped, "Pa_IsStreamStopped");

    function pa_is_stream_active
           (Stream : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, pa_is_stream_active, "Pa_IsStreamActive");

    function pa_get_stream_info
           (Stream : in System.Address)
        return System.Address;
    pragma Import (C, pa_get_stream_info, "Pa_GetStreamInfo");

    function pa_get_stream_time
           (Stream : in System.Address)
        return Interfaces.C.double;
    pragma Import (C, pa_get_stream_time, "Pa_GetStreamTime");

    function pa_get_stream_cpu_load
           (Stream : in System.Address)
        return Interfaces.C.double;
    pragma Import (C, pa_get_stream_cpu_load, "Pa_GetStreamCpuLoad");

    function pa_read_stream
           (Stream : in System.Address;
            Buffer : in System.Address;
            Frames : in Interfaces.C.unsigned_long)
        return Interfaces.C.int;
    pragma Import (C, pa_read_stream, "Pa_ReadStream");

    function pa_write_stream
           (Stream : in System.Address;
            Buffer : in System.Address;
            Frames : in Interfaces.C.unsigned_long)
        return Interfaces.C.int;
    pragma Import (C, pa_write_stream, "Pa_WriteStream");

    function pa_get_stream_read_available
           (Stream : in System.Address)
        return Interfaces.C.long;
    pragma Import (C, pa_get_stream_read_available, "Pa_GetStreamReadAvailable");

    function pa_get_stream_write_available
           (Stream : in System.Address)
        return Interfaces.C.long;
    pragma Import (C, pa_get_stream_write_available, "Pa_GetStreamWriteAvailable");

    function pa_get_sample_size
           (Form : in Interfaces.C.unsigned_long)
        return Interfaces.C.int;
    pragma Import (C, pa_get_sample_size, "Pa_GetSampleSize");




    ------------------------
    --  Internal Utility  --
    ------------------------

    function To_For_Sam
           (Num : in Interfaces.C.unsigned_long)
        return Sample_Format is
    begin
        if Num = pa_float_32 then
            return Float_32_Format;
        elsif Num = pa_int_32 then
            return Int_32_Format;
        elsif Num = pa_int_24 then
            return Int_24_Format;
        elsif Num = pa_int_16 then
            return Int_16_Format;
        elsif Num = pa_int_8 then
            return Int_8_Format;
        elsif Num = pa_uint_8 then
            return UInt_8_Format;
        else
            raise Program_Error;
        end if;
    end To_For_Sam;

    function To_Cnum
           (Format : in Sample_Format)
        return Interfaces.C.unsigned_long is
    begin
        case Format is
            when Float_32_Format => return pa_float_32;
            when Int_32_Format   => return pa_int_32;
            when Int_24_Format   => return pa_int_24;
            when Int_16_Format   => return pa_int_16;
            when Int_8_Format    => return pa_int_8;
            when UInt_8_Format   => return pa_uint_8;
        end case;
    end To_Cnum;

    function To_Cint
           (Ret_Value : in Callback_Result)
        return Interfaces.C.int is
    begin
        case Ret_Value is
            when Continue => return pa_continue;
            when Complete => return pa_complete;
            when Finish   => return pa_abort;
        end case;
    end To_Cint;




    ----------------------
    --  Callback Hooks  --
    ----------------------

    function Stream_Callback_Hook
           (Input_Ptr   : in System.Address;
            Output_Ptr  : in System.Address;
            Frame_Count : in Interfaces.C.unsigned_long;
            Time_Ptr    : in System.Address;
            Flag_Mask   : in Interfaces.C.unsigned_long;
            Userdata    : in System.Address)
        return Interfaces.C.int
    is
        Stream_Actual : Audio_Stream;
        for Stream_Actual'Address use Userdata;
        pragma Import (Ada, Stream_Actual);

        Input_Buffer : Buffer :=
           (My_Sam_Code => Stream_Actual.Sin,
            My_Channels => Natural (Stream_Actual.Chin),
            My_Frames   => Frame_Amount (Frame_Count),
            My_Array    => Input_Ptr);

        Output_Buffer : Buffer :=
           (My_Sam_Code => Stream_Actual.Sout,
            My_Channels => Natural (Stream_Actual.Chout),
            My_Frames   => Frame_Amount (Frame_Count),
            My_Array    => Output_Ptr);

        Raw_Time : C_Callback_Time_Info;
        for Raw_Time'Address use Time_Ptr;
        pragma Import (Ada, Raw_Time);

        Result : Callback_Result;
    begin
        Result := Stream_Actual.Func
           (Input_Buffer,
            Output_Buffer,
            Frame_Amount (Frame_Count),
            (Input_ADC  => Time (Raw_Time.My_Input_ADC),
             Current    => Time (Raw_Time.My_Current),
             Output_DAC => Time (Raw_Time.My_Output_DAC)),
            (Input_Underflow  => (Flag_Mask and pa_input_underflow) /= 0,
             Input_Overflow   => (Flag_Mask and pa_input_overflow) /= 0,
             Output_Underflow => (Flag_Mask and pa_output_underflow) /= 0,
             Output_Overflow  => (Flag_Mask and pa_output_overflow) /= 0,
             Priming_Output   => (Flag_Mask and pa_priming_output) /= 0));
        return To_Cint (Result);
    end Stream_Callback_Hook;




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

    function Kind
           (Store : in Buffer)
        return Sample_Format is
    begin
        return To_For_Sam (Store.My_Sam_Code);
    end Kind;

    function Channels
           (Store : in Buffer)
        return Natural is
    begin
        return Store.My_Channels;
    end Channels;

    function Frames
           (Store : in Buffer)
        return Frame_Amount is
    begin
        return Store.My_Frames;
    end Frames;

    function Get
           (Store   : in Buffer;
            Frame   : in Frame_Amount;
            Channel : in Positive)
        return Interfaces.IEEE_Float_32
    is
        Actual : Float_32_Array (1 .. Store.My_Channels * Integer (Store.My_Frames));
        for Actual'Address use Store.My_Array;
        pragma Import (Ada, Actual);
    begin
        return Actual ((Integer (Frame) - 1) * Store.My_Channels + Channel);
    end Get;

    function Get
           (Store   : in Buffer;
            Frame   : in Frame_Amount;
            Channel : in Positive)
        return Interfaces.Integer_32
    is
        Actual : Int_32_Array (1 .. Store.My_Channels * Integer (Store.My_Frames));
        for Actual'Address use Store.My_Array;
        pragma Import (Ada, Actual);
    begin
        return Actual ((Integer (Frame) - 1) * Store.My_Channels + Channel);
    end Get;

    function Get
           (Store   : in Buffer;
            Frame   : in Frame_Amount;
            Channel : in Positive)
        return Integer_24
    is
        Actual : Int_24_Array (1 .. Store.My_Channels * Integer (Store.My_Frames));
        for Actual'Address use Store.My_Array;
        pragma Import (Ada, Actual);
    begin
        return Actual ((Integer (Frame) - 1) * Store.My_Channels + Channel);
    end Get;

    function Get
           (Store   : in Buffer;
            Frame   : in Frame_Amount;
            Channel : in Positive)
        return Interfaces.Integer_16
    is
        Actual : Int_16_Array (1 .. Store.My_Channels * Integer (Store.My_Frames));
        for Actual'Address use Store.My_Array;
        pragma Import (Ada, Actual);
    begin
        return Actual ((Integer (Frame) - 1) * Store.My_Channels + Channel);
    end Get;

    function Get
           (Store   : in Buffer;
            Frame   : in Frame_Amount;
            Channel : in Positive)
        return Interfaces.Integer_8
    is
        Actual : Int_8_Array (1 .. Store.My_Channels * Integer (Store.My_Frames));
        for Actual'Address use Store.My_Array;
        pragma Import (Ada, Actual);
    begin
        return Actual ((Integer (Frame) - 1) * Store.My_Channels + Channel);
    end Get;

    function Get
           (Store   : in Buffer;
            Frame   : in Frame_Amount;
            Channel : in Positive)
        return Interfaces.Unsigned_8
    is
        Actual : UInt_8_Array (1 .. Store.My_Channels * Integer (Store.My_Frames));
        for Actual'Address use Store.My_Array;
        pragma Import (Ada, Actual);
    begin
        return Actual ((Integer (Frame) - 1) * Store.My_Channels + Channel);
    end Get;

    procedure Put
           (Store   : in Buffer;
            Frame   : in Frame_Amount;
            Channel : in Positive;
            Value   : in Interfaces.IEEE_Float_32)
    is
        Actual : Float_32_Array (1 .. Store.My_Channels * Integer (Store.My_Frames));
        for Actual'Address use Store.My_Array;
        pragma Import (Ada, Actual);
    begin
        Actual ((Integer (Frame) - 1) * Store.My_Channels + Channel) := Value;
    end Put;

    procedure Put
           (Store   : in Buffer;
            Frame   : in Frame_Amount;
            Channel : in Positive;
            Value   : in Interfaces.Integer_32)
    is
        Actual : Int_32_Array (1 .. Store.My_Channels * Integer (Store.My_Frames));
        for Actual'Address use Store.My_Array;
        pragma Import (Ada, Actual);
    begin
        Actual ((Integer (Frame) - 1) * Store.My_Channels + Channel) := Value;
    end Put;

    procedure Put
           (Store   : in Buffer;
            Frame   : in Frame_Amount;
            Channel : in Positive;
            Value   : in Integer_24)
    is
        Actual : Int_24_Array (1 .. Store.My_Channels * Integer (Store.My_Frames));
        for Actual'Address use Store.My_Array;
        pragma Import (Ada, Actual);
    begin
        Actual ((Integer (Frame) - 1) * Store.My_Channels + Channel) := Value;
    end Put;

    procedure Put
           (Store   : in Buffer;
            Frame   : in Frame_Amount;
            Channel : in Positive;
            Value   : in Interfaces.Integer_16)
    is
        Actual : Int_16_Array (1 .. Store.My_Channels * Integer (Store.My_Frames));
        for Actual'Address use Store.My_Array;
        pragma Import (Ada, Actual);
    begin
        Actual ((Integer (Frame) - 1) * Store.My_Channels + Channel) := Value;
    end Put;

    procedure Put
           (Store   : in Buffer;
            Frame   : in Frame_Amount;
            Channel : in Positive;
            Value   : in Interfaces.Integer_8)
    is
        Actual : Int_8_Array (1 .. Store.My_Channels * Integer (Store.My_Frames));
        for Actual'Address use Store.My_Array;
        pragma Import (Ada, Actual);
    begin
        Actual ((Integer (Frame) - 1) * Store.My_Channels + Channel) := Value;
    end Put;

    procedure Put
           (Store   : in Buffer;
            Frame   : in Frame_Amount;
            Channel : in Positive;
            Value   : in Interfaces.Unsigned_8)
    is
        Actual : UInt_8_Array (1 .. Store.My_Channels * Integer (Store.My_Frames));
        for Actual'Address use Store.My_Array;
        pragma Import (Ada, Actual);
    begin
        Actual ((Integer (Frame) - 1) * Store.My_Channels + Channel) := Value;
    end Put;

    function Wrap
           (Store    : in Float_32_Array;
            Frames   : in Frame_Amount;
            Channels : in Natural)
        return Buffer is
    begin
        return
           (My_Sam_Code => pa_float_32,
            My_Channels => Channels,
            My_Frames   => Frames,
            My_Array    => Store'Address);
    end Wrap;

    function Wrap
           (Store    : in Int_32_Array;
            Frames   : in Frame_Amount;
            Channels : in Natural)
        return Buffer is
    begin
        return
           (My_Sam_Code => pa_int_32,
            My_Channels => Channels,
            My_Frames   => Frames,
            My_Array    => Store'Address);
    end Wrap;

    function Wrap
           (Store    : in Int_24_Array;
            Frames   : in Frame_Amount;
            Channels : in Natural)
        return Buffer is
    begin
        return
           (My_Sam_Code => pa_int_24,
            My_Channels => Channels,
            My_Frames   => Frames,
            My_Array    => Store'Address);
    end Wrap;

    function Wrap
           (Store    : in Int_16_Array;
            Frames   : in Frame_Amount;
            Channels : in Natural)
        return Buffer is
    begin
        return
           (My_Sam_Code => pa_int_16,
            My_Channels => Channels,
            My_Frames   => Frames,
            My_Array    => Store'Address);
    end Wrap;

    function Wrap
           (Store    : in Int_8_Array;
            Frames   : in Frame_Amount;
            Channels : in Natural)
        return Buffer is
    begin
        return
           (My_Sam_Code => pa_int_8,
            My_Channels => Channels,
            My_Frames   => Frames,
            My_Array    => Store'Address);
    end Wrap;

    function Wrap
           (Store    : in UInt_8_Array;
            Frames   : in Frame_Amount;
            Channels : in Natural)
        return Buffer is
    begin
        return
           (My_Sam_Code => pa_uint_8,
            My_Channels => Channels,
            My_Frames   => Frames,
            My_Array    => Store'Address);
    end Wrap;

    function Create
           (Device   : in Device_Index;
            Channels : in Natural;
            Format   : in Sample_Format;
            Latency  : in Time)
        return Parameters is
    begin
        return
           (My_Device   => Interfaces.C.int (Device) - 1,
            My_Channels => Interfaces.C.int (Channels),
            My_Samples  => To_Cnum (Format),
            My_Latency  => Interfaces.C.double (Latency),
            My_Specific => System.Null_Address);
    end Create;


    function "+"
           (A, B : in Stream_Flags)
        return Stream_Flags is
    begin
        return Stream_Flags (Interfaces.C.unsigned_long (A) or Interfaces.C.unsigned_long (B));
    end "+";




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

    function Is_Open
           (Stream : in Audio_Stream)
        return Boolean is
    begin
        return Stream.Open;
    end Is_Open;




    -----------------------
    --  API Subprograms  --
    -----------------------

    function Is_Format_Supported
           (Input  : access Parameters;
            Output : access Parameters;
            Rate   : in     Hertz)
        return Boolean
    is
        Code : Interfaces.C.int;
        package Param_Conversions is new System.Address_To_Access_Conversions (Parameters);
        Input_Address  : System.Address :=
            Param_Conversions.To_Address (Param_Conversions.Object_Pointer (Input));
        Output_Address : System.Address :=
            Param_Conversions.To_Address (Param_Conversions.Object_Pointer (Output));
    begin
        Code := pa_is_format_supported
           (Input_Address,
            Output_Address,
            Interfaces.C.double (Rate));
        return Code = pa_format_is_supported;
    end Is_Format_Supported;

    procedure Open_Input
           (Stream        : in out Audio_Stream;
            Input_Params  : in     Parameters;
            Sample_Rate   : in     Hertz;
            Buffer_Frames : in     Frame_Amount;
            Bunting       : in     Stream_Flags;
            Callback      : in     Callback_Function)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_open_stream
           (Stream.Ptr,
            Input_Params'Address,
            System.Null_Address,
            Interfaces.C.double (Sample_Rate),
            Interfaces.C.unsigned_long (Buffer_Frames),
            Interfaces.C.unsigned_long (Bunting),
            Stream_Callback_Hook'Address,
            Stream'Address);
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        else
            Stream.Open  := True;
            Stream.Func  := Callback;
            Stream.Chin  := Input_Params.My_Channels;
            Stream.Chout := 0;
            Stream.Sin   := Input_Params.My_Samples;
            Stream.Sout  := 0;
        end if;
    end Open_Input;

    procedure Open_Output
           (Stream        : in out Audio_Stream;
            Output_Params : in     Parameters;
            Sample_Rate   : in     Hertz;
            Buffer_Frames : in     Frame_Amount;
            Bunting       : in     Stream_Flags;
            Callback      : in     Callback_Function)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_open_stream
           (Stream.Ptr,
            System.Null_Address,
            Output_Params'Address,
            Interfaces.C.double (Sample_Rate),
            Interfaces.C.unsigned_long (Buffer_Frames),
            Interfaces.C.unsigned_long (Bunting),
            Stream_Callback_Hook'Address,
            Stream'Address);
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        else
            Stream.Open  := True;
            Stream.Func  := Callback;
            Stream.Chin  := 0;
            Stream.Chout := Output_Params.My_Channels;
            Stream.Sin   := 0;
            Stream.Sout  := Output_Params.My_Samples;
        end if;
    end Open_Output;

    procedure Open_Full
           (Stream        : in out Audio_Stream;
            Input_Params  : in     Parameters;
            Output_Params : in     Parameters;
            Sample_Rate   : in     Hertz;
            Buffer_Frames : in     Frame_Amount;
            Bunting       : in     Stream_Flags;
            Callback      : in     Callback_Function)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_open_stream
           (Stream.Ptr,
            Input_Params'Address,
            Output_Params'Address,
            Interfaces.C.double (Sample_Rate),
            Interfaces.C.unsigned_long (Buffer_Frames),
            Interfaces.C.unsigned_long (Bunting),
            Stream_Callback_Hook'Address,
            Stream'Address);
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        else
            Stream.Open  := True;
            Stream.Func  := Callback;
            Stream.Chin  := Input_Params.My_Channels;
            Stream.Chout := Output_Params.My_Channels;
            Stream.Sin   := Input_Params.My_Samples;
            Stream.Sout  := Output_Params.My_Samples;
        end if;
    end Open_Full;

    procedure Open_Input_Blocking
           (Stream        : in out Audio_Stream;
            Input_Params  : in     Parameters;
            Sample_Rate   : in     Hertz;
            Buffer_Frames : in     Frame_Amount;
            Bunting       : in     Stream_Flags)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_open_stream
           (Stream.Ptr,
            Input_Params'Address,
            System.Null_Address,
            Interfaces.C.double (Sample_Rate),
            Interfaces.C.unsigned_long (Buffer_Frames),
            Interfaces.C.unsigned_long (Bunting),
            System.Null_Address,
            System.Null_Address);
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        else
            Stream.Open := True;
        end if;
    end Open_Input_Blocking;

    procedure Open_Output_Blocking
           (Stream        : in out Audio_Stream;
            Output_Params : in     Parameters;
            Sample_Rate   : in     Hertz;
            Buffer_Frames : in     Frame_Amount;
            Bunting       : in     Stream_Flags)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_open_stream
           (Stream.Ptr,
            System.Null_Address,
            Output_Params'Address,
            Interfaces.C.double (Sample_Rate),
            Interfaces.C.unsigned_long (Buffer_Frames),
            Interfaces.C.unsigned_long (Bunting),
            System.Null_Address,
            System.Null_Address);
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        else
            Stream.Open := True;
        end if;
    end Open_Output_Blocking;

    procedure Open_Full_Blocking
           (Stream        : in out Audio_Stream;
            Input_Params  : in     Parameters;
            Output_Params : in     Parameters;
            Sample_Rate   : in     Hertz;
            Buffer_Frames : in     Frame_Amount;
            Bunting       : in     Stream_Flags)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_open_stream
           (Stream.Ptr,
            Input_Params'Address,
            Output_Params'Address,
            Interfaces.C.double (Sample_Rate),
            Interfaces.C.unsigned_long (Buffer_Frames),
            Interfaces.C.unsigned_long (Bunting),
            System.Null_Address,
            System.Null_Address);
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        else
            Stream.Open := True;
        end if;
    end Open_Full_Blocking;

    procedure Open_Default
           (Stream          : in out Audio_Stream;
            Input_Channels  : in     Natural;
            Output_Channels : in     Natural;
            Format          : in     Sample_Format;
            Sample_Rate     : in     Hertz;
            Buffer_Frames   : in     Frame_Amount;
            Callback        : in     Callback_Function)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_open_default_stream
           (Stream.Ptr,
            Interfaces.C.int (Input_Channels),
            Interfaces.C.int (Output_Channels),
            To_Cnum (Format),
            Interfaces.C.double (Sample_Rate),
            Interfaces.C.unsigned_long (Buffer_Frames),
            Stream_Callback_Hook'Address,
            Stream'Address);
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        else
            Stream.Open  := True;
            Stream.Func  := Callback;
            Stream.Chin  := Interfaces.C.int (Input_Channels);
            Stream.Chout := Interfaces.C.int (Output_Channels);
            Stream.Sin   := To_Cnum (Format);
            Stream.Sout  := To_Cnum (Format);
        end if;
    end Open_Default;

    procedure Open_Default_Blocking
           (Stream          : in out Audio_Stream;
            Input_Channels  : in     Natural;
            Output_Channels : in     Natural;
            Format          : in     Sample_Format;
            Sample_Rate     : in     Hertz;
            Buffer_Frames   : in     Frame_Amount)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_open_default_stream
           (Stream.Ptr,
            Interfaces.C.int (Input_Channels),
            Interfaces.C.int (Output_Channels),
            To_Cnum (Format),
            Interfaces.C.double (Sample_Rate),
            Interfaces.C.unsigned_long (Buffer_Frames),
            System.Null_Address,
            System.Null_Address);
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        else
            Stream.Open := True;
        end if;
    end Open_Default_Blocking;

    procedure Close
           (Stream : in out Audio_Stream)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_close_stream (Stream.Ptr);
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        else
            Stream.Open  := False;
            Stream.Chin  := 0;
            Stream.Chout := 0;
            Stream.Sin   := 0;
            Stream.Sout  := 0;
        end if;
    end Close;

    procedure Start
           (Stream : in Audio_Stream)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_start_stream (Stream.Ptr);
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        end if;
    end Start;

    procedure Stop
           (Stream : in Audio_Stream)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_stop_stream (Stream.Ptr);
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        end if;
    end Stop;

    procedure Term
           (Stream : in Audio_Stream)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_abort_stream (Stream.Ptr);
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        end if;
    end Term;

    function Is_Stopped
           (Stream : in Audio_Stream)
        return Boolean
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_is_stream_stopped (Stream.Ptr);
        if Code = 1 then
            return True;
        elsif Code = 0 then
            return False;
        else
            Raise_Error (Code);
            raise Program_Error;
        end if;
    end Is_Stopped;

    function Is_Active
           (Stream : in Audio_Stream)
        return Boolean
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_is_stream_active (Stream.Ptr);
        if Code = 1 then
            return True;
        elsif Code = 0 then
            return False;
        else
            Raise_Error (Code);
            raise Program_Error;
        end if;
    end Is_Active;

    function Get_Info
           (Stream : in Audio_Stream)
        return Stream_Info
    is
        Result : System.Address;
    begin
        Result := pa_get_stream_info (Stream.Ptr);
        if Result = System.Null_Address then
            raise General_Failure;
        else
            declare
                C_Data : C_Stream_Info;
                for C_Data'Address use Result;
                pragma Import (Ada, C_Data);
            begin
                return
                   (Input_Latency  => Time (C_Data.My_Input_Latency),
                    Output_Latency => Time (C_Data.My_Output_Latency),
                    Sample_Rate    => Hertz (C_Data.My_Sample_Rate));
            end;
        end if;
    end Get_Info;

    function Get_Time
           (Stream : in Audio_Stream)
        return Time
    is
        Result : Interfaces.C.double;
    begin
        Result := pa_get_stream_time (Stream.Ptr);
        if Result = 0.0 then
            raise General_Failure;
        else
            return Time (Result);
        end if;
    end Get_Time;

    function Get_CPU_Load
           (Stream : in Audio_Stream)
        return Load is
    begin
        return Load (pa_get_stream_cpu_load (Stream.Ptr));
    end Get_CPU_Load;

    procedure Read_Blocking
           (Stream : in Audio_Stream;
            Store  : in Buffer'Class;
            Frames : in Frame_Amount)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_read_stream
           (Stream.Ptr,
            Store.My_Array,
            Interfaces.C.unsigned_long (Frames));
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        end if;
    end Read_Blocking;

    procedure Write_Blocking
           (Stream : in Audio_Stream;
            Store  : in Buffer'Class;
            Frames : in Frame_Amount)
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_write_stream
           (Stream.Ptr,
            Store.My_Array,
            Interfaces.C.unsigned_long (Frames));
        if Code /= pa_no_error then
            Raise_Error (Code);
            raise Program_Error;
        end if;
    end Write_Blocking;

    function Get_Read_Available
           (Stream : in Audio_Stream)
        return Frame_Amount
    is
        Code : Interfaces.C.long;
    begin
        Code := pa_get_stream_read_available (Stream.Ptr);
        if Code < 0 then
            Raise_Error (Interfaces.C.int (Code));
            raise Program_Error;
        else
            return Frame_Amount (Code);
        end if;
    end Get_Read_Available;

    function Get_Write_Available
           (Stream : in Audio_Stream)
        return Frame_Amount
    is
        Code : Interfaces.C.long;
    begin
        Code := pa_get_stream_write_available (Stream.Ptr);
        if Code < 0 then
            Raise_Error (Interfaces.C.int (Code));
            raise Program_Error;
        else
            return Frame_Amount (Code);
        end if;
    end Get_Write_Available;

    function Get_Sample_Size
           (Format : in Sample_Format)
        return Positive
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_get_sample_size (To_Cnum (Format));
        if Code <= 0 then
            Raise_Error (Code);
            raise Program_Error;
        else
            return Positive (Code);
        end if;
    end Get_Sample_Size;


end Portaudio.Streams;