o
    ٷi_L                  
   @   s  d Z ddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
 ddl	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dl
mZ ddl
mZ ddlmZ ddlmZ g dZed ZG dd deZdeeeef  defddZdee defddZdee defddZ		d)dee deee  deeee   dee fd d!Z dee d"ee dee fd#d$Z!dee fd%d&Z"dee fd'd(Z#dS )*zT
Python wrapper around the SoX library.
This module requires that SoX is installed.
    )Path)UnionOptionalList)Literal   )core)	file_info)ENCODING_VALSEncodingValue)SoxError)	SoxiErrorVALID_FORMATS	is_number)play)sox)logger)Transformer)concatenatemergemixz	mix-powermultiplyc                       s   e Zd ZdZ fddZ	ddeeef deeef dede	e
e  fd	d
Z	dde
eeef  dede	e
e  fddZ						dde	e
e  de	e
e  de	e
e  de	e
e  de	e
e  de	e
e  fddZ  ZS )CombinerzAudio file combiner.
    Class which allows multiple files to be combined to create an output
    file, saved to output_filepath.

    Inherits all methods from the Transformer class, thus any effects can be
    applied after combining.
    c                    s   t    d S N)super__init__)self	__class__ ?/home/ubuntu/.local/lib/python3.10/site-packages/sox/combine.pyr   )   s   zCombiner.__init__Ninput_filepath_listoutput_filepathcombine_typeinput_volumesc                 C   s  t | t | t| t| t||| j}zt|| W n ty.   t	
d Y nw g }|| j |d|g t||}|| || | j || || j t|\}}	}
|dkrqtd|	 d|
 t	d||d| j |	durt	d	|	  d
S )a  Builds the output_file by executing the current set of commands.

        Parameters
        ----------
        input_filepath_list : list of str
            List of paths to input audio files.
        output_filepath : str
            Path to desired output file. If a file already exists at the given
            path, the file will be overwritten.
        combine_type : str
            Input file combining method. One of the following values:
                * concatenate : combine input files by concatenating in the
                    order given.
                * merge : combine input files by stacking each input file into
                    a new channel of the output file.
                * mix : combine input files by summing samples in corresponding
                    channels.
                * mix-power : combine input files with volume adjustments such
                    that the output volume is roughly equivlent to one of the
                    input signals.
                * multiply : combine input files by multiplying samples in
                    corresponding samples.
        input_volumes : list of float, default=None
            List of volumes to be applied upon combining input files. Volumes
            are applied to the input files in order.
            If None, input files will be combined at their original volumes.

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

        z unable to validate file formats.	--combiner   zStdout: z	
Stderr: z,Created %s with combiner %s and  effects: %s Nz[SoX] T)r	   validate_input_file_listvalidate_output_file_validate_combine_type_validate_volumes_build_input_format_listinput_format_validate_file_formatsr   r   warningextendglobals_build_input_args_output_format_argsoutput_formatappendeffectsr   r   infojoineffects_log)r   r#   r$   r%   r&   input_format_listargs
input_argsstatusouterrr!   r!   r"   build,   sF   
&




zCombiner.buildc                 C   s\   ddg}| | j | d|g t||| j}t||}| | | | j t| dS )a  Play a preview of the output with the current set of effects

        Parameters
        ----------
        input_filepath_list : list of str
            List of paths to input audio files.
        combine_type : str
            Input file combining method. One of the following values:
                * concatenate : combine input files by concatenating in the
                    order given.
                * merge : combine input files by stacking each input file into
                    a new channel of the output file.
                * mix : combine input files by summing samples in corresponding
                    channels.
                * mix-power : combine input files with volume adjustments such
                    that the output volume is roughly equivlent to one of the
                    input signals.
                * multiply : combine input files by multiplying samples in
                    corresponding samples.
        input_volumes : list of float, default=None
            List of volumes to be applied upon combining input files. Volumes
            are applied to the input files in order.
            If None, input files will be combined at their original volumes.

        r   z--no-show-progressr'   N)r1   r2   r-   r.   r3   r7   r   )r   r#   r%   r&   r<   r;   r=   r!   r!   r"   preview}   s   

zCombiner.preview	file_typeratebitschannelsencodingignore_lengthc                 C   s  |durt |tstd|dur"tdd |D s!tdtng }|dur1t |ts1td|durCtdd |D sBtdng }|durRt |tsRtd	|durdtd
d |D sctdng }|durst |tsstd|durtdd |D stdng }|durt |tstd|durtdd |D stdtng }|durt |tstd|durtdd |D stdng }tt|t|t|t|t|t|g}g }t	|D ]}	|
g  qt|D ]\}
}||
 d| g qt|D ]\}
}||
 d| g qt|D ]\}
}||
 d| g qt|D ]\}
}||
 d| g q/t|D ]\}
}||
 d| g qCt|D ]\}
}|du rg||
 
