//! Synthesised waveform for environments with no real input device.
//!
//! Generates a 16-bit-PCM mono WAV containing a sine wave at the
//! requested frequency and duration. This lets the playback path be
//! exercised against the platform output stack even when capture is
//! unavailable (typical headless CI host).

use std::path::Path;
use std::time::Duration;

use hound::{SampleFormat, WavSpec, WavWriter};

use crate::capture::AudioError;

/// Write a sine-wave WAV file. Returns (frames_written, sample_rate).
pub fn synth_sine_wav(
    path: &Path,
    freq_hz: f32,
    duration: Duration,
    sample_rate: u32,
) -> Result<(u64, u32), AudioError> {
    let spec = WavSpec {
        channels: 1,
        sample_rate,
        bits_per_sample: 16,
        sample_format: SampleFormat::Int,
    };
    let mut w = WavWriter::create(path, spec)?;
    let total_frames = ((duration.as_secs_f64() * sample_rate as f64) as u64).max(1);
    let amp = f32::from(i16::MAX) * 0.5;
    for n in 0..total_frames {
        let t = n as f32 / sample_rate as f32;
        let s = (2.0 * std::f32::consts::PI * freq_hz * t).sin() * amp;
        w.write_sample(s as i16)?;
    }
    w.finalize()?;
    Ok((total_frames, sample_rate))
}
