use std::collections::HashMap;
use sdl2::mixer::Chunk;
use sdl2::mixer::Channel;
use sdl2::mixer::Sdl2MixerContext;
use sdl2::mixer;
use sdl2::AudioSubsystem;
use std::path::PathBuf;
use audio::Playlist;
pub struct Audio {
sound_cache: HashMap<String, Chunk>,
_audio: AudioSubsystem,
_mixer: Sdl2MixerContext,
pub playlist: Playlist,
}
impl Audio {
pub fn play_sound(&mut self, path: &str, volume: f32) -> Result<(), String> {
if let Some(chunk) = self.sound_cache.get(path) {
let channel = Channel::all().play(chunk, 0)?;
channel.set_volume((volume * 128.0) as i32);
return Ok(());
}
let mut file_path = PathBuf::from("assets");
file_path.push("sounds");
file_path.push(path);
let chunk = Chunk::from_file(file_path)?;
let channel = Channel::all().play(&chunk, 0)?;
channel.set_volume((volume * 128.0) as i32);
self.sound_cache.insert(path.to_owned(), chunk);
Ok(())
}
pub fn play_sound_on_channel(&mut self, channel_id: i32, path: &str) -> Result<(), String> {
if channel_id >= 0 && channel_id < 128 {
if let Some(chunk) = self.sound_cache.get(path) {
mixer::channel(channel_id).play(chunk, 0)?;
return Ok(());
}
let mut file_path = PathBuf::from("assets");
file_path.push("sounds");
file_path.push(path);
let chunk = Chunk::from_file(file_path)?;
Channel::all().play(&chunk, 0)?;
self.sound_cache.insert(path.to_owned(), chunk);
return Ok(());
}
Err("Channel out of range.".to_string())
}
pub fn set_channel_volume(&mut self, channel_id: i32, volume: f32) -> Result<(), String> {
if channel_id >= 0 && channel_id < 128 {
mixer::channel(channel_id).set_volume((volume * 128.0) as i32);
return Ok(());
}
Err("Channel out of range.".to_string())
}
pub fn get_channel_volume(&self, channel_id: i32) -> Result<f32, String> {
if channel_id >= 0 && channel_id < 128 {
return Ok((mixer::channel(channel_id).get_volume() as f32) / 128.0);
}
Err("Channel out of range.".to_string())
}
pub fn pause_channel(&mut self, channel_id: i32) -> Result<(), String> {
if channel_id >= 0 && channel_id < 128 {
mixer::channel(channel_id).pause();
return Ok(());
}
Err("Channel out of range.".to_string())
}
pub fn resume_channel(&mut self, channel_id: i32) -> Result<(), String> {
if channel_id >= 0 && channel_id < 128 {
mixer::channel(channel_id).resume();
return Ok(());
}
Err("Channel out of range.".to_string())
}
pub fn channel_idle(&mut self, channel_id: i32) -> Option<bool> {
if channel_id >= 0 && channel_id < 128 {
return Some(mixer::channel(channel_id).is_playing());
}
None
}
pub(crate) fn new(audio: AudioSubsystem, mixer: Sdl2MixerContext) -> Audio {
Audio {
playlist: Playlist::new(),
sound_cache: HashMap::new(),
_audio: audio,
_mixer: mixer,
}
}
}