Skip to main content
← PSoXide SDK guides and examples · SDK 80d1c329e977 · All Cargo features enabled; check feature gates before use.

Mixer

Struct Mixer 

Source
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>

Source

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.

Source

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);
Source

pub const fn cull(&self) -> i16

The cull threshold.

Source

pub const fn master(&self) -> &Master

The master volumes and duck.

Source

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)));
Source

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);
}
Source

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:

  1. The same non-zero Play::with_key already playing in the lane: take its voice.
  2. An idle voice.
  3. A voice that is only ringing from a key-off, or a one-shot with a tick or two left.
  4. 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.
  5. 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.

Source

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));
Source

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)));
Source

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 up
Source

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));
Source

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 gone
Source

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);
Source

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));
Source

pub fn playing(&self) -> u32

The voices holding a live sound, as a mask.

assert_eq!(mixer.playing(), 1 << h.voice());
Source

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()));
Source

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 lane

Trait Implementations§

Source§

impl<const L: usize> Clone for Mixer<L>

Source§

fn clone(&self) -> Mixer<L>

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const L: usize> Debug for Mixer<L>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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 T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.