audir 0.1.0

Low-level audio library
Documentation
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
use crate::{api, api::Result, handle::Handle};
use libpulse_sys as pulse;
use std::collections::HashMap;
use std::ffi::c_void;
use std::ffi::CStr;
use std::ptr;

struct PhysicalDevice {
    device_name: String,
    streams: api::StreamFlags,
    sample_spec: pulse::pa_sample_spec,
    channels: api::ChannelMask,
}

type PhysicalDeviceMap = HashMap<String, Handle<PhysicalDevice>>;

impl PhysicalDevice {
    fn default_format(&self) -> Result<api::FrameDesc> {
        let format = match self.sample_spec.format {
            pulse::pa_sample_format_t::F32le => api::Format::F32,
            pulse::pa_sample_format_t::S16le => api::Format::I16,
            format => {
                return Err(api::Error::Internal {
                    cause: format!("unhandled format: {:?}", format),
                })
            }
        };

        Ok(api::FrameDesc {
            format,
            channels: self.channels,
            sample_rate: self.sample_spec.rate as _,
        })
    }
}

fn map_channels(channel_map: &pulse::pa_channel_map) -> api::ChannelMask {
    let mut channels = api::ChannelMask::empty();
    for i in 0..channel_map.channels {
        channels |= match channel_map.map[i as usize] {
            pulse::PA_CHANNEL_POSITION_FRONT_LEFT => api::ChannelMask::FRONT_LEFT,
            pulse::PA_CHANNEL_POSITION_FRONT_RIGHT => api::ChannelMask::FRONT_RIGHT,
            pos => panic!("unsupported {:?}", pos),
        };
    }
    channels
}

extern "C" fn sink_info_cb(
    _context: *mut pulse::pa_context,
    info: *const pulse::pa_sink_info,
    _: i32,
    user: *mut c_void,
) {
    if info.is_null() {
        return;
    }

    let info = unsafe { &*info };
    let physical_devices = unsafe { &mut *(user as *mut PhysicalDeviceMap) };

    let name = unsafe { CStr::from_ptr(info.name).to_string_lossy().into_owned() };
    let device_name = unsafe {
        CStr::from_ptr(info.description)
            .to_string_lossy()
            .into_owned()
    };
    physical_devices
        .entry(name)
        .and_modify(|device| {
            assert_eq!(device.sample_spec, info.sample_spec); // TODO: is this right?

            device.streams |= api::StreamFlags::OUTPUT;
        })
        .or_insert_with(|| {
            Handle::new(PhysicalDevice {
                device_name,
                streams: api::StreamFlags::OUTPUT,
                sample_spec: info.sample_spec,
                channels: map_channels(&info.channel_map),
            })
        });
}

extern "C" fn source_info_cb(
    _context: *mut pulse::pa_context,
    info: *const pulse::pa_source_info,
    _: i32,
    user: *mut c_void,
) {
    if info.is_null() {
        return;
    }

    let info = unsafe { &*info };
    let physical_devices = unsafe { &mut *(user as *mut PhysicalDeviceMap) };

    let name = unsafe {
        CStr::from_ptr(info.description)
            .to_string_lossy()
            .into_owned()
    };
    let device_name = unsafe {
        CStr::from_ptr(info.description)
            .to_string_lossy()
            .into_owned()
    };
    physical_devices
        .entry(name)
        .and_modify(|device| {
            assert_eq!(device.sample_spec, info.sample_spec); // TODO: is this right?

            device.streams |= api::StreamFlags::INPUT;
        })
        .or_insert_with(|| {
            Handle::new(PhysicalDevice {
                device_name,
                streams: api::StreamFlags::INPUT,
                sample_spec: info.sample_spec,
                channels: map_channels(&info.channel_map),
            })
        });
}

fn map_format(format: api::Format) -> pulse::pa_sample_format_t {
    match format {
        api::Format::I16 => pulse::pa_sample_format_t::S16le,
        api::Format::F32 => pulse::pa_sample_format_t::F32le,
        _ => unimplemented!(),
    }
}

pub struct Instance {
    mainloop: *mut pulse::pa_mainloop,
    context: *mut pulse::pa_context,
    physical_devices: PhysicalDeviceMap,
}

