
    rhP0                        S SK Jr  S SKrS SKrS SKrS SKrS SKJrJr  S SK	J
r
Jr  S SKJr  S SKJr  S SKJr   S SKr\R*                  S:  a  \rS	S
SS.rSSSS.rSSSS.rS rS)S jrS rS*S jrS)S jrS+S jr S,S jr!S r"S r#S r$S r%S r&S r'S  r(S-S! jr)S" r*S# r+\+S$ 5       r,S% r-S& r.S' r/S( r0g! \ a    S SKr Nf = f).    )divisionN)PopenPIPE)logceil)TemporaryFile)warnwraps)   r            )          bhi)i   )i i  )i   ic                     [         U    $ N)FRAME_WIDTHS	bit_depths    E/home/james-whalen/.local/lib/python3.13/site-packages/pydub/utils.pyget_frame_widthr   &       	""    c                 F    [         U    nU(       d  UR                  5       nU$ r   )ARRAY_TYPESupper)r   signedts      r   get_array_typer%   *   s    IAGGIHr   c                     [         U    $ r   )ARRAY_RANGESr   s    r   get_min_max_valuer(   1   r   r   c                     SnU c  U(       a  [        US9n Sn[        U [        5      (       a  [        XS9n Sn [        U [        R
                  5      (       a  [        XS9n SnX4$ ! [         a     X4$ f = f)NF)modeT)r   
isinstance
basestringopenosPathLikeAttributeError)fdr*   tempfileclose_fds       r   _fd_or_path_or_tempfiler4   5   s    H	zh%"j!!" b"++&&b$BH <   	<s   *A& &
A54A5c                 F    [        U 5      n U(       a  SU S-  -  $ SU S-  -  $ )zS
Converts the input db to a float, which represents the equivalent
ratio in power.

      )float)dbusing_amplitudes     r   db_to_floatr;   K   s,    
 
