1mod wav;
26
27use std::{
28 cell::RefCell,
29 fmt,
30 ops::Index,
31 rc::Rc,
32 sync::{Arc, OnceLock},
33};
34
35use cranpose_core::{CompositionLocal, CompositionLocalProvider, compositionLocalOfWithPolicy};
36use cranpose_macros::composable;
37use parking_lot::Mutex;
38
39use crate::registry::ServiceRegistry;
40
41#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
46pub struct SoundId(u32);
47
48impl SoundId {
49 pub const NONE: SoundId = SoundId(0);
51
52 pub fn from_raw(raw: u32) -> Self {
54 SoundId(raw)
55 }
56
57 pub fn raw(self) -> u32 {
59 self.0
60 }
61
62 pub fn is_valid(self) -> bool {
64 self.0 != 0
65 }
66}
67
68#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
71pub struct VoiceId(u64);
72
73impl VoiceId {
74 pub const NONE: VoiceId = VoiceId(0);
76
77 pub fn from_raw(raw: u64) -> Self {
79 VoiceId(raw)
80 }
81
82 pub fn raw(self) -> u64 {
84 self.0
85 }
86
87 pub fn is_valid(self) -> bool {
89 self.0 != 0
90 }
91}
92
93#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
96pub enum AudioBus {
97 #[default]
99 Effects,
100 Music,
102}
103
104impl AudioBus {
105 pub const ALL: [AudioBus; 2] = [AudioBus::Effects, AudioBus::Music];
107
108 pub fn index(self) -> usize {
110 match self {
111 AudioBus::Effects => 0,
112 AudioBus::Music => 1,
113 }
114 }
115
116 pub fn from_index(index: usize) -> Option<AudioBus> {
118 AudioBus::ALL.get(index).copied()
119 }
120}
121
122#[derive(Clone, Copy, PartialEq, Debug)]
127pub struct PlaybackParams {
128 pub volume: f32,
130 pub rate: f32,
133 pub pan: f32,
135 pub bus: AudioBus,
137}
138
139impl PlaybackParams {
140 pub const DEFAULT: PlaybackParams = PlaybackParams {
142 volume: 1.0,
143 rate: 1.0,
144 pan: 0.0,
145 bus: AudioBus::Effects,
146 };
147
148 pub const MIN_RATE: f32 = 0.05;
150 pub const MAX_RATE: f32 = 8.0;
152 pub const MAX_VOLUME: f32 = 4.0;
154
155 pub const fn new() -> PlaybackParams {
157 PlaybackParams::DEFAULT
158 }
159
160 pub fn volume(mut self, volume: f32) -> PlaybackParams {
162 self.volume = volume;
163 self
164 }
165
166 pub fn rate(mut self, rate: f32) -> PlaybackParams {
168 self.rate = rate;
169 self
170 }
171
172 pub fn pan(mut self, pan: f32) -> PlaybackParams {
174 self.pan = pan;
175 self
176 }
177
178 pub fn bus(mut self, bus: AudioBus) -> PlaybackParams {
180 self.bus = bus;
181 self
182 }
183
184 pub fn pitch_semitones(self, semitones: f32) -> PlaybackParams {
189 let semitones = if semitones.is_finite() {
190 semitones
191 } else {
192 0.0
193 };
194 self.rate(2.0f32.powf(semitones / 12.0))
195 }
196
197 pub fn sanitized(self) -> PlaybackParams {
202 fn finite(value: f32, fallback: f32) -> f32 {
203 if value.is_finite() { value } else { fallback }
204 }
205 PlaybackParams {
206 volume: finite(self.volume, 1.0).clamp(0.0, PlaybackParams::MAX_VOLUME),
207 rate: finite(self.rate, 1.0).clamp(PlaybackParams::MIN_RATE, PlaybackParams::MAX_RATE),
208 pan: finite(self.pan, 0.0).clamp(-1.0, 1.0),
209 bus: self.bus,
210 }
211 }
212
213 pub fn gains(self) -> (f32, f32) {
217 let params = self.sanitized();
218 let angle = (params.pan + 1.0) * std::f32::consts::FRAC_PI_4;
219 (params.volume * angle.cos(), params.volume * angle.sin())
220 }
221}
222
223impl Default for PlaybackParams {
224 fn default() -> PlaybackParams {
225 PlaybackParams::DEFAULT
226 }
227}
228
229#[derive(Clone)]
235pub struct AudioClip {
236 samples: Arc<[f32]>,
237 channels: u16,
238 sample_rate: u32,
239}
240
241impl AudioClip {
242 pub const MAX_FRAMES: usize = 1 << 26;
245
246 pub fn from_samples(
248 samples: Vec<f32>,
249 channels: u16,
250 sample_rate: u32,
251 ) -> Result<AudioClip, AudioError> {
252 if channels == 0 || channels > 2 {
253 return Err(AudioError::UnsupportedFormat(format!(
254 "clips must be mono or stereo, got {channels} channels"
255 )));
256 }
257 if sample_rate == 0 {
258 return Err(AudioError::UnsupportedFormat(
259 "clips must declare a non-zero sample rate".to_string(),
260 ));
261 }
262 if samples.is_empty() {
263 return Err(AudioError::Decode("clip holds no samples".to_string()));
264 }
265 if !samples.len().is_multiple_of(usize::from(channels)) {
266 return Err(AudioError::Decode(format!(
267 "clip holds {} samples, which is not a whole number of {channels}-channel frames",
268 samples.len()
269 )));
270 }
271 if samples.len() / usize::from(channels) > AudioClip::MAX_FRAMES {
272 return Err(AudioError::Decode(
273 "clip exceeds the maximum in-memory length".to_string(),
274 ));
275 }
276 Ok(AudioClip {
277 samples: samples.into(),
278 channels,
279 sample_rate,
280 })
281 }
282
283 pub fn decode(bytes: &[u8]) -> Result<AudioClip, AudioError> {
289 if wav::is_wav(bytes) {
290 wav::decode(bytes)
291 } else {
292 Err(AudioError::UnsupportedFormat(
293 "only RIFF/WAVE clips are decoded by the framework".to_string(),
294 ))
295 }
296 }
297
298 pub fn samples(&self) -> &[f32] {
300 &self.samples
301 }
302
303 pub fn shared_samples(&self) -> Arc<[f32]> {
305 Arc::clone(&self.samples)
306 }
307
308 pub fn channels(&self) -> u16 {
310 self.channels
311 }
312
313 pub fn sample_rate(&self) -> u32 {
315 self.sample_rate
316 }
317
318 pub fn frames(&self) -> usize {
320 self.samples.len() / usize::from(self.channels)
321 }
322
323 pub fn duration_secs(&self) -> f32 {
325 self.frames() as f32 / self.sample_rate as f32
326 }
327}
328
329impl fmt::Debug for AudioClip {
330 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
331 f.debug_struct("AudioClip")
332 .field("frames", &self.frames())
333 .field("channels", &self.channels)
334 .field("sample_rate", &self.sample_rate)
335 .finish()
336 }
337}
338
339#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
341pub enum AudioError {
342 #[error("no audio backend is installed")]
344 Unsupported,
345 #[error("unsupported audio format: {0}")]
347 UnsupportedFormat(String),
348 #[error("failed to decode audio: {0}")]
350 Decode(String),
351 #[error("the audio engine already holds its maximum of {capacity} clips")]
353 ClipTableFull {
354 capacity: usize,
356 },
357 #[error("audio backend failure: {0}")]
359 Backend(String),
360}
361
362pub trait AudioPlayer: Send + Sync {
370 fn load_clip(&self, clip: AudioClip) -> Result<SoundId, AudioError>;
372
373 fn load(&self, bytes: &[u8]) -> Result<SoundId, AudioError> {
380 self.load_clip(AudioClip::decode(bytes)?)
381 }
382
383 fn unload(&self, _id: SoundId) {}
385
386 fn play(&self, id: SoundId, params: PlaybackParams);
389
390 fn play_loop(&self, id: SoundId, params: PlaybackParams) -> VoiceId;
392
393 fn stop(&self, id: SoundId);
395
396 fn stop_voice(&self, voice: VoiceId);
398
399 fn stop_all(&self) {}
401
402 fn set_voice_params(&self, _voice: VoiceId, _params: PlaybackParams) {}
405
406 fn set_master_volume(&self, volume: f32);
408
409 fn master_volume(&self) -> f32 {
411 1.0
412 }
413
414 fn set_bus_volume(&self, _bus: AudioBus, _volume: f32) {}
416
417 fn bus_volume(&self, _bus: AudioBus) -> f32 {
419 1.0
420 }
421
422 fn set_bus_enabled(&self, _bus: AudioBus, _enabled: bool) {}
425
426 fn bus_enabled(&self, _bus: AudioBus) -> bool {
428 true
429 }
430
431 fn suspend(&self) {}
434
435 fn resume(&self) {}
437
438 fn is_available(&self) -> bool {
441 false
442 }
443}
444
445pub type AudioPlayerRef = Arc<dyn AudioPlayer>;
447
448#[derive(Default)]
454pub struct NoopAudioPlayer {
455 next_sound: Mutex<u32>,
456 next_voice: Mutex<u64>,
457 master: Mutex<f32>,
458 bus_volumes: Mutex<[f32; 2]>,
459 bus_enabled: Mutex<[bool; 2]>,
460}
461
462impl NoopAudioPlayer {
463 pub fn new() -> NoopAudioPlayer {
465 NoopAudioPlayer {
466 next_sound: Mutex::new(0),
467 next_voice: Mutex::new(0),
468 master: Mutex::new(1.0),
469 bus_volumes: Mutex::new([1.0, 1.0]),
470 bus_enabled: Mutex::new([true, true]),
471 }
472 }
473}
474
475impl AudioPlayer for NoopAudioPlayer {
476 fn load_clip(&self, _clip: AudioClip) -> Result<SoundId, AudioError> {
477 let mut next = self.next_sound.lock();
478 *next = next.saturating_add(1);
479 Ok(SoundId::from_raw(*next))
480 }
481
482 fn play(&self, _id: SoundId, _params: PlaybackParams) {}
483
484 fn play_loop(&self, id: SoundId, _params: PlaybackParams) -> VoiceId {
485 if !id.is_valid() {
486 return VoiceId::NONE;
487 }
488 let mut next = self.next_voice.lock();
489 *next = next.saturating_add(1);
490 VoiceId::from_raw(*next)
491 }
492
493 fn stop(&self, _id: SoundId) {}
494
495 fn stop_voice(&self, _voice: VoiceId) {}
496
497 fn set_master_volume(&self, volume: f32) {
498 *self.master.lock() = if volume.is_finite() {
499 volume.clamp(0.0, 1.0)
500 } else {
501 1.0
502 };
503 }
504
505 fn master_volume(&self) -> f32 {
506 *self.master.lock()
507 }
508
509 fn set_bus_volume(&self, bus: AudioBus, volume: f32) {
510 let mut volumes = self.bus_volumes.lock();
511 volumes[bus.index()] = if volume.is_finite() {
512 volume.clamp(0.0, 1.0)
513 } else {
514 1.0
515 };
516 }
517
518 fn bus_volume(&self, bus: AudioBus) -> f32 {
519 self.bus_volumes.lock()[bus.index()]
520 }
521
522 fn set_bus_enabled(&self, bus: AudioBus, enabled: bool) {
523 self.bus_enabled.lock()[bus.index()] = enabled;
524 }
525
526 fn bus_enabled(&self, bus: AudioBus) -> bool {
527 self.bus_enabled.lock()[bus.index()]
528 }
529}
530
531static PLATFORM_AUDIO: ServiceRegistry<dyn AudioPlayer> = ServiceRegistry::new();
532static NOOP_AUDIO: OnceLock<AudioPlayerRef> = OnceLock::new();
533static DEFAULT_AUDIO: OnceLock<AudioPlayerRef> = OnceLock::new();
534
535struct PlatformAudioPlayer;
536
537fn registered_audio() -> AudioPlayerRef {
538 PLATFORM_AUDIO.get_or_warn("audio").unwrap_or_else(|| {
539 NOOP_AUDIO
540 .get_or_init(|| Arc::new(NoopAudioPlayer::new()))
541 .clone()
542 })
543}
544
545impl AudioPlayer for PlatformAudioPlayer {
546 fn load_clip(&self, clip: AudioClip) -> Result<SoundId, AudioError> {
547 registered_audio().load_clip(clip)
548 }
549
550 fn unload(&self, id: SoundId) {
551 registered_audio().unload(id);
552 }
553
554 fn play(&self, id: SoundId, params: PlaybackParams) {
555 registered_audio().play(id, params);
556 }
557
558 fn play_loop(&self, id: SoundId, params: PlaybackParams) -> VoiceId {
559 registered_audio().play_loop(id, params)
560 }
561
562 fn stop(&self, id: SoundId) {
563 registered_audio().stop(id);
564 }
565
566 fn stop_voice(&self, voice: VoiceId) {
567 registered_audio().stop_voice(voice);
568 }
569
570 fn stop_all(&self) {
571 registered_audio().stop_all();
572 }
573
574 fn set_voice_params(&self, voice: VoiceId, params: PlaybackParams) {
575 registered_audio().set_voice_params(voice, params);
576 }
577
578 fn set_master_volume(&self, volume: f32) {
579 registered_audio().set_master_volume(volume);
580 }
581
582 fn master_volume(&self) -> f32 {
583 registered_audio().master_volume()
584 }
585
586 fn set_bus_volume(&self, bus: AudioBus, volume: f32) {
587 registered_audio().set_bus_volume(bus, volume);
588 }
589
590 fn bus_volume(&self, bus: AudioBus) -> f32 {
591 registered_audio().bus_volume(bus)
592 }
593
594 fn set_bus_enabled(&self, bus: AudioBus, enabled: bool) {
595 registered_audio().set_bus_enabled(bus, enabled);
596 }
597
598 fn bus_enabled(&self, bus: AudioBus) -> bool {
599 registered_audio().bus_enabled(bus)
600 }
601
602 fn suspend(&self) {
603 registered_audio().suspend();
604 }
605
606 fn resume(&self) {
607 registered_audio().resume();
608 }
609
610 fn is_available(&self) -> bool {
611 registered_audio().is_available()
612 }
613}
614
615pub fn set_platform_audio(player: AudioPlayerRef) {
617 PLATFORM_AUDIO.set(player);
618}
619
620pub fn clear_platform_audio() {
622 PLATFORM_AUDIO.clear();
623}
624
625pub fn default_audio() -> AudioPlayerRef {
627 DEFAULT_AUDIO
628 .get_or_init(|| Arc::new(PlatformAudioPlayer))
629 .clone()
630}
631
632pub fn local_audio() -> CompositionLocal<AudioPlayerRef> {
634 thread_local! {
635 static LOCAL_AUDIO: RefCell<Option<CompositionLocal<AudioPlayerRef>>> = const { RefCell::new(None) };
636 }
637
638 LOCAL_AUDIO.with(|cell| {
639 let mut local = cell.borrow_mut();
640 local
641 .get_or_insert_with(|| compositionLocalOfWithPolicy(default_audio, Arc::ptr_eq))
642 .clone()
643 })
644}
645
646#[composable]
648pub fn ProvideAudio(content: impl FnOnce()) {
649 let player = cranpose_core::remember(default_audio).with(|state| state.clone());
650 let local = local_audio();
651 CompositionLocalProvider(vec![local.provides(player)], move || {
652 content();
653 });
654}
655
656#[derive(Clone, Copy, Debug)]
659pub struct SoundSpec<'a> {
660 pub name: &'static str,
662 pub bytes: &'a [u8],
664 pub base_volume: f32,
667 pub bus: AudioBus,
669}
670
671impl<'a> SoundSpec<'a> {
672 pub fn new(name: &'static str, bytes: &'a [u8]) -> SoundSpec<'a> {
674 SoundSpec {
675 name,
676 bytes,
677 base_volume: 1.0,
678 bus: AudioBus::Effects,
679 }
680 }
681
682 pub fn volume(mut self, base_volume: f32) -> SoundSpec<'a> {
684 self.base_volume = base_volume;
685 self
686 }
687
688 pub fn bus(mut self, bus: AudioBus) -> SoundSpec<'a> {
690 self.bus = bus;
691 self
692 }
693}
694
695#[derive(Clone, Copy, Debug)]
697pub struct SoundBankEntry {
698 pub name: &'static str,
700 pub id: SoundId,
702 pub base_volume: f32,
704 pub bus: AudioBus,
706}
707
708#[derive(Clone, Debug)]
710pub struct SoundBankFailure {
711 pub name: &'static str,
713 pub error: AudioError,
715}
716
717struct SoundBankInner {
718 player: AudioPlayerRef,
719 entries: Vec<SoundBankEntry>,
720 failures: Vec<SoundBankFailure>,
721}
722
723impl Drop for SoundBankInner {
724 fn drop(&mut self) {
725 for entry in &self.entries {
726 self.player.unload(entry.id);
727 }
728 }
729}
730
731#[derive(Clone)]
738pub struct SoundBank {
739 inner: Rc<SoundBankInner>,
740}
741
742impl SoundBank {
743 pub fn load(player: AudioPlayerRef, specs: &[SoundSpec<'_>]) -> SoundBank {
746 let mut entries = Vec::with_capacity(specs.len());
747 let mut failures = Vec::new();
748 for spec in specs {
749 match player.load(spec.bytes) {
750 Ok(id) => entries.push(SoundBankEntry {
751 name: spec.name,
752 id,
753 base_volume: spec.base_volume,
754 bus: spec.bus,
755 }),
756 Err(error) => {
757 entries.push(SoundBankEntry {
758 name: spec.name,
759 id: SoundId::NONE,
760 base_volume: spec.base_volume,
761 bus: spec.bus,
762 });
763 failures.push(SoundBankFailure {
764 name: spec.name,
765 error,
766 });
767 }
768 }
769 }
770 SoundBank {
771 inner: Rc::new(SoundBankInner {
772 player,
773 entries,
774 failures,
775 }),
776 }
777 }
778
779 pub fn len(&self) -> usize {
781 self.inner.entries.len()
782 }
783
784 pub fn is_empty(&self) -> bool {
786 self.inner.entries.is_empty()
787 }
788
789 pub fn entries(&self) -> &[SoundBankEntry] {
791 &self.inner.entries
792 }
793
794 pub fn failures(&self) -> &[SoundBankFailure] {
796 &self.inner.failures
797 }
798
799 pub fn player(&self) -> AudioPlayerRef {
801 Arc::clone(&self.inner.player)
802 }
803
804 pub fn id(&self, index: usize) -> SoundId {
806 self.inner
807 .entries
808 .get(index)
809 .map_or(SoundId::NONE, |entry| entry.id)
810 }
811
812 pub fn find(&self, name: &str) -> Option<SoundId> {
814 self.inner
815 .entries
816 .iter()
817 .find(|entry| entry.name == name)
818 .map(|entry| entry.id)
819 }
820
821 pub fn play(&self, index: usize) {
823 self.play_with(index, PlaybackParams::DEFAULT);
824 }
825
826 pub fn play_with(&self, index: usize, params: PlaybackParams) {
829 let Some(entry) = self.inner.entries.get(index) else {
830 return;
831 };
832 if !entry.id.is_valid() {
833 return;
834 }
835 self.inner.player.play(entry.id, entry.apply(params));
836 }
837
838 pub fn play_named(&self, name: &str, params: PlaybackParams) {
840 if let Some(index) = self.inner.entries.iter().position(|e| e.name == name) {
841 self.play_with(index, params);
842 }
843 }
844
845 pub fn play_loop(&self, index: usize, params: PlaybackParams) -> VoiceId {
847 let Some(entry) = self.inner.entries.get(index) else {
848 return VoiceId::NONE;
849 };
850 if !entry.id.is_valid() {
851 return VoiceId::NONE;
852 }
853 self.inner.player.play_loop(entry.id, entry.apply(params))
854 }
855
856 pub fn stop(&self, index: usize) {
858 let id = self.id(index);
859 if id.is_valid() {
860 self.inner.player.stop(id);
861 }
862 }
863}
864
865impl SoundBankEntry {
866 fn apply(&self, params: PlaybackParams) -> PlaybackParams {
867 PlaybackParams {
868 volume: params.volume * self.base_volume,
869 rate: params.rate,
870 pan: params.pan,
871 bus: self.bus,
872 }
873 }
874}
875
876impl fmt::Debug for SoundBank {
877 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
878 f.debug_struct("SoundBank")
879 .field("entries", &self.inner.entries.len())
880 .field("failures", &self.inner.failures.len())
881 .finish()
882 }
883}
884
885impl Index<usize> for SoundBank {
886 type Output = SoundId;
887
888 fn index(&self, index: usize) -> &SoundId {
889 static NONE: SoundId = SoundId::NONE;
890 self.inner
891 .entries
892 .get(index)
893 .map_or(&NONE, |entry| &entry.id)
894 }
895}
896
897#[composable(no_skip)]
903#[track_caller]
904pub fn rememberSoundBank(specs: &[SoundSpec<'_>]) -> SoundBank {
905 let key = sound_bank_key(specs);
906 let player = local_audio().current();
907 cranpose_core::rememberKeyed((key, Arc::as_ptr(&player) as *const () as usize), |_| {
908 SoundBank::load(Arc::clone(&player), specs)
909 })
910}
911
912fn sound_bank_key(specs: &[SoundSpec<'_>]) -> (usize, u64) {
913 let mut hash = 0xcbf2_9ce4_8422_2325u64;
914 for spec in specs {
915 for byte in spec.name.as_bytes() {
916 hash ^= u64::from(*byte);
917 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
918 }
919 hash ^= spec.bytes.len() as u64;
920 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
921 }
922 (specs.len(), hash)
923}
924
925#[cfg(test)]
926#[path = "tests/audio_tests.rs"]
927mod tests;