o
    ±ƒðjN  ã                   @   s2   d Z ddlZddlZddlmZ G dd„ dƒZdS )zAudio playback using ffplay.é    N)ÚOptionalc                   @   sV   e Zd ZdZdeddfdd„Zdd„ Zd	d
„ Zdeddfdd„Z	e
defdd„ƒZdS )ÚAudioPlayerúPlays raw audio using ffplay.Úsample_rateÚreturnNc                 C   s   || _ d| _dS )zInitializes audio player.N)r   Ú_proc)Úselfr   © r	   ú–/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/piper/audio_playback.pyÚ__init__   s   
zAudioPlayer.__init__c                 C   s8   t jddddddt| jƒddd	g
t jt jt jd
�| _| S )z,Starts ffplay subprocess and returns player.Úffplayz-nodispz	-autoexitz-fÚs16lez-sample_ratez
-ch_layoutÚmonoú-)ÚstdinÚstdoutÚstderr)Ú
subprocessÚPopenÚstrr   ÚPIPEÚDEVNULLr   )r   r	   r	   r
   Ú	__enter__   s"   öñzAudioPlayer.__enter__c                 C   sH   | j r"z| j jr| j j ¡  W n	 ty   Y nw | j jdd� dS dS )zStops ffplay subprocess.é   )ÚtimeoutN)r   r   ÚcloseÚ	ExceptionÚwait)r   Úexc_typeÚexc_valÚexc_tbr	   r	   r
   Ú__exit__%   s   €ÿúzAudioPlayer.__exit__Úaudio_bytesc                 C   s<   | j dusJ ‚| j jdusJ ‚| j j |¡ | j j ¡  dS )r   N)r   r   ÚwriteÚflush)r   r"   r	   r	   r
   Úplay/   s   zAudioPlayer.playc                   C   s   t t d¡ƒS )z$Returns true if ffplay is available.r   )ÚboolÚshutilÚwhichr	   r	   r	   r
   Úis_available7   s   zAudioPlayer.is_available)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úintr   r   r!   Úbytesr%   Ústaticmethodr&   r)   r	   r	   r	   r
   r      s    
r   )r-   r'   r   Útypingr   r   r	   r	   r	   r
   Ú<module>   s
    