o
    êim                     @   sP   d Z ddlZddlmZ ddlmZ dedefdd	Zd
ee	 de	fddZ
dS )zAudio streaming utility.    N)Iterator   )DependencyErrorcommandreturnc                 C   s2   zt jd| gddd W dS  t jy   Y dS w )z(Check if a command is available in PATH.whichT)capture_outputcheckF)
subprocessrunCalledProcessError)r    r   J/home/ubuntu/.local/lib/python3.10/site-packages/fishaudio/utils/stream.py_is_installed	   s   r   audio_streamc                 C   s   t ds	tddtjg dtjtjtjd}d}z(| D ]}|r2|jr2|j| |j  ||7 }qW |jr<|j	  |
  |S |jrJ|j	  |
  w )a%  
    Stream audio in real-time while playing it with mpv.

    This function plays the audio as it's being generated and
    simultaneously captures it to return the complete audio buffer.

    Args:
        audio_stream: Iterator of audio byte chunks

    Returns:
        Complete audio bytes after streaming finishes

    Raises:
        DependencyError: If mpv is not installed

    Examples:
        ```python
        from fishaudio import FishAudio, stream

        client = FishAudio(api_key="...")
        audio_stream = client.tts.convert(text="Hello world")

        # Stream and play in real-time, get complete audio
        complete_audio = stream(audio_stream)

        # Save the captured audio
        with open("output.mp3", "wb") as f:
            f.write(complete_audio)
        ```
    mpvz_brew install mpv  # macOS
sudo apt install mpv  # Linux
https://mpv.io/installation/  # Windows)r   z
--no-cachez--no-terminalz--zfd://0)stdinstdoutstderr    )r   r   r
   PopenPIPEDEVNULLr   writeflushclosewait)r   mpv_processaudio_bufferchunkr   r   r   stream   s6   




r    )__doc__r
   typingr   
exceptionsr   strboolr   bytesr    r   r   r   r   <module>   s    	