
    v;3h                     D    d Z ddlZddlmZ ddlmZmZ d Zd Zd Z	d	 Z
y)
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                     t               }t        dd      5 }| j                  |j                  d       t	        j
                  |ddd|j                  g       d d d        y # 1 sw Y   y xY w)Nzw+bz.wav)suffixwavz-nodispz	-autoexitz-hide_banner)r   r   exportname
subprocesscall)segPLAYERfs      N/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/pydub/playback.py_play_with_ffplayr      s\    F	E&	1 RQ

1665!KPQR R Rs   AA""A+c                    dd l }|j                         }|j                  |j                  | j                        | j
                  | j                  d      }	 t        | d      D ]  }|j                  |j                          	 |j                          |j                          |j                          y # |j                          |j                          |j                          w xY 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chunks        r   _play_with_pyaudior&      s    AVV12233C3CD \\  !F c* 	&ELL%	& 		 		s   ,B6 62C(c                     dd l }|j                  | j                  | j                  | j                  | j
                        S )Nr   )num_channelsbytes_per_samplesample_rate)simpleaudioplay_bufferraw_datar   r   r   )r   r+   s     r   _play_with_simpleaudior.   )   s;    ""\\))NN	 #      c                     	 t        |       }	 |j                          y # t        $ r |j                          Y y w xY w# t        $ r Y nw xY w	 t        |        y # t        $ r Y nw xY wt        |        y )N)r.   	wait_doneKeyboardInterruptstopImportErrorr&   r   )audio_segmentplaybacks     r   playr7   3   s    	)-8	  	 ! 	MMO 		 
=) 
 m$s5   >  ;> ;> 	A
	A
A 	A&%A&)__doc__r   tempfiler   utilsr   r   r   r&   r.   r7    r/   r   <module>r<      s*     ' /R,%r/   