sokol/
audio.rs

1//! sokol::audio - cross-platform audio-streaming API
2//!
3//! A Rust API to the [sokol_audio.h](https://github.com/floooh/sokol/blob/master/sokol_audio.h)
4//! header-only C library.
5
6pub mod ffi {
7    use std::os::raw::c_int;
8    use std::os::raw::c_void;
9    use std::ptr::null;
10
11    #[repr(C)]
12    #[derive(Debug)]
13    pub struct SAudioDesc {
14        sample_rate: c_int,
15        num_channels: c_int,
16        buffer_frames: c_int,
17        packet_frames: c_int,
18        num_packets: c_int,
19        stream_cb: *const c_void,
20        stream_userdata_cb: Option<unsafe extern fn(*mut f32, c_int, c_int, *mut c_void)>,
21        user_data: *mut c_void,
22    }
23
24    extern {
25        pub fn saudio_setup(desc: *const SAudioDesc);
26        pub fn saudio_shutdown();
27        pub fn saudio_isvalid() -> bool;
28        pub fn saudio_sample_rate() -> c_int;
29        //pub fn saudio_buffer_size() -> c_int;
30        pub fn saudio_channels() -> c_int;
31        pub fn saudio_expect() -> c_int;
32        pub fn saudio_push(frames: *const f32, num_frames: c_int) -> c_int;
33    }
34
35    pub fn saudio_make_desc(desc: super::SAudioDesc) -> SAudioDesc {
36        let app_ptr = unsafe {
37            super::super::app::ffi::sapp_get_userdata()
38        };
39
40        SAudioDesc {
41            sample_rate: desc.sample_rate,
42            num_channels: desc.num_channels,
43            buffer_frames: desc.buffer_frames,
44            packet_frames: desc.packet_frames,
45            num_packets: desc.num_packets,
46            stream_cb: null(),
47            stream_userdata_cb: if desc.use_stream_cb {
48                Some(super::super::app::ffi::stream_userdata_cb)
49            } else {
50                None
51            },
52            user_data: app_ptr,
53        }
54    }
55}
56
57#[derive(Default, Debug)]
58pub struct SAudioDesc {
59    pub sample_rate: i32,
60    pub num_channels: i32,
61    pub buffer_frames: i32,
62    pub packet_frames: i32,
63    pub num_packets: i32,
64    pub use_stream_cb: bool,
65}
66
67pub fn saudio_setup(desc: SAudioDesc) {
68    unsafe {
69        ffi::saudio_setup(&ffi::saudio_make_desc(desc))
70    }
71}
72
73pub fn saudio_shutdown() {
74    unsafe {
75        ffi::saudio_shutdown();
76    }
77}
78
79pub fn saudio_isvalid() -> bool {
80    unsafe {
81        ffi::saudio_isvalid()
82    }
83}
84
85pub fn saudio_sample_rate() -> i32 {
86    unsafe {
87        ffi::saudio_sample_rate()
88    }
89}
90
91/*pub fn saudio_buffer_size() -> i32 {
92    unsafe {
93        ffi::saudio_buffer_size()
94    }
95}*/
96
97pub fn saudio_channels() -> i32 {
98    unsafe {
99        ffi::saudio_channels()
100    }
101}
102
103pub fn saudio_expect() -> i32 {
104    unsafe {
105        ffi::saudio_expect()
106    }
107}
108
109pub fn saudio_push(frames: &[f32], num_frames: i32) -> i32 {
110    unsafe {
111        ffi::saudio_push(frames.as_ptr(), num_frames)
112    }
113}