use audio_samples::{AudioProcessing, AudioStatistics, sine_wave};
use criterion::{Criterion, black_box, criterion_group, criterion_main};
use std::time::Duration;

fn bench_processing_scale(c: &mut Criterion) {
    let audio = sine_wave::<f32, f32>(440.0, Duration::from_secs(5), 44100, 0.8);

    c.bench_function("processing::scale_clone", |b| {
        b.iter(|| {
            let mut a = audio.clone();
            a.scale(0.5f32);
            black_box(&a);
        })
    });
}

fn bench_processing_peak(c: &mut Criterion) {
    let audio = sine_wave::<f32, f32>(300.0, Duration::from_secs(5), 44100, 0.6);
    c.bench_function("processing::peak", |b| b.iter(|| black_box(audio.peak())));
}

fn bench_processing_apply_window_filter(c: &mut Criterion) {
    let audio = sine_wave::<f32, f32>(440.0, Duration::from_secs(5), 44100, 0.8);
    let samples = audio.samples_per_channel();

    c.bench_function("processing::apply_window", |b| {
        b.iter(|| {
            let mut a = audio.clone();
            let window: Vec<f32> = vec![1.0f32; samples];
            let _ = a.apply_window(&window).unwrap();
            black_box(&a);
        })
    });

    c.bench_function("processing::apply_filter", |b| {
        b.iter(|| {
            let mut a = audio.clone();
            let coeffs = vec![0.5f32, 0.5f32];
            let _ = a.apply_filter(&coeffs).unwrap();
            black_box(&a);
        })
    });
}

fn bench_processing_mu_and_misc(c: &mut Criterion) {
    let audio = sine_wave::<f32, f32>(220.0, Duration::from_secs(5), 44100, 0.6);

    c.bench_function("processing::mu_compress_expand", |b| {
        b.iter(|| {
            let mut a = audio.clone();
            let _ = a.mu_compress(255.0f32).unwrap();
            let _ = a.mu_expand(255.0f32).unwrap();
            black_box(&a);
        })
    });

    c.bench_function("processing::remove_dc_and_clip", |b| {
        b.iter(|| {
            let mut a = audio.clone();
            let _ = a.remove_dc_offset().unwrap();
            let _ = a.clip(-0.5f32, 0.5f32).unwrap();
            black_box(&a);
        })
    });
}

criterion_group!(
    benches,
    bench_processing_scale,
    bench_processing_peak,
    bench_processing_apply_window_filter,
    bench_processing_mu_and_misc
);
criterion_main!(benches);
