notan_audio/
backend.rs

1use crate::tracker::{ResourceId, ResourceTracker};
2use std::sync::Arc;
3
4/// Represent the audio implementation backend
5pub trait AudioBackend {
6    fn set_global_volume(&mut self, volume: f32);
7    fn global_volume(&self) -> f32;
8    fn create_source(&mut self, bytes: &[u8]) -> Result<u64, String>;
9    fn play_sound(&mut self, source: u64, volume: f32, repeat: bool) -> Result<u64, String>;
10    fn pause(&mut self, sound: u64);
11    fn resume(&mut self, sound: u64);
12    fn stop(&mut self, sound: u64);
13    #[allow(clippy::wrong_self_convention)]
14    fn is_stopped(&mut self, sound: u64) -> bool;
15    #[allow(clippy::wrong_self_convention)]
16    fn is_paused(&mut self, sound: u64) -> bool;
17    fn set_volume(&mut self, sound: u64, volume: f32);
18    fn volume(&self, sound: u64) -> f32;
19    fn clean(&mut self, sources: &[u64], sounds: &[u64]);
20    // fn remaining_time(&self, sound: u64) -> f32;
21}
22
23#[derive(Debug)]
24struct SourceIdRef {
25    id: u64,
26    tracker: Arc<ResourceTracker>,
27}
28
29impl Drop for SourceIdRef {
30    fn drop(&mut self) {
31        self.tracker.push(ResourceId::Source(self.id));
32    }
33}
34
35#[derive(Debug, Clone)]
36pub struct AudioSource {
37    pub(crate) id: u64,
38    _id_ref: Arc<SourceIdRef>,
39}
40
41impl AudioSource {
42    pub(crate) fn new(id: u64, tracker: Arc<ResourceTracker>) -> Self {
43        let _id_ref = Arc::new(SourceIdRef { id, tracker });
44        Self { id, _id_ref }
45    }
46}
47
48impl PartialEq for AudioSource {
49    fn eq(&self, other: &Self) -> bool {
50        self.id == other.id
51    }
52}
53
54#[derive(Debug)]
55struct SoundIdRef {
56    id: u64,
57    tracker: Arc<ResourceTracker>,
58}
59
60impl Drop for SoundIdRef {
61    fn drop(&mut self) {
62        self.tracker.push(ResourceId::Sound(self.id));
63    }
64}
65
66#[derive(Debug, Clone)]
67pub struct Sound {
68    pub(crate) id: u64,
69    _id_ref: Arc<SoundIdRef>,
70}
71
72impl Sound {
73    pub(crate) fn new(id: u64, tracker: Arc<ResourceTracker>) -> Self {
74        let _id_ref = Arc::new(SoundIdRef { id, tracker });
75        Self { id, _id_ref }
76    }
77}
78
79impl PartialEq for Sound {
80    fn eq(&self, other: &Self) -> bool {
81        self.id == other.id
82    }
83}