o
    Yôj-)  ã                   @   sþ   d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ dZejdkr6d	ZndZG d
d„ de
ƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ dejƒZdd„ Zdd„ Ze ¡ ZG dd„ deƒZdS )zLRead audio data using the ffmpeg command line tool via its standard
output.
é    N)ÚDEFAULT_BUFFER_SIZEé   )ÚDecodeError)Ú	AudioFile)ÚffmpegZavconvÚwin32i   c                   @   s   e Zd ZdS )ÚFFmpegErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú`/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/audioread/ffdec.pyr   &   s    r   c                   @   ó   e Zd ZdZdS )ÚCommunicationErrorz2Raised when the output of FFmpeg is not parseable.N©r	   r
   r   Ú__doc__r   r   r   r   r   *   ó    r   c                   @   r   )ÚUnsupportedErrorz(The file could not be decoded by FFmpeg.Nr   r   r   r   r   r   .   r   r   c                   @   r   )ÚNotInstalledErrorz!Could not find the ffmpeg binary.Nr   r   r   r   r   r   2   r   r   c                   @   r   )ÚReadTimeoutErrorz4Reading from the ffmpeg command-line tool timed out.Nr   r   r   r   r   r   6   r   r   c                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )	ÚQueueReaderThreadzWA thread that consumes data from a filehandle and sends the data
    over a Queue.
    é   Fc                    s>   t ƒ  ¡  || _|| _d| _|| _|rd | _d S t ¡ | _d S )NT)ÚsuperÚ__init__ÚfhÚ	blocksizeÚdaemonÚdiscardÚqueueÚQueue)Úselfr   r   r   ©Ú	__class__r   r   r   >   s   
zQueueReaderThread.__init__c                 C   s,   	 | j  | j¡}| js| j |¡ |sd S q©N)r   Úreadr   r   r   Úput)r    Údatar   r   r   ÚrunF   s   úzQueueReaderThread.run)r   F)r	   r
   r   r   r   r'   Ú__classcell__r   r   r!   r   r   :   s    r   c              	   O   sb   t | ƒD ]*\}}|g| }ztj|g|¢R i |¤ŽW   S  ty.   |t| ƒd kr,‚ Y qw dS )ak  Like `subprocess.Popen`, but can try multiple commands in case
    some are not available.

    `commands` is an iterable of command names and `command_args` are
    the rest of the arguments that, when appended to the command name,
    make up the full first argument to `subprocess.Popen`. The
    other positional and keyword arguments are passed through.
    r   N)Ú	enumerateÚ
subprocessÚPopenÚOSErrorÚlen)ÚcommandsZcommand_argsÚargsÚkwargsÚiÚcommandÚcmdr   r   r   Úpopen_multipleP   s   	
þÿür4   c                  C   sD   zt tdgtjtjtd�} W n
 ty   Y dS w |  ¡  | jdkS )zBDetect whether the FFmpeg backend can be used on this system.
    z-version)ÚstdoutÚstderrÚcreationflagsFr   )r4   ÚCOMMANDSr*   ÚPIPEÚ
PROC_FLAGSr,   ÚcommunicateÚ
returncode)Úprocr   r   r   Ú	availablec   s   
ûÿ
r>   c                   @   s^   e Zd ZdZefdd„Zddd„Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚFFmpegAudioFilez9An audio file decoded by the ffmpeg command-line utility.c                 C   s.  t j d¡}|r"t ¡  d}dd l}|jj |¡}|jj ||B ¡ z;zt	t
d|dddgtjtjtjtd�| _W n
 tyB   tƒ ‚w W |r]zdd l}|jj |¡ W t ¡  n t ¡  w n|rwzdd l}|jj |¡ W t ¡  w t ¡  w w t| jj|ƒ| _| j ¡  |  ¡  t| jjƒ| _| j ¡  d S )	NÚwiné   r   z-iz-fÚs16leú-)r5   r6   Ústdinr7   )ÚsysÚplatformÚ
startswithÚwindows_error_mode_lockÚacquireÚctypesÚwindllÚkernel32ÚSetErrorModer4   r8   r*   r9   ÚDEVNULLr:   r=   r,   r   Úreleaser   r5   Ústdout_readerÚstartÚ	_get_infor6   Ústderr_reader)r    ÚfilenameÚ
block_sizeÚwindowsZSEM_NOGPFAULTERRORBOXrJ   Zprevious_error_moder   r   r   r   |   sN   ÿÿú	ÿ÷ûû	
zFFmpegAudioFile.__init__ç      $@c              	   c   sˆ   � t   ¡ }	 d}z| jjj|d�}|r|V  nW dS W n' tjyB   t   ¡ }|s@|| |kr<td d | jjj¡¡ƒ‚|}Y qY nw q)z*Read blocks of raw PCM data from the file.TN)Útimeoutzffmpeg output: {}ó    )	ÚtimerP   r   ÚgetÚEmptyr   ÚformatÚjoinrS   )r    rX   Ú
start_timer&   Úend_timer   r   r   Ú	read_data³   s,   €ýÿöýözFFmpegAudioFile.read_datac                 C   sš   g }	 | j j ¡ }|stdƒ‚t|tƒr| dd¡}| ¡  ¡ }d|v r(t	dƒ‚d|v r/t
ƒ ‚d|v r9| |¡ nd	|v rL| |¡ |  d
 |¡¡ dS q)zrReads the tool's output from its stderr stream, extracts the
        relevant information, and parses it.
        Tzstream info not foundÚutf8Úignorezno such filezfile not foundzinvalid data foundz	duration:zaudio:Ú N)r=   r6   Úreadliner   Ú
isinstanceÚbytesÚdecodeÚstripÚlowerr,   r   ÚappendÚ_parse_infor^   )r    Z	out_partsÚliner   r   r   rR   Ñ   s&   

ëzFFmpegAudioFile._get_infoc                 C   sö   t  d|¡}|rt| d¡ƒ| _nd| _t  d|¡}|rC| d¡}|dkr)d| _nt  d|¡}|r?ttt| ¡  	d¡ƒƒ| _nd| _nd| _t  d	|¡}|rvt
tt| ¡ ƒƒ}|d d
 d
 |d d
  |d  t|d ƒd  }|| _dS d| _dS )zgGiven relevant data from the ffmpeg output, set audio
        parameter fields on this object.
        z(\d+) hzr   r   zhz, ([^,]+),ÚstereorA   z(\d+)\.?(\d)?Ú.z duration: (\d+):(\d+):(\d+).(\d)é<   é   é
   N)ÚreÚsearchÚintÚgroupÚ
samplerateÚchannelsÚmatchÚsumÚmapÚsplitÚlistÚgroupsÚfloatÚduration)r    Úsry   ÚmodeZcmatchZdurpartsr€   r   r   r   rl   í   s:   
ÿ
ÿþýÿ

zFFmpegAudioFile._parse_infoc                 C   s|   t | dƒr<| j ¡  | jjdu r| j ¡  | j ¡  t | dƒr$| j ¡  t | dƒr.| j ¡  | jj	 
¡  | jj 
¡  dS dS )z6Close the ffmpeg process used to perform the decoding.r=   NrS   rP   )Úhasattrr=   Úpollr<   ÚkillÚwaitrS   r^   rP   r5   Úcloser6   ©r    r   r   r   r‡     s   







ézFFmpegAudioFile.closec                 C   s   |   ¡  d S r#   ©r‡   rˆ   r   r   r   Ú__del__3  s   zFFmpegAudioFile.__del__c                 C   s   |   ¡ S r#   )ra   rˆ   r   r   r   Ú__iter__7  s   zFFmpegAudioFile.__iter__c                 C   s   | S r#   r   rˆ   r   r   r   Ú	__enter__;  s   zFFmpegAudioFile.__enter__c                 C   s   |   ¡  dS )NFr‰   )r    Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit__>  s   zFFmpegAudioFile.__exit__N)rW   )r	   r
   r   r   r   r   ra   rR   rl   r‡   rŠ   r‹   rŒ   r�   r   r   r   r   r?   z   s    
7+r?   )r   r   rs   r*   rE   Ú	threadingrZ   Úior   Ú
exceptionsr   Úbaser   r8   rF   r:   r   r   r   r   r   ÚThreadr   r4   r>   ÚLockrH   r?   r   r   r   r   Ú<module>   s0   
