o
    ڷi*                     @   s  d Z ddlZddlmZ ddlm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 ddlmZ de
eef de	e fddZde
eef de	e fddZde
eef defddZde
eef defddZde
eef de	e fddZde
eef defddZde
eef defddZde
eef de	e fddZde
eef defddZd8de
eef d!edefd"d#Zde
eef ddfd$d%Zd&ee
eef  ddfd'd(Z d)e
eef ddfd*d+Z!d,e
eef defd-d.Z"d,e
eef deee
eef f fd/d0Z#d,e
eef deee	e f fd1d2Z$d,e
eef defd3d4Z%d5edeee	e f fd6d7Z&dS )9z# Audio file info computed by soxi.
    N)Number)Path)ListDict)OptionalUnion   )VALID_FORMATS)sox)soxi)loggerinput_filepathreturnc                 C   s2   t |  t| d}|dkrtd|  dS t|S )a  
    Number of bits per sample, or None if not applicable.

    Parameters
    ----------
    input_filepath : str
        Path to audio file.

    Returns
    -------
    bitdepth : int or None
        Number of bits per sample.
        Returns None if not applicable.
    b0zBit depth unavailable for %sN)validate_input_filer   r   warningintr   output r   C/home/ubuntu/vllm_env/lib/python3.10/site-packages/sox/file_info.pybitdepth   s   
r   c                 C   sp   t |  t| d}d}|dkrtd|  dS |d |v r0d||d  }t|dd | S t|dd S )aV  
    Bit rate averaged over the whole file.
    Expressed in bytes per second (bps), or None if not applicable.

    Parameters
    ----------
    input_filepath : str
        Path to audio file.

    Returns
    -------
    bitrate : float or None
        Bit rate, expressed in bytes per second.
        Returns None if not applicable.
    Bz	 kMGTPEZYr   zBit rate unavailable for %sNg     @@)r   r   r   r   indexfloat)r   r   greek_prefixes
multiplierr   r   r   bitrate'   s   
r   c                 C      t |  t| d}t|S )z
    Show number of channels.

    Parameters
    ----------
    input_filepath : str
        Path to audio file.

    Returns
    -------
    channels : int
        number of channels
    c)r   r   r   r   r   r   r   channelsF      
r"   c                 C   r    )a  
    Show file comments (annotations) if available.

    Parameters
    ----------
    input_filepath : str
        Path to audio file.

    Returns
    -------
    comments : str
        File comments from header.
        If no comments are present, returns an empty string.
    ar   r   strr   r   r   r   commentsY   s   
r'   c                 C   s6   t |  t| d}t|dkrtd|  dS t|S )a!  
    Show duration in seconds, or None if not available.

    Parameters
    ----------
    input_filepath : str
        Path to audio file.

    Returns
    -------
    duration : float or None
        Duration of audio file in seconds.
        If unavailable or empty, returns None.
    Dg        zDuration unavailable for %sN)r   r   r   r   r   r   r   r   r   durationm   s   
r)   c                 C   r    )z
    Show the name of the audio encoding.

    Parameters
    ----------
    input_filepath : str
        Path to audio file.

    Returns
    -------
    encoding : str
        audio encoding type
    er%   r   r   r   r   encoding   r#   r+   c                 C   r    )z
    Show detected file-type.

    Parameters
    ----------
    input_filepath : str
        Path to audio file.

    Returns
    -------
    file_type : str
        file format type (ex. 'wav')
    tr%   r   r   r   r   	file_type   r#   r-   c                 C   s:   t | } t|  t| d}|dkrtd|  dS t|S )a;  
    Show number of samples, or None if unavailable.

    Parameters
    ----------
    input_filepath : path-like (str or pathlib.Path)
        Path to audio file.

    Returns
    -------
    n_samples : int or None
        total number of samples in audio file.
        Returns None if empty or unavailable.
    sr   z$Number of samples unavailable for %sN)r&   r   r   r   r   r   r   r   r   r   num_samples   s   
