o
    ڷiv                     @   sf  d Z ddlZddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlZddlmZ ddlmZ dd	lmZ g d
Zg dZed Z		d%de
e deej dede	eeeeejf  ee f fddZG dd deZdee fddZe Zdeeef dedefddZde
e defddZ G dd deZ!dedefd d!Z"d"ee defd#d$Z#dS )&zBase module for calling SoX     N)Path)CalledProcessError)UnionListOptionalTupleIterableAny)Literal   )NO_SOX)logger)	BbcaDetsr)	zsigned-integerzunsigned-integerzfloating-pointza-lawzu-lawz	oki-adpcmz	ima-adpcmzms-adpcmzgsm-full-rateTargs	src_arraydecode_out_with_utfreturnc              
   C   sT  dd | D } | d   dkr| dd nd| d< z^tdd|  |du rHtj| tjtjd}| \}}|r?|	d	}|	d	}|j
}n+t|tjrotj| tjtjtjd
}||jjdd\}}|	d	}|j
}ntd|||fW S  ty } ztd| W Y d}~dS d}~w ty } ztd| W Y d}~dS d}~ww )a  Pass an argument list to SoX.

    Parameters
    ----------
    args : iterable
        Argument list for SoX. The first item can, but does not
        need to, be 'sox'.
    src_array : np.ndarray, or None
        If src_array is not None, then we make sure it's a numpy
        array and pass it into stdin.
    decode_out_with_utf : bool, default=True
        Whether or not sox is outputting a bytestring that should be
        decoded with utf-8.

    Returns
    -------
    status : bool
        True on success.
    out : str, np.ndarray, or None
        Returns a np.ndarray if src_array was an np.ndarray.
        Returns the stdout produced by sox if src_array is None.
        Otherwise, returns None if there's an error.
    err : str, or None
        Returns stderr as a string.

    c                 S      g | ]}t |qS  str.0xr   r   >/home/ubuntu/vllm_env/lib/python3.10/site-packages/sox/core.py
<listcomp>9       zsox.<locals>.<listcomp>r   soxExecuting: %s Nstdoutstderrutf-8)stdinr)   r*   F)orderz src_array must be an np.ndarray!zOSError: SoX failed! %sTypeError: %s)r   NN)lowerinsertr   infojoin
subprocessPopenPIPEcommunicatedecode
returncode
isinstancenpndarrayTtobytes	TypeErrorOSErrorerror)r   r   r   process_handleouterrstatus	error_msgr   r   r"   r%      sH   


	
r%   c                   @      e Zd ZdZdd ZdS )SoxErrorz@Exception to be raised when SoX exits with non-zero status.
    c                 O      t j| g|R i | d S N	Exception__init__selfr   kwargsr   r   r"   rM   k      zSoxError.__init__N__name__
__module____qualname____doc__rM   r   r   r   r"   rH   g       rH   c                     sp   t rg S tddg t turt dd  d  fddtt D d }  |  d	d
d }|S )z Calls SoX help for a lists of audio formats available with the current
    install of SoX.

    Returns
    -------
    formats : list
        List of audio file extensions that SoX can process.

    r%   z-hzUTF-8)encoding
c                    s   g | ]
}d  | v r|qS )zAUDIO FILE FORMATS:r   )r    isor   r"   r#      s    z&_get_valid_formats.<locals>.<listcomp>r   r'      N)r   r4   check_outputtyper   splitrangelen)idxformatsr   r[   r"   _get_valid_formatso   s   

re   filepathargumentc              
   C   s   t | } |tvrtd| dddg}|d|  ||  z
tj|tjd}W n tyH } zt	d|j
  td|j d	}~ww |d
}t |dS )a   Base call to SoXI.

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

    argument : str
        Argument to pass to SoXI.

    Returns
    -------
    shell_output : str
        Command line output of SoXI
    zInvalid argument 'z	' to SoXIr%   z--i-)r*   zSoXI error message: zSoXI failed with exit code Nr+   z
)r   	SOXI_ARGS
ValueErrorappendr4   r^   r6   r   r   r2   output	SoxiErrorr9   r8   strip)rf   rg   r   shell_outputcper   r   r"   soxi   s$   


rq   c              
   C   s   dd | D } | d   dkr| dd nd| d< z2tdd|  tj| tjtjd}| }|j	dur=t|j	 |dkrDW d	S td
| W dS  t
ye } ztd| W Y d}~dS d}~w ty} } ztd| W Y d}~dS d}~ww )zPass an argument list to play.

    Parameters
    ----------
    args : iterable
        Argument list for play. The first item can, but does not
        need to, be 'play'.

    Returns
    -------
    status : bool
        True on success.

    c                 S   r   r   r   r   r   r   r"   r#      r$   zplay.<locals>.<listcomp>r   playr&   r'   r(   NTz Play returned with error code %sFzOSError: Play failed! %sr/   )r0   r1   r   r2   r3   r4   r5   r6   waitr*   r@   rA   r?   )r   rB   rE   rF   r   r   r"   rr      s2   

rr   c                   @   rG   )rm   zAException to be raised when SoXI exits with non-zero status.
    c                 O   rI   rJ   rK   rN   r   r   r"   rM      rQ   zSoxiError.__init__NrR   r   r   r   r"   rm      rW   rm   varc                 C   s6   zt |  W dS  ty   Y dS  ty   Y dS w )zCheck if variable is a numeric value.

    Parameters
    ----------
    var : object

    Returns
    -------
    is_number : bool
        True if var is numeric, False otherwise.
    TF)floatrj   r?   )rt   r   r   r"   	is_number   s   rv   list_of_thingsc                 C   s   t t| dkS )zCheck if a list contains identical elements.

    Parameters
    ----------
    list_of_things : list
        list of objects

    Returns
    -------
    all_equal : bool
        True if all list elements are the same.
    r   )rb   set)rw   r   r   r"   	all_equal   s   ry   )NT)$rV   r4   pathlibr   r   typingr   r   r   r   r   r	   numpyr;   typing_extensionsr
    r   logr   ri   ENCODING_VALSEncodingValuer   r<   boolr%   rL   rH   re   VALID_FORMATSrq   rr   rm   rv   ry   r   r   r   r"   <module>   s>     
M'-