o
    "i
                  	   @   s   d dl Z d dlZd dlmZmZ dedefddZ		dd	eeee f d
ededdfddZ	d	eeee f deddfddZ
dee defddZdS )    N)IteratorUnionlib_namereturnc                 C   s   t | }|d u rdS dS )NFT)shutilwhich)r   lib r	   S/var/www/html/voicebot/backend/venv/lib/python3.10/site-packages/elevenlabs/play.pyis_installed   s   
r   FTaudionotebook
use_ffmpegc                 C   s   t | tr
d| } |rddlm}m} ||| ddd d S |rItds+d}t|g d	}tj	|tj
tj
tj
d
}|j| d\}}	|  d S zdd l}
dd l}dd l}W n tyd   d}t|w |j||
|   |  d S )N    r   )AudiodisplayiD  T)rateautoplayffplayzffplay from ffmpeg not found, necessary to play audio. On mac you can install it with 'brew install ffmpeg'. On linux and windows you can install it from https://ffmpeg.org/)r   z	-autoexit-z-nodisp)argsstdoutstdinstderr)inputzE`pip install sounddevice soundfile` required when `use_ffmpeg=False` )
isinstancer   joinIPython.displayr   r   r   
ValueError
subprocessPopenPIPEcommunicatepolliosounddevice	soundfileModuleNotFoundErrorplayreadBytesIOwait)r   r   r   r   r   messager   procouterrr$   sdsfr	   r	   r
   r(      s<   

r(   filenamec                 C   sN   t | tr
d| } t|d}||  W d    d S 1 s w   Y  d S )Nr   wb)r   r   r   openwrite)r   r2   fr	   r	   r
   save8   s
   

"r7   audio_streamc                 C   s   t ds
d}t|g d}tj|tjtjtjd}d}| D ]}|d ur3|j| |j  ||7 }q|jr<|j	  |
  |S )Nmpvzmpv not found, necessary to stream audio. On mac you can install it with 'brew install mpv'. On linux and windows you can install it from https://mpv.io/)r9   z
--no-cachez--no-terminalz--zfd://0)r   r   r   r   )r   r   r   r    r!   DEVNULLr   r5   flushcloser+   )r8   r,   mpv_commandmpv_processr   chunkr	   r	   r
   stream?   s,   

r@   )FT)r   r   typingr   r   strboolr   bytesr(   r7   r@   r	   r	   r	   r
   <module>   s"    	
"+