
    1;ji-)                     z   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    rd	ZndZ G d
 de
          Z G d de          Z G d de          Z G d de          Z G d de          Z G d dej                  Zd Zd Z ej                    Z G 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)ffmpegavconvwin32i   c                       e Zd ZdS )FFmpegErrorN)__name__
__module____qualname__     I/root/voice-cloning/.venv/lib/python3.11/site-packages/audioread/ffdec.pyr   r   &   s        Dr   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   *   s        <<<<r   r   c                       e Zd ZdZdS )UnsupportedErrorz(The file could not be decoded by FFmpeg.Nr   r   r   r   r   r   .   s        2222r   r   c                       e Zd ZdZdS )NotInstalledErrorz!Could not find the ffmpeg binary.Nr   r   r   r   r   r   2   s        ++++r   r   c                       e Zd ZdZdS )ReadTimeoutErrorz4Reading from the ffmpeg command-line tool timed out.Nr   r   r   r   r   r   6   s        >>>>r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )QueueReaderThreadzWA thread that consumes data from a filehandle and sends the data
    over a Queue.
       Fc                     t                                                       || _        || _        d| _        || _        |rd nt          j                    | _        d S )NT)super__init__fh	blocksizedaemondiscardqueueQueue)selfr"   r#   r%   	__class__s       r   r!   zQueueReaderThread.__init__>   sO    "$7TT%+--


r   c                     	 | j                             | j                  }| j        s| j                            |           |sd S EN)r"   readr#   r%   r&   put)r(   datas     r   runzQueueReaderThread.runF   sL    	7<<//D< %
t$$$ 	r   )r   F)r   r   r   r   r!   r/   __classcell__)r)   s   @r   r   r   :   sV         8 8 8 8 8 8      r   r   c                     t          |           D ]I\  }}|g|z   }	 t          j        |g|R i |c S # t          $ r |t	          |           dz
  k    r Y Fw xY 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)commandscommand_argsargskwargsicommandcmds          r   popen_multipler>   P   s      ))  
7i,&	#C9$999&99999 	 	 	CMMA%%% &%		 s   3!AAc                      	 t          t          dgt          j        t          j        t                    } |                                  | j        dk    S # t          $ r Y dS w xY w)zBDetect whether the FFmpeg backend can be used on this system.
    z-version)stdoutstderrcreationflagsr   F)r>   COMMANDSr3   PIPE
PROC_FLAGScommunicate
returncoder5   )procs    r   	availablerI   c   su    $L??$
 
 
 	!##	    uus   3A 
A"!A"c                   N    e Zd ZdZefdZddZd Zd Zd Z	d Z
d	 Zd
 Zd ZdS )FFmpegAudioFilez9An audio file decoded by the ffmpeg command-line utility.c                 ^   t           j                            d          }|r`t                                           d}dd l}|j        j                            |          }|j        j                            ||z             	 t          t          d|dddgt          j        t          j        t          j        t                    | _        n# t           $ r t#                      w xY w	 |r\	 dd l}|j        j                            |           t                                           n# t                                           w xY wnc# |r\	 dd l}|j        j                            |           t                                           w # t                                           w xY ww xY wt'          | j        j        |          | _        | j                                         |                                  t'          | j        j                  | _        | j                                         d S )	Nwin   r   z-iz-fs16le-)r@   rA   stdinrB   )sysplatform
startswithwindows_error_mode_lockacquirectypeswindllkernel32SetErrorModer>   rC   r3   rD   DEVNULLrE   rH   r5   r   releaser   r@   stdout_readerstart	_get_inforA   stderr_reader)r(   filename
block_sizewindowsSEM_NOGPFAULTERRORBOXrW   previous_error_modes          r   r!   zFFmpegAudioFile.__init__|   s2    ,))%00 
	#++---$*!MMM &334IJJ  M"//#&;;  	6&xw4!! ((  DII  	& 	& 	&#%%%	&    66!MMMM*778KLLL+335555+3355556w 66!MMMM*778KLLL+335555+3355556 /ty/?LL  """ 	
 /ty/?@@  """""sC   AC 
E C$$E +#D( (EF'#F.F'F##F'      $@c              #     K   t          j                     }	 d}	 | j        j                            |          }|r|V  ndS n{# t          j        $ ri t          j                     }|sQ||z
  |k    rDt          d                    d                    | j        j        j                                      |}Y Y nw xY w)z*Read blocks of raw PCM data from the file.TN)timeoutzffmpeg output: {}r   )	timer]   r&   getEmptyr   formatjoinr`   )r(   rh   
start_timer.   end_times        r   	read_datazFFmpegAudioFile.read_data   s       Y[[
	!D!)/33G3DD JJJJ E  ; ! ! !9;; 
!*,77 //B/I/IHHT%7%=%CDD0 0    &.
 
! 
!!	!s   'A A3B<;B<c                    g }	 | j         j                                        }|st          d          t	          |t
                    r|                    dd          }|                                                                }d|v rt          d          d|v rt                      d|v r|                    |           nCd	|v r?|                    |           |                     d
                    |                     dS )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)rH   rA   readliner   
