// This file is compiled only when the `spectral-analysis` feature is enabled.
#![cfg(feature = "spectral-analysis")]

use audio_samples::operations::traits::AudioTransforms;
use audio_samples::operations::types::{SpectrogramScale, WindowType};
use audio_samples::sine_wave;
use criterion::{Criterion, criterion_group, criterion_main};
use std::time::Duration;

fn bench_fft(c: &mut Criterion) {
    let audio = sine_wave::<f32, f32>(440.0, Duration::from_secs(5), 44100, 0.6);
    c.bench_function("spectral::fft", |b| {
        b.iter(|| {
            let _ = audio.fft::<f32>().unwrap();
        })
    });
}

fn bench_fft_info(c: &mut Criterion) {
    let audio = sine_wave::<f32, f32>(440.0, Duration::from_secs(5), 44100, 0.6);
    c.bench_function("spectral::fft_info", |b| {
        b.iter(|| {
            let _ = audio
                .fft_info::<f32>(Some(1024), Some(WindowType::Hanning), true)
                .unwrap();
        })
    });
}

fn bench_stft_and_spectrogram(c: &mut Criterion) {
    let audio = sine_wave::<f32, f32>(440.0, Duration::from_secs(5), 44100, 0.6);
    c.bench_function("spectral::stft", |b| {
        b.iter(|| {
            let _ = audio.stft::<f32>(512, 256, WindowType::Hanning).unwrap();
        })
    });

    c.bench_function("spectral::spectrogram", |b| {
        b.iter(|| {
            let _ = audio
                .spectrogram::<f32>(
                    512,
                    256,
                    WindowType::Hanning,
                    SpectrogramScale::Linear,
                    false,
                )
                .unwrap();
        })
    });
}

fn bench_mfcc(c: &mut Criterion) {
    let audio = sine_wave::<f32, f32>(440.0, Duration::from_secs(5), 44100, 0.6);
    c.bench_function("spectral::mfcc", |b| {
        b.iter(|| {
            let _ = audio.mfcc::<f32>(13, 40, 20.0_f32, 8000.0_f32).unwrap();
        })
    });
}

criterion_group!(
    benches,
    bench_fft,
    bench_fft_info,
    bench_stft_and_spectrogram,
    bench_mfcc
);
criterion_main!(benches);
