Debian Codegraph @

third_party/rust/coreaudio-sys-utils/src/audio_unit.rs

raw
1 use crate::dispatch::debug_assert_running_serially;
2 use coreaudio_sys::*;
3 use std::convert::TryFrom;
4 use std::os::raw::c_void;
5 use std::ptr;
6
7 pub fn audio_unit_get_property_info(
8 unit: AudioUnit,
9 property: AudioUnitPropertyID,
10 scope: AudioUnitScope,
11 element: AudioUnitElement,
12 size: &mut usize,
13 writable: Option<&mut bool>, // Use `Option` since `writable` is nullable.
14 ) -> OSStatus {
15 assert!(!unit.is_null());
16 assert!(UInt32::try_from(*size).is_ok()); // Check if `size` can be converted to a UInt32.
17 debug_assert_running_serially();
18 unsafe {
19 AudioUnitGetPropertyInfo(
20 unit,
21 property,
22 scope,
23 element,
24 size as *mut usize as *mut UInt32,
25 writable.map_or(ptr::null_mut(), |v| v as *mut bool as *mut Boolean),
26 )
27 }
28 }
29
30 pub fn audio_unit_get_property<T>(
31 unit: AudioUnit,
32 property: AudioUnitPropertyID,
33 scope: AudioUnitScope,
34 element: AudioUnitElement,
35 data: &mut T,
36 size: &mut usize,
37 ) -> OSStatus {
38 assert!(!unit.is_null());
39 assert!(UInt32::try_from(*size).is_ok()); // Check if `size` can be converted to a UInt32.
40 debug_assert_running_serially();
41 unsafe {
42 AudioUnitGetProperty(
43 unit,
44 property,
45 scope,
46 element,
47 data as *mut T as *mut c_void,
48 size as *mut usize as *mut UInt32,
49 )
50 }
51 }
52
53 pub fn audio_unit_set_property<T>(
54 unit: AudioUnit,
55 property: AudioUnitPropertyID,
56 scope: AudioUnitScope,
57 element: AudioUnitElement,
58 data: &T,
59 size: usize,
60 ) -> OSStatus {
61 assert!(!unit.is_null());
62 debug_assert_running_serially();
63 unsafe {
64 AudioUnitSetProperty(
65 unit,
66 property,
67 scope,
68 element,
69 data as *const T as *const c_void,
70 size as UInt32,
71 )
72 }
73 }
74
75 pub fn audio_unit_get_parameter(
76 unit: AudioUnit,
77 id: AudioUnitParameterID,
78 scope: AudioUnitScope,
79 element: AudioUnitElement,
80 value: &mut AudioUnitParameterValue,
81 ) -> OSStatus {
82 assert!(!unit.is_null());
83 debug_assert_running_serially();
84 unsafe {
85 AudioUnitGetParameter(
86 unit,
87 id,
88 scope,
89 element,
90 value as *mut AudioUnitParameterValue,
91 )
92 }
93 }
94
95 pub fn audio_unit_set_parameter(
96 unit: AudioUnit,
97 id: AudioUnitParameterID,
98 scope: AudioUnitScope,
99 element: AudioUnitElement,
100 value: AudioUnitParameterValue,
101 buffer_offset_in_frames: UInt32,
102 ) -> OSStatus {
103 assert!(!unit.is_null());
104 debug_assert_running_serially();
105 unsafe { AudioUnitSetParameter(unit, id, scope, element, value, buffer_offset_in_frames) }
106 }
107
108 pub fn audio_unit_initialize(unit: AudioUnit) -> OSStatus {
109 assert!(!unit.is_null());
110 debug_assert_running_serially();
111 unsafe { AudioUnitInitialize(unit) }
112 }
113
114 pub fn audio_unit_uninitialize(unit: AudioUnit) -> OSStatus {
115 assert!(!unit.is_null());
116 debug_assert_running_serially();
117 unsafe { AudioUnitUninitialize(unit) }
118 }
119
120 pub fn dispose_audio_unit(unit: AudioUnit) -> OSStatus {
121 debug_assert_running_serially();
122 unsafe { AudioComponentInstanceDispose(unit) }
123 }
124
125 pub fn audio_output_unit_start(unit: AudioUnit) -> OSStatus {
126 assert!(!unit.is_null());
127 debug_assert_running_serially();
128 unsafe { AudioOutputUnitStart(unit) }
129 }
130
131 pub fn audio_output_unit_stop(unit: AudioUnit) -> OSStatus {
132 assert!(!unit.is_null());
133 debug_assert_running_serially();
134 unsafe { AudioOutputUnitStop(unit) }
135 }
136
137 pub fn audio_unit_render(
138 in_unit: AudioUnit,
139 io_action_flags: &mut AudioUnitRenderActionFlags,
140 in_time_stamp: &AudioTimeStamp,
141 in_output_bus_number: u32,
142 in_number_frames: u32,
143 io_data: &mut AudioBufferList,
144 ) -> OSStatus {
145 assert!(!in_unit.is_null());
146 unsafe {
147 AudioUnitRender(
148 in_unit,
149 io_action_flags,
150 in_time_stamp,
151 in_output_bus_number,
152 in_number_frames,
153 io_data,
154 )
155 }
156 }
157
158 #[allow(non_camel_case_types)]
159 pub type audio_unit_property_listener_proc =
160 extern "C" fn(*mut c_void, AudioUnit, AudioUnitPropertyID, AudioUnitScope, AudioUnitElement);
161
162 pub fn audio_unit_add_property_listener<T>(
163 unit: AudioUnit,
164 id: AudioUnitPropertyID,
165 listener: audio_unit_property_listener_proc,
166 data: *mut T,
167 ) -> OSStatus {
168 assert!(!unit.is_null());
169 debug_assert_running_serially();
170 unsafe { AudioUnitAddPropertyListener(unit, id, Some(listener), data as *mut c_void) }
171 }
172
173 pub fn audio_unit_remove_property_listener_with_user_data<T>(
174 unit: AudioUnit,
175 id: AudioUnitPropertyID,
176 listener: audio_unit_property_listener_proc,
177 data: *mut T,
178 ) -> OSStatus {
179 assert!(!unit.is_null());
180 debug_assert_running_serially();
181 unsafe {
182 AudioUnitRemovePropertyListenerWithUserData(unit, id, Some(listener), data as *mut c_void)
183 }
184 }