summaryrefslogtreecommitdiff
path: root/src/portaudio-devices.adb
blob: 7e0e5e6d6cf9f08966a948d5ad97e9fcba27ce8e (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


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


pragma Ada_2012;


with

    Interfaces.C.Strings,
    System;

use type

    Interfaces.C.int,
    System.Address;


package body Portaudio.Devices is


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

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




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

    function pa_get_host_api_count
        return Interfaces.C.int;
    pragma Import (C, pa_get_host_api_count, "Pa_GetHostApiCount");

    function pa_get_default_host_api
        return Interfaces.C.int;
    pragma Import (C, pa_get_default_host_api, "Pa_GetDefaultHostApi");

    function pa_get_host_api_info
           (Index : in Interfaces.C.int)
        return System.Address;
    pragma Import (C, pa_get_host_api_info, "Pa_GetHostApiInfo");

    function pa_host_api_type_id_to_host_api_index
           (Kind : in Interfaces.C.int)
        return Interfaces.C.int;
    pragma Import (C, pa_host_api_type_id_to_host_api_index, "Pa_HostApiTypeIdToHostApiIndex");

    function pa_host_api_device_index_to_device_index
           (Host : in Interfaces.C.int;
            Dev  : in Interfaces.C.int)
        return Interfaces.C.int;
    pragma Import (C, pa_host_api_device_index_to_device_index,
        "Pa_HostApiDeviceIndexToDeviceIndex");

    function pa_get_device_count
        return Interfaces.C.int;
    pragma Import (C, pa_get_device_count, "Pa_GetDeviceCount");

    function pa_get_default_input_device
        return Interfaces.C.int;
    pragma Import (C, pa_get_default_input_device, "Pa_GetDefaultInputDevice");

    function pa_get_default_output_device
        return Interfaces.C.int;
    pragma Import (C, pa_get_default_output_device, "Pa_GetDefaultOutputDevice");

    function pa_get_device_info
           (Index : in Interfaces.C.int)
        return System.Address;
    pragma Import (C, pa_get_device_info, "Pa_GetDeviceInfo");




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

    function Kind
           (Info : in Host_API_Info)
        return Host_API_Kind
    is
        Internal : C_Host_API_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        return To_Hat_Kind (Internal.My_Host_API_Type);
    end Kind;

    function Name
           (Info : in Host_API_Info)
        return String
    is
        Internal : C_Host_API_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        return Interfaces.C.Strings.Value (Internal.My_Name);
    end Name;

    function Device_Count
           (Info : in Host_API_Info)
        return Natural
    is
        Internal : C_Host_API_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        return Natural (Internal.My_Device_Count);
    end Device_Count;

    function Default_Input_Device
           (Info : in Host_API_Info)
        return Device_Index
    is
        Internal : C_Host_API_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        if Internal.My_Default_Input = pa_no_device then
            return No_Device;
        else
            return Device_Index (Internal.My_Default_Input) + 1;
        end if;
    end Default_Input_Device;

    function Default_Output_Device
           (Info : in Host_API_Info)
        return Device_Index
    is
        Internal : C_Host_API_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        if Internal.My_Default_Output = pa_no_device then
            return No_Device;
        else
            return Device_Index (Internal.My_Default_Output) + 1;
        end if;
    end Default_Output_Device;


    function Name
           (Info : in Device_Info)
        return String
    is
        Internal : C_Device_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        return Interfaces.C.Strings.Value (Internal.My_Name);
    end Name;

    function Host_API
           (Info : in Device_Info)
        return Host_API_Index
    is
        Internal : C_Device_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        return Host_API_Index (Internal.My_Host_API_Index + 1);
    end Host_API;

    function Max_Input_Channels
           (Info : in Device_Info)
        return Natural
    is
        Internal : C_Device_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        return Natural (Internal.My_Input_Channels);
    end Max_Input_Channels;

    function Max_Output_Channels
           (Info : in Device_Info)
        return Natural
    is
        Internal : C_Device_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        return Natural (Internal.My_Output_Channels);
    end Max_Output_Channels;

    function Default_Low_Input_Latency
           (Info : in Device_Info)
        return Time
    is
        Internal : C_Device_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        return Time (Internal.My_Low_Input_Latency);
    end Default_Low_Input_Latency;

    function Default_Low_Output_Latency
           (Info : in Device_Info)
        return Time
    is
        Internal : C_Device_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        return Time (Internal.My_Low_Output_Latency);
    end Default_Low_Output_Latency;

    function Default_High_Input_Latency
           (Info : in Device_Info)
        return Time
    is
        Internal : C_Device_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        return Time (Internal.My_High_Input_Latency);
    end Default_High_Input_Latency;

    function Default_High_Output_Latency
           (Info : in Device_Info)
        return Time
    is
        Internal : C_Device_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        return Time (Internal.My_High_Output_Latency);
    end Default_High_Output_Latency;

    function Default_Sample_Rate
           (Info : in Device_Info)
        return Hertz
    is
        Internal : C_Device_Info;
        for Internal'Address use Info.Ptr;
        pragma Import (Ada, Internal);
    begin
        return Hertz (Internal.My_Sample_Rate);
    end Default_Sample_Rate;




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

    function Get_Host_API_Count
        return Natural
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_get_host_api_count;
        if Code < 0 then
            Raise_Error (Code);
            raise Program_Error;
        else
            return Natural (Code);
        end if;
    end Get_Host_API_Count;

    function Get_Default_Host_API
        return Host_API_Index
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_get_default_host_api;
        if Code < 0 then
            Raise_Error (Code);
            raise Program_Error;
        else
            return Host_API_Index (Code) + 1;
        end if;
    end Get_Default_Host_API;

    function Get_Host_API_Info
           (Index : in Host_API_Index)
        return Host_API_Info
    is
        Result : System.Address;
    begin
        Result := pa_get_host_api_info (Interfaces.C.int (Index) - 1);
        if Result = System.Null_Address then
            raise General_Failure;
        else
            return (Ptr => Result);
        end if;
    end Get_Host_API_Info;

    function To_Host_API_Index
           (Kind : in Host_API_Kind)
        return Host_API_Index
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_host_api_type_id_to_host_api_index (To_Cint (Kind));
        if Code < 0 then
            Raise_Error (Code);
            raise Program_Error;
        else
            return Host_API_Index (Code) + 1;
        end if;
    end To_Host_API_Index;

    function To_Device_Index
           (Host_API    : in Host_API_Index;
            Host_Device : in Positive)
        return Device_Index
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_host_api_device_index_to_device_index
           (Interfaces.C.int (Host_API) - 1,
            Interfaces.C.int (Host_API) - 1);
        if Code < 0 then
            Raise_Error (Code);
            raise Program_Error;
        else
            return Device_Index (Code + 1);
        end if;
    end To_Device_Index;

    function Get_Device_Count
        return Natural
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_get_device_count;
        if Code < 0 then
            Raise_Error (Code);
            raise Program_Error;
        else
            return Natural (Code);
        end if;
    end Get_Device_Count;

    function Get_Default_Input_Device
        return Device_Index
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_get_default_input_device;
        if Code = pa_no_device then
            return No_Device;
        else
            return Device_Index (Code + 1);
        end if;
    end Get_Default_Input_Device;

    function Get_Default_Output_Device
        return Device_Index
    is
        Code : Interfaces.C.int;
    begin
        Code := pa_get_default_output_device;
        if Code = pa_no_device then
            return No_Device;
        else
            return Device_Index (Code + 1);
        end if;
    end Get_Default_Output_Device;

    function Get_Device_Info
           (Index : in Device_Index)
        return Device_Info
    is
        Result : System.Address;
    begin
        Result := pa_get_device_info (Interfaces.C.int (Index) - 1);
        if Result = System.Null_Address then
            raise General_Failure;
        else
            return (Ptr => Result);
        end if;
    end Get_Device_Info;


end Portaudio.Devices;