o
    'Wg                     @   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   s\   t  }tddd}| |jd t|ddd|jg W d    d S 1 s'w   Y  d S )Nzw+bz.wav)suffixwavz-nodispz	-autoexitz-hide_banner)r   r   exportname
subprocesscall)segPLAYERf r   M/var/www/html/audio_editor/env/lib/python3.10/site-packages/pydub/playback.py_play_with_ffplay   s
   "r   c                 C   s   dd l }| }|j|| j| j| jdd}zt| dD ]}||j	 qW |
  |  |  d S |
  |  |  w )Nr   T)formatchannelsrateoutputi  )pyaudioPyAudioopenget_format_from_widthsample_widthr   
frame_rater   write_datastop_streamclose	terminate)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   )num_channelsbytes_per_samplesample_rate)simpleaudioplay_bufferraw_datar   r   r   )r   r(   r   r   r   _play_with_simpleaudio)   s   r+   c                 C   sz   zt | }z|  W W d S  ty   |  Y W d S w  ty%   Y nw zt|  W d S  ty6   Y nw t|  d S )N)r+   	wait_doneKeyboardInterruptstopImportErrorr$   r   )audio_segmentplaybackr   r   r   play3   s&   r2   )__doc__r
   tempfiler   utilsr   r   r   r$   r+   r2   r   r   r   r   <module>   s    