isinstancebytesdecodestriplowerr5   r   append_parse_inform   )r(   	out_partslines      r   r_   zFFmpegAudioFile._get_info   s    		9#,,..D B()@AAA $&& 5{{6844::<<%%''D%%.///%--&((($$  &&&&T!!  &&&  !3!3444+	r   c                    t          j        d|          }|r(t          |                    d                    | _        nd| _        t          j        d|          }|r|                    d          }|dk    rd| _        nst          j        d|          }|rMt          t          t          |                                	                    d                              | _        nd| _        nd| _        t          j        d	|          }|ryt          t          t          |                                                    }|d         d
z  d
z  |d         d
z  z   |d         z   t          |d                   dz  z   }|| _        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, ([^,]+),stereorN   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(   sr   modecmatchdurpartsr   s          r   r|   zFFmpegAudioFile._parse_info   s   
 	+q)) 	 !%++a..11DOODO 	/1-- 	;;q>>Dx !"2D99 &$'C1E1Ec1J1J(K(K$L$LDMM$%DMMDM 	/
 
  	CU\\^^4455Hb 2%b ! hqk""R'(  %DMMM DMMMr   c                    t          | d          r| j                                         | j        j        2| 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.rH   Nr`   r]   )hasattrrH   pollrG   killwaitr`   rm   r]   r@   closerA   r(   s    r   r   zFFmpegAudioFile.close  s    4   	%
 INN y#+	   	    t_-- *"'')))t_-- *"'')))
 I""$$$I""$$$$$/	% 	%r   c                 .    |                                   d S r+   r   r   s    r   __del__zFFmpegAudioFile.__del__3  s    

r   c                 *    |                                  S r+   )rp   r   s    r   __iter__zFFmpegAudioFile.__iter__7  s    ~~r   c                     | S r+   r   r   s    r   	__enter__zFFmpegAudioFile.__enter__;  s    r   c                 .    |                                   dS )NFr   )r(   exc_typeexc_valexc_tbs       r   __exit__zFFmpegAudioFile.__exit__>  s    

ur   N)rf   )r   r   r   r   r   r!   rp   r_   r|   r   r   r   r   r   r   r   r   rK   rK   z   s        CC,? 5# 5# 5# 5#n! ! ! !<  8) ) )V% % %6             r   rK   )r   r&   r   r3   rR   	threadingri   ior   
exceptionsr   baser   rC   rS   rE   r   r   r   r   r   Threadr   r>   rI   LockrU   rK   r   r   r   <module>r      s     				     



      " " " " " " # # # # # #      <7JJJ	 	 	 	 	+ 	 	 	= = = = = = = =3 3 3 3 3{ 3 3 3, , , , , , , ,? ? ? ? ?{ ? ? ?    	(   ,  &$ $ $( )).** F F F F Fi F F F F Fr   