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
use ll;
use util::{to_pa_result, pa_time_to_duration};
use hostapi::HostApiIndex;
use pa::PaError;
use std::time::Duration;
use std::ffi::CStr;
pub type DeviceIndex = u32;
pub struct DeviceInfo
{
pub name: String,
pub host_api: HostApiIndex,
pub max_input_channels: u32,
pub max_output_channels: u32,
pub default_low_input_latency: Duration,
pub default_low_output_latency: Duration,
pub default_high_input_latency: Duration,
pub default_high_output_latency: Duration,
pub default_sample_rate: f64,
}
impl DeviceInfo
{
fn from_ll(input: &ll::PaDeviceInfo) -> DeviceInfo
{
DeviceInfo
{
name: String::from_utf8_lossy(unsafe { CStr::from_ptr(input.name).to_bytes() }).into_owned(),
host_api: input.hostApi as HostApiIndex,
max_input_channels: input.maxInputChannels as u32,
max_output_channels: input.maxOutputChannels as u32,
default_low_input_latency: pa_time_to_duration(input.defaultLowInputLatency),
default_low_output_latency: pa_time_to_duration(input.defaultLowOutputLatency),
default_high_input_latency: pa_time_to_duration(input.defaultHighInputLatency),
default_high_output_latency: pa_time_to_duration(input.defaultHighOutputLatency),
default_sample_rate: input.defaultSampleRate,
}
}
}
pub fn get_count() -> Result<u32, PaError>
{
match unsafe { ll::Pa_GetDeviceCount() }
{
n if n >= 0 => Ok(n as u32),
m => to_pa_result(m).map(|_| 0),
}
}
pub fn get_default_input_index() -> Option<DeviceIndex>
{
match unsafe { ll::Pa_GetDefaultInputDevice() }
{
n if n >= 0 => Some(n as u32),
_ => None,
}
}
pub fn get_default_output_index() -> Option<DeviceIndex>
{
match unsafe { ll::Pa_GetDefaultOutputDevice() }
{
n if n >= 0 => Some(n as u32),
_ => None,
}
}
pub fn get_info(index: DeviceIndex) -> Option<DeviceInfo>
{
unsafe
{
match ll::Pa_GetDeviceInfo(index as i32) {
p if p.is_null() => None,
p => Some(DeviceInfo::from_ll(&*p)),
}
}
}
pub fn get_from_host_api_device_index(host_api: HostApiIndex, host_api_device_index: u32) -> Result<DeviceIndex, PaError>
{
match unsafe { ll::Pa_HostApiDeviceIndexToDeviceIndex(host_api as i32, host_api_device_index as i32) }
{
n if n >= 0 => Ok(n as DeviceIndex),
m => to_pa_result(m).map(|_| 0),
}
}