use audio_samples::operations::traits::AudioPitchAnalysis;
use audio_samples::operations::types::PitchDetectionMethod;
use audio_samples::sine_wave;
use criterion::{Criterion, criterion_group, criterion_main};
use std::sync::Arc;
use std::time::Duration;

fn bench_pitch_detection(c: &mut Criterion) {
    let audio = Arc::new(sine_wave::<f32, f32>(
        440.0,
        Duration::from_secs_f32(0.5),
        44100,
        0.8,
    ));

    c.bench_function("pitch::detect_yin", |b| {
        let audio = audio.clone();
        b.iter(move || {
            let a = audio.as_ref().clone();
            let _ = a
                .detect_pitch_yin::<f32>(0.15_f32, 80.0_f32, 1000.0_f32)
                .unwrap();
        })
    });

    c.bench_function("pitch::detect_autocorr", |b| {
        let audio = audio.clone();
        b.iter(move || {
            let a = audio.as_ref().clone();
            let _ = a
                .detect_pitch_autocorr::<f32>(80.0_f32, 1000.0_f32)
                .unwrap();
        })
    });

    c.bench_function("pitch::track_pitch", |b| {
        let audio = audio.clone();
        b.iter(move || {
            let a = audio.as_ref().clone();
            let _ = a
                .track_pitch::<f32>(
                    2048,
                    512,
                    PitchDetectionMethod::Yin,
                    0.15_f32,
                    80.0_f32,
                    1000.0_f32,
                )
                .unwrap();
        })
    });
}

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