Encode-speed witness -- examples/aacenc_rt prints the crate's encode realtime factor at the seat's rates (stereo 48k 256k, 5.1 48k 512k), deterministic tonal-plus-transient 10 s program, min-of-7 alternating repetitions, one warmup
Browse files at this commitb1fb10acc978cee42738dc381429f0b5b4f94d3bParent: d5d7c0a- perf
- encode-speed witness -- examples/aacenc_rt prints the crate's encode realtime factor at the seat's rates (stereo 48k 256k, 5.1 48k 512k), deterministic tonal-plus-transient 10 s program, min-of-7 alternating repetitions, one warmup
- baseline on the 9600: 34.5x stereo / 12.9x 5.1, byte counts matching the scratch A/B probe's byte-identity outputs exactly (320825 / 641810 B), so witness and probe drive the identical encode path
1 changed file +105 -0
crates/ec-aac/examples/aacenc_rt.rsadded+105 -0
| @@ -0,0 +1,105 @@ | |||
| 1 | +//! AAC-LC encode speed relative to realtime, which is how the crate's encode | ||
| 2 | +//! perf claim is measured: a deterministic tonal-plus-transient program, ten | ||
| 3 | +//! seconds, at the seat's rates (stereo 256 kbit/s, 5.1 512 kbit/s), the | ||
| 4 | +//! shortest wall of several alternating repetitions. | ||
| 5 | +//! | ||
| 6 | +//! ```text | ||
| 7 | +//! cargo run --release -p ec-aac --example aacenc_rt | ||
| 8 | +//! ``` | ||
| 9 | + | ||
| 10 | +use std::time::Instant; | ||
| 11 | + | ||
| 12 | +use ec_aac::{AacEncoder, AacEncoderConfig}; | ||
| 13 | + | ||
| 14 | +/// Deterministic tonal stacks with periodic noise bursts: enough tonality for | ||
| 15 | +/// the spreading function to bite, enough attacks to exercise the short-block | ||
| 16 | +/// path. | ||
| 17 | +struct Lcg(u64); | ||
| 18 | +impl Lcg { | ||
| 19 | + fn next_f32(&mut self) -> f32 { | ||
| 20 | + self.0 = self | ||
| 21 | + .0 | ||
| 22 | + .wrapping_mul(6364136223846793005) | ||
| 23 | + .wrapping_add(1442695040888963407); | ||
| 24 | + ((self.0 >> 40) as f32 / 16777216.0) * 2.0 - 1.0 | ||
| 25 | + } | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +fn channel(f0: f64, seed: u64, rate: u32, secs: usize, burst_period: f64) -> Vec<f32> { | ||
| 29 | + let mut noise = Lcg(seed); | ||
| 30 | + let mut out = Vec::with_capacity(secs * rate as usize); | ||
| 31 | + for i in 0..secs * rate as usize { | ||
| 32 | + let t = i as f64 / rate as f64; | ||
| 33 | + let frac = (t % burst_period) / burst_period; | ||
| 34 | + let burst = (-frac * 18.0_f64).exp() as f32; | ||
| 35 | + let stack = (f0.sin() * 0.5 | ||
| 36 | + + (f0 * 3.0).sin() * 0.15 | ||
| 37 | + + (f0 * 5.0 + 0.31 * (6.0 * std::f64::consts::TAU * t).sin()).sin() * 0.08) | ||
| 38 | + as f32 | ||
| 39 | + * (0.7 + 0.3 * (0.5 * std::f64::consts::TAU * t).sin()) as f32; | ||
| 40 | + out.push(stack + burst * noise.next_f32() * 0.3); | ||
| 41 | + } | ||
| 42 | + out | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +fn program(channels: usize, rate: u32, secs: usize) -> Vec<f32> { | ||
| 46 | + let stereo = [0x5EED_1234u64, 0xC0FF_EE01]; | ||
| 47 | + let five_one = [0x5EED_1234, 0xC0FF_EE01, 0xABCD_0001, 0xABCD_0002, 0xABCD_0003, 0xABCD_0004]; | ||
| 48 | + let f0s = [220.0f64, 277.0, 330.0, 55.0, 196.0, 246.9]; | ||
| 49 | + let periods = [0.9f64, 0.8, 1.1, 1.3, 0.7, 0.85]; | ||
| 50 | + let seeds: &[u64] = if channels == 2 { | ||
| 51 | + &stereo | ||
| 52 | + } else { | ||
| 53 | + &five_one | ||
| 54 | + }; | ||
| 55 | + let chans: Vec<Vec<f32>> = (0..channels) | ||
| 56 | + .map(|c| channel(f0s[c], seeds[c], rate, secs, periods[c])) | ||
| 57 | + .collect(); | ||
| 58 | + let mut pcm = Vec::with_capacity(chans[0].len() * channels); | ||
| 59 | + for i in 0..chans[0].len() { | ||
| 60 | + for c in &chans { | ||
| 61 | + pcm.push(c[i]); | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + pcm | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | +fn encode(pcm: &[f32], channels: usize, kbps: u32) -> usize { | ||
| 68 | + let mut enc = AacEncoder::new(AacEncoderConfig { | ||
| 69 | + bitrate_bps: kbps * 1_000, | ||
| 70 | + ..Default::default() | ||
| 71 | + }); | ||
| 72 | + enc.push_pcm(pcm, channels as u16, 48_000).expect("pcm accepted"); | ||
| 73 | + enc.finish(); | ||
| 74 | + let mut bytes = 0usize; | ||
| 75 | + while let Ok(p) = enc.next_packet() { | ||
| 76 | + bytes += p.data.len(); | ||
| 77 | + } | ||
| 78 | + bytes | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +fn main() { | ||
| 82 | + let reps: usize = std::env::var("REPS") | ||
| 83 | + .ok() | ||
| 84 | + .and_then(|s| s.parse().ok()) | ||
| 85 | + .unwrap_or(7); | ||
| 86 | + for (channels, kbps) in [(2usize, 256u32), (6, 512)] { | ||
| 87 | + let pcm = program(channels, 48_000, 10); | ||
| 88 | + let secs = pcm.len() as f64 / (48_000.0 * channels as f64); | ||
| 89 | + let _ = encode(&pcm, channels, kbps); // warm | ||
| 90 | + let mut walls = Vec::with_capacity(reps); | ||
| 91 | + for _ in 0..reps { | ||
| 92 | + let t0 = Instant::now(); | ||
| 93 | + let bytes = encode(&pcm, channels, kbps); | ||
| 94 | + walls.push((t0.elapsed().as_secs_f64(), bytes)); | ||
| 95 | + } | ||
| 96 | + walls.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap()); | ||
| 97 | + let (wall, bytes) = walls[0]; | ||
| 98 | + println!( | ||
| 99 | + "{channels}ch {kbps}k: min {wall:.4}s = {:.1}x realtime ({} B, {} reps)", | ||
| 100 | + secs / wall, | ||
| 101 | + bytes, | ||
| 102 | + reps | ||
| 103 | + ); | ||
| 104 | + } | ||
| 105 | +} | ||