impl api::Instance for Instance {
    type Device = Device;
    type Session = (); // TODO

    unsafe fn properties() -> api::InstanceProperties {
        api::InstanceProperties {
            driver_id: api::DriverId::PulseAudio,
            stream_mode: api::StreamMode::Polling,
            sharing: api::SharingModeFlags::CONCURRENT,
        }
    }

    unsafe fn create(name: &str) -> Self {
        let name = std::ffi::CString::new(name).unwrap();
        let mainloop = pulse::pa_mainloop_new();
        let api = pulse::pa_mainloop_get_api(mainloop);
        let context = pulse::pa_context_new(api, name.as_ptr() as *const _);
        pulse::pa_context_connect(context, ptr::null(), 0, ptr::null());

        loop {
            pulse::pa_mainloop_iterate(mainloop, true as _, ptr::null_mut());
            let state = pulse::pa_context_get_state(context);
            if state == pulse::PA_CONTEXT_READY {
                break;
            }
        }

        let mut physical_devices = PhysicalDeviceMap::new();

        // input devices
        let operation = pulse::pa_context_get_sink_info_list(
            context,
            Some(sink_info_cb),
            &mut physical_devices as *mut _ as _,
        );
        Self::await_operation(mainloop, operation);

        // output devices
        let operation = pulse::pa_context_get_source_info_list(
            context,
            Some(source_info_cb),
            &mut physical_devices as *mut _ as _,
        );
        Self::await_operation(mainloop, operation);

        Instance {
            mainloop,
            context,
            physical_devices,
        }
    }

    unsafe fn enumerate_physical_devices(&self) -> Vec<api::PhysicalDevice> {
        self.physical_devices
            .values()
            .map(|device| device.raw())
            .collect()
    }

    unsafe fn default_physical_input_device(&self) -> Option<api::PhysicalDevice> {
        self.physical_devices
            .get("default")
            .filter(|device| device.streams.contains(api::StreamFlags::INPUT))
            .map(|device| device.raw())
    }

    unsafe fn default_physical_output_device(&self) -> Option<api::PhysicalDevice> {
        self.physical_devices
            .get("default")
            .filter(|device| device.streams.contains(api::StreamFlags::OUTPUT))
            .map(|device| device.raw())
    }

    unsafe fn physical_device_properties(
        &self,
        physical_device: api::PhysicalDevice,
    ) -> Result<api::PhysicalDeviceProperties> {
        let physical_device = Handle::<PhysicalDevice>::from_raw(physical_device);

        Ok(api::PhysicalDeviceProperties {
            device_name: physical_device.device_name.clone(),
            streams: physical_device.streams,
            form_factor: api::FormFactor::Unknown, // TODO?
        })
    }

    unsafe fn physical_device_supports_format(
        &self,
        _physical_device: api::PhysicalDevice,
        sharing: api::SharingMode,
        _frame_desc: api::FrameDesc,
    ) -> bool {
        if sharing == api::SharingMode::Exclusive {
            // concurrent only
            return false;
        }

        // TODO: supporting everything?
        return true;
    }

    unsafe fn physical_device_default_concurrent_format(
        &self,
        physical_device: api::PhysicalDevice,
    ) -> Result<api::FrameDesc> {
        let physical_device = Handle::<PhysicalDevice>::from_raw(physical_device);
        physical_device.default_format()
    }