d qW|| _| S )u  Sets input file format arguments. This is primarily useful when
        dealing with audio files without a file extension. Overwrites any
        previously set input file arguments.

        If this function is not explicity called the input format is inferred
        from the file extension or the file's header.

        Parameters
        ----------
        file_type : list of str or None, default=None
            The file type of the input audio file. Should be the same as what
            the file extension would be, for ex. 'mp3' or 'wav'.
        rate : list of float or None, default=None
            The sample rate of the input audio file. If None the sample rate
            is inferred.
        bits : list of int or None, default=None
            The number of bits per sample. If None, the number of bits per
            sample is inferred.
        channels : list of int or None, default=None
            The number of channels in the audio file. If None the number of
            channels is inferred.
        encoding : list of str or None, default=None
            The audio encoding type. Sometimes needed with file-types that
            support more than one encoding type. One of:
                * signed-integer : PCM data stored as signed (‘two’s
                    complement’) integers. Commonly used with a 16 or 24−bit
                    encoding size. A value of 0 represents minimum signal
                    power.
                * unsigned-integer : PCM data stored as unsigned integers.
                    Commonly used with an 8-bit encoding size. A value of 0
                    represents maximum signal power.
                * floating-point : PCM data stored as IEEE 753 single precision
                    (32-bit) or double precision (64-bit) floating-point
                    (‘real’) numbers. A value of 0 represents minimum signal
                    power.
                * a-law : International telephony standard for logarithmic
                    encoding to 8 bits per sample. It has a precision
                    equivalent to roughly 13-bit PCM and is sometimes encoded
                    with reversed bit-ordering.
                * u-law : North American telephony standard for logarithmic
                    encoding to 8 bits per sample. A.k.a. μ-law. It has a
                    precision equivalent to roughly 14-bit PCM and is sometimes
                    encoded with reversed bit-ordering.
                * oki-adpcm : OKI (a.k.a. VOX, Dialogic, or Intel) 4-bit ADPCM;
                    it has a precision equivalent to roughly 12-bit PCM. ADPCM
                    is a form of audio compression that has a good compromise
                    between audio quality and encoding/decoding speed.
                * ima-adpcm : IMA (a.k.a. DVI) 4-bit ADPCM; it has a precision
                    equivalent to roughly 13-bit PCM.
                * ms-adpcm : Microsoft 4-bit ADPCM; it has a precision
                    equivalent to roughly 14-bit PCM.
                * gsm-full-rate : GSM is currently used for the vast majority
                    of the world’s digital wireless telephone calls. It
                    utilises several audio formats with different bit-rates and
                    associated speech quality. SoX has support for GSM’s
                    original 13kbps ‘Full Rate’ audio format. It is usually
                    CPU-intensive to work with GSM audio.
        ignore_length : list of bool or None, default=None
            If True, overrides an (incorrect) audio length given in an audio
            file’s header. If this option is given then SoX will keep reading
            audio until it reaches the end of the input file.

        Nz!file_type must be a list or None.c                 S      g | ]}|t v qS r!   r   .0fr!   r!   r"   
<listcomp>       z-Combiner.set_input_format.<locals>.<listcomp>z$file_type elements must be one of {}zrate must be a list or None.c                 S   s   g | ]
}t |o|d kqS r   r   )rK   rr!   r!   r"   rM      s    z&rate elements must be positive floats.zbits must be a list or None.c                 S      g | ]}t |to|d kqS rO   
isinstanceint)rK   br!   r!   r"   rM         z#bit elements must be positive ints.z channels must be a list or None.c                 S   rQ   rO   rR   )rK   cr!   r!   r"   rM     rV   z'channel elements must be positive ints.z encoding must be a list or None.c                 S   rI   r!   )r
   )rK   er!   r!   r"   rM     rN   z&elements of encoding must be one of {}z%ignore_length must be a list or None.c                 S   s   g | ]}t |tqS r!   )rS   bool)rK   lr!   r!   r"   rM   $  s    z(ignore_length elements must be booleans.z-tz-rz-bz-cz-eTz--ignore-length)rS   list
ValueErrorallformatr   r
   maxlenranger6   	enumerater1   r.   )r   rC   rD   rE   rF   rG   rH   max_input_arg_lenr.   _irL   rP   rU   rW   rX   rZ   r!   r!   r"   set_input_format   s   F
zCombiner.set_input_formatr   )NNNNNN)__name__
__module____qualname____doc__r   r   strr   CombineTyper   r   floatrA   rB   rT   r   rY   rf   __classcell__r!   r!   r   r"   r       sP    



