pub struct Mixer<const L: usize> { /* private fields */ }Expand description
The voice allocator: lanes, policies, handles and lifetimes over the SPU’s 24
voices, for L lanes.
§Example
use psx_sfx::hw::{Event, Recorder};
use psx_sfx::mixer::*;
use psx_sfx::Sample;
use psx_spu::SpuAddr;
let mut mixer = Mixer::new([Lane::new(0b1, Policy::Smart)], 60);
let mut hw = Recorder::<16>::new();
let hum = Sound::looped(SoundId(2), Sample::resident(SpuAddr::new(0x2000), 11025, 8), 0);
let h = mixer.play(&mut hw, &hum, Play::new(1000, 1000)).unwrap();
mixer.update(&mut hw, h, 2000, 500);
assert_eq!(hw.last_volume(0), Some((2000, 500)));
mixer.stop(&mut hw, h);
assert!(!mixer.is_alive(h));
assert!(!mixer.update(&mut hw, h, 1, 1)); // a stale handle is a no-op
assert_eq!(hw.keyed_off(), 1);Implementations§
Source§impl<const L: usize> Mixer<L>
impl<const L: usize> Mixer<L>
Sourcepub const fn new(lanes: [Lane; L], ticks_hz: u32) -> Self
pub const fn new(lanes: [Lane; L], ticks_hz: u32) -> Self
A mixer over lanes, with durations measured against a clock that runs
at ticks_hz (60 for NTSC frames, 50 for PAL).
Lanes must lie inside the 24 voices and must not share a voice. Built in
a const, a violation is a compile error.
§Panics
If a lane is empty, uses a voice above 23, or overlaps another lane.
Sourcepub fn set_cull(&mut self, cull: i16)
pub fn set_cull(&mut self, cull: i16)
Sounds whose louder ear is at or below cull do not take a voice (unless
protected). Default 0.
let mut mixer = Mixer::new([Lane::new(1, Policy::Smart)], 60);
mixer.set_cull(100);
assert_eq!(mixer.cull(), 100);Sourcepub fn set_master<H: Hw>(&mut self, hw: &mut H, master: Master)
pub fn set_master<H: Hw>(&mut self, hw: &mut H, master: Master)
Replace the master volumes and re-apply them to every live voice at once, from the unscaled levels the mixer kept.
use psx_sfx::master::{Master, Ratio};
let mut half = Master::new();
half.set_sfx(Ratio::new(1, 2));
mixer.set_master(&mut hw, half);
assert_eq!(hw.last_volume(h.voice()), Some((500, 500)));Sourcepub fn tick<H: Hw>(&mut self, hw: &mut H, now: u32)
pub fn tick<H: Hw>(&mut self, hw: &mut H, now: u32)
Advance the clock to now and retire what has finished.
Call once per frame before anything that might start a sound. When no sound is due and no duck is moving this is two compares.
let mut mixer = Mixer::new([Lane::new(1, Policy::Smart)], 60);
for frame in 0..3 {
mixer.tick(&mut NullHw, frame);
}Sourcepub fn play<H: Hw>(
&mut self,
hw: &mut H,
sound: &Sound,
play: Play,
) -> Option<Handle>
pub fn play<H: Hw>( &mut self, hw: &mut H, sound: &Sound, play: Play, ) -> Option<Handle>
Start sound, or decide not to.
Returns a Handle for the new sound, or None when it was culled or
the lane had nothing to give. The order in which a lane under
Policy::Smart finds a voice:
- The same non-zero
Play::with_keyalready playing in the lane: take its voice. - An idle voice.
- A voice that is only ringing from a key-off, or a one-shot with a tick or two left.
- The least important live sound that is not protected and is no more important than the new one: lowest importance first, then quietest, then least time left. Equal importance may steal, but not a loop, and not a louder sound.
- Otherwise the new sound is dropped. A protected sound is never taken.
Sounds that are inaudible (see set_cull) are
dropped before any of this, unless protected.
Sourcepub fn is_alive(&self, handle: Handle) -> bool
pub fn is_alive(&self, handle: Handle) -> bool
Is handle still the sound it was issued for?
assert!(mixer.is_alive(h));
mixer.stop(&mut hw, h);
assert!(!mixer.is_alive(h));Sourcepub fn update<H: Hw>(
&mut self,
hw: &mut H,
handle: Handle,
left: i16,
right: i16,
) -> bool
pub fn update<H: Hw>( &mut self, hw: &mut H, handle: Handle, left: i16, right: i16, ) -> bool
Change a live sound’s levels (before the master volume). Writes nothing
when the levels are unchanged. Returns false and does nothing for a
stale handle.
assert!(mixer.update(&mut hw, h, 3000, 100));
assert_eq!(hw.last_volume(h.voice()), Some((3000, 100)));Sourcepub fn set_pitch<H: Hw>(
&mut self,
hw: &mut H,
handle: Handle,
pitch_q12: u16,
) -> bool
pub fn set_pitch<H: Hw>( &mut self, hw: &mut H, handle: Handle, pitch_q12: u16, ) -> bool
Change a live sound’s pitch multiplier (4096 = as cooked), clamped to
what the hardware can reach. Returns false for a stale handle. The
sound’s cutoff is not recomputed; use it for loops.
assert!(mixer.set_pitch(&mut hw, h, 6144)); // a fifth upSourcepub fn end_in(&mut self, handle: Handle, ticks: u32) -> bool
pub fn end_in(&mut self, handle: Handle, ticks: u32) -> bool
End a live sound ticks ticks from now, whatever it was going to do. A
retail-style “kill with a release”. Returns false for a stale handle.
mixer.tick(&mut hw, 0);
mixer.end_in(h, 8);
mixer.tick(&mut hw, 7);
assert!(mixer.is_alive(h));
mixer.tick(&mut hw, 8);
assert!(!mixer.is_alive(h));Sourcepub fn stop<H: Hw>(&mut self, hw: &mut H, handle: Handle) -> bool
pub fn stop<H: Hw>(&mut self, hw: &mut H, handle: Handle) -> bool
Silence a live sound and release its voice. Returns false for a stale
handle.
assert!(mixer.stop(&mut hw, h));
assert!(!mixer.stop(&mut hw, h)); // already goneSourcepub fn stop_all<H: Hw>(&mut self, hw: &mut H)
pub fn stop_all<H: Hw>(&mut self, hw: &mut H)
Stop every playing sound.
mixer.stop_all(&mut hw);
assert_eq!(mixer.playing(), 0);Sourcepub fn stop_range<H: Hw>(&mut self, hw: &mut H, lo: u32, hi: u32)
pub fn stop_range<H: Hw>(&mut self, hw: &mut H, lo: u32, hi: u32)
Stop every sound whose sample starts in lo..hi (SPU RAM byte offsets).
Call before a DMA over samples that may be playing.
mixer.stop_range(&mut hw, 0x2000, 0x3000); // the sound starts at 0x2000
assert!(!mixer.is_alive(h));Sourcepub fn playing(&self) -> u32
pub fn playing(&self) -> u32
The voices holding a live sound, as a mask.
assert_eq!(mixer.playing(), 1 << h.voice());Sourcepub fn free_in_lane(&self, lane: usize) -> u32
pub fn free_in_lane(&self, lane: usize) -> u32
The voices of lane that could take a sound without stealing: idle, or
only ringing from a key-off.
assert_eq!(mixer.free_in_lane(0), 0b11 & !(1 << h.voice()));Sourcepub fn has_free(&self, lane: usize) -> bool
pub fn has_free(&self, lane: usize) -> bool
Is any voice of lane free? See free_in_lane.
assert!(mixer.has_free(0));
assert!(!mixer.has_free(7)); // no such laneTrait Implementations§
Auto Trait Implementations§
impl<const L: usize> Freeze for Mixer<L>
impl<const L: usize> RefUnwindSafe for Mixer<L>
impl<const L: usize> Send for Mixer<L>
impl<const L: usize> Sync for Mixer<L>
impl<const L: usize> Unpin for Mixer<L>
impl<const L: usize> UnsafeUnpin for Mixer<L>
impl<const L: usize> UnwindSafe for Mixer<L>
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)