    unsafe fn create_device(
        &self,
        desc: api::DeviceDesc,
        channels: api::Channels,
        callback: api::StreamCallback,
    ) -> Result<Self::Device> {
        let stream = if !channels.output.is_empty() {
            let spec = pulse::pa_sample_spec {
                format: map_format(desc.sample_desc.format),
                channels: channels.output.bits().count_ones() as _,
                rate: desc.sample_desc.sample_rate as _,
            };

            let stream = dbg!(pulse::pa_stream_new(
                self.context,
                b"audir\0".as_ptr() as _,
                &spec,
                ptr::null()
            )); // TODO: name, channel map

            // TODO
            let attribs = pulse::pa_buffer_attr {
                maxlength: !0,
                tlength: !0,
                prebuf: !0,
                minreq: !0,
                fragsize: !0,
            };

            dbg!(pulse::pa_stream_connect_playback(
                stream,
                ptr::null(),
                &attribs,
                0,
                ptr::null(),
                ptr::null_mut(),
            ));

            loop {
                let state = dbg!(pulse::pa_stream_get_state(stream));
                if state == pulse::PA_STREAM_READY {
                    break;
                }
                pulse::pa_mainloop_iterate(self.mainloop, true as _, ptr::null_mut());
            }

            stream
        } else {
            todo!()
        };

        let sample_spec = &*pulse::pa_stream_get_sample_spec(stream);
        let frame_size = pulse::pa_frame_size(sample_spec);

        Ok(Device {
            mainloop: self.mainloop,
            stream,
            cur_buffer: ptr::null_mut(),
            frame_size,
            callback,
        })
    }

    unsafe fn create_session(&self, _sample_rate: usize) -> Result<Self::Session> {
        Ok(())
    }

    unsafe fn set_event_callback<F>(&mut self, _callback: Option<F>) -> Result<()>
    where
        F: FnMut(api::Event) + Send + 'static,
    {
        unimplemented!()
    }
}

impl Instance {
    unsafe fn await_operation(
        mainloop: *mut pulse::pa_mainloop,
        operation: *mut pulse::pa_operation,
    ) {
        loop {
            let state = pulse::pa_operation_get_state(operation);
            if state != pulse::PA_OPERATION_RUNNING {
                pulse::pa_operation_unref(operation);
                break;
            }
            pulse::pa_mainloop_iterate(mainloop, true as _, ptr::null_mut());
        }
    }
}

pub struct Device {
    mainloop: *mut pulse::pa_mainloop,
    stream: *mut pulse::pa_stream,
    cur_buffer: *mut c_void,
    frame_size: usize,
    callback: api::StreamCallback,
}

impl Device {
    unsafe fn acquire_buffers(&mut self, timeout_ms: u32) -> Result<api::StreamBuffers> {
        let mut size = loop {
            let size = pulse::pa_stream_writable_size(self.stream);
            if size > 0 {
                break size;
            }

            pulse::pa_mainloop_prepare(self.mainloop, timeout_ms as _); // TODO: timeout
            pulse::pa_mainloop_poll(self.mainloop);
            pulse::pa_mainloop_dispatch(self.mainloop);
        };

        let mut data = ptr::null_mut();
        pulse::pa_stream_begin_write(self.stream, &mut data, &mut size);
        self.cur_buffer = data;
        Ok(api::StreamBuffers {
            input: ptr::null(),
            output: data as _,
            frames: (size / self.frame_size) as _,
        })
    }

    unsafe fn release_buffers(&mut self, num_frames: api::Frames) -> Result<()> {
        pulse::pa_stream_write(
            self.stream,
            self.cur_buffer,
            num_frames * self.frame_size,
            None,
            0,
            pulse::PA_SEEK_RELATIVE,
        );
        Ok(())
    }
}

impl api::Device for Device {
    unsafe fn start(&self) {
        println!("Device::start unimplemented");
    }

    unsafe fn stop(&self) {
        println!("Device::stop unimplemented");
    }

    unsafe fn stream_properties(&self) -> api::StreamProperties {
        let stream = self.stream;

        let buffer_attrs = &*pulse::pa_stream_get_buffer_attr(stream);
        let sample_spec = &*pulse::pa_stream_get_sample_spec(stream);
        let channel_map = &*pulse::pa_stream_get_channel_map(stream);

        api::StreamProperties {
            channels: map_channels(channel_map),
            sample_rate: sample_spec.rate as _,
            buffer_size: buffer_attrs.minreq as _,
        }
    }

    unsafe fn submit_buffers(&mut self, timeout_ms: u32) -> Result<()> {
        let buffers = self.acquire_buffers(timeout_ms)?;
        let properties = self.stream_properties();
        (self.callback)(api::Stream {
            properties,
            buffers,
        });
        self.release_buffers(buffers.frames)
    }
}