o
    'WgP0                     @   s  d dl mZ d dlZd dlZd dlZd dlZd dlmZmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZ zd dlZW n eyK   d dlZY nw ejdkrSeZd	d
ddZddddZddddZdd ZdBddZdd ZdCddZdBddZdDdd Z dEd!d"Z!d#d$ Z"d%d& Z#d'd( Z$d)d* Z%d+d, Z&d-d. Z'd/d0 Z(dFd2d3Z)d4d5 Z*d6d7 Z+e+d8d9 Z,d:d; Z-d<d= Z.d>d? Z/d@dA Z0dS )G    )divisionN)PopenPIPE)logceil)TemporaryFile)warnwraps)   r            )          bhi)i   )i i  )i   ic                 C      t |  S N)FRAME_WIDTHS	bit_depth r   J/var/www/html/audio_editor/env/lib/python3.10/site-packages/pydub/utils.pyget_frame_width&      r   Tc                 C   s   t |  }|s
| }|S r   )ARRAY_TYPESupper)r   signedtr   r   r   get_array_type*   s   r#   c                 C   r   r   )ARRAY_RANGESr   r   r   r   get_min_max_value1   r   r%   w+bc                 C   s   d}| d u r|rt |d} d}t| trt| |d} d}zt| tjr0t| |d} d}W | |fS W | |fS  ty@   Y | |fS w )NF)modeT)r   
isinstance
basestringopenosPathLikeAttributeError)fdr'   tempfileclose_fdr   r   r   _fd_or_path_or_tempfile5   s$   

r1   c                 C   s$   t | } |rd| d  S d| d  S )z_
    Converts the input db to a float, which represents the equivalent
    ratio in power.
    
      )float)dbusing_amplituder   r   r   db_to_floatK   s   r7   c                 C   sJ   t | } |dur| | } | dkrt d S |rdt| d S dt| d S )z
    Converts the input float to db, which represents the equivalent
    to the ratio in power represented by the multiplier passed in.
    Nr   infr3   r2   )r4   r   )ratioval2r6   r   r   r   ratio_to_dbW   s   
r;   c                    sD   t | tr|   fddS  du r| j ddlm} t| |  | S )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                    s
   t |  S r   )register_pydub_effect)fnnamer   r   <lambda>z   s   
 z'register_pydub_effect.<locals>.<lambda>Nr   )AudioSegment)r(   r)   __name__audio_segmentrA   setattr)r=   r?   rA   r   r>   r   r<   l   s   
r<   c                    s0   t t t } fddtt|D S )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)
    c                    s$   g | ]} | |d    qS )r   r   ).0r   rC   chunk_lengthr   r   
<listcomp>   s    zmake_chunks.<locals>.<listcomp>)r   lenr4   rangeint)rC   rG   number_of_chunksr   rF   r   make_chunks   s   
rM   c                 C   st   t jdkr| ds| d7 } t jgt jd t j }|D ]}t j|| }t j	|r7t 
|t jr7|  S qdS )z0
    Mimics behavior of UNIX which command.
    ntz.exePATHN)r+   r?   endswithcurdirenvironsplitpathseppathjoinisfileaccessX_OK)programenvdir_listenvdirprogram_pathr   r   r   which   s   r^   c                   C   &   t drdS t drdS tdt dS )Q
    Return enconder default application for system, either avconv or ffmpeg
    avconvffmpegzGCouldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not workr^   r   RuntimeWarningr   r   r   r   get_encoder_name      
re   c                   C   r_   )r`   avplayffplayzGCouldn't find ffplay or avplay - defaulting to ffplay, but may not workrc   r   r   r   r   get_player_name   rf   ri   c                   C   r_   )z;
    Return probe application, either avconv or ffmpeg
    avprobeffprobezJCouldn't find ffprobe or avprobe - defaulting to ffprobe, but may not workrc   r   r   r   r   get_prober_name   rf   rl   c                 C   sv   t jdkrttf}t jdkr|tjf7 }t| |rt| S nt| tr+| t 	 S t| tr2| S t
dt| )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PathLikeTypesr   r   r   rq      s   





rq   c                 C   s   i }d}t || D ]>}|ddur/t|dt|dkr/d|d|dg}n|d}dd	 t d
|D }||t|d< q
|S )a|  
    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_endNspace_start,	content_0	content_1c                 S   s   g | ]}|r|  qS r   )striprE   xr   r   r   rH      s    z"get_extra_info.<locals>.<listcomp>z[:,]	stream_id)refinditergrouprI   rV   rS   rK   )stderr
extra_info	re_streamr   content_linetokensr   r   r   get_extra_info   s   
r   c                 C   sD  t  }g d}z|t| g7 }d}d}W n6 tyJ   |dkr)|dt|dg7 }n|dg7 }t}t| ddd	\}}|d
 | }|rH|  Y nw |ddg| }t	||ttd}	|	j
|d\}
}|
dd}
|dd}t|
}|sw|S t|}dd |d D }t|d
kr|S |d
 }dd }||d  D ]}td|}td|}|r||d|d ||dt|d ||dt|d q|r||d|d ||dt|d ||dt|d qtd|r||d| ||dd  ||dd  qtd!|r||d| ||dd" ||dd" q|S )#z\Return json dictionary with media info(codec, duration, size, bitrate...) from filepath
    )-vinfo-show_format-show_streamsNrk   z-read_ahead_limitzcache:pipe:0-rbF)r/   r   -ofjson)stdinstdoutr   )inpututf-8ignorec                 S   s   g | ]
}|d  dkr|qS )
codec_typeaudior   r   r   r   r   rH      s    z"mediainfo_json.<locals>.<listcomp>streamsc                 S   s$   || vs
| | dkr|| |< d S d S Nr   r   )streampropvaluer   r   r   set_property'  s   z$mediainfo_json.<locals>.set_property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\))?$@   )rl   rq   rt   strr   r1   seekreadcloser   communicaterr   r   loadsr   rI   r   matchr   rK   )filepathread_ahead_limitprobercommand_argsstdin_parameter
stdin_datafile
close_filecommandresoutputr   r   r   audio_streamsr   r   tokenmm2r   r   r   mediainfo_json   sn   



r   c              	   C   s  t  }dddd| g}|ddg| }t|td}| d d	}|jdkr7|g| }t|td d d	}td
}i }tj	dkrI|
dd}|dD ]1}||}	|	r|	 \}
}}|
r{z||
  W n tys   i ||
< Y nw |||
 |< qN|||< qN|S )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 
)rl   r   r   r   rr   
returncoder   compilern   platformreplacerS   r   groupsKeyError)r   r   r   r   r   r   rgxr   linemobj
inner_dictkeyr   r   r   r   	mediainfoA  s<   




r   c                    s   i  t  fdd}|S )Nc                      s(   z d W S      d<  d  Y S r   r   r   cachefunctionr   r   wrapperr  s
   

zcache_codecs.<locals>.wrapperr	   )r   r   r   r   r   cache_codecso  s   r   c                  C   s   t  } | dg}t|ttd}| d d}|jdkrg S tjdkr)|dd}t	
d}t }t }|d	D ])}|| }|sEq9| \}	}
}|	d d
krW||
 |	d dkrb||
 q9||fS )Nz-codecs)r   r   r   r   r   r   r   z)^([D.][E.][AVS.][I.][L.][S.]) (\w*) +(.*)r   Dr   E)re   r   r   r   rr   r   rn   r   r   r   r   setrS   r   r~   r   add)encoderr   r   r   r   decodersencodersr   r   flagscodecr?   r   r   r   get_supported_codecs}  s,   




r   c                   C   
   t  d S r   r   r   r   r   r   get_supported_decoders     
r   c                   C   r   )Nr   r   r   r   r   r   get_supported_encoders  r   r   c                 C   sD   |   }|d |d |d |d  g}t|d |d S )z
	Left-Right -> Mid-Side
	r   r   split_to_monooverlayinvert_phaserA   from_mono_audiosegmentsrC   channelr   r   r   stereo_to_ms  s   (r   c                 C   sL   |   }|d |d d |d |d  d g}t|d |d S )z
	Mid-Side -> Left-Right
	r   r   r   r   r   r   r   r   ms_to_stereo  s   0r   )T)r&   T)NTr   )r   )1
__future__r   r   r+   r   rn   
subprocessr   r   mathr   r   r/   r   warningsr   	functoolsr
   audioopImportError	pyaudioopro   r   r)   r   r   r$   r   r#   r%   r1   r7   r;   r<   rM   r^   re   ri   rl   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sh    






J.
