rustyray_sys/
audio.rs

1use std::{ffi::CString, fmt::Debug};
2
3use libc::{c_int, c_uint, c_void};
4
5use crate::ffi;
6
7/// Wave, audio wave data
8#[repr(C)]
9#[derive(Debug, Clone)]
10pub struct Wave {
11    pub frame_count: c_uint,
12    pub sample_rate: c_uint,
13    pub sample_size: c_uint,
14    pub channels: c_uint,
15    pub data: *const c_void,
16}
17
18pub type AudioCallback = extern "C" fn(buffer_data: *const c_void, frames: c_uint);
19
20/// AudioStream, custom audio stream
21#[repr(C)]
22#[derive(Debug, Clone)]
23pub struct AudioStream {
24    /// Pointer to internal data used by the audio system
25    pub buffer: *const c_void,
26    /// Pointer to internal data processor, useful for audio effects
27    pub processor: *const c_void,
28
29    /// Frequency (samples per second)
30    pub sample_rate: c_uint,
31    /// Bit detph (bits per sample): 8, 16, 32 (24 not supported)
32    pub sample_size: c_uint,
33    /// Number of channels (1-mono, 2-stereo, ...)
34    pub channels: c_uint,
35}
36
37/// Sound
38#[repr(C)]
39#[derive(Debug, Clone)]
40pub struct Sound {
41    /// Audio Stream
42    pub stream: AudioStream,
43    /// Total number of frames (considering channels)
44    pub frame_count: c_uint,
45}
46
47impl Sound {
48    pub fn new(path: String) -> Self {
49        unsafe { ffi::load_sound(CString::new(path).unwrap().as_ptr()) }
50    }
51
52    pub fn unload(self) {
53        unsafe {
54            ffi::unload_sound(self);
55        }
56    }
57
58    pub fn unload_alias(self) {
59        unsafe {
60            ffi::unload_sound_alias(self);
61        }
62    }
63}
64
65#[repr(C)]
66#[derive(Debug, Clone)]
67pub struct Music {
68    pub stream: AudioStream,
69    pub frame_count: c_uint,
70    pub looping: bool,
71
72    pub ctx_type: c_int,
73    pub ctx_data: *const c_void,
74}
75
76impl Music {
77    pub fn new(path: String) -> Self {
78        unsafe { ffi::load_music_stream(CString::new(path).unwrap().as_ptr()) }
79    }
80
81    pub fn unload(self) {
82        unsafe {
83            ffi::unload_music_stream(self);
84        }
85    }
86}