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
use obs_sys::{audio_output_get_channels, audio_output_get_sample_rate, audio_t, obs_audio_data};

pub struct AudioDataContext {
    pointer: *mut obs_audio_data,
}

impl AudioDataContext {
    pub(crate) unsafe fn from_raw(pointer: *mut obs_audio_data) -> Self {
        Self { pointer }
    }

    pub fn frames(&self) -> usize {
        unsafe {
            self.pointer
                .as_ref()
                .expect("Audio pointer was null!")
                .frames as usize
        }
    }

    pub fn channels(&self) -> usize {
        unsafe {
            self.pointer
                .as_ref()
                .expect("Audio pointer was null!")
                .data
                .len()
        }
    }

    pub fn get_channel_as_mut_slice(&self, channel: usize) -> Option<&'_ mut [f32]> {
        unsafe {
            let data = self.pointer.as_ref()?.data;

            if channel >= data.len() {
                return None;
            }

            let frames = self.pointer.as_ref()?.frames;

            Some(core::slice::from_raw_parts_mut(
                data[channel] as *mut f32,
                frames as usize,
            ))
        }
    }
}

pub struct AudioRef {
    pointer: *mut audio_t,
}

impl AudioRef {
    pub(crate) unsafe fn from_raw(pointer: *mut audio_t) -> Self {
        Self { pointer }
    }

    pub fn output_sample_rate(&self) -> usize {
        unsafe { audio_output_get_sample_rate(self.pointer) as usize }
    }

    pub fn output_channels(&self) -> usize {
        unsafe { audio_output_get_channels(self.pointer) as usize }
    }
}