#![warn(missing_docs)]
use std::{
hash::Hash,
sync::{Arc, Mutex},
};
pub mod converter;
mod sine;
#[cfg(feature = "ogg")]
mod ogg;
#[cfg(feature = "wav")]
mod wav;
mod engine;
pub use engine::AudioEngine;
mod mixer;
pub use mixer::Mixer;
pub use sine::SineWave;
#[cfg(feature = "ogg")]
pub use ogg::OggDecoder;
#[cfg(feature = "wav")]
pub use wav::WavDecoder;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct SampleRate(pub u32);
type SoundId = u64;
pub struct Sound<G: Eq + Hash + Send + 'static = ()> {
mixer: Arc<Mutex<Mixer<G>>>,
id: SoundId,
}
impl<G: Eq + Hash + Send + 'static> Sound<G> {
pub fn play(&mut self) {
self.mixer.lock().unwrap().play(self.id);
}
pub fn pause(&mut self) {
self.mixer.lock().unwrap().pause(self.id);
}
pub fn stop(&mut self) {
self.mixer.lock().unwrap().stop(self.id);
}
pub fn reset(&mut self) {
self.mixer.lock().unwrap().reset(self.id);
}
pub fn set_volume(&mut self, volume: f32) {
self.mixer.lock().unwrap().set_volume(self.id, volume);
}
pub fn set_loop(&mut self, looping: bool) {
self.mixer.lock().unwrap().set_loop(self.id, looping);
}
}
impl<G: Eq + Hash + Send + 'static> Drop for Sound<G> {
fn drop(&mut self) {
self.mixer.lock().unwrap().mark_to_remove(self.id, true);
}
}
pub trait SoundSource {
fn channels(&self) -> u16;
fn sample_rate(&self) -> u32;
fn reset(&mut self);
fn write_samples(&mut self, buffer: &mut [i16]) -> usize;
}
impl<T: SoundSource + ?Sized> SoundSource for Box<T> {
fn channels(&self) -> u16 {
(**self).channels()
}
fn sample_rate(&self) -> u32 {
(**self).sample_rate()
}
fn reset(&mut self) {
(**self).reset()
}
fn write_samples(&mut self, buffer: &mut [i16]) -> usize {
(**self).write_samples(buffer)
}
}
impl<T: SoundSource + ?Sized> SoundSource for Arc<Mutex<T>> {
fn channels(&self) -> u16 {
(*self).lock().unwrap().channels()
}
fn sample_rate(&self) -> u32 {
(*self).lock().unwrap().sample_rate()
}
fn reset(&mut self) {
(*self).lock().unwrap().reset()
}
fn write_samples(&mut self, buffer: &mut [i16]) -> usize {
(*self).lock().unwrap().write_samples(buffer)
}
}