pub struct Mixer {
pub listener: [f32; 3],
pub right: [f32; 3],
/* private fields */
}Fields§
§listener: [f32; 3]§right: [f32; 3]Implementations§
Source§impl Mixer
impl Mixer
pub fn new(sample_rate: u32, limit: usize) -> Option<Mixer>
pub fn sample_rate(&self) -> u32
Sourcepub fn set_sample_rate(&mut self, rate: u32) -> bool
pub fn set_sample_rate(&mut self, rate: u32) -> bool
Does not restart voices. Fractional source positions survive a device-rate change. Active gain fades keep their current value and remaining duration, rounded up to the next frame at the new rate. Returns false without changes for an unsupported rate or if a fade’s remaining frame count would overflow u64.
Sourcepub fn master_gain(&self) -> f32
pub fn master_gain(&self) -> f32
Current master gain, including any rendered or skipped portion of a fade.
Sourcepub fn set_master_gain(&mut self, gain: f32) -> bool
pub fn set_master_gain(&mut self, gain: f32) -> bool
Set a finite gain in 0..=1 immediately, cancelling its fade. Invalid input leaves both the current gain and any active fade unchanged.
Sourcepub fn bus_gain(&self, bus: Bus) -> f32
pub fn bus_gain(&self, bus: Bus) -> f32
Current bus gain, including any rendered or skipped portion of a fade.
Sourcepub fn set_bus_gain(&mut self, bus: Bus, gain: f32) -> bool
pub fn set_bus_gain(&mut self, bus: Bus, gain: f32) -> bool
Set a finite gain in 0..=1 immediately, cancelling this bus’s fade. Invalid input leaves the current gain and fade unchanged.
Sourcepub fn fade_master_gain(&mut self, gain: f32, seconds: f64) -> bool
pub fn fade_master_gain(&mut self, gain: f32, seconds: f64) -> bool
Fade linearly from the current master gain to gain over seconds.
Retargeting starts from the current value without a jump. Each rendered or skipped output frame advances the fade once, even without voices. Duration rounds up to whole output frames; the last frame reaches the target exactly. Zero seconds applies immediately. Gain must be finite in 0..=1; duration must be finite, nonnegative and fit in a u64 frame count. Invalid input returns false without changing an existing fade.
Fading to zero mutes exactly, independently of voice gain smoothing.
Muted voices keep advancing; fade back to a saved gain to unmute.
Self::suspend does not advance or cancel fades; Self::skip does
advance them when a host stops rendering PCM.
use ferravex_play::audio::Mixer;
let mut mixer = Mixer::new(48_000, 16).unwrap();
let resume_gain = mixer.master_gain();
assert!(mixer.fade_master_gain(0.0, 0.01));
mixer.render(&mut [[0.0; 2]; 480]);
assert_eq!(mixer.master_gain(), 0.0);
assert!(!mixer.is_master_fading());
assert!(mixer.fade_master_gain(resume_gain, 0.01));Sourcepub fn fade_bus_gain(&mut self, bus: Bus, gain: f32, seconds: f64) -> bool
pub fn fade_bus_gain(&mut self, bus: Bus, gain: f32, seconds: f64) -> bool
Fade one bus independently of the master and other buses. Validation,
timing, retargeting and mute behavior match Self::fade_master_gain.
use ferravex_play::audio::{Bus, Mixer};
let mut mixer = Mixer::new(48_000, 16).unwrap();
assert!(mixer.fade_bus_gain(Bus::Music, 0.2, 0.25));
mixer.skip(6_000);
assert_eq!(mixer.bus_gain(Bus::Music), 0.6);
// A new fade starts at 0.6, including when it reverses direction.
assert!(mixer.fade_bus_gain(Bus::Music, 1.0, 0.25));
assert_eq!(mixer.bus_gain(Bus::Effects), 1.0);Sourcepub fn cancel_master_fade(&mut self) -> bool
pub fn cancel_master_fade(&mut self) -> bool
Freeze the master at its current gain; return whether a fade was active.
use ferravex_play::audio::Mixer;
let mut mixer = Mixer::new(8_000, 1).unwrap();
mixer.fade_master_gain(0.0, 1.0);
mixer.skip(4_000);
assert!(mixer.cancel_master_fade());
mixer.skip(8_000);
assert_eq!(mixer.master_gain(), 0.5);
assert!(!mixer.is_master_fading());Sourcepub fn cancel_bus_fade(&mut self, bus: Bus) -> bool
pub fn cancel_bus_fade(&mut self, bus: Bus) -> bool
Freeze this bus at its current gain; return whether a fade was active.
use ferravex_play::audio::{Bus, Mixer};
let mut mixer = Mixer::new(8_000, 1).unwrap();
mixer.fade_bus_gain(Bus::Music, 0.0, 1.0);
mixer.skip(4_000);
assert!(mixer.cancel_bus_fade(Bus::Music));
assert!(!mixer.is_bus_fading(Bus::Music));
assert!(!mixer.cancel_bus_fade(Bus::Music));Sourcepub fn is_master_fading(&self) -> bool
pub fn is_master_fading(&self) -> bool
Whether the master fade still has output frames to advance.
See Self::fade_master_gain for an example.
Sourcepub fn is_bus_fading(&self, bus: Bus) -> bool
pub fn is_bus_fading(&self, bus: Bus) -> bool
Whether this bus’s fade still has output frames to advance.
See Self::cancel_bus_fade for an example.
pub fn stats(&self) -> MixStats
Sourcepub fn voice_policy(&self) -> Option<VoicePolicy>
pub fn voice_policy(&self) -> Option<VoicePolicy>
Current opt-in policy; None preserves legacy priority-only admission.
See Self::set_voice_policy for configuration and validation.
Sourcepub fn set_voice_policy(&mut self, policy: Option<VoicePolicy>) -> bool
pub fn set_voice_policy(&mut self, policy: Option<VoicePolicy>) -> bool
Enable, replace or disable critical-slot reservations without allocating.
Returns false without changes if the critical threshold is zero, the
reserve exceeds the voice limit, or existing cosmetic voices (including
releases) exceed the proposed cosmetic budget. Stop those voices first
or wait for them to finish. None disables the policy immediately.
Successful changes preserve voices, handles, statistics and all fades.
use ferravex_play::audio::Mixer;
let mut mixer = Mixer::new(48_000, 16).unwrap();
assert_eq!(mixer.voice_policy(), None);
assert!(mixer.set_voice_policy(Some(Default::default())));
let mut policy = mixer.voice_policy().unwrap();
policy.critical_priority = 200;
policy.reserved_critical = 4; // At most 12 cosmetic voices.
assert!(mixer.set_voice_policy(Some(policy)));
policy.reserved_critical = 17;
assert!(!mixer.set_voice_policy(Some(policy)));
assert_eq!(mixer.voice_policy().unwrap().reserved_critical, 4);
assert!(mixer.set_voice_policy(None));Sourcepub fn play(
&mut self,
clip: Clip,
position: [f32; 3],
gain: f32,
looping: bool,
) -> bool
pub fn play( &mut self, clip: Clip, position: [f32; 3], gain: f32, looping: bool, ) -> bool
Compatibility entry point. Use a validated Sound for repeated playback.
Sourcepub fn start(
&mut self,
sound: &Sound,
params: VoiceParams,
) -> Option<VoiceHandle>
pub fn start( &mut self, sound: &Sound, params: VoiceParams, ) -> Option<VoiceHandle>
Bounded insertion; a higher-priority sound can replace the oldest voice
of the lowest lower priority. Equal-priority overload drops the new voice.
Honors any opt-in Self::set_voice_policy reservation. Use
Self::start_with_outcome to distinguish rejection from stealing.
Sourcepub fn start_with_outcome(
&mut self,
sound: &Sound,
params: VoiceParams,
) -> VoiceAdmission
pub fn start_with_outcome( &mut self, sound: &Sound, params: VoiceParams, ) -> VoiceAdmission
Admit a voice with an explicit result, scanning at most 128 fixed slots.
A cosmetic cue at its budget can replace a lower-priority cosmetic even when reserved slots are empty. Critical cues first use any empty slot, then steal the oldest voice of the lowest strictly lower priority. Critical cues can still be rejected when all voices have equal or higher priority. A disabled policy uses the original priority-only behavior.
Stealing immediately retires the replaced voice, including its release
fade, and invalidates its handle. The new voice uses its own gain ramp;
no overlapping tail or extra voice is allocated. Other voices and
bus/master fades are unchanged. Rejections increment stats().dropped
except for invalid parameters; steals increment stats().stolen.
Keep a bank owner alive if the last sample Arc must not free on stealing.
use ferravex_play::audio::{Clip, Mixer, Sound, VoiceParams};
let sound = Sound::new(Clip::tone(8_000, 440.0, 0.1).unwrap()).unwrap();
let mut mixer = Mixer::new(8_000, 2).unwrap();
assert!(mixer.set_voice_policy(Some(Default::default())));
let cosmetic = mixer.start_with_outcome(&sound, VoiceParams::default());
let first = cosmetic.handle().unwrap();
assert_eq!(cosmetic.replaced(), None);
let denied = mixer.start_with_outcome(&sound, VoiceParams::default());
assert!(denied.rejection().is_some()); // One slot is reserved.
let critical = VoiceParams { priority: 128, ..Default::default() };
assert!(mixer.start_with_outcome(&sound, critical).handle().is_some());
let replacement = mixer.start_with_outcome(&sound, critical);
assert_eq!(replacement.replaced(), Some(first));
assert!(mixer.params(first).is_none());
assert_eq!(mixer.stats().stolen, 1);pub fn params(&self, handle: VoiceHandle) -> Option<VoiceParams>
Sourcepub fn set_params(&mut self, handle: VoiceHandle, params: VoiceParams) -> bool
pub fn set_params(&mut self, handle: VoiceHandle, params: VoiceParams) -> bool
Change a live voice’s parameters. Invalid input or a stale handle leaves it unchanged. With a voice policy, demoting a critical voice to cosmetic also fails if the cosmetic budget is full; other parameter changes keep their existing behavior, including during release fades.
pub fn stop(&mut self, handle: VoiceHandle, fade_frames: u32) -> bool
pub fn stop_all(&mut self)
Sourcepub fn suspend(&mut self)
pub fn suspend(&mut self)
Drop transient/releasing voices and prime continuous loops for a quiet resume. Used at output focus/mute boundaries, with no allocation.
pub fn active_voices(&self) -> usize
Sourcepub fn render(&mut self, out: &mut [[f32; 2]])
pub fn render(&mut self, out: &mut [[f32; 2]])
Bounded work, no allocations. Finished voices release their Sound Arc.
Sourcepub fn skip(&mut self, frames: u32)
pub fn skip(&mut self, frames: u32)
Advance inaudible/suspended hosts in bounded O(voices), without generating PCM. Gain fades advance by the same output time, including when no voices exist.