Files
audio/audio/voices.sakura
T
2026-11-14 15:37:01 -06:00

246 lines
8.2 KiB
Plaintext

#! Contains a mixer's voices started, changed, and stopped, and its buses' and
#! master gains.
import std::math;
module malunal::audio;
#! Plays a sound.
#! @param id The sound.
#! @param how How it plays.
#! @returns The voice playing it, or none where the id names no sound, or a
#! streamed one's bytes can't be read again.
pub def mixer::play(&mut this, id: sound_id, how: playback = playback{}): voice {
return voice{} if !this.holds(id);
var stream: maybe<box<streaming>> = nothing {};
if this.bank[id.index as u64].streamed {
val bytes := this.bank[id.index as u64].encoded.span;
var opened := stream::open(bytes) else (_) { return voice{}; };
var made := streaming{ decoding = move(opened), window = vector::<f32>::new(), marks = vector::<stream_mark>::new() };
made.marks.append(stream_mark{});
stream = box::new(move(made));
}
val index := this.slot();
val at := &mut this.voices[index as u64];
at.live = true;
at.sound = id;
at.how = how;
at.how.bus = how.bus < bus_count ? how.bus : 0;
at.position = 0;
at.gains = [0, 0];
at.started = false;
at.stopping = false;
at.level = how.fade_in > 0 ? 0 : 1;
at.fade = how.fade_in > 0 ? 1 / (how.fade_in * this.mix_rate as f32) : 0;
at.waiting = how.delay > 0 ? (how.delay * this.mix_rate as f32) as u64 : 0u;
at.filtering = filters{};
at.stream = move(stream);
return voice{ index = index, generation = at.generation };
}
#! Whether a voice is playing, or paused, rather than ended.
pub def mixer::playing(&this, which: voice): bool {
return false if which.generation == 0 || which.index as u64 >= this.voices.length;
val at := &this.voices[which.index as u64];
return at.live && at.generation == which.generation;
}
#! How far into its sound a voice plays, in seconds, or 0 where it ended: of a
#! looping one, how far into it this time.
pub def mixer::position(&this, which: voice): f64 {
return 0 if !this.playing(which);
val at := &this.voices[which.index as u64];
val rate := this.bank[at.sound.index as u64].rate as f64;
if at.stream is some(&s) {
val marks := &s.get().marks;
var sound := at.position;
for i in 0..marks.length {
break if marks[i].voice as f64 > at.position;
sound = marks[i].sound as f64 + (at.position - marks[i].voice as f64);
}
return sound / rate;
}
return at.position / rate;
}
#! Plays a voice on from a place in its sound, decoded or streamed, as exactly
#! as from its start, or, streamed of Opus, within a few ten-thousandths; past
#! its end, it ends, or, looping, goes back to where it loops.
#! @param which The voice.
#! @param seconds How far into its sound.
#! @returns Nothing, or why a streamed sound couldn't be read there, which ends
#! the voice.
pub def mixer::seek(&mut this, which: voice, seconds: f64): result<void, audio_error> {
return if !this.playing(which);
val held := &this.bank[this.voices[which.index as u64].sound.index as u64];
val rate := held.rate as f64;
var frame := math::max(seconds, 0.0) * rate;
val looped := held.loop_points;
val end := looped.end != 0 && (held.frames == 0 || looped.end < held.frames) ? looped.end : held.frames;
if end > looped.start && frame >= end as f64 && this.voices[which.index as u64].how.looping {
val length := (end - looped.start) as f64;
val past := frame - looped.start as f64;
frame = looped.start as f64 + past - math::floor(past / length) * length;
}
val at := &mut this.voices[which.index as u64];
if at.stream is some(&mut s) {
val window := &mut s.get();
# The voice's own frames go on from where it is, its sound's from the frame.
val now := math::floor(at.position) as u64;
val whole := math::floor(frame);
window.window.clear();
window.base = now;
window.ended = false;
window.marks.clear();
window.marks.append(stream_mark{ voice = now, sound = whole as u64 });
window.at = whole as u64;
at.position = now as f64 + (frame - whole);
window.decoding.seek(whole as u64) else (e) {
this.end_voice(which.index as u64);
return e;
};
return;
}
at.position = frame;
return;
}
#! Ends a voice, at once, or faded out over some seconds.
pub def mixer::stop(&mut this, which: voice, fade: f32 = 0): void {
return if !this.playing(which);
if fade <= 0 {
this.end_voice(which.index as u64);
return;
}
val at := &mut this.voices[which.index as u64];
at.stopping = true;
at.fade = -1 / (fade * this.mix_rate as f32);
}
#! Ends every voice at once.
pub def mixer::stop_all(&mut this): void {
for i in 0..this.voices.length {
if this.voices[i].live {
this.end_voice(i);
}
}
}
#! Changes how a voice plays: its gain, pan, pitch, whether it loops, whether
#! it's paused, and its bus. Its fade in is what it was.
pub def mixer::change(&mut this, which: voice, how: playback): void {
return if !this.playing(which);
val at := &mut this.voices[which.index as u64];
val fade_in := at.how.fade_in;
at.how = how;
at.how.fade_in = fade_in;
at.how.bus = how.bus < bus_count ? how.bus : 0;
}
#! How a voice plays, or as `playback{}` says where it ended.
pub def mixer::how(&this, which: voice): playback {
return playback{} if !this.playing(which);
return this.voices[which.index as u64].how;
}
#! Changes a voice's gain.
pub def mixer::set_gain(&mut this, which: voice, gain: f32): void {
return if !this.playing(which);
this.voices[which.index as u64].how.gain = gain;
}
#! Changes a voice's pan.
pub def mixer::set_pan(&mut this, which: voice, pan: f32): void {
return if !this.playing(which);
this.voices[which.index as u64].how.pan = math::clamp(pan, -1 as f32, 1 as f32);
}
#! Changes a voice's pitch.
pub def mixer::set_pitch(&mut this, which: voice, pitch: f32): void {
return if !this.playing(which);
this.voices[which.index as u64].how.pitch = pitch;
}
#! Pauses a voice, or plays it on from where it paused.
pub def mixer::pause(&mut this, which: voice, paused: bool = true): void {
return if !this.playing(which);
this.voices[which.index as u64].how.paused = paused;
}
#! A bus's gain.
pub def mixer::bus(&this, which: u32): f32 {
return which < bus_count ? this.buses[which as u64] : 0;
}
#! Changes a bus's gain, which every voice on it is multiplied by.
pub def mixer::set_bus(&mut this, which: u32, gain: f32): void {
return if which >= bus_count;
this.buses[which as u64] = gain;
}
#! A bus's effects, or none of a bus there isn't.
pub def mixer::bus_effects(&this, which: u32): bus_effects {
return which < bus_count ? this.bus_states[which as u64].effects : bus_effects{};
}
#! Changes a bus's effects, put on what its voices mix into it, and what's
#! sent to it, before its gain: filters at once, and reverb as soon as the
#! next frames are mixed, its echoes going on after what feeds it ends.
pub def mixer::set_bus_effects(&mut this, which: u32, effects: bus_effects): void {
return if which >= bus_count;
this.bus_states[which as u64].effects = effects;
}
#! The limiter on the mix, off at first.
pub val mixer::limiting: limiter {
get => this.limits;
}
#! Changes the limiter on the mix, which, on, has the mix its `lookahead` late.
pub def mixer::set_limiter(&mut this, how: limiter): void {
if how.lookahead != this.limits.lookahead || !how.on {
this.limited = nothing {};
}
this.limits = how;
}
#! The gain every voice is multiplied by.
pub val mixer::master: f32 {
get => this.master_gain;
}
pub def mixer::set_master(&mut this, gain: f32): void {
this.master_gain = gain;
}
#! How many voices are playing, paused ones among them.
pub def mixer::voice_count(&this): u32 {
var count: u32 = 0;
for i in 0..this.voices.length {
if this.voices[i].live {
count++;
}
}
return count;
}
# A free place for a voice.
def mixer::slot(&mut this): u32 {
if !this.idle.empty {
val index := this.idle.last;
this.idle.truncate();
return index;
}
this.voices.append(voice_state{ generation = 1, stream = nothing {} });
return (this.voices.length - 1) as u32;
}
# Ends a voice, freeing its place.
def mixer::end_voice(&mut this, index: u64): void {
val at := &mut this.voices[index];
at.live = false;
at.generation = at.generation == 0xFFFFFFFF ? 1 : at.generation + 1;
at.stream = nothing {};
this.idle.append(index as u32);
}