// SDD-120 §4 — deterministic synthetic input generation.
//
// Shared by the three bench files (`realtime_capture.rs`,
// `opus_codec.rs`, `resampler.rs`). Included via `mod common;` in
// each bench file because criterion bench files compile as
// independent binaries — there is no shared crate boundary between
// them and `common.rs` is NOT registered as a `[[bench]]` entry.
//
// Determinism is load-bearing for §5 baseline stability: a 440 Hz
// sine at amplitude 0.5 over the 48 kHz mixer rate, no RNG, no
// fade-in, no DC offset. Same input bytes across runs and hosts.
#![allow(dead_code)]

use std::f32::consts::PI;

/// Frame count of one 20 ms Opus frame at 48 kHz mono — matches
/// `chanora_audio::engine::FRAME_SAMPLES` (private constant; the
/// value `960` is fixed by the Opus codec protocol and SAD-088).
pub const FRAME_SAMPLES: usize = 960;

/// 48 kHz mixer rate — matches `chanora_audio::engine::SAMPLE_RATE`.
pub const SAMPLE_RATE: u32 = 48_000;

/// Interleaved synthetic capture buffer. For multi-channel layouts
/// the same scalar value is replicated across each frame's channels
/// (matches cpal's interleaved `Stream` data layout).
pub fn synthetic_capture_buffer(frames: usize, channels: usize) -> Vec<f32> {
    let mut out = Vec::with_capacity(frames * channels);
    for n in 0..frames {
        let s = 0.5 * (2.0 * PI * 440.0 * (n as f32) / 48_000.0).sin();
        for _ in 0..channels {
            out.push(s);
        }
    }
    out
}

/// One Opus-frame-sized synthetic PCM buffer (960 samples mono).
pub fn synthetic_opus_frame() -> Vec<f32> {
    synthetic_capture_buffer(FRAME_SAMPLES, 1)
}

/// Synthetic encoded Opus bytes for decode benches. Built by
/// encoding `synthetic_opus_frame()` with a one-shot encoder.
pub fn synthetic_opus_bytes() -> Vec<u8> {
    use audiopus::coder::Encoder;
    use audiopus::{Application, Channels, SampleRate};
    let enc = Encoder::new(SampleRate::Hz48000, Channels::Mono, Application::Voip)
        .expect("opus encoder init");
    let pcm = synthetic_opus_frame();
    let mut out = vec![0u8; 1275]; // MAX_OPUS_FRAME
    let len = enc
        .encode_float(&pcm, &mut out)
        .expect("opus encode synthetic frame");
    out.truncate(len);
    out
}
