summaryrefslogtreecommitdiff
path: root/audio/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'audio/src/lib.rs')
-rw-r--r--audio/src/lib.rs71
1 files changed, 16 insertions, 55 deletions
diff --git a/audio/src/lib.rs b/audio/src/lib.rs
index 97121f5..02800b9 100644
--- a/audio/src/lib.rs
+++ b/audio/src/lib.rs
@@ -3,7 +3,7 @@
use raylib::audio::RaylibAudio;
use std::time::{Duration, Instant};
-use channel::{Channel, NoiseChannel, PulseChannel, TriangleChannel};
+use channel::Device;
use data::Data;
mod channel;
@@ -20,15 +20,16 @@ pub type Result<T> = std::result::Result<T, crate::Error>;
const AUDIO_FPS: u32 = 60;
const TIME_SLICE: Duration = Duration::from_millis(1000 / AUDIO_FPS as u64);
-/// Holds each audio channel
-pub struct Channels {
- pub pulse_a: PulseChannel,
- pub pulse_b: PulseChannel,
- pub triangle: TriangleChannel,
- pub noise: NoiseChannel,
+/// The `Audio` container initalizes the audio subsystem
+/// for raylib, leaks it (to gurentee audio is statically loaded),
+/// then loads all needed audio samples
+pub struct Audio {
+ device: Device<'static>,
+ last: Instant,
+ pub data: Data,
}
-impl Channels {
- pub(crate) fn load() -> crate::Result<Self> {
+impl Audio {
+ pub fn load() -> crate::Result<Self> {
// Phantom handle to the raylib audio subsystem
// Raylib doesnt use a handle, but the rust bindings
// have one to ensure memory safety.
@@ -39,60 +40,20 @@ impl Channels {
// NOTE: would this cause issues if `Audio::load` was
// called multiple times?
let handle = Box::leak(Box::new(RaylibAudio::init_audio_device()?));
-
- let pulse_a = PulseChannel::load(handle)?;
- pulse_a.set_volume(0.0);
- pulse_a.play();
- let pulse_b = PulseChannel::load(handle)?;
- pulse_b.set_volume(0.0);
- pulse_b.play();
- let triangle = TriangleChannel::load(handle)?;
- triangle.set_volume(0.0);
- triangle.play();
- let noise = NoiseChannel::load(handle)?;
- noise.set_volume(0.0);
- noise.play();
-
- Ok(Self {
- pulse_a,
- pulse_b,
- triangle,
- noise,
- })
- }
-}
-
-/// The `Audio` container initalizes the audio subsystem
-/// for raylib, leaks it (to gurentee audio is statically loaded),
-/// then loads all needed audio samples
-pub struct Audio {
- channels: Channels,
- last: Instant,
- pub data: Data,
-}
-impl Audio {
- pub fn load() -> crate::Result<Self> {
- let channels = Channels::load()?;
+ let device = Device::load(handle)?;
let last = Instant::now();
let data = Data::load()?;
- Ok(Self {
- channels,
- last,
- data,
- })
+ Ok(Self { device, last, data })
}
+ #[expect(clippy::unwrap_used)]
pub fn update(&mut self) {
if self.last.elapsed() >= TIME_SLICE {
- self.data.explore.exec(&mut self.channels);
- self.data.megalovania.exec(&mut self.channels);
+ let mut channels = self.device.channels.lock().unwrap();
+ self.data.explore.exec(&mut channels);
+ self.data.megalovania.exec(&mut channels);
self.last = Instant::now();
}
-
- self.channels.pulse_a.update();
- self.channels.pulse_b.update();
- self.channels.triangle.update();
- self.channels.noise.update();
}
}