convert_bench/
convert_bench.rs1use std::time::Instant;
12use usbdac_core::convert::{
13 ConversionPlan, ConvertOptions, Converter, PcmFormat, Quality, SampleFormat,
14};
15
16fn run(label: &str, source: PcmFormat, target: PcmFormat, quality: Quality) {
17 let options = ConvertOptions {
18 quality,
19 ..ConvertOptions::default()
20 };
21 let plan = ConversionPlan::pcm(source, target, &options).unwrap();
22 let build = Instant::now();
23 let mut converter = Converter::new(&plan).unwrap();
24 let build_ms = build.elapsed().as_secs_f64() * 1000.0;
25
26 let chunk_frames = source.sample_rate as usize / 100;
27 let chunk: Vec<u8> =
28 (0..chunk_frames * usize::from(source.channels) * source.sample.bytes_per_sample())
29 .map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8)
30 .collect();
31 let mut output = vec![0u8; converter.min_output_bytes().max(1 << 20)];
32 let seconds = 10;
33 let start = Instant::now();
34 let mut produced = 0usize;
35 for _ in 0..seconds * 100 {
36 let done = converter.process(&chunk, &mut output).unwrap();
37 produced += done.produced;
38 }
39 let elapsed = start.elapsed().as_secs_f64();
40 std::hint::black_box(produced);
41 println!(
42 "{label:<46} build {build_ms:6.1} ms {:6.1}x realtime {:5.2}% of a core",
43 seconds as f64 / elapsed,
44 elapsed / seconds as f64 * 100.0
45 );
46}
47
48fn main() {
49 let stereo = |rate, sample| PcmFormat::new(rate, 2, sample);
50 println!("{}", plan_line());
51 run(
52 "copy 44.1k 16-bit",
53 stereo(44_100, SampleFormat::I16),
54 stereo(44_100, SampleFormat::I16),
55 Quality::High,
56 );
57 run(
58 "widen 16 -> 24 bit (integer engine)",
59 stereo(44_100, SampleFormat::I16),
60 stereo(44_100, SampleFormat::I24),
61 Quality::High,
62 );
63 run(
64 "narrow 32 -> 16 bit, TPDF dither",
65 stereo(96_000, SampleFormat::I32),
66 stereo(96_000, SampleFormat::I16),
67 Quality::High,
68 );
69 for (label, from, to) in [
70 ("44.1k -> 48k", 44_100, 48_000),
71 ("48k -> 44.1k", 48_000, 44_100),
72 ("96k -> 44.1k", 96_000, 44_100),
73 ("44.1k -> 96k", 44_100, 96_000),
74 ("44.1k -> 192k", 44_100, 192_000),
75 ("192k -> 48k", 192_000, 48_000),
76 ("44.1k -> 768k (most phases)", 44_100, 768_000),
77 ("768k -> 44.1k (longest filter)", 768_000, 44_100),
78 ] {
79 for quality in [Quality::High, Quality::Fast] {
80 run(
81 &format!("resample {label}, 24-bit, {quality:?}"),
82 stereo(from, SampleFormat::I24),
83 stereo(to, SampleFormat::I24),
84 quality,
85 );
86 }
87 }
88}
89
90fn plan_line() -> String {
91 let profile = if cfg!(debug_assertions) {
92 "debug (slow: use --release)"
93 } else {
94 "release"
95 };
96 format!("usbdac-core conversion throughput, {profile} build, stereo, 10 s of audio per case")
97}