audio_samples 1.0.5

A typed audio processing library for Rust that treats audio as a first-class, invariant-preserving object rather than an unstructured numeric buffer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//! FFmpeg CLI vs `audio_samples` library benchmark suite.
//!
//! ## What is being measured
//!
//! Each benchmark group contains paired measurements:
//!
//! - `audio_samples/*` — in-process Rust operations on pre-built `AudioSamples`
//!   objects (no I/O, no process overhead).
//! - `ffmpeg/*` — `std::process::Command` shell-out including process fork/exec,
//!   WAV bytes written to stdin, filter/codec processing, and pipe teardown.
//!
//! ## Framing
//!
//! `audio_samples` operates entirely in-memory within the calling program.
//! FFmpeg is measured as the realistic "shell-out" cost — what any program pays
//! when it uses FFmpeg as a pipeline stage rather than embedding a library.
//! These are not pure algorithm comparisons; they measure two different
//! integration strategies for the same audio transformation.
//!
//! ## Running
//!
//! ```bash
//! cargo bench --bench ffmpeg_comparison \
//!   --no-default-features \
//!   --features "resampling,iir-filtering,channels,processing,statistics,editing"
//! ```
//!
//! Run a single group only (e.g. resampling):
//!
//! ```bash
//! cargo bench --bench ffmpeg_comparison \
//!   --no-default-features \
//!   --features "resampling,iir-filtering,channels,processing,statistics,editing" \
//!   -- resampling
//! ```

use audio_samples::{
    AudioChannelOps, AudioIirFiltering, AudioSamples, AudioStatistics, AudioTypeConversion,
    StandardSample,
    operations::{ResamplingQuality, types::MonoConversionMethod},
    resample, sample_rate, sine_wave, stereo_sine_wave,
};
use audio_samples_io::traits::{AudioStreamWrite, AudioStreamWriter};
use audio_samples_io::wav::StreamedWavWriter;
use criterion::{BatchSize, BenchmarkId, Criterion, criterion_group, criterion_main};
use std::hint::black_box;
use std::io::Write;
use std::num::NonZeroUsize;
use std::process::{Command, Stdio};
use std::time::Duration;

// ---------------------------------------------------------------------------
// WAV encoding helper (uses audio_samples_io)
// ---------------------------------------------------------------------------

/// Encode any `AudioSamples<T>` to an in-memory WAV byte vector.
///
/// Multi-channel audio is automatically interleaved by `StreamedWavWriter`.
/// The output is always written as 32-bit IEEE float.
fn audio_to_wav_bytes<T>(audio: &AudioSamples<T>) -> Vec<u8>
where
    T: StandardSample + 'static,
{
    let channels = audio.num_channels().get() as u16;
    let sample_rate = audio.sample_rate().get();

    let mut buffer = Vec::new();
    {
        let cursor = std::io::Cursor::new(&mut buffer);
        let mut writer = StreamedWavWriter::new_f32(cursor, channels, sample_rate)
            .expect("StreamedWavWriter creation failed");
        writer.write_frames(audio).expect("write_frames failed");
        writer.finalize().expect("finalize failed");
    }
    buffer
}

// ---------------------------------------------------------------------------
// FFmpeg helpers
// ---------------------------------------------------------------------------

