"""
Audio utility functions for FMUS-VID.
"""

import numpy as np
from pathlib import Path
import wave
import logging

logger = logging.getLogger(__name__)

def save_audio(path, audio_data, sample_rate):
    """
    Save audio data to a WAV file.

    Args:
        path: Output file path
        audio_data: Audio data as numpy array
        sample_rate: Sample rate in Hz

    Example:
        >>> import numpy as np
        >>> sample_rate = 44100
        >>> duration = 3.0
        >>> t = np.linspace(0, duration, int(sample_rate * duration))
        >>> audio_data = np.sin(2 * np.pi * 440 * t)
        >>> save_audio("output.wav", audio_data, sample_rate)
    """
    path = str(path)

    # Convert to int16 if needed
    if audio_data.dtype == np.float64 or audio_data.dtype == np.float32:
        # Normalize to [-1, 1] if needed
        max_val = np.max(np.abs(audio_data))
        if max_val > 1.0:
            audio_data = audio_data / max_val
        audio_data = (audio_data * 32767).astype(np.int16)
    elif audio_data.dtype != np.int16:
        audio_data = audio_data.astype(np.int16)

    # Ensure 2D array for stereo
    if audio_data.ndim == 1:
        audio_data = audio_data.reshape(-1, 1)

    # Write WAV file
    with wave.open(path, 'w') as wav_file:
        wav_file.setnchannels(audio_data.shape[1])
        wav_file.setsampwidth(2)  # 16-bit
        wav_file.setframerate(sample_rate)
        wav_file.writeframes(audio_data.tobytes())

    logger.info(f"Saved audio to {path}: {audio_data.shape[1]} channels, {sample_rate}Hz")
