Module batch

Source
Expand description

Bounded presentation-frame batching of caller-accepted events.

This is a borrowed adapter over GameAudio, not an event validator or ID deduplicator. Create one batch per presentation frame, collect across actual fixed updates, then flush once. Dropping it cancels pending work. Its exclusive borrow prevents changing/replacing the controller behind queued cues; use CueBatch::pause / CueBatch::reset during the borrow. Release the batch before resuming or restarting the owner. Never retain it across host frames. Runtime replacement cancels pending cues using the runtime’s weak identity, the same lifetime boundary used by GameAudio’s ownership repair.

Category IDs are caller-authored, not gameplay event IDs. While a category is pending, its cue, policy and critical flag must agree at every position. Only equal category IDs and equal positions coalesce (None is distinct from Some(Vec3::ZERO)). No spatial averaging, gain summing, hidden volume multiplication or automatic event deduplication occurs. Strongest means the greatest raw gain; equal gains keep the first. Replacement retains the first slot’s order. A new critical cue at capacity evicts the oldest noncritical slot and appends; otherwise overflow drops the incoming cue. All-critical overflow therefore keeps the earlier cues, regardless of gains. Critical is solely a storage guarantee against noncritical arrivals, not mixer priority or admission. Distinct positions can consume multiple slots of one category.

Collection/flush uses only fixed arrays, bounded scans (at most 128 slots), and existing GameAudio submissions. After audio bank/mixer setup these paths allocate nothing. Runtime/bank creation, arbitrary host callbacks and their failures are outside that claim. Flush has no callbacks and consumes every retained request, including suppressed/rejected requests; it never retries. Mute may make admitted voices silent. PCM tests are not listening evidence.

Runnable headless recipe: actual Magazine acceptance plus a timed first-party objective, driven by GameSession ticks. The objective completion, rather than a fabricated game result, supplies the terminal cue. No wall time or device is used. For spatial contacts use Some(event.position) instead of None.

use ferravex_core::session::GameSession;
use ferravex_play::{gameplay::magazine::Magazine,
    objective::{ObjectiveGraph, ObjectiveSpec, ObjectiveKind}};
use ferravex_render::audio::{self, Cue, GameAudio,
    batch::{CueBatch, CueCategory, Coalesce}};
let shot = CueCategory { key: 1, cue: Cue::Fire,
    policy: Coalesce::Strongest, critical: false };
let terminal = CueCategory { key: 2, cue: Cue::Success,
    policy: Coalesce::First, critical: true };
let mut session = GameSession::new(64., 4, true);
let mut magazine = Magazine::new(3, 0, 1.).unwrap();
let spec = ObjectiveSpec {
    id: 1, all: 0, any: 0, exclusive_group: 0,
    kind: ObjectiveKind::Timer { seconds: 3. / 64. },
};
let mut objective = ObjectiveGraph::new(1, [spec]).unwrap();
let mut owner = GameAudio::new(); // Prepare bank outside collection/flush.
{
    let mut cues = CueBatch::<2>::new(&mut owner).unwrap();
    assert_eq!(session.advance(0.).steps, 0);
    assert_eq!(cues.flush().submitted, 0); // Zero-tick/empty frame.
}
{
    let mut cues = CueBatch::<2>::new(&mut owner).unwrap();
    let frame = session.advance(4. / 64.);
    let mut executed = 0;
    for dt in frame.ticks() {
        if !session.is_playing() { break; }
        executed += 1;
        if magazine.fire() { let _ = cues.queue(shot, None, 0.4).unwrap(); }
        if objective.advance(dt).unwrap().completed != 0 {
            assert!(session.finish(true));
            let _ = cues.queue(terminal, None, 0.6).unwrap();
        }
    }
    assert_eq!((frame.steps, executed, session.tick()), (4, 3, 4));
    let report = cues.flush(); // One presentation flush for three ticks.
    assert_eq!((report.batch.coalesced, report.submitted, report.admitted()), (2, 2, 2));
    assert_eq!(cues.flush().submitted, 0); // No replay on an empty frame.
}
let mut pcm = [[0.; 2]; 256];
assert!(audio::render(&mut pcm, audio::SAMPLE_RATE)); // Offline PCM mixing.
assert!(pcm.iter().flatten().all(|v| v.is_finite()));
{
    let mut cues = CueBatch::<2>::new(&mut owner).unwrap();
    let _ = cues.queue(shot, None, 0.4).unwrap();
    assert_eq!(cues.pause(), 1); // Discard pending work and owned gameplay voices.
    assert_eq!(cues.flush().submitted, 0);
    cues.reset(); // Preserves pause; no retry of the cancelled shot.
}
session.restart();
magazine = Magazine::new(3, 0, 1.).unwrap();
objective = ObjectiveGraph::new(1, [spec]).unwrap();
owner.resume(); // Release batch first: no pending work survives resume.
assert_eq!(session.tick(), 0);
let mut retry = CueBatch::<2>::new(&mut owner).unwrap();
assert_eq!(retry.flush().submitted, 0);
for dt in session.advance(1. / 64.).ticks() {
    if magazine.fire() { let _ = retry.queue(shot, None, 0.4).unwrap(); }
    assert_eq!(objective.advance(dt).unwrap().completed, 0);
}
assert_eq!(retry.flush().submitted, 1); // Fresh accepted shot, no stale cue.

Structs§

BatchCounts
Saturating counts since the preceding flush (or construction). retained counts insertions, including evicting insertions; replaced is a subset of coalesced. Dropped arrivals, evictions, explicit cancellations and runtime discards are separate. Current occupancy is CueBatch::len.
BatchedCue
One retained raw request, before GameAudio acoustics or mixer admission. Construct through CueBatch::queue; see the module recipe.
CueBatch
Exclusive, thread-bound, one-frame accumulator. No implicit flush on drop. See the module recipe for fixed updates, terminal feedback and lifecycle.
CueCategory
Caller-authored category. Configuration must agree while its key is pending. No key value is reserved. See the module recipe.
CueOutcome
Exact result of submitting one retained request through GameAudio.
FlushReport
Fixed-storage flush result. The first submitted entries of outcomes are Some, in retained insertion order; all remaining entries are None. Counts describe submissions, not voices still live after later steals.

Enums§

BatchError
Validation errors leave pending work and counters unchanged, even if the runtime has changed. A subsequent valid operation/flush reconciles runtime.
Coalesce
Selection among requests at the same category and exact position. See the module recipe for category construction and submission.
QueueOutcome
Immediate storage decision; independent of later voice admission.