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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use batbox_la::*;
use batbox_time as time;
use std::rc::Rc;

mod platform;

#[derive(Clone)]
pub struct Audio {
    inner: Rc<platform::Context>,
}

impl Default for Audio {
    fn default() -> Self {
        Self::new()
    }
}

impl Audio {
    pub fn new() -> Self {
        Self {
            inner: Rc::new(platform::Context::new()),
        }
    }
    pub fn set_volume(&self, volume: f64) {
        self.inner.set_volume(volume);
    }
    pub fn set_listener_position(&self, position: vec3<f64>) {
        self.inner.set_listener_position(position);
    }
    pub fn set_listener_orientation(&self, forward: vec3<f64>, up: vec3<f64>) {
        self.inner.set_listener_orientation(forward, up);
    }
    pub async fn load(&self, path: impl AsRef<std::path::Path>) -> anyhow::Result<Sound> {
        Ok(Sound {
            inner: platform::Sound::load(&self.inner, path.as_ref()).await?,
        })
    }
    pub async fn decode_bytes(&self, data: Vec<u8>) -> anyhow::Result<Sound> {
        Ok(Sound {
            inner: platform::Sound::decode_bytes(&self.inner, data).await?,
        })
    }
}

pub struct Sound {
    inner: platform::Sound,
}

impl Sound {
    pub fn looped(&self) -> bool {
        self.inner.looped
    }
    pub fn set_looped(&mut self, looped: bool) {
        self.inner.looped = looped;
    }
    pub fn duration(&self) -> time::Duration {
        self.inner.duration()
    }
    pub fn effect(&self) -> SoundEffect {
        SoundEffect {
            inner: self.inner.effect(),
        }
    }
    pub fn play(&self) -> SoundEffect {
        let mut effect = self.effect();
        effect.play();
        effect
    }
}

pub struct SoundEffect {
    inner: platform::SoundEffect,
}

impl SoundEffect {
    pub fn play(&mut self) {
        self.inner.play();
    }
    pub fn play_from(&mut self, offset: time::Duration) {
        self.inner.play_from(offset);
    }
    pub fn stop(&mut self) {
        self.inner.stop();
    }
    pub fn set_volume(&mut self, volume: f64) {
        self.inner.set_volume(volume);
    }
    pub fn set_speed(&mut self, speed: f64) {
        self.inner.set_speed(speed);
    }
    pub fn set_position(&mut self, position: vec3<f64>) {
        self.inner.set_position(position);
    }
    pub fn set_ref_distance(&mut self, ref_distance: f64) {
        self.inner.set_ref_distance(ref_distance);
    }
    pub fn set_max_distance(&mut self, max_distance: f64) {
        self.inner.set_max_distance(max_distance);
    }
}