rBb2gb2gr   c                     [        U 5      n Ub  X-  n U S:X  a  [        S5      * $ U(       a  S[        U S5      -  $ S[        U S5      -  $ )z
Converts the input float to db, which represents the equivalent
to the ratio in power represented by the multiplier passed in.
r   infr7   r6   )r8   r   )ratioval2r:   s      r   ratio_to_dbr@   W   sV    
 %LE  ze}CrN""CrN""r   c                    ^ [        U [        5      (       a  U mU4S j$ Tc  U R                  mSSKJn  [        UTU 5        U $ )a  
decorator for adding pydub effects to the AudioSegment objects.
example use:
    @register_pydub_effect
    def normalize(audio_segment):
        ...
or you can specify a name:
    @register_pydub_effect("normalize")
    def normalize_audio_segment(audio_segment):
        ...
c                    > [        U T5      $ r   )register_pydub_effect)fnnames    r   <lambda>'register_pydub_effect.<locals>.<lambda>z   s    /D9r   r   )AudioSegment)r+   r,   __name__audio_segmentrH   setattr)rD   rE   rH   s    ` r   rC   rC   l   s?     "j!!99|{{+L$#Ir   c                     [        [        U 5      [        U5      -  5      n[        [	        U5      5       Vs/ s H  nXU-  US-   U-   PM     sn$ s  snf )z
Breaks an AudioSegment into chunks that are <chunk_length> milliseconds
long.
if chunk_length is 50 then you'll get a list of 50 millisecond long audio
segments back (except the last one, which can be shorter)
r   )r   lenr8   rangeint)rJ   chunk_lengthnumber_of_chunksr   s       r   make_chunksrR      sa     C.|1DDE3/0131 l*AE\+AB13 3 3s   Ac                    [         R                  S:X  a  U R                  S5      (       d  U S-  n [         R                  /[         R                  S   R                  [         R                  5      -   nU Hv  n[         R                  R                  X 5      n[         R                  R                  U5      (       d  MH  [         R                  " U[         R                  5      (       d  Mt  Us  $    g)z(
Mimics behavior of UNIX which command.
ntz.exePATHN)r.   rE   endswithcurdirenvironsplitpathseppathjoinisfileaccessX_OK)programenvdir_listenvdirprogram_paths       r   whichrd      s    
 
ww$w//77699+

6 2 8 8 DDKww||F477>>,''BIIlBGG,L,L r   c                  h    [        S5      (       a  g[        S5      (       a  g[        S[        5        g)I
Return enconder default application for system, either avconv or ffmpeg
avconvffmpegzGCouldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not workrd   r	   RuntimeWarning r   r   get_encoder_namerl      -     X	x 	VXfgr   c                  h    [        S5      (       a  g[        S5      (       a  g[        S[        5        g)rf   avplayffplayzGCouldn't find ffplay or avplay - defaulting to ffplay, but may not workri   rk   r   r   get_player_namerq      rm   r   c                  h    [        S5      (       a  g[        S5      (       a  g[        S[        5        g)z3
Return probe application, either avconv or ffmpeg
avprobeffprobezJCouldn't find ffprobe or avprobe - defaulting to ffprobe, but may not workri   rk   r   r   get_prober_nameru      s1     Y	y		 	Y[ijr   c                    [         R                  S:  a[  [        [        4n[         R                  S:  a  U[        R
                  4-  n[        X5      (       a  [        R                  " U 5      $ OP[        U [        5      (       a$  U R                  [         R                  " 5       5      $ [        U [        5      (       a  U $ [        SR                  [        U 5      5      5      e)z<Wrapper for os.fsdecode which was introduced in python 3.2 .)r   r   )r      z!type {0} not accepted by fsdecode)sysversion_infor,   bytesr.   r/   r+   fsdecodedecodegetfilesystemencoding	TypeErrorformattype)filenamePathLikeTypess     r   r{   r{      s     6!#U+v%bkk^+Mh..;;x(( / h&&??3#<#<#>??h
++O
7>>tH~N
OOr   c                    0 nSn[         R                  " X 5       H  nUR                  S5      bh  [        UR                  S5      5      [        UR                  S5      5      ::  a2  SR	                  UR                  S5      UR                  S5      /5      nOUR                  S5      n[         R
                  " SU5       Vs/ s H  oU(       d  M  UR                  5       PM     nnXa[        UR                  S5      5      '   M     U$ s  snf )	aT  
avprobe sometimes gives more information on stderr than
on the json output. The information has to be extracted
from stderr of the format of:
'    Stream #0:0: Audio: flac, 88200 Hz, stereo, s32 (24 bit)'
or (macOS version):
'    Stream #0:0: Audio: vorbis'
'      44100 Hz, stereo, fltp, 320 kb/s'

:type stderr: str
:rtype: list of dict
z}(?P<space_start> +)Stream #0[:\.](?P<stream_id>([0-9]+))(?P<content_0>.+)\n?(?! *Stream)((?P<space_end> +)(?P<content_1>.+))?	space_endspace_start,	content_0	content_1z[:,]	stream_id)refinditergrouprM   r\   rY   striprO   )stderr
extra_info	re_streamr   content_linextokenss          r   get_extra_infor      s     J QI[[+77;+AGGM4J0Ks$P& 1&88QWW[%9177;;O$PQL77;/L%'XXfl%CI%Cq)!'')%CI063qww{+,- ,  Js   
DDc           
         [        5       n/ SQn U[        U 5      /-  nSnSnUSS/U-   n[        X[        [        S9n	U	R                  US9u  pU
R                  SS5      n
UR                  SS5      n[        R                  " U
5      nU(       d  U$ [        U5      nUS    Vs/ s H  oS   S:X  d  M  UPM     nn[        U5      S
:X  a  U$ US
   nS nUUS       GHx  n[         R"                  " SU5      n[         R"                  " SU5      nU(       a_  U" USUR%                  S5      5        U" US['        UR%                  S5      5      5        U" US['        UR%                  S5      5      5        M  U(       a_  U" USUR%                  S5      5        U" US['        UR%                  S5      5      5        U" US['        UR%                  S5      5      5        M  [         R"                  " SU5      (       a!  U" USU5        U" USS5        U" USS5        GM;  [         R"                  " S U5      (       d  GMZ  U" USU5        U" USS!5        U" USS!5        GM{     U$ ! [         am    US:X  a  US[        U5      S/-  nOUS/-  n[        n[        U SSS	9u  pgUR                  S
5        UR                  5       nU(       a  UR                  5          GNf = fs  snf )"z\Return json dictionary with media info(codec, duration, size, bitrate...) from filepath
    )-vinfo-show_format-show_streamsNrt   z-read_ahead_limitzcache:pipe:0-rbF)r2   r   -ofjson)stdinstdoutr   )inpututf-8ignorestreams
codec_typeaudioc                 (    X;  d  X   S:X  a  X U'   g g Nr   rk   )streampropvalues      r   set_property$mediainfo_json.<locals>.set_property'  s    !2 4L "3r   indexz*([su]([0-9]{1,2})p?) \(([0-9]{1,2}) bit\)$z$([su]([0-9]{1,2})p?)( \(default\))?$
sample_fmtr   bits_per_sampler   bits_per_raw_sampler   z(flt)p?( \(default\))?$r   z(dbl)p?( \(default\))?$@   )ru   r{   r~   strr   r4   seekreadcloser   communicater|   r   loadsr   rM   r   matchr   rO   )filepathread_ahead_limitprobercommand_argsstdin_parameter
stdin_datafile
close_filecommandresoutputr   r   r   r   audio_streamsr   r   tokenmm2s                        r   mediainfo_jsonr      s    FL
(+,,
 uf%4G
tD
IC__:_6NF]]7H-F]]7H-F::fD 'J $YN1\?g3MQMN
=Q 1F! F7O,HHA5IXX<eDqwwqz:!2C
OD!6AGGAJHrxx{;!2C4DE!6BHHQK8HIXX/77u5!2B7!6;XX/77u5!2B7!6;% -& Ku  Y0#6F2G+- -L SE!L28TER		!YY[
JJL8 Os   I $K4KA3KKc                    [        5       nSSSSU /nUSS/U-   n[        U[        S9nUR                  5       S   R	                  S	5      nUR
                  S:w  a4  U/U-   n[        U[        S9R                  5       S   R	                  S	5      n[        R                  " S
5      n0 n[        R                  S:X  a  UR                  SS5      nUR                  S5       HI  nUR                  U5      n	U	(       d  M  U	R                  5       u  pnU
(       a   Xz     XU
   U'   ME  XU'   MK     U$ ! [         a    0 Xz'    N#f = f)zWReturn dictionary with media info(codec, duration, size, bitrate...) from filepath
    r   quietr   r   r   old)r   r   r   z6(?:(?P<inner_dict>.*?):)?(?P<key>.*?)\=(?P<value>.*?)$win32 
)ru   r   r   r   r|   
returncoder   compilerx   platformreplacerY   r   groupsKeyError)r   r   r   r   r   r   rgxr   linemobj
inner_dictkeyr   s                r   	mediainfor   A  sB    Fg	L ue$|3G

%C__q!((1F
~~(\)wt,88:1=DDWM
**N
OCD
||wb)T"yy4%)[[]"JU*$ ).Z %!S	 #" K   *')D$*s   D,,D=<D=c                 8   ^ ^ 0 m[        T 5      UU 4S j5       nU$ )Nc                  <   >  T S   $ !   T" 5       T S'   T S   s $ = fr   rk   )cachefunctions   r   wrappercache_codecs.<locals>.wrapperr  s,    	8O	zE!H8Os    r
   )r   r   r   s   ` @r   cache_codecsr   o  s%    E
8_  Nr   c                  d   [        5       n U S/n[        U[        [        S9nUR                  5       S   R	                  S5      nUR
                  S:w  a  / $ [        R                  S:X  a  UR                  SS5      n[        R                  " S5      n[        5       n[        5       nUR                  S	5       Ht  nUR                  UR                  5       5      nU(       d  M+  UR                  5       u  pnU	S   S
:X  a  UR!                  U
5        U	S   S:X  d  Mc  UR!                  U
5        Mv     XV4$ )Nz-codecs)r   r   r   r   r   r   r   z)^([D.][E.][AVS.][I.][L.][S.]) (\w*) +(.*)r   Dr   E)rl   r   r   r   r|   r   rx   r   r   r   r   setrY   r   r   r   add)encoderr   r   r   r   decodersencodersr   r   flagscodecrE   s               r   get_supported_codecsr   }  s     G	"G
T
2C__q!((1F
~~	
||wb) **A
BCuHuHT"		$**,'"\\^d8s?LL8s?LL # r   c                      [        5       S   $ r   r   rk   r   r   get_supported_decodersr         !!$$r   c                      [        5       S   $ )Nr   r   rk   r   r   get_supported_encodersr     r   r   c                     U R                  5       nUS   R                  US   5      US   R                  US   R                  5       5      /n[        R	                  US   US   5      $ )z
Left-Right -> Mid-Side
r   r   split_to_monooverlayinvert_phaserH   from_mono_audiosegmentsrJ   channels     r   stereo_to_msr     sf     &&(AJwqz*GAJ,>,>wqz?V?V?X,YZ,,WQZDDr   c                     U R                  5       nUS   R                  US   5      S-
  US   R                  US   R                  5       5      S-
  /n[        R	                  US   US   5      $ )z
Mid-Side -> Left-Right
r   r   r   r   r   s     r   ms_to_stereor     sq     &&(AJwqz*Q.
0B0B71:CZCZC\0]`a0ab,,WQZDDr   )T)zw+bT)NTr   ))1
__future__r   r   r.   r   rx   
subprocessr   r   mathr   r   r2   r   warningsr	   	functoolsr   audioopImportError	pyaudioopry   r   r,   r   r!   r'   r   r%   r(   r4   r;   r@   rC   rR   rd   rl   rq   ru   r{   r   r   r   r   r   r   r   r   r   rk   r   r   <module>r     s     	 	 
 "  "    vJ 			  !##,	#*0	3  P$6GT+\    <%%EEy    s   B6 6
CC