/// Returns `true` if `ffmpeg` is on PATH and responds to `ffmpeg -version`.
fn ffmpeg_available() -> bool {
    Command::new("ffmpeg")
        .arg("-version")
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

/// Print the installed FFmpeg version once at benchmark startup.
fn print_ffmpeg_version() {
    match Command::new("ffmpeg").arg("-version").output() {
        Ok(out) => {
            let text = String::from_utf8_lossy(&out.stdout);
            let first = text.lines().next().unwrap_or("(unknown)");
            println!("FFmpeg: {first}");
        }
        Err(_) => println!("FFmpeg: not found on PATH — FFmpeg benchmarks will be skipped"),
    }
}

/// Spawn `ffmpeg`, pipe `wav_bytes` to stdin, apply `extra_args`, and wait.
///
/// Both stdout and stderr are discarded. Returns the exit status so callers
/// can wrap it in `black_box`.
fn run_ffmpeg_stdin(wav_bytes: &[u8], extra_args: &[&str]) -> std::process::ExitStatus {
    let mut child = Command::new("ffmpeg")
        .arg("-y")
        .arg("-i")
        .arg("pipe:0")
        .args(extra_args)
        .stdin(Stdio::piped())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .expect("failed to spawn ffmpeg — is it installed and on PATH?");

    child
        .stdin
        .as_mut()
        .expect("stdin pipe was requested")
        .write_all(wav_bytes)
        .expect("failed to write WAV bytes to ffmpeg stdin");

    child.wait().expect("ffmpeg process did not complete")
}

// ---------------------------------------------------------------------------
// 1. Resampling
// ---------------------------------------------------------------------------

fn bench_resampling(c: &mut Criterion) {
    let have_ffmpeg = ffmpeg_available();

    // (label, duration_secs, dst_hz, quality)
    let cases: &[(&str, u64, u32, ResamplingQuality)] = &[
        ("1s_44100_to_16000_fast", 1, 16000, ResamplingQuality::Fast),
        (
            "1s_44100_to_16000_medium",
            1,
            16000,
            ResamplingQuality::Medium,
        ),
        ("1s_44100_to_16000_high", 1, 16000, ResamplingQuality::High),
        ("1s_44100_to_22050_fast", 1, 22050, ResamplingQuality::Fast),
        ("1s_44100_to_22050_high", 1, 22050, ResamplingQuality::High),
        ("1s_44100_to_48000_fast", 1, 48000, ResamplingQuality::Fast),
        ("1s_44100_to_48000_high", 1, 48000, ResamplingQuality::High),
        (
            "10s_44100_to_16000_fast",
            10,
            16000,
            ResamplingQuality::Fast,
        ),
        (
            "10s_44100_to_16000_high",
            10,
            16000,
            ResamplingQuality::High,
        ),
        (
            "10s_44100_to_48000_high",
            10,
            48000,
            ResamplingQuality::High,
        ),
    ];

    let mut group = c.benchmark_group("resampling");

    for &(label, dur_secs, dst_hz, quality) in cases {
        let audio = sine_wave::<f32>(
            440.0,
            Duration::from_secs(dur_secs),
            sample_rate!(44100),
            0.8,
        );
        let dst_sr = std::num::NonZeroU32::new(dst_hz).unwrap();

        // audio_samples side
        group.bench_with_input(
            BenchmarkId::new("audio_samples", label),
            &(dst_sr, quality),
            |b, &(dst, q)| {
                b.iter(|| {
                    let result = resample(black_box(&audio), black_box(dst), black_box(q))
                        .expect("resample should not fail in benchmark");
                    black_box(result);
                });
            },
        );

        // FFmpeg side
        if have_ffmpeg {
            let wav_bytes = audio_to_wav_bytes(&audio);
            let dst_str = dst_hz.to_string();

            group.bench_with_input(BenchmarkId::new("ffmpeg", label), label, |b, _| {
                b.iter(|| {
                    let status = run_ffmpeg_stdin(
                        black_box(&wav_bytes),
                        &["-ar", &dst_str, "-f", "wav", "pipe:1"],
                    );
                    black_box(status);
                });
            });
        } else {
            eprintln!("resampling/{label}: skipping FFmpeg (not found)");
        }
    }

    group.finish();
}

// ---------------------------------------------------------------------------
// 2. IIR Filtering
// ---------------------------------------------------------------------------

fn bench_iir_filtering(c: &mut Criterion) {
    let have_ffmpeg = ffmpeg_available();

    // (label, duration_secs, filter_type, cutoff_hz, high_hz_for_bandpass, order)
    let cases: &[(&str, u64, &str, f64, f64, usize)] = &[
        ("1s_lowpass_1000hz_order4", 1, "lowpass", 1000.0, 0.0, 4),
        ("1s_highpass_1000hz_order4", 1, "highpass", 1000.0, 0.0, 4),
        (
            "1s_bandpass_500_2000hz_order4",
            1,
            "bandpass",
            500.0,
            2000.0,
            4,
        ),
        ("10s_lowpass_1000hz_order4", 10, "lowpass", 1000.0, 0.0, 4),
        ("10s_highpass_1000hz_order4", 10, "highpass", 1000.0, 0.0, 4),
    ];

    let mut group = c.benchmark_group("iir_filtering");

    for &(label, dur_secs, filter_type, cutoff_hz, high_hz, order) in cases {
        let base_audio = sine_wave::<f32>(
            440.0,
            Duration::from_secs(dur_secs),
            sample_rate!(44100),
            0.8,
        );
        let order_nz = NonZeroUsize::new(order).unwrap();

        // audio_samples side — butterworth_* mutates in place; clone per batch
        group.bench_with_input(BenchmarkId::new("audio_samples", label), label, |b, _| {
            b.iter_batched(
                || base_audio.clone(),
                |mut audio| {
                    match filter_type {
                        "lowpass" => audio.butterworth_lowpass(order_nz, cutoff_hz),
                        "highpass" => audio.butterworth_highpass(order_nz, cutoff_hz),
                        "bandpass" => audio.butterworth_bandpass(order_nz, cutoff_hz, high_hz),
                        _ => unreachable!(),
                    }
                    .expect("IIR filter should not fail in benchmark");
                    black_box(audio);
                },
                BatchSize::SmallInput,
            );
        });

        // FFmpeg side
        if have_ffmpeg {
            let wav_bytes = audio_to_wav_bytes(&base_audio);

            let ffmpeg_filter = match filter_type {
                "lowpass" => format!("lowpass=f={cutoff_hz:.0}"),
                "highpass" => format!("highpass=f={cutoff_hz:.0}"),
                "bandpass" => format!(
                    "bandpass=f={:.0}:width_type=h:width={:.0}",
                    (cutoff_hz + high_hz) / 2.0,
                    high_hz - cutoff_hz,
                ),
                _ => unreachable!(),
            };

            group.bench_with_input(BenchmarkId::new("ffmpeg", label), label, |b, _| {
                b.iter(|| {
                    let status = run_ffmpeg_stdin(
                        black_box(&wav_bytes),
                        &["-af", &ffmpeg_filter, "-f", "wav", "pipe:1"],
                    );
                    black_box(status);
                });
            });
        } else {
            eprintln!("iir_filtering/{label}: skipping FFmpeg (not found)");
        }
    }

    group.finish();
}

// ---------------------------------------------------------------------------
// 3. Volume / Loudness Analysis
// ---------------------------------------------------------------------------

fn bench_volume_analysis(c: &mut Criterion) {
    let have_ffmpeg = ffmpeg_available();

    let cases: &[(&str, u64)] = &[("1s_mono", 1), ("10s_mono", 10)];

    let mut group = c.benchmark_group("volume_analysis");

    for &(label, dur_secs) in cases {
        let audio = sine_wave::<f32>(
            440.0,
            Duration::from_secs(dur_secs),
            sample_rate!(44100),
            0.5,
        );

        // audio_samples: rms() and peak() are infallible — no clone needed
        group.bench_with_input(BenchmarkId::new("audio_samples", label), label, |b, _| {
            b.iter(|| {
                let rms = audio.rms();
                let peak = audio.peak();
                black_box((rms, peak));
            });
        });

        if have_ffmpeg {
            let wav_bytes = audio_to_wav_bytes(&audio);

            // volumedetect is a sink filter — must use `-f null /dev/null`,
            // NOT `-f wav pipe:1` (which would error because there is no audio output).
            group.bench_with_input(BenchmarkId::new("ffmpeg", label), label, |b, _| {
                b.iter(|| {
                    let status = run_ffmpeg_stdin(
                        black_box(&wav_bytes),
                        &["-af", "volumedetect", "-f", "null", "/dev/null"],
                    );
                    black_box(status);
                });
            });
        } else {
            eprintln!("volume_analysis/{label}: skipping FFmpeg (not found)");
        }
    }

    group.finish();
}

// ---------------------------------------------------------------------------
// 4. Channel Mixing (Stereo → Mono)
// ---------------------------------------------------------------------------

fn bench_channel_mixing(c: &mut Criterion) {
    let have_ffmpeg = ffmpeg_available();

    let cases: &[(&str, u64)] = &[("1s_stereo_to_mono", 1), ("10s_stereo_to_mono", 10)];

    let mut group = c.benchmark_group("channel_mixing");

    for &(label, dur_secs) in cases {
        let stereo = stereo_sine_wave::<f32>(
            440.0,
            Duration::from_secs(dur_secs),
            sample_rate!(44100),
            0.8,
        );

        // audio_samples: to_mono returns a new owned AudioSamples — no mutation
        group.bench_with_input(BenchmarkId::new("audio_samples", label), label, |b, _| {
            b.iter(|| {
                let mono = stereo
                    .to_mono(black_box(MonoConversionMethod::Average))
                    .expect("to_mono should not fail in benchmark");
                black_box(mono);
            });
        });

        if have_ffmpeg {
            // audio_to_wav_bytes handles interleaving internally via StreamedWavWriter
            let wav_bytes = audio_to_wav_bytes(&stereo);

            group.bench_with_input(BenchmarkId::new("ffmpeg", label), label, |b, _| {
                b.iter(|| {
                    let status = run_ffmpeg_stdin(
                        black_box(&wav_bytes),
                        &["-ac", "1", "-f", "wav", "pipe:1"],
                    );
                    black_box(status);
                });
            });
        } else {
            eprintln!("channel_mixing/{label}: skipping FFmpeg (not found)");
        }
    }

    group.finish();
}

// ---------------------------------------------------------------------------
// 5. Sample Format Conversion
// ---------------------------------------------------------------------------

fn bench_format_conversion(c: &mut Criterion) {
    let have_ffmpeg = ffmpeg_available();

    let cases: &[(&str, u64)] = &[("1s_f32_to_i16", 1), ("10s_f32_to_i16", 10)];

    let mut group = c.benchmark_group("format_conversion");

    for &(label, dur_secs) in cases {
        let audio_f32 = sine_wave::<f32>(
            440.0,
            Duration::from_secs(dur_secs),
            sample_rate!(44100),
            0.8,
        );

        // audio_samples: to_format borrows self and returns a new owned AudioSamples<i16>
        group.bench_with_input(BenchmarkId::new("audio_samples", label), label, |b, _| {
            b.iter(|| {
                let converted = audio_f32.to_format::<i16>();
                black_box(converted);
            });
        });

        if have_ffmpeg {
            let wav_bytes = audio_to_wav_bytes(&audio_f32);

            group.bench_with_input(BenchmarkId::new("ffmpeg", label), label, |b, _| {
                b.iter(|| {
                    let status = run_ffmpeg_stdin(
                        black_box(&wav_bytes),
                        &["-sample_fmt", "s16", "-f", "wav", "pipe:1"],
                    );
                    black_box(status);
                });
            });
        } else {
            eprintln!("format_conversion/{label}: skipping FFmpeg (not found)");
        }
    }

    group.finish();
}

// ---------------------------------------------------------------------------
// Entry point
// ---------------------------------------------------------------------------

fn run_all(c: &mut Criterion) {
    print_ffmpeg_version();
    bench_resampling(c);
    bench_iir_filtering(c);
    bench_volume_analysis(c);
    bench_channel_mixing(c);
    bench_format_conversion(c);
}

criterion_group!(benches, run_all);
criterion_main!(benches);