geng_audio/
lib.rs

1use batbox_la::*;
2use batbox_time as time;
3use std::rc::Rc;
4
5mod platform;
6
7#[derive(Clone)]
8pub struct Audio {
9    inner: Rc<platform::Context>,
10}
11
12impl Default for Audio {
13    fn default() -> Self {
14        Self::new()
15    }
16}
17
18impl Audio {
19    pub fn new() -> Self {
20        Self {
21            inner: Rc::new(platform::Context::new()),
22        }
23    }
24    pub fn set_volume(&self, volume: f64) {
25        self.inner.set_volume(volume);
26    }
27    pub fn set_listener_position(&self, position: vec3<f64>) {
28        self.inner.set_listener_position(position);
29    }
30    pub fn set_listener_orientation(&self, forward: vec3<f64>, up: vec3<f64>) {
31        self.inner.set_listener_orientation(forward, up);
32    }
33    pub async fn load(&self, path: impl AsRef<std::path::Path>) -> anyhow::Result<Sound> {
34        Ok(Sound {
35            inner: platform::Sound::load(&self.inner, path.as_ref()).await?,
36        })
37    }
38    pub async fn decode_bytes(&self, data: Vec<u8>) -> anyhow::Result<Sound> {
39        Ok(Sound {
40            inner: platform::Sound::decode_bytes(&self.inner, data).await?,
41        })
42    }
43}
44
45pub struct Sound {
46    inner: platform::Sound,
47}
48
49impl Sound {
50    pub fn looped(&self) -> bool {
51        self.inner.looped
52    }
53    pub fn set_looped(&mut self, looped: bool) {
54        self.inner.looped = looped;
55    }
56    pub fn duration(&self) -> time::Duration {
57        self.inner.duration()
58    }
59    pub fn effect(&self) -> SoundEffect {
60        SoundEffect {
61            inner: self.inner.effect(),
62        }
63    }
64    pub fn play(&self) -> SoundEffect {
65        let mut effect = self.effect();
66        effect.play();
67        effect
68    }
69}
70
71pub struct SoundEffect {
72    inner: platform::SoundEffect,
73}
74
75impl SoundEffect {
76    pub fn play(&mut self) {
77        self.inner.play();
78    }
79    pub fn play_from(&mut self, offset: time::Duration) {
80        self.inner.play_from(offset);
81    }
82    pub fn stop(&mut self) {
83        self.inner.stop();
84    }
85    pub fn set_volume(&mut self, volume: f64) {
86        self.inner.set_volume(volume);
87    }
88    pub fn set_speed(&mut self, speed: f64) {
89        self.inner.set_speed(speed);
90    }
91    pub fn set_position(&mut self, position: vec3<f64>) {
92        self.inner.set_position(position);
93    }
94    pub fn set_ref_distance(&mut self, ref_distance: f64) {
95        self.inner.set_ref_distance(ref_distance);
96    }
97    pub fn set_max_distance(&mut self, max_distance: f64) {
98        self.inner.set_max_distance(max_distance);
99    }
100}