This repository has been archived on 2021-10-11. You can view files and clone it, but cannot push or open issues or pull requests.
ludum-dare-49/game/src/utilities/audio_player.rs
2021-10-03 14:07:23 -04:00

50 lines
1.1 KiB
Rust

use raylib::audio::RaylibAudio;
/// A thin wrapper around `raylib::core::audio::RaylibAudio` that keeps track of the volume of its audio channels.
#[derive(Debug)]
pub struct AudioPlayer {
backend: RaylibAudio,
// Volume
pub master_volume: f32,
}
impl AudioPlayer {
/// Construct an AudioPlayer around a RaylibAudio
pub fn new(backend: RaylibAudio) -> Self {
Self {
backend,
master_volume: 1.0,
}
}
/// Set the master volume for all tracks. `0.0` to `1.0`
pub fn set_master_volume(&mut self, volume: f32) {
// The volume must be 0-1
let volume = volume.clamp(0.0, 1.0);
// Set the volume
self.master_volume = volume;
self.backend.set_master_volume(volume);
}
/// Get the master volume
pub fn get_master_volume(&self) -> f32 {
self.master_volume
}
}
impl std::ops::Deref for AudioPlayer {
type Target = RaylibAudio;
fn deref(&self) -> &Self::Target {
&self.backend
}
}
impl std::ops::DerefMut for AudioPlayer {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.backend
}
}