U
    9%e                     @   sL   d Z ddlZddlmZ ddlmZmZ dd Zdd	 Zd
d Z	dd Z
dS )a$  
Support for playing AudioSegments. Pyaudio will be used if it's installed,
otherwise will fallback to ffplay. Pyaudio is a *much* nicer solution, but
is tricky to install. See my notes on installing pyaudio in a virtualenv (on
OSX 10.10): https://gist.github.com/jiaaro/9767512210a1d80a8a0d
    N)NamedTemporaryFile   )get_player_namemake_chunksc              	   C   sF   t  }tddd*}| |jd t|ddd|jg W 5 Q R X d S )Nzw+bz.wav)suffixZwavz-nodispz	-autoexitz-hide_banner)r   r   Zexportname
subprocesscall)segZPLAYERf r   M/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/pydub/playback.py_play_with_ffplay   s    r   c                 C   sn   dd l }| }|j|| j| j| jdd}z t
| dD ]}||j q:W 5 |  |  |	  X d S )Nr   T)formatchannelsZrateoutputi  )pyaudioZPyAudioopenZget_format_from_widthsample_widthr   
frame_rateZstop_streamclose	terminater   write_data)r
   r   pstreamchunkr   r   r   _play_with_pyaudio   s    r   c                 C   s"   dd l }|j| j| j| j| jdS )Nr   )Znum_channelsZbytes_per_sampleZsample_rate)simpleaudioZplay_bufferraw_datar   r   r   )r
   r   r   r   r   _play_with_simpleaudio)   s    r    c                 C   s   z6t | }z|  W n tk
r2   |  Y nX W n tk
rJ   Y nX d S zt|  W d S  tk
rr   Y nX d S t|  d S )N)r    Z	wait_doneKeyboardInterruptstopImportErrorr   r   )Zaudio_segmentZplaybackr   r   r   play3   s     r$   )__doc__r   tempfiler   utilsr   r   r   r   r    r$   r   r   r   r   <module>   s   
