mod wav;
use std::{
cell::RefCell,
fmt,
ops::Index,
rc::Rc,
sync::{Arc, OnceLock},
};
use cranpose_core::{CompositionLocal, CompositionLocalProvider, compositionLocalOfWithPolicy};
use cranpose_macros::composable;
use parking_lot::Mutex;
use crate::registry::ServiceRegistry;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct SoundId(u32);
impl SoundId {
pub const NONE: SoundId = SoundId(0);
pub fn from_raw(raw: u32) -> Self {
SoundId(raw)
}
pub fn raw(self) -> u32 {
self.0
}
pub fn is_valid(self) -> bool {
self.0 != 0
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct VoiceId(u64);
impl VoiceId {
pub const NONE: VoiceId = VoiceId(0);
pub fn from_raw(raw: u64) -> Self {
VoiceId(raw)
}
pub fn raw(self) -> u64 {
self.0
}
pub fn is_valid(self) -> bool {
self.0 != 0
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
pub enum AudioBus {
#[default]
Effects,
Music,
}
impl AudioBus {
pub const ALL: [AudioBus; 2] = [AudioBus::Effects, AudioBus::Music];
pub fn index(self) -> usize {
match self {
AudioBus::Effects => 0,
AudioBus::Music => 1,
}
}
pub fn from_index(index: usize) -> Option<AudioBus> {
AudioBus::ALL.get(index).copied()
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct PlaybackParams {
pub volume: f32,
pub rate: f32,
pub pan: f32,
pub bus: AudioBus,
}
impl PlaybackParams {
pub const DEFAULT: PlaybackParams = PlaybackParams {
volume: 1.0,
rate: 1.0,
pan: 0.0,
bus: AudioBus::Effects,
};
pub const MIN_RATE: f32 = 0.05;
pub const MAX_RATE: f32 = 8.0;
pub const MAX_VOLUME: f32 = 4.0;
pub const fn new() -> PlaybackParams {
PlaybackParams::DEFAULT
}
pub fn volume(mut self, volume: f32) -> PlaybackParams {
self.volume = volume;
self
}
pub fn rate(mut self, rate: f32) -> PlaybackParams {
self.rate = rate;
self
}
pub fn pan(mut self, pan: f32) -> PlaybackParams {
self.pan = pan;
self
}
pub fn bus(mut self, bus: AudioBus) -> PlaybackParams {
self.bus = bus;
self
}
pub fn pitch_semitones(self, semitones: f32) -> PlaybackParams {
let semitones = if semitones.is_finite() {
semitones
} else {
0.0
};
self.rate(2.0f32.powf(semitones / 12.0))
}
pub fn sanitized(self) -> PlaybackParams {
fn finite(value: f32, fallback: f32) -> f32 {
if value.is_finite() { value } else { fallback }
}
PlaybackParams {
volume: finite(self.volume, 1.0).clamp(0.0, PlaybackParams::MAX_VOLUME),
rate: finite(self.rate, 1.0).clamp(PlaybackParams::MIN_RATE, PlaybackParams::MAX_RATE),
pan: finite(self.pan, 0.0).clamp(-1.0, 1.0),
bus: self.bus,
}
}
pub fn gains(self) -> (f32, f32) {
let params = self.sanitized();
let angle = (params.pan + 1.0) * std::f32::consts::FRAC_PI_4;
(params.volume * angle.cos(), params.volume * angle.sin())
}
}
impl Default for PlaybackParams {
fn default() -> PlaybackParams {
PlaybackParams::DEFAULT
}
}
#[derive(Clone)]
pub struct AudioClip {
samples: Arc<[f32]>,
channels: u16,
sample_rate: u32,
}
impl AudioClip {
pub const MAX_FRAMES: usize = 1 << 26;
pub fn from_samples(
samples: Vec<f32>,
channels: u16,
sample_rate: u32,
) -> Result<AudioClip, AudioError> {
if channels == 0 || channels > 2 {
return Err(AudioError::UnsupportedFormat(format!(
"clips must be mono or stereo, got {channels} channels"
)));
}
if sample_rate == 0 {
return Err(AudioError::UnsupportedFormat(
"clips must declare a non-zero sample rate".to_string(),
));
}
if samples.is_empty() {
return Err(AudioError::Decode("clip holds no samples".to_string()));
}
if !samples.len().is_multiple_of(usize::from(channels)) {
return Err(AudioError::Decode(format!(
"clip holds {} samples, which is not a whole number of {channels}-channel frames",
samples.len()
)));
}
if samples.len() / usize::from(channels) > AudioClip::MAX_FRAMES {
return Err(AudioError::Decode(
"clip exceeds the maximum in-memory length".to_string(),
));
}
Ok(AudioClip {
samples: samples.into(),
channels,
sample_rate,
})
}
pub fn decode(bytes: &[u8]) -> Result<AudioClip, AudioError> {
if wav::is_wav(bytes) {
wav::decode(bytes)
} else {
Err(AudioError::UnsupportedFormat(
"only RIFF/WAVE clips are decoded by the framework".to_string(),
))
}
}
pub fn samples(&self) -> &[f32] {
&self.samples
}
pub fn shared_samples(&self) -> Arc<[f32]> {
Arc::clone(&self.samples)
}
pub fn channels(&self) -> u16 {
self.channels
}
pub fn sample_rate(&self) -> u32 {
self.sample_rate
}
pub fn frames(&self) -> usize {
self.samples.len() / usize::from(self.channels)
}
pub fn duration_secs(&self) -> f32 {
self.frames() as f32 / self.sample_rate as f32
}
}
impl fmt::Debug for AudioClip {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AudioClip")
.field("frames", &self.frames())
.field("channels", &self.channels)
.field("sample_rate", &self.sample_rate)
.finish()
}
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum AudioError {
#[error("no audio backend is installed")]
Unsupported,
#[error("unsupported audio format: {0}")]
UnsupportedFormat(String),
#[error("failed to decode audio: {0}")]
Decode(String),
#[error("the audio engine already holds its maximum of {capacity} clips")]
ClipTableFull {
capacity: usize,
},
#[error("audio backend failure: {0}")]
Backend(String),
}
pub trait AudioPlayer: Send + Sync {
fn load_clip(&self, clip: AudioClip) -> Result<SoundId, AudioError>;
fn load(&self, bytes: &[u8]) -> Result<SoundId, AudioError> {
self.load_clip(AudioClip::decode(bytes)?)
}
fn unload(&self, _id: SoundId) {}
fn play(&self, id: SoundId, params: PlaybackParams);
fn play_loop(&self, id: SoundId, params: PlaybackParams) -> VoiceId;
fn stop(&self, id: SoundId);
fn stop_voice(&self, voice: VoiceId);
fn stop_all(&self) {}
fn set_voice_params(&self, _voice: VoiceId, _params: PlaybackParams) {}
fn set_master_volume(&self, volume: f32);
fn master_volume(&self) -> f32 {
1.0
}
fn set_bus_volume(&self, _bus: AudioBus, _volume: f32) {}
fn bus_volume(&self, _bus: AudioBus) -> f32 {
1.0
}
fn set_bus_enabled(&self, _bus: AudioBus, _enabled: bool) {}
fn bus_enabled(&self, _bus: AudioBus) -> bool {
true
}
fn suspend(&self) {}
fn resume(&self) {}
fn is_available(&self) -> bool {
false
}
}
pub type AudioPlayerRef = Arc<dyn AudioPlayer>;
#[derive(Default)]
pub struct NoopAudioPlayer {
next_sound: Mutex<u32>,
next_voice: Mutex<u64>,
master: Mutex<f32>,
bus_volumes: Mutex<[f32; 2]>,
bus_enabled: Mutex<[bool; 2]>,
}
impl NoopAudioPlayer {
pub fn new() -> NoopAudioPlayer {
NoopAudioPlayer {
next_sound: Mutex::new(0),
next_voice: Mutex::new(0),
master: Mutex::new(1.0),
bus_volumes: Mutex::new([1.0, 1.0]),
bus_enabled: Mutex::new([true, true]),
}
}
}
impl AudioPlayer for NoopAudioPlayer {
fn load_clip(&self, _clip: AudioClip) -> Result<SoundId, AudioError> {
let mut next = self.next_sound.lock();
*next = next.saturating_add(1);
Ok(SoundId::from_raw(*next))
}
fn play(&self, _id: SoundId, _params: PlaybackParams) {}
fn play_loop(&self, id: SoundId, _params: PlaybackParams) -> VoiceId {
if !id.is_valid() {
return VoiceId::NONE;
}
let mut next = self.next_voice.lock();
*next = next.saturating_add(1);
VoiceId::from_raw(*next)
}
fn stop(&self, _id: SoundId) {}
fn stop_voice(&self, _voice: VoiceId) {}
fn set_master_volume(&self, volume: f32) {
*self.master.lock() = if volume.is_finite() {
volume.clamp(0.0, 1.0)
} else {
1.0
};
}
fn master_volume(&self) -> f32 {
*self.master.lock()
}
fn set_bus_volume(&self, bus: AudioBus, volume: f32) {
let mut volumes = self.bus_volumes.lock();
volumes[bus.index()] = if volume.is_finite() {
volume.clamp(0.0, 1.0)
} else {
1.0
};
}
fn bus_volume(&self, bus: AudioBus) -> f32 {
self.bus_volumes.lock()[bus.index()]
}
fn set_bus_enabled(&self, bus: AudioBus, enabled: bool) {
self.bus_enabled.lock()[bus.index()] = enabled;
}
fn bus_enabled(&self, bus: AudioBus) -> bool {
self.bus_enabled.lock()[bus.index()]
}
}
static PLATFORM_AUDIO: ServiceRegistry<dyn AudioPlayer> = ServiceRegistry::new();
static NOOP_AUDIO: OnceLock<AudioPlayerRef> = OnceLock::new();
static DEFAULT_AUDIO: OnceLock<AudioPlayerRef> = OnceLock::new();
struct PlatformAudioPlayer;
fn registered_audio() -> AudioPlayerRef {
PLATFORM_AUDIO.get_or_warn("audio").unwrap_or_else(|| {
NOOP_AUDIO
.get_or_init(|| Arc::new(NoopAudioPlayer::new()))
.clone()
})
}
impl AudioPlayer for PlatformAudioPlayer {
fn load_clip(&self, clip: AudioClip) -> Result<SoundId, AudioError> {
registered_audio().load_clip(clip)
}
fn unload(&self, id: SoundId) {
registered_audio().unload(id);
}
fn play(&self, id: SoundId, params: PlaybackParams) {
registered_audio().play(id, params);
}
fn play_loop(&self, id: SoundId, params: PlaybackParams) -> VoiceId {
registered_audio().play_loop(id, params)
}
fn stop(&self, id: SoundId) {
registered_audio().stop(id);
}
fn stop_voice(&self, voice: VoiceId) {
registered_audio().stop_voice(voice);
}
fn stop_all(&self) {
registered_audio().stop_all();
}
fn set_voice_params(&self, voice: VoiceId, params: PlaybackParams) {
registered_audio().set_voice_params(voice, params);
}
fn set_master_volume(&self, volume: f32) {
registered_audio().set_master_volume(volume);
}
fn master_volume(&self) -> f32 {
registered_audio().master_volume()
}
fn set_bus_volume(&self, bus: AudioBus, volume: f32) {
registered_audio().set_bus_volume(bus, volume);
}
fn bus_volume(&self, bus: AudioBus) -> f32 {
registered_audio().bus_volume(bus)
}
fn set_bus_enabled(&self, bus: AudioBus, enabled: bool) {
registered_audio().set_bus_enabled(bus, enabled);
}
fn bus_enabled(&self, bus: AudioBus) -> bool {
registered_audio().bus_enabled(bus)
}
fn suspend(&self) {
registered_audio().suspend();
}
fn resume(&self) {
registered_audio().resume();
}
fn is_available(&self) -> bool {
registered_audio().is_available()
}
}
pub fn set_platform_audio(player: AudioPlayerRef) {
PLATFORM_AUDIO.set(player);
}
pub fn clear_platform_audio() {
PLATFORM_AUDIO.clear();
}
pub fn default_audio() -> AudioPlayerRef {
DEFAULT_AUDIO
.get_or_init(|| Arc::new(PlatformAudioPlayer))
.clone()
}
pub fn local_audio() -> CompositionLocal<AudioPlayerRef> {
thread_local! {
static LOCAL_AUDIO: RefCell<Option<CompositionLocal<AudioPlayerRef>>> = const { RefCell::new(None) };
}
LOCAL_AUDIO.with(|cell| {
let mut local = cell.borrow_mut();
local
.get_or_insert_with(|| compositionLocalOfWithPolicy(default_audio, Arc::ptr_eq))
.clone()
})
}
#[composable]
pub fn ProvideAudio(content: impl FnOnce()) {
let player = cranpose_core::remember(default_audio).with(|state| state.clone());
let local = local_audio();
CompositionLocalProvider(vec![local.provides(player)], move || {
content();
});
}
#[derive(Clone, Copy, Debug)]
pub struct SoundSpec<'a> {
pub name: &'static str,
pub bytes: &'a [u8],
pub base_volume: f32,
pub bus: AudioBus,
}
impl<'a> SoundSpec<'a> {
pub fn new(name: &'static str, bytes: &'a [u8]) -> SoundSpec<'a> {
SoundSpec {
name,
bytes,
base_volume: 1.0,
bus: AudioBus::Effects,
}
}
pub fn volume(mut self, base_volume: f32) -> SoundSpec<'a> {
self.base_volume = base_volume;
self
}
pub fn bus(mut self, bus: AudioBus) -> SoundSpec<'a> {
self.bus = bus;
self
}
}
#[derive(Clone, Copy, Debug)]
pub struct SoundBankEntry {
pub name: &'static str,
pub id: SoundId,
pub base_volume: f32,
pub bus: AudioBus,
}
#[derive(Clone, Debug)]
pub struct SoundBankFailure {
pub name: &'static str,
pub error: AudioError,
}
struct SoundBankInner {
player: AudioPlayerRef,
entries: Vec<SoundBankEntry>,
failures: Vec<SoundBankFailure>,
}
impl Drop for SoundBankInner {
fn drop(&mut self) {
for entry in &self.entries {
self.player.unload(entry.id);
}
}
}
#[derive(Clone)]
pub struct SoundBank {
inner: Rc<SoundBankInner>,
}
impl SoundBank {
pub fn load(player: AudioPlayerRef, specs: &[SoundSpec<'_>]) -> SoundBank {
let mut entries = Vec::with_capacity(specs.len());
let mut failures = Vec::new();
for spec in specs {
match player.load(spec.bytes) {
Ok(id) => entries.push(SoundBankEntry {
name: spec.name,
id,
base_volume: spec.base_volume,
bus: spec.bus,
}),
Err(error) => {
entries.push(SoundBankEntry {
name: spec.name,
id: SoundId::NONE,
base_volume: spec.base_volume,
bus: spec.bus,
});
failures.push(SoundBankFailure {
name: spec.name,
error,
});
}
}
}
SoundBank {
inner: Rc::new(SoundBankInner {
player,
entries,
failures,
}),
}
}
pub fn len(&self) -> usize {
self.inner.entries.len()
}
pub fn is_empty(&self) -> bool {
self.inner.entries.is_empty()
}
pub fn entries(&self) -> &[SoundBankEntry] {
&self.inner.entries
}
pub fn failures(&self) -> &[SoundBankFailure] {
&self.inner.failures
}
pub fn player(&self) -> AudioPlayerRef {
Arc::clone(&self.inner.player)
}
pub fn id(&self, index: usize) -> SoundId {
self.inner
.entries
.get(index)
.map_or(SoundId::NONE, |entry| entry.id)
}
pub fn find(&self, name: &str) -> Option<SoundId> {
self.inner
.entries
.iter()
.find(|entry| entry.name == name)
.map(|entry| entry.id)
}
pub fn play(&self, index: usize) {
self.play_with(index, PlaybackParams::DEFAULT);
}
pub fn play_with(&self, index: usize, params: PlaybackParams) {
let Some(entry) = self.inner.entries.get(index) else {
return;
};
if !entry.id.is_valid() {
return;
}
self.inner.player.play(entry.id, entry.apply(params));
}
pub fn play_named(&self, name: &str, params: PlaybackParams) {
if let Some(index) = self.inner.entries.iter().position(|e| e.name == name) {
self.play_with(index, params);
}
}
pub fn play_loop(&self, index: usize, params: PlaybackParams) -> VoiceId {
let Some(entry) = self.inner.entries.get(index) else {
return VoiceId::NONE;
};
if !entry.id.is_valid() {
return VoiceId::NONE;
}
self.inner.player.play_loop(entry.id, entry.apply(params))
}
pub fn stop(&self, index: usize) {
let id = self.id(index);
if id.is_valid() {
self.inner.player.stop(id);
}
}
}
impl SoundBankEntry {
fn apply(&self, params: PlaybackParams) -> PlaybackParams {
PlaybackParams {
volume: params.volume * self.base_volume,
rate: params.rate,
pan: params.pan,
bus: self.bus,
}
}
}
impl fmt::Debug for SoundBank {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SoundBank")
.field("entries", &self.inner.entries.len())
.field("failures", &self.inner.failures.len())
.finish()
}
}
impl Index<usize> for SoundBank {
type Output = SoundId;
fn index(&self, index: usize) -> &SoundId {
static NONE: SoundId = SoundId::NONE;
self.inner
.entries
.get(index)
.map_or(&NONE, |entry| &entry.id)
}
}
#[composable(no_skip)]
#[track_caller]
pub fn rememberSoundBank(specs: &[SoundSpec<'_>]) -> SoundBank {
let key = sound_bank_key(specs);
let player = local_audio().current();
cranpose_core::rememberKeyed((key, Arc::as_ptr(&player) as *const () as usize), |_| {
SoundBank::load(Arc::clone(&player), specs)
})
}
fn sound_bank_key(specs: &[SoundSpec<'_>]) -> (usize, u64) {
let mut hash = 0xcbf2_9ce4_8422_2325u64;
for spec in specs {
for byte in spec.name.as_bytes() {
hash ^= u64::from(*byte);
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
hash ^= spec.bytes.len() as u64;
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
(specs.len(), hash)
}
#[cfg(test)]
#[path = "tests/audio_tests.rs"]
mod tests;