makepad/libs/game/audio/examples/vorbis_head.rs
andodeki 435135ed50 feat(arcade): port makepad game libs + arcade from makepad-internal onto the fork
Reintroduce apps/arcade and the ten game crates that were ripped out of
the fork (assets, audio, blocks, coedit, gen, net, pkg, render, script,
session), from makepad-internal/dev. Reconcile against the fork's layout:

- makepad-game-sim/math come from the fork's existing libs/sim + sim/math;
  the copied internal libs/game/{sim,math} are dropped and the kept game
  crates plus arcade point at ../../sim and ../../sim/math.
- arcade AI is rewritten off the removed makepad-ai agent API onto the
  makepad-ai-hub headless ChatProvider worker (example/cad pattern):
  AiWorker{send,cancel,poll}, ai_worker_loop driving
  ClaudeApiChatProvider(ClaudeCli), AiWorkerEvent availability/delta/
  done/error mapped into the chat feed and authoring land_edit, with
  main.rs send_message/cancel_request/event-drain rebuilt around it.
- the game-script sandbox is adapted to the fork's jailed splash storage:
  every isolate gets mod.fs = splash_storage jail; splaes splash_storage
  set_root_for_heap is now public so the ScriptHost (or Splash) can grant
  a per-game jail root. ENT font: build.rs/dispatch.rs entities fill new
  fork sim Entity/Part fields via ..Default::default(); world_raycast now
  borrows mutably and returns the material id.

cargo check -p makepad-arcade and the game + arcade test suites pass.
2026-09-05 05:49:12 +03:00

63 lines
2.5 KiB
Rust

//! Where does the reference's opening actually live in our raw buffer?
//!
//! `vorbis_exact` compares the TRIMMED output, so a wrong head and a wrong
//! trim look alike. This searches the untrimmed overlap-add buffer for the
//! reference's first frames. If they appear exactly somewhere, the decode is
//! fine and only `first_center` is wrong; if they appear nowhere, the early
//! packets genuinely decode wrong and the trim is innocent.
//!
//! Usage: vorbis_head <file.ogg> <reference.wav> [probe_frames]
use makepad_game_audio as audio;
fn main() {
let args: Vec<String> = std::env::args().collect();
let ogg = std::fs::read(&args[1]).expect("read ogg");
let refwav = std::fs::read(&args[2]).expect("read ref");
let probe: usize = args.get(3).and_then(|s| s.parse().ok()).unwrap_or(512);
let (raw, _ch, granule) = audio::vorbis::debug_raw(&ogg).expect("raw");
let sizes = audio::vorbis::debug_block_sizes(&ogg).expect("sizes");
let want = audio::wav::decode(&refwav).expect("ref");
let wch = want.channels as usize;
println!(
"raw {} chans x {} samples, granule {granule}, first block sizes {:?}",
raw.len(),
raw[0].len(),
&sizes[..sizes.len().min(12)]
);
// Reference channel 0 only: coupling is symmetric here and one channel is
// enough to locate the head.
let w0: Vec<f32> = (0..want.frames().min(probe)).map(|f| want.samples[f * wch]).collect();
let q = |x: f32| (x * 32768.0).round() as i32;
let g0 = &raw[0];
let mut best = (0usize, 0usize, f64::INFINITY);
for off in 0..g0.len().saturating_sub(w0.len()) {
let mut exact = 0usize;
let mut sad = 0f64;
for i in 0..w0.len() {
if q(g0[off + i]) == q(w0[i]) {
exact += 1;
}
sad += (g0[off + i] - w0[i]).abs() as f64;
}
let m = sad / w0.len() as f64;
if exact > best.1 || (exact == best.1 && m < best.2) {
best = (off, exact, m);
}
}
println!(
"reference head best match at raw offset {} : {}/{} exact ({:.1}%), mean|diff| {:.6}",
best.0,
best.1,
w0.len(),
100.0 * best.1 as f64 / w0.len() as f64,
best.2
);
if best.1 * 100 / w0.len().max(1) >= 99 {
println!("VERDICT: head decodes correctly — first_center should be {}", best.0);
} else {
println!("VERDICT: the head is nowhere in the raw buffer — early packets decode wrong");
}
}