T

+





r   r#   r%   c                 C   s$   t | | |dkrt| | dS dS )zValidate that combine method can be performed with given files.
    Raises IOError if input file formats are incompatible.
    r   N)_validate_sample_rates_validate_num_channels)r#   r%   r!   r!   r"   r/   I  s   
r/   c                 C   *   dd | D }t |std|dS )zA Check if files in input file list have the same sample rate
    c                 S      g | ]}t |qS r!   )r	   sample_raterJ   r!   r!   r"   rM   X      
z*_validate_sample_rates.<locals>.<listcomp>zsInput files do not have the same sample rate. The {} combine type requires that all files have the same sample rateNr   	all_equalOSErrorr^   )r#   r%   sample_ratesr!   r!   r"   ro   T  s   
ro   c                 C   rq   )zH Check if files in input file list have the same number of channels
    c                 S   rr   r!   )r	   rF   rJ   r!   r!   r"   rM   g  rt   z*_validate_num_channels.<locals>.<listcomp>zInput files do not have the same number of channels. The {} combine type requires that all files have the same number of channelsNru   )r#   r%   rF   r!   r!   r"   rp   c  s   
rp   Nr&   r.   returnc                 C   s  t | }g }t|D ]}|g  q
|du rdg| }n8t |}||k r8td||||  |dg||   }n||krMtd||||  |d| }ndd |D }|du rbdd t|D }nDt |}	|	|k rtd|	|||	  d	d |D }|d
d t||	 D  n|	|krtd|	||	|  |d| }ndd |D }tt||D ]\}
\}}||
 d| g ||
 | q|S )a  Set input formats given input_volumes.

    Parameters
    ----------
    input_filepath_list : list of str
        List of input files
    input_volumes : list of float, default=None
        List of volumes to be applied upon combining input files. Volumes
        are applied to the input files in order.
        If None, input files will be combined at their original volumes.
    input_format : list of lists, default=None
        List of input formats to be applied to each input file. Formatting
        arguments are applied to the input files in order.
        If None, the input formats will be inferred from the file header.

    Nr   zkVolumes were only specified for %s out of %s files.The last %s files will remain at their original volumes.z\%s volumes were specified but only %s input files exist.The last %s volumes will be ignored.c                 S      g | ]}|qS r!   r!   )rK   vr!   r!   r"   rM         z,_build_input_format_list.<locals>.<listcomp>c                 S      g | ]}g qS r!   r!   rK   rd   r!   r!   r"   rM     r|   zcInput formats were only specified for %s out of %s files.The last %s files will remain unformatted.c                 S   rz   r!   r!   rJ   r!   r!   r"   rM     r|   c                 S   r}   r!   r!   r~   r!   r!   r"   rM     r|   zc%s Input formats were specified but only %s input files exist. The last %s formats will be ignored.c                 S   rz   r!   r!   rJ   r!   r!   r"   rM     r|   z-v)r`   ra   r6   r   r0   r1   rb   zip)r#   r&   r.   n_inputsr;   rd   vols	n_volumesfmtsn_fmtsre   volfmtr!   r!   r"   r-   s  sR   



r-   r;   c                 C   sZ   dd | D } t |t | krtdg }t| |}|D ]\}}|| || q|S )zY Builds input arguments by stitching input filepaths and input
    formats together.
    c                 S   s   g | ]}t |qS r!   )rk   )rK   xr!   r!   r"   rM     rN   z%_build_input_args.<locals>.<listcomp>z=input_format_list & input_filepath_list are not the same size)r`   r\   r   r1   r6   )r#   r;   r=   zipped
input_file	input_fmtr!   r!   r"   r3     s   

r3   c                 C   s   | t vrtdt dS )zwCheck that the combine_type is valid.

    Parameters
    ----------
    combine_type : str
        Combine type.

    z1Invalid value for combine_type. Must be one of {}N)COMBINE_VALSr\   r^   )r%   r!   r!   r"   r+     s   	r+   c                 C   sN   | du st | tstdt | tr#| D ]}t|s"td|qdS dS )zCheck input_volumes contains a valid list of volumes.

    Parameters
    ----------
    input_volumes : list
        list of volume values. Castable to numbers.

    Nz%input_volumes must be None or a list.z3Elements of input_volumes must be numbers: found {})rS   r[   	TypeErrorr   r   r\   r^   )r&   r   r!   r!   r"   r,     s   	

r,   )NN)$rj   pathlibr   typingr   r   r   typing_extensionsr    r   r	   r
   r   r   r   r   r   r   r   logr   	transformr   r   rl   r   rk   r/   ro   rp   rm   r-   r3   r+   r,   r!   r!   r!   r"   <module>   sZ     +







M
