obs_wrapper/source/
audio.rs

1use obs_sys::{audio_output_get_channels, audio_output_get_sample_rate, audio_t, obs_audio_data};
2
3pub struct AudioDataContext {
4    pointer: *mut obs_audio_data,
5}
6
7impl AudioDataContext {
8    pub(crate) unsafe fn from_raw(pointer: *mut obs_audio_data) -> Self {
9        Self { pointer }
10    }
11
12    pub fn frames(&self) -> usize {
13        unsafe {
14            self.pointer
15                .as_ref()
16                .expect("Audio pointer was null!")
17                .frames as usize
18        }
19    }
20
21    pub fn channels(&self) -> usize {
22        unsafe {
23            self.pointer
24                .as_ref()
25                .expect("Audio pointer was null!")
26                .data
27                .len()
28        }
29    }
30
31    pub fn get_channel_as_mut_slice(&self, channel: usize) -> Option<&'_ mut [f32]> {
32        unsafe {
33            let data = self.pointer.as_ref()?.data;
34
35            if channel >= data.len() {
36                return None;
37            }
38
39            let frames = self.pointer.as_ref()?.frames;
40
41            Some(core::slice::from_raw_parts_mut(
42                data[channel] as *mut f32,
43                frames as usize,
44            ))
45        }
46    }
47}
48
49pub struct AudioRef {
50    pointer: *mut audio_t,
51}
52
53impl AudioRef {
54    pub(crate) unsafe fn from_raw(pointer: *mut audio_t) -> Self {
55        Self { pointer }
56    }
57
58    pub fn output_sample_rate(&self) -> usize {
59        unsafe { audio_output_get_sample_rate(self.pointer) as usize }
60    }
61
62    pub fn output_channels(&self) -> usize {
63        unsafe { audio_output_get_channels(self.pointer) as usize }
64    }
65}