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

fn bench_resampling_peak(c: &mut Criterion) {
    let audio = sine_wave::<f32, f32>(720.0, Duration::from_secs(5), 48000, 0.6);
    c.bench_function("resampling::peak", |b| b.iter(|| black_box(audio.peak())));
}

// Feature-gated resampling benches. These will only be compiled when the
// `resampling` feature is enabled for the crate.
#[cfg(feature = "resampling")]
mod resampling_benches {
    use super::*;
    use audio_samples::operations::{AudioProcessing, ResamplingQuality};

    pub fn bench_resample_to_44100(c: &mut Criterion) {
        let audio = sine_wave::<f32, f32>(720.0, Duration::from_secs(5), 48000, 0.6);
        c.bench_function("resampling::to_44100", |b| {
            b.iter(|| {
                // call resample to 44100, high quality
                let _ = audio
                    .resample::<f32>(44100, ResamplingQuality::High)
                    .unwrap();
            })
        });
    }

    pub fn bench_resample_by_ratio(c: &mut Criterion) {
        let audio = sine_wave::<f32, f32>(720.0, Duration::from_secs(5), 48000, 0.6);
        c.bench_function("resampling::by_ratio_0.5", |b| {
            b.iter(|| {
                // downsample by 0.5
                let _ = audio
                    .resample_by_ratio::<f32>(0.5_f32, ResamplingQuality::Medium)
                    .unwrap();
            })
        });
    }

    criterion::criterion_group!(
        resamp_group,
        bench_resample_to_44100,
        bench_resample_by_ratio
    );
}

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