unbanished_audio/
device.rs1use std::sync::Arc;
16use std::time::Duration;
17
18use crate::Pcm;
19
20pub const CHANNELS: usize = 64;
22
23const SEMITONE: f32 = 1.059_463_1;
25
26pub fn pitch_ratio(semitones: f32) -> f32 {
31 SEMITONE.powf(semitones)
32}
33
34#[derive(Clone)]
37pub struct Sound {
38 pcm: Arc<Pcm>,
39 pub looping: bool,
42 #[cfg(target_arch = "wasm32")]
43 web: std::rc::Rc<std::cell::RefCell<Option<web_sys::AudioBuffer>>>,
44}
45
46impl Sound {
47 pub fn new(pcm: Pcm, looping: bool) -> Sound {
49 Sound {
50 pcm: Arc::new(pcm),
51 looping,
52 #[cfg(target_arch = "wasm32")]
53 web: Default::default(),
54 }
55 }
56
57 pub fn load(
59 archive: &unbanished_formats::pkg::Archive,
60 reference: &str,
61 ) -> Result<Sound, String> {
62 let (pcm, looping) = crate::load_sound(archive, reference)?;
63 Ok(Sound::new(pcm, looping))
64 }
65
66 pub fn pcm(&self) -> &Pcm {
68 &self.pcm
69 }
70
71 pub fn frames(&self) -> usize {
73 self.pcm.samples.len() / self.pcm.channels.max(1) as usize
74 }
75
76 pub fn duration(&self) -> Duration {
78 Duration::from_secs_f64(self.frames() as f64 / self.pcm.sample_rate.max(1) as f64)
79 }
80
81 pub fn wrap(&self, offset: Duration) -> Duration {
83 let d = self.duration().as_secs_f64();
84 if d <= 0.0 {
85 return Duration::ZERO;
86 }
87 Duration::from_secs_f64(offset.as_secs_f64() % d)
88 }
89}
90
91#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
94pub struct Channel {
95 index: u8,
96 generation: u32,
97}
98
99impl Channel {
100 pub fn index(self) -> usize {
102 self.index as usize
103 }
104
105 #[cfg(test)]
106 pub(crate) fn test(n: u32) -> Channel {
107 Channel {
108 index: (n % 64) as u8,
109 generation: n,
110 }
111 }
112}
113
114#[derive(Debug)]
116pub struct Slots<T> {
117 slots: Vec<Option<(u32, T)>>,
118 generation: u32,
119}
120
121impl<T> Default for Slots<T> {
122 fn default() -> Self {
123 Slots {
124 slots: (0..CHANNELS).map(|_| None).collect(),
125 generation: 0,
126 }
127 }
128}
129
130impl<T> Slots<T> {
131 pub fn insert(&mut self, value: T) -> Option<Channel> {
133 let index = self.slots.iter().position(Option::is_none)?;
134 self.generation = self.generation.wrapping_add(1);
135 self.slots[index] = Some((self.generation, value));
136 Some(Channel {
137 index: index as u8,
138 generation: self.generation,
139 })
140 }
141
142 pub fn get(&self, c: Channel) -> Option<&T> {
144 match self.slots.get(c.index())? {
145 Some((g, v)) if *g == c.generation => Some(v),
146 _ => None,
147 }
148 }
149
150 pub fn get_mut(&mut self, c: Channel) -> Option<&mut T> {
152 match self.slots.get_mut(c.index())? {
153 Some((g, v)) if *g == c.generation => Some(v),
154 _ => None,
155 }
156 }
157
158 pub fn remove(&mut self, c: Channel) -> Option<T> {
160 self.get(c)?;
161 self.slots[c.index()].take().map(|(_, v)| v)
162 }
163
164 pub fn iter(&self) -> impl Iterator<Item = (Channel, &T)> {
166 self.slots.iter().enumerate().filter_map(|(i, s)| {
167 s.as_ref().map(|(g, v)| {
168 (
169 Channel {
170 index: i as u8,
171 generation: *g,
172 },
173 v,
174 )
175 })
176 })
177 }
178
179 pub fn len(&self) -> usize {
181 self.slots.iter().filter(|s| s.is_some()).count()
182 }
183
184 pub fn is_empty(&self) -> bool {
186 self.len() == 0
187 }
188}
189
190#[derive(Debug, Clone, Copy)]
192pub struct PlayParams {
193 pub offset: Duration,
195 pub volume: f32,
197 pub pitch: f32,
199 pub looping: Option<bool>,
201}
202
203impl Default for PlayParams {
204 fn default() -> Self {
205 PlayParams {
206 offset: Duration::ZERO,
207 volume: 1.0,
208 pitch: 0.0,
209 looping: None,
210 }
211 }
212}
213
214#[cfg(not(target_arch = "wasm32"))]
215pub use native::Device;
216#[cfg(target_arch = "wasm32")]
217pub use web::Device;
218
219#[cfg(not(target_arch = "wasm32"))]
220mod native;
221
222#[cfg(target_arch = "wasm32")]
223mod web;
224
225#[cfg(test)]
226mod tests;