r/   c                 C   r    )z
    Show sample-rate.

    Parameters
    ----------
    input_filepath : str
        Path to audio file.

    Returns
    -------
    samplerate : float
        number of samples/second
    r)r   r   r   r   r   r   r   sample_rate   r#   r1   MbP?	thresholdc                 C   s8   t |  t| }|d }|tdur||krdS dS dS )a  
    Determine if an input file is silent.

    Parameters
    ----------
    input_filepath : str
        The input filepath.
    threshold : float
        Threshold for determining silence

    Returns
    -------
    is_silent : bool
        True if file is determined silent.
    zMean    normnanFT)r   statr   )r   r3   stat_dictionary	mean_normr   r   r   silent   s   r8   c                 C   s\   t | } |  std|  dt| }|tvr,tddt td| d dS dS )zInput file validation function. Checks that file exists and can be
    processed by SoX.

    Parameters
    ----------
    input_filepath : path-like (str or pathlib.Path)
        The input filepath.

    zinput_filepath z does not exist.Valid formats: %s $This install of SoX cannot process . files.N)	r   existsOSErrorfile_extensionr	   r   infojoinr   )r   extr   r   r   r      s   


r   input_filepath_listc                 C   s<   t | ts	tdt| dk rtd| D ]}t| qdS )zInput file list validation function. Checks that object is a list and
    contains valid filepaths that can be processed by SoX.

    Parameters
    ----------
    input_filepath_list : list
        A list of filepaths.

    z#input_filepath_list must be a list.   z/input_filepath_list must have at least 2 files.N)
isinstancelist	TypeErrorlen
ValueErrorr   )rC   r   r   r   r   validate_input_file_list  s   


rJ   output_filepathc                 C   s   t | } | dkr
dS ttj| ptt tj ttj| tj g}t|r3t	d|  t
| }|tvrMtddt td| d tj| r[td|  dS dS )	a2  Output file validation function. Checks that file can be written, and
    has a valid file extension. Throws a warning if the path already exists,
    as it will be overwritten on build.

    Parameters
    ----------
    output_filepath : path-like (str or pathlib.Path)
        The output filepath.

    -nNz$SoX cannot write to output_filepath r9   r:   r;   r<   z?output_file: %s already exists and will be overwritten on build)r&   boolospathdirnameaccessgetcwdW_OKallr>   r?   r	   r   r@   rA   r   r=   )rK   nowrite_conditionsrB   r   r   r   validate_output_file  s.   
rV   filepathc                 C   s   t | jdd  S )zGet the extension of a filepath.

    Parameters
    ----------
    filepath : path-like (str or pathlib.Path)
        File path.

    Returns
    -------
    extension : str
        The file's extension
    r   N)r   suffixlower)rW   r   r   r   r?   E  s   r?   c              	   C   s:   t | t| t| t| t| t| t| t| d}|S )a  Get a dictionary of file information

    Parameters
    ----------
    filepath : str
        File path.

    Returns
    -------
    info_dictionary : dict
        Dictionary of file information. Fields are:
            * channels
            * sample_rate
            * bitdepth
            * bitrate
            * duration
            * num_samples
            * encoding
            * silent
    r"   r1   r   r   r)   r/   r+   r8   rZ   )rW   info_dictionaryr   r   r   r@   U  s   
r@   c                 C   s   t | }t|}|S )zReturns a dictionary of audio statistics.

    Parameters
    ----------
    filepath : str
        File path.

    Returns
    -------
    stat_dictionary : dict
        Dictionary of audio statistics.
    )
_stat_call_parse_stat)rW   stat_outputr6   r   r   r   r5   w  s   r5   c                 C   s&   t |  d| ddg}t|\}}}|S )zCall sox's stat function.

    Parameters
    ----------
    filepath : str
        File path.

    Returns
    -------
    stat_output : str
        Sox output from stderr.
    r
   rL   r5   )r   r
   )rW   args_r^   r   r   r   r\     s   r\   r^   c              	   C   st   |  d}i }|D ].}| d}t|dkr7|d }|d d}zt|}W n ty2   d}Y nw |||< q	|S )zParse the string output from sox's stat function

    Parameters
    ----------
    stat_output : str
        Sox output from stderr.

    Returns
    -------
    stat_dictionary : dict
        Dictionary of audio statistics.
    
:rD   r   r   r:   N)splitrH   stripr   rI   )r^   lines	stat_dictline
split_linekeyvalr   r   r   r]     s   

r]   )r2   )'__doc__rN   numbersr   pathlibr   typingr   r   r   r   corer	   r
   r   logr   r&   r   r   r   r   r"   r'   r)   r+   r-   r/   r1   rM   r8   r   rJ   rV   r?   r@   r5   r\   r]   r   r   r   r   <module>   s8     **&""