| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use std::ffi::c_void; |
| 9 | use std::path::Path; |
| 10 | use std::sync::OnceLock; |
| 11 | |
| 12 | use libloading::Library; |
| 13 | |
| 14 | const LIB_NAME: &str = "libengine_audio.so"; |
| 15 | |
| 16 | struct Plugin { |
| 17 | open: unsafe extern "C" fn(u32, u32) -> *mut c_void, |
| 18 | write: unsafe extern "C" fn(*mut c_void, *const f32, usize) -> isize, |
| 19 | position: unsafe extern "C" fn(*mut c_void) -> i64, |
| 20 | set_active: unsafe extern "C" fn(*mut c_void, u32) -> i32, |
| 21 | set_volume: unsafe extern "C" fn(*mut c_void, f32) -> i32, |
| 22 | flush: unsafe extern "C" fn(*mut c_void) -> i32, |
| 23 | close: unsafe extern "C" fn(*mut c_void), |
| 24 | |
| 25 | _lib: Library, |
| 26 | } |
| 27 | |
| 28 | fn plugin() -> Option<&'static Plugin> { |
| 29 | static PLUGIN: OnceLock<Option<Plugin>> = OnceLock::new(); |
| 30 | PLUGIN.get_or_init(load).as_ref() |
| 31 | } |
| 32 | |
| 33 | fn load() -> Option<Plugin> { |
| 34 | |
| 35 | |
| 36 | let beside_exe = std::env::current_exe() |
| 37 | .ok() |
| 38 | .and_then(|exe| exe.parent().map(|dir| dir.join(LIB_NAME))); |
| 39 | let candidates = beside_exe |
| 40 | .as_deref() |
| 41 | .into_iter() |
| 42 | .chain(std::iter::once(Path::new(LIB_NAME))); |
| 43 | |
| 44 | for candidate in candidates { |
| 45 | |
| 46 | |
| 47 | |
| 48 | let lib = match unsafe { Library::new(candidate) } { |
| 49 | Ok(lib) => lib, |
| 50 | Err(_) => continue, |
| 51 | }; |
| 52 | let plugin = unsafe { |
| 53 | (|| { |
| 54 | Some(Plugin { |
| 55 | open: *lib.get(b"ao_open\0").ok()?, |
| 56 | write: *lib.get(b"ao_write\0").ok()?, |
| 57 | position: *lib.get(b"ao_position\0").ok()?, |
| 58 | set_active: *lib.get(b"ao_set_active\0").ok()?, |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | set_volume: *lib.get(b"ao_set_volume\0").ok()?, |
| 65 | flush: *lib.get(b"ao_flush\0").ok()?, |
| 66 | close: *lib.get(b"ao_close\0").ok()?, |
| 67 | _lib: lib, |
| 68 | }) |
| 69 | })() |
| 70 | }; |
| 71 | if plugin.is_some() { |
| 72 | return plugin; |
| 73 | } |
| 74 | } |
| 75 | None |
| 76 | } |
| 77 | |
| 78 | |
| 79 | pub struct AoSession { |
| 80 | plugin: &'static Plugin, |
| 81 | handle: *mut c_void, |
| 82 | } |
| 83 | |
| 84 | impl AoSession { |
| 85 | |
| 86 | |
| 87 | pub fn open(sample_rate: u32, channels: u32) -> Option<Self> { |
| 88 | let plugin = plugin()?; |
| 89 | |
| 90 | let handle = unsafe { (plugin.open)(sample_rate, channels) }; |
| 91 | if handle.is_null() { |
| 92 | return None; |
| 93 | } |
| 94 | Some(Self { plugin, handle }) |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | pub fn probe() -> bool { |
| 101 | plugin().is_some() |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | pub fn write(&mut self, samples: &[f32]) -> Option<usize> { |
| 108 | |
| 109 | match unsafe { (self.plugin.write)(self.handle, samples.as_ptr(), samples.len()) } { |
| 110 | n if n < 0 => None, |
| 111 | n => Some(n as usize), |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | |
| 117 | pub fn position(&self) -> Option<i64> { |
| 118 | |
| 119 | match unsafe { (self.plugin.position)(self.handle) } { |
| 120 | n if n < 0 => None, |
| 121 | n => Some(n), |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | pub fn set_active(&self, active: bool) -> bool { |
| 127 | |
| 128 | unsafe { (self.plugin.set_active)(self.handle, active as u32) == 0 } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | pub fn set_volume(&self, gain: f32) -> bool { |
| 136 | |
| 137 | unsafe { (self.plugin.set_volume)(self.handle, gain) == 0 } |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | pub fn flush(&self) -> bool { |
| 143 | |
| 144 | unsafe { (self.plugin.flush)(self.handle) == 0 } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl Drop for AoSession { |
| 149 | fn drop(&mut self) { |
| 150 | |
| 151 | unsafe { (self.plugin.close)(self.handle) } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | |
| 157 | unsafe impl Send for AoSession {} |