o
    ٷi                    @   s   d Z ddlZddlZddlmZ ddl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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jdejdejdejdiZ ed Z!G dd dZ"dS )zT
Python wrapper around the SoX library.
This module requires that SoX is installed.
    N)Path)ListOptionalDictUnionTuple)Literal   )	file_info)ENCODING_VALSEncodingValue)SoxError)VALID_FORMATS	is_number)play)sox)logger)r   r	            s16s8f32f64	amplitudepowerdbc                   @   s	  e Zd ZdZdd Z					dAdededed	ed
ef
ddZdd Zdd Z							dBde
e de
e de
e de
e de
e defddZdd Zdd Z							dCde
e de
e de
e de
e de
e de
e d efd!d"Zd#d$ Zd%d& Z						dBd'e
eeef  d(e
eeef  d)e
e d*e
e d+e
ee  d,efd-d.Z						dBd'e
eeef  d(e
eeef  d)e
ej d*e
e d+e
ee  d,efd/d0Z				dDd'e
eeef  d)e
ej d*e
e d+e
ee  fd1d2Zd'eeef fd3d4ZdEd6ed7efd8d9Z	5	dFd6ed7ed:efd;d<Z	5	dFd6ed7ed:efd=d>Z	?	@dGdAed6edBefdCdDZ 	E	FdHdGedHee dIee dJee dKedLefdMdNZ!dOee dPee fdQdRZ"dSefdTdUZ#	V	W					dIdXedYedZed[e
ee  d\e
ee  d]e
ee  d^e
ee  d_e
ee$d`   fdadbZ%dcdddeg dffdgedhediedjee&eef  fdkdlZ'dJdndoZ(			dKdpe
e dSe
e dqe
e fdrdsZ)dLduefdvdwZ*dxdy Z+dzee fd{d|Z,dMd}efd~dZ-dd Z.dddVddgdgfdXedYeded[ee d\ee f
ddZ/dddVddgdgfdXedYeded[ee d\ee f
ddZ0d6ed7edAefddZ1	t	t	dNdedede$d fddZ2dee fddZ3		@		E	dOdededededede$d dede$d fddZ4	t			dPdAededede
e$d  fddZ5		dQd6ed7edefddZ6		dQd6ed7edefddZ7dRde
e fddZ8dSdAedefddZ9ddgddgddgdedgg dg dgddgfdedee dgee dhee diee
e  djeee&eef   dee
e  fddńZ:d'eeef deeef fddȄZ;dTdeeef defdd˄Z<dUdefddτZ=ddф Z>dVdAedefddՄZ?dWdedefddلZ@		@	ېdXdXedYedededede$d fddZAdYdedefddZB	dZdpede$d fddZC		d[de
eDeee f  de
e fddZEd\defddZF							d]dededededededefddZGdd ZH				d^de$d d ededefddZI					d_de$d	 d
eeee f dede
eeee f  de
e f
ddZJd}efddZK		d`d'eeef de
e de
e fddZLd'eeef fddZMd'eeef fddZNdad}edefddZOdd ZP		d`d}ed e
e$d!  defd"d#ZQ	$	@dbdAed6edBefd%d&ZRdcdedefd(d)ZSdRd*ed+e
e fd,d-ZTdMd}efd.d/ZU			0	1	2	1	tddde$d3 ded4ed5ed6ed7ed8efd9d:ZV	;	deded<e$d= d>e
e fd?d@ZWdS (f  Transformera  Audio file transformer.
    Class which allows multiple effects to be chained to create an output
    file, saved to output_filepath.



    Methods
    -------
    set_globals
        Overwrite the default global arguments.
    build
        Execute the current chain of commands to create an output file.
    build_file
        Alias of build.
    build_array
        Execute the current chain of commands to create an output array.

    c                 C   s*   i | _ i | _g | _g | _g | _|   dS )a  
        Attributes
        ----------
        input_format : list of str
            Input file format arguments that will be passed to SoX.
        output_format : list of str
            Output file format arguments that will be bassed to SoX.
        effects : list of str
            Effects arguments that will be passed to SoX.
        effects_log : list of str
            Ordered sequence of effects applied.
        globals : list of str
            Global arguments that will be passed to SoX.

        N)input_formatoutput_formateffectseffects_logglobalsset_globalsself r(   A/home/ubuntu/.local/lib/python3.10/site-packages/sox/transform.py__init__9   s   zTransformer.__init__Fr   ditherguardmultithreadreplay_gain	verbosityc                 C   s   t |ts	tdt |tstdt |tstdt |ts$td|tvr/tdtg }|s8|d |r?|d |rF|d |rR|d	 |d
 |d|  || _| S )u  Sets SoX's global arguments.
        Overwrites any previously set global arguments.
        If this function is not explicity called, globals are set to this
        function's defaults.

        Parameters
        ----------
        dither : bool, default=False
            If True, dithering is applied for low files with low bit rates.
        guard : bool, default=False
            If True, invokes the gain effect to guard against clipping.
        multithread : bool, default=False
            If True, each channel is processed in parallel.
        replay_gain : bool, default=False
            If True, applies replay-gain adjustment to input-files.
        verbosity : int, default=2
            SoX's verbosity level. One of:
                * 0 : No messages are shown at all
                * 1 : Only error messages are shown. These are generated if SoX
                    cannot complete the requested commands.
                * 2 : Warning messages are also shown. These are generated if
                    SoX can complete the requested commands, but not exactly
                    according to the requested command parameters, or if
                    clipping occurs.
                * 3 : Descriptions of SoX’s processing phases are also shown.
                    Useful for seeing exactly how SoX is processing your audio.
                * 4, >4 : Messages to help with debugging SoX are also shown.

        zdither must be a boolean.zguard must be a boolean.zmultithread must be a boolean.zreplay_gain must be a boolean.z+Invalid value for VERBOSITY. Must be one {}z-Dz-Gz--multi-threadedz--replay-gaintrackz-V)
isinstancebool
ValueErrorVERBOSITY_VALSformatappendr$   )r'   r+   r,   r-   r.   r/   global_argsr(   r(   r)   r%   R   s6   
"







zTransformer.set_globalsc                 C   s"  | d}| d}| d}| d}| d}| dd}|tdg vr-td	t t|s9|dur9td
|durE|dkrEtdt|tsR|durRtd|dur^|dkr^tdt|tsk|durktd|durw|dkrwtd|tdg vrtd|tt|tstddS )=Private helper function for validating input formats
        	file_typeratebitschannelsencodingignore_lengthFN"Invalid file_type. Must be one of rate must be a float or Noner   rate must be a positive numberbits must be an int or Nonebits must be a positive numberchannels must be an int or None"channels must be a positive numberz&Invalid encoding {}. Must be one of {}zignore_length must be a boolean)	getr   r3   r   r1   intr   r5   r2   )r'   r    r9   r:   r;   r<   r=   r>   r(   r(   r)   _validate_input_format   s>   





z"Transformer._validate_input_formatc           	      C   s   |  | |d}|d}|d}|d}|d}|dd}g }|dur2|d	| g |dur?|d
|dg |durK|d| g |durW|d| g |durc|d| g |rj|d |S )z5Private helper function for set_input_format
        r9   r:   r;   r<   r=   r>   FN-t-rf-b-c-ez--ignore-length)rH   rF   extendr6   )	r'   r    r9   r:   r;   r<   r=   r>   input_format_argsr(   r(   r)   _input_format_args   s*   






zTransformer._input_format_argsNr9   r:   r;   r<   r=   r>   c                 C   s&   ||||||d}|  | || _d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 : 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 : float or None, default=None
            The sample rate of the input audio file. If None the sample rate
            is inferred.
        bits : int or None, default=None
            The number of bits per sample. If None, the number of bits per
            sample is inferred.
        channels : int or None, default=None
            The number of channels in the audio file. If None the number of
            channels is inferred.
        encoding : 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 : bool, default=False
            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.
        r9   r:   r;   r<   r=   r>   N)rH   r    )r'   r9   r:   r;   r<   r=   r>   r    r(   r(   r)   set_input_format   s   F

zTransformer.set_input_formatc           	      C   sD  | d}| d}| d}| d}| d}| d}| dd}|td	g vr2td
t t|s>|d	ur>td|d	urJ|dkrJtdt|tsW|d	urWtd|d	urc|dkrctdt|tsp|d	urptd|d	ur||dkr|td|td	g vrtdt |d	urt|tstdt|tstdd	S )r8   r9   r:   r;   r<   r=   commentsappend_commentsTNr?   r@   r   rA   rB   rC   rD   rE   z!Invalid encoding. Must be one of z!comments must be a string or Nonez!append_comments must be a boolean)	rF   r   r3   r   r1   rG   r   strr2   )	r'   r!   r9   r:   r;   r<   r=   rT   rU   r(   r(   r)   _validate_output_format6  s@   






z#Transformer._validate_output_formatc           
      C   s   |  | |d}|d}|d}|d}|d}|d}|dd}g }	|d	ur7|	d
| g |d	urD|	d|dg |d	urP|	d| g |d	ur\|	d| g |d	urh|	d| g |d	ur~|rw|	d|g |	S |	d|g |	S )z6Private helper function for set_output_format
        r9   r:   r;   r<   r=   rT   rU   TNrI   rJ   rK   rL   rM   rN   z--add-commentz	--comment)rW   rF   rO   )
r'   r!   r9   r:   r;   r<   r=   rT   rU   output_format_argsr(   r(   r)   _output_format_argsc  s2   






zTransformer._output_format_argsTrT   rU   c           	      C   s(   |||||||d}|  | || _dS )u  Sets output file format arguments. These arguments will overwrite
        any format related arguments supplied by other effects (e.g. rate).

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

        Parameters
        ----------
        file_type : str or None, default=None
            The file type of the output audio file. Should be the same as what
            the file extension would be, for ex. 'mp3' or 'wav'.
        rate : float or None, default=None
            The sample rate of the output audio file. If None the sample rate
            is inferred.
        bits : int or None, default=None
            The number of bits per sample. If None, the number of bits per
            sample is inferred.
        channels : int or None, default=None
            The number of channels in the audio file. If None the number of
            channels is inferred.
        encoding : 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.
        comments : str or None, default=None
            If not None, the string is added as a comment in the header of the
            output audio file. If None, no comments are added.
        append_comments : bool, default=True
            If True, comment strings are appended to SoX's default comments. If
            False, the supplied comment replaces the existing comment.
        r9   r:   r;   r<   r=   rT   rU   N)rW   r!   )	r'   r9   r:   r;   r<   r=   rT   rU   r!   r(   r(   r)   set_output_format  s   H
	
zTransformer.set_output_formatc                 C   s   t  | _t  | _| S )z&Remove all effects processes.
        )listr"   r#   r&   r(   r(   r)   clear_effects  s   zTransformer.clear_effectsc                 C   s   |dur|durt d|dur*t| | j}|ddu r&t||d< ||fS |dur_t|tjs8t	d|du r@t dd}t
|jj |dt|jdkrU|jd nddd	d
}||fS t d)aM  Private helper function for parsing inputs to build and build_array

        Parameters
        ----------
        input_filepath : str or None
            Either path to input audio file or None.
        input_array : np.ndarray or None
            A np.ndarray of an waveform with shape (n_samples, n_channels)
            or None
        sample_rate_in : int or None
            Sample rate of input_array or None

        Returns
        -------
        input_format : dict
            Input format dictionary
        input_filepath : str
            Formatted input filepath.
        Nz;Only one of input_filepath and input_array may be specifiedr<   z)input_array must be a numpy array or Nonez1sample_rate_in must be specified for array inputs-r	   FrR   z6One of input_filepath or input_array must be specified)r3   r
   validate_input_filer    rF   r<   r1   npndarray	TypeErrorENCODINGS_MAPPINGdtypetypelenshape)r'   input_filepathinput_arraysample_rate_inr    r(   r(   r)   _parse_inputs  s:   

zTransformer._parse_inputsri   output_filepathrj   rk   
extra_argsreturn_outputc                 C   s  |  |||\}}|du rtd||krtdt| g }|| j || | || || | j	 || || j
 |durYt|tsTtd|| t||d\}	}
}|	dkrptd|
 d| td	|d
| j |r|	|
|fS dS )a
  Given an input file or array, creates an output_file on disk by
        executing the current set of commands. This function returns True on
        success. If return_output is True, this function returns a triple of
        (status, out, err), giving the success state, along with stdout and
        stderr returned by sox.

        Parameters
        ----------
        input_filepath : str or None
            Either path to input audio file or None for array input.
        output_filepath : str
            Path to desired output file. If a file already exists at
            the given path, the file will be overwritten.
            If '-n', no file is created.
        input_array : np.ndarray or None
            An np.ndarray of an waveform with shape (n_samples, n_channels).
            sample_rate_in must also be provided.
            If None, input_filepath must be specified.
        sample_rate_in : int
            Sample rate of input_array.
            This argument is ignored if input_array is None.
        extra_args : list or None, default=None
            If a list is given, these additional arguments are passed to SoX
            at the end of the list of effects.
            Don't use this argument unless you know exactly what you're doing!
        return_output : bool, default=False
            If True, returns the status and information sent to stderr and
            stdout as a tuple (status, stdout, stderr).
            If output_filepath is None, return_output=True by default.
            If False, returns True on success.

        Returns
        -------
        status : bool
            True on success.
        out : str (optional)
            This is not returned unless return_output is True.
            When returned, captures the stdout produced by sox.
        err : str (optional)
            This is not returned unless return_output is True.
            When returned, captures the stderr produced by sox.

        Examples
        --------
        >>> import numpy as np
        >>> import sox
        >>> tfm = sox.Transformer()
        >>> sample_rate = 44100
        >>> y = np.sin(2 * np.pi * 440.0 * np.arange(sample_rate * 1.0) / sample_rate)

        file in, file out - basic usage

        >>> status = tfm.build('path/to/input.wav', 'path/to/output.mp3')

        file in, file out - equivalent usage

        >>> status = tfm.build(
                input_filepath='path/to/input.wav',
                output_filepath='path/to/output.mp3'
            )

        array in, file out

        >>> status = tfm.build(
                input_array=y, sample_rate_in=sample_rate,
                output_filepath='path/to/output.mp3'
            )

        Nz!output_filepath is not specified!z6input_filepath must be different from output_filepath.extra_args must be a list.Tr   Stdout: 	
Stderr: zCreated %s with effects: %s )rl   r3   r
   validate_output_filerO   r$   rQ   r6   rY   r!   r"   r1   r\   r   r   r   infojoinr#   )r'   ri   rm   rj   rk   rn   ro   r    argsstatusouterrr(   r(   r)   build  sD   L






zTransformer.buildc                 C   s   |  ||||||S )a
  An alias for build.
        Given an input file or array, creates an output_file on disk by
        executing the current set of commands. This function returns True on
        success. If return_output is True, this function returns a triple of
        (status, out, err), giving the success state, along with stdout and
        stderr returned by sox.

        Parameters
        ----------
        input_filepath : str or None
            Either path to input audio file or None for array input.
        output_filepath : str
            Path to desired output file. If a file already exists at
            the given path, the file will be overwritten.
            If '-n', no file is created.
        input_array : np.ndarray or None
            An np.ndarray of an waveform with shape (n_samples, n_channels).
            sample_rate_in must also be provided.
            If None, input_filepath must be specified.
        sample_rate_in : int
            Sample rate of input_array.
            This argument is ignored if input_array is None.
        extra_args : list or None, default=None
            If a list is given, these additional arguments are passed to SoX
            at the end of the list of effects.
            Don't use this argument unless you know exactly what you're doing!
        return_output : bool, default=False
            If True, returns the status and information sent to stderr and
            stdout as a tuple (status, stdout, stderr).
            If output_filepath is None, return_output=True by default.
            If False, returns True on success.

        Returns
        -------
        status : bool
            True on success.
        out : str (optional)
            This is not returned unless return_output is True.
            When returned, captures the stdout produced by sox.
        err : str (optional)
            This is not returned unless return_output is True.
            When returned, captures the stderr produced by sox.

        Examples
        --------
        >>> import numpy as np
        >>> import sox
        >>> tfm = sox.Transformer()
        >>> sample_rate = 44100
        >>> y = np.sin(2 * np.pi * 440.0 * np.arange(sample_rate * 1.0) / sample_rate)

        file in, file out - basic usage

        >>> status = tfm.build('path/to/input.wav', 'path/to/output.mp3')

        file in, file out - equivalent usage

        >>> status = tfm.build(
                input_filepath='path/to/input.wav',
                output_filepath='path/to/output.mp3'
            )

        array in, file out

        >>> status = tfm.build(
                input_array=y, sample_rate_in=sample_rate,
                output_filepath='path/to/output.mp3'
            )

        )r{   )r'   ri   rm   rj   rk   rn   ro   r(   r(   r)   
build_file  s   MzTransformer.build_filec                    s<  |  |||\ }g d}t|t| j@ rtd d} ddu r(tj}n fddt	 D d }t
|jd	 }d
|| d dddd}	| jddurX| jd |	d< | jddurg| jd |	d< | jddurx| jd }||	d< |d	krtj}n|dkrtj}n|dkrtj}n|dkrtj}ntd| g }
|
| j |
|   |
| |
| |	 |
| |
| j |durt|tstd|
| t|
|d\}}}|dkrtd| d| tj||d}|	d dkr|j|	d tt||	d  fddj}t dd!| j |S )a\  Given an input file or array, returns the ouput as a numpy array
        by executing the current set of commands. By default the array will
        have the same sample rate as the input file unless otherwise specified
        using set_output_format. Functions such as rate, channels and convert
        will be ignored!

        Parameters
        ----------
        input_filepath : str or None
            Either path to input audio file or None.
        input_array : np.ndarray or None
            A np.ndarray of an waveform with shape (n_samples, n_channels).
            If this argument is passed, sample_rate_in must also be provided.
            If None, input_filepath must be specified.
        sample_rate_in : int
            Sample rate of input_array.
            This argument is ignored if input_array is None.
        extra_args : list or None, default=None
            If a list is given, these additional arguments are passed to SoX
            at the end of the list of effects.
            Don't use this argument unless you know exactly what you're doing!

        Returns
        -------
        output_array : np.ndarray
            Output audio as a numpy array

        Examples
        --------

        >>> import numpy as np
        >>> import sox
        >>> tfm = sox.Transformer()
        >>> sample_rate = 44100
        >>> y = np.sin(2 * np.pi * 440.0 * np.arange(sample_rate * 1.0) / sample_rate)

        file in, array out

        >>> output_array = tfm.build(input_filepath='path/to/input.wav')

        array in, array out

        >>> output_array = tfm.build(input_array=y, sample_rate_in=sample_rate)

        specifying the output sample rate

        >>> tfm.set_output_format(rate=8000)
        >>> output_array = tfm.build(input_array=y, sample_rate_in=sample_rate)

        if an effect changes the number of channels, you must explicitly
        specify the number of output channels

        >>> tfm.remix(remix_dictionary={1: [1], 2: [1], 3: [1]})
        >>> tfm.set_output_format(channels=3)
        >>> output_array = tfm.build(input_array=y, sample_rate_in=sample_rate)


        )r:   r<   convertzWhen outputting to an array, rate, channels and convert effects may be ignored. Use set_output_format() to specify output formats.r^   r9   Nc                    s    g | ]\}} d  |kr|qS )r9   r(   ).0kvr    r(   r)   
<listcomp>4  s
    z+Transformer.build_array.<locals>.<listcomp>r      rawr<   TrZ   r:   r;          @   zinvalid n_bits rp   Frq   rr   )re   r	   F)orderzCreated array with effects: %srs   )"rl   setr#   r   warningrF   ra   int16rd   itemsre   itemsizer!   int8float32float64r3   rO   r$   rQ   r6   rY   r"   r1   r\   r   r   
frombufferreshaperG   rg   Tru   rv   )r'   ri   rj   rk   rn   ignored_commandsrm   encoding_outn_bitsr!   rw   rx   ry   rz   r(   r   r)   build_array  s   ?







zTransformer.build_arrayc                 C   sB   ddg}| | j | | j || | | j t| dS )zPlay a preview of the output with the current set of effects

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

        r   z--no-show-progressN)rO   r$   r    r6   r"   r   )r'   ri   rw   r(   r(   r)   preview|  s   	
zTransformer.preview       @	frequencywidth_qc                 C   sb   t |r|dkrtdt |r|dkrtdd|d|ddg}| j| | jd | S )u1  Apply a two-pole all-pass filter. An all-pass filter changes the
        audio’s frequency to phase relationship without changing its frequency
        to amplitude relationship. The filter is described in detail in at
        http://musicdsp.org/files/Audio-EQ-Cookbook.txt

        Parameters
        ----------
        frequency : float
            The filter's center frequency in Hz.
        width_q : float, default=2.0
            The filter's width as a Q-factor.

        See Also
        --------
        equalizer, highpass, lowpass, sinc

        r   $frequency must be a positive number."width_q must be a positive number.allpassrK   qr   r3   r"   rO   r#   r6   )r'   r   r   effect_argsr(   r(   r)   r     s   zTransformer.allpassconstant_skirtc                 C      t |r|dkrtdt |r|dkrtdt|ts!tddg}|r+|d ||d|ddg | j| | jd | S )	a  Apply a two-pole Butterworth band-pass filter with the given central
        frequency, and (3dB-point) band-width. The filter rolls off at 6dB per
        octave (20dB per decade) and is described in detail in
        http://musicdsp.org/files/Audio-EQ-Cookbook.txt

        Parameters
        ----------
        frequency : float
            The filter's center frequency in Hz.
        width_q : float, default=2.0
            The filter's width as a Q-factor.
        constant_skirt : bool, default=False
            If True, selects constant skirt gain (peak gain = width_q).
            If False, selects constant 0dB peak gain.

        See Also
        --------
        bandreject, sinc

        r   r   r   !constant_skirt must be a boolean.bandpassrM   rK   r   r   r3   r1   r2   r6   rO   r"   r#   r'   r   r   r   r   r(   r(   r)   r        

zTransformer.bandpassc                 C   r   )	a  Apply a two-pole Butterworth band-reject filter with the given
        central frequency, and (3dB-point) band-width. The filter rolls off at
        6dB per octave (20dB per decade) and is described in detail in
        http://musicdsp.org/files/Audio-EQ-Cookbook.txt

        Parameters
        ----------
        frequency : float
            The filter's center frequency in Hz.
        width_q : float, default=2.0
            The filter's width as a Q-factor.
        constant_skirt : bool, default=False
            If True, selects constant skirt gain (peak gain = width_q).
            If False, selects constant 0dB peak gain.

        See Also
        --------
        bandreject, sinc

        r   r   r   r   
bandrejectrM   rK   r   r   r   r(   r(   r)   r     r   zTransformer.bandreject      Y@      ?gain_dbslopec                 C      t |stdt |r|dkrtdt |r |dks |dkr$tdd|d|d|ddg}| j| | jd | S )	u&  Boost or cut the bass (lower) frequencies of the audio using a
        two-pole shelving filter with a response similar to that of a standard
        hi-fi’s tone-controls. This is also known as shelving equalisation.

        The filters are described in detail in
        http://musicdsp.org/files/Audio-EQ-Cookbook.txt

        Parameters
        ----------
        gain_db : float
            The gain at 0 Hz.
            For a large cut use -20, for a large boost use 20.
        frequency : float, default=100.0
            The filter's cutoff frequency in Hz.
        slope : float, default=0.5
            The steepness of the filter's shelf transition.
            For a gentle slope use 0.3, and use 1.0 for a steep slope.

        See Also
        --------
        treble, equalizer

        gain_db must be a numberr   r         ?r   bassrK   sr   r'   r   r   r   r   r(   r(   r)   r     s   
zTransformer.bass   r   n_bendsstart_times	end_timescents
frame_rateoversample_ratec              	   C   s  t |tr	|dk rtdt |trt||krtdtdd |D r)tdt||kr3tdt |tr>t||krBtdtd	d |D rOtd
t||krYtdtdd t||D ritdtdd t|dd |dd D rtdt |trt||krtdtdd |D rtdt |tr|dk s|dkrtdt |tr|dk s|dkrtddd| d| g}d}t|D ],}	t	||	 | d}
t	||	 ||	  d}|
|
d d!||	 d d!|d  ||	 }q| j| | j
d | S )"a  Changes pitch by specified amounts at specified times.
        The pitch-bending algorithm utilises the Discrete Fourier Transform
        (DFT) at a particular frame rate and over-sampling rate.

        Parameters
        ----------
        n_bends : int
            The number of intervals to pitch shift
        start_times : list of floats
            A list of absolute start times (in seconds), in order
        end_times : list of floats
            A list of absolute end times (in seconds) in order.
            [start_time, end_time] intervals may not overlap!
        cents : list of floats
            A list of pitch shifts in cents. A positive value shifts the pitch
            up, a negative value shifts the pitch down.
        frame_rate : int, default=25
            The number of DFT frames to process per second, between 10 and 80
        oversample_rate: int, default=16
            The number of frames to over sample per second, between 4 and 32

        See Also
        --------
        pitch

        r	   z#n_bends must be a positive integer.z-start_times must be a list of length n_bends.c                 S      g | ]}t | p|d kqS r   r   r~   pr(   r(   r)   r   T      z$Transformer.bend.<locals>.<listcomp>z$start_times must be positive floats.z(start_times must be in increasing order.z+end_times must be a list of length n_bends.c                 S   r   r   r   r   r(   r(   r)   r   ]  r   z"end_times must be positive floats.z&end_times must be in increasing order.c                 S   s   g | ]\}}||kqS r(   r(   r~   r   er(   r(   r)   r   c      z8end_times must be element-wise greater than start_times.c                 S   s   g | ]\}}||kqS r(   r(   r   r(   r(   r)   r   h  r   Nr_   z9[start_time, end_time] intervals must be non-overlapping.z'cents must be a list of length n_bends.c                 S   s   g | ]}t | qS r(   r   r   r(   r(   r)   r   p      z!elements of cents must be floats.
   P   z/frame_rate must be an integer between 10 and 80r   r   z4oversample_rate must be an integer between 4 and 32.bendz-fz-or   r   rK   ,)r1   rG   r3   r\   rg   anysortedziprangeroundr6   r"   rO   r#   )r'   r   r   r   r   r   r   r   lastit_startt_endr(   r(   r)   r   -  sb   !(


zTransformer.bendbac                 C   s   t |ts	tdt |tstdt|dkrtdt|dkr&tdtdd |D s3tdtd	d |D s@td
d|d d|d d|d d|d d|d d|d dg}| j| | jd | S )aQ  Apply a biquad IIR filter with the given coefficients.

        Parameters
        ----------
        b : list of floats
            Numerator coefficients. Must be length 3
        a : list of floats
            Denominator coefficients. Must be length 3

        See Also
        --------
        fir, treble, bass, equalizer

        zb must be a list.za must be a list.r   zb must be a length 3 list.za must be a length 3 list.c                 S      g | ]}t |qS r(   r   )r~   b_valr(   r(   r)   r         z&Transformer.biquad.<locals>.<listcomp>z"all elements of b must be numbers.c                 S   r   r(   r   )r~   a_valr(   r(   r)   r     r   z"all elements of a must be numbers.biquadr   rK   r	   r   )	r1   r\   r3   rg   allr"   rO   r#   r6   )r'   r   r   r   r(   r(   r)   r     s&   

zTransformer.biquad
n_channelsc                 C   s@   t |tr	|dkrtdd| g}| j| | jd | S )a  Change the number of channels in the audio signal. If decreasing the
        number of channels it mixes channels together, if increasing the number
        of channels it duplicates.

        Note: This overrides arguments used in the convert effect!

        Parameters
        ----------
        n_channels : int
            Desired number of channels.

        See Also
        --------
        convert

        r   &n_channels must be a positive integer.r<   r1   rG   r3   r"   rO   r#   r6   )r'   r   r   r(   r(   r)   r<     s   
zTransformer.channels?r   gain_ingain_outn_voicesdelaysdecaysspeedsdepthsshapes)r   tc	              
   C   s  t |r|dks|dkrtdt |r|dks|dkr tdt|tr)|dkr-td|du s:t|ts:td|durVt||krHtdtd	d
 |D rUtdn	dd t|D }|du slt|tsltd|durt||krztdtdd
 |D rtdn	dd t|D }|du st|tstd|durt||krtdtdd
 |D rtdn	dd t|D }|du st|tstd|durt||krtdtdd
 |D rtdn	dd t|D }|du st|tstd|dur#t||krtdtdd
 |D r"td n	d!d t|D }d"| | g}	t|D ]"}
|	||
 d#||
 d#||
 d#||
 d#d$||
  g q7| j	|	 | j
d" | S )%a}	  Add a chorus effect to the audio. This can makeasingle vocal sound
        like a chorus, but can also be applied to instrumentation.

        Chorus resembles an echo effect with a short delay, but whereas with
        echo the delay is constant, with chorus, it is varied using sinusoidal
        or triangular modulation. The modulation depth defines the range the
        modulated delay is played before or after the delay. Hence the delayed
        sound will sound slower or faster, that is the delayed sound tuned
        around the original one, like in a chorus where some vocals are
        slightly off key.

        Parameters
        ----------
        gain_in : float, default=0.3
            The time in seconds over which the instantaneous level of the input
            signal is averaged to determine increases in volume.
        gain_out : float, default=0.8
            The time in seconds over which the instantaneous level of the input
            signal is averaged to determine decreases in volume.
        n_voices : int, default=3
            The number of voices in the chorus effect.
        delays : list of floats > 20 or None, default=None
            If a list, the list of delays (in miliseconds) of length n_voices.
            If None, the individual delay parameters are chosen automatically
            to be between 40 and 60 miliseconds.
        decays : list of floats or None, default=None
            If a list, the list of decays (as a fraction of gain_in) of length
            n_voices.
            If None, the individual decay parameters are chosen automatically
            to be between 0.3 and 0.4.
        speeds : list of floats or None, default=None
            If a list, the list of modulation speeds (in Hz) of length n_voices
            If None, the individual speed parameters are chosen automatically
            to be between 0.25 and 0.4 Hz.
        depths : list of floats or None, default=None
            If a list, the list of depths (in miliseconds) of length n_voices.
            If None, the individual delay parameters are chosen automatically
            to be between 1 and 3 miliseconds.
        shapes : list of 's' or 't' or None, default=None
            If a list, the list of modulation shapes - 's' for sinusoidal or
            't' for triangular - of length n_voices.
            If None, the individual shapes are chosen automatically.

        r   r	   )gain_in must be a number between 0 and 1.*gain_out must be a number between 0 and 1.z$n_voices must be a positive integer.Nzdelays must be a list or Nonez(the length of delays must equal n_voicesc                 s   s"    | ]}t | p|d k V  qdS )   Nr   r   r(   r(   r)   	<genexpr>       z%Transformer.chorus.<locals>.<genexpr>z+the elements of delays must be numbers > 20c                 S      g | ]}t d dqS )(   <   randomuniformr~   _r(   r(   r)   r     r   z&Transformer.chorus.<locals>.<listcomp>zdecays must be a list or Nonez(the length of decays must equal n_voicesc                 s   *    | ]}t | p|d kp|dkV  qdS r   r	   Nr   r   r(   r(   r)   r   !     ( .the elements of decays must be between 0 and 1c                 S   r   )333333?皙?r   r   r(   r(   r)   r   &  r   zspeeds must be a list or Nonez(the length of speeds must equal n_voicesc                 s   "    | ]}t | p|d kV  qdS r   Nr   r   r(   r(   r)   r   .  r   z*the elements of speeds must be numbers > 0c                 S   r   )      ?r   r   r   r(   r(   r)   r   1  r   zdepths must be a list or Nonez(the length of depths must equal n_voicesc                 s   r   r   r   r   r(   r(   r)   r   9  r   z*the elements of depths must be numbers > 0c                 S   r   )r   g      @r   r   r(   r(   r)   r   <  r   zshapes must be a list or Nonez(the length of shapes must equal n_voicesc                 s   s    | ]}|d vV  qdS )r   r   Nr(   r   r(   r(   r)   r   D  s    z)the elements of shapes must be 's' or 't'c                 S   s   g | ]	}t d dgqS r   )r   choicer   r(   r(   r)   r   G  s    chorusrK   r^   r   r3   r1   rG   r\   rg   r   r   rO   r"   r#   r6   )r'   r   r   r   r   r   r   r   r   r   r   r(   r(   r)   r     s   4





zTransformer.chorusr   皙?      @))r  )iir   r   attack_time
decay_timesoft_knee_db	tf_pointsc                 C   s  t |r|dkrtdt |r|dkrtd||kr!td t |s-|du s-tdt|ts6tdt|dkr@tdtd	d
 |D rMtdtdd
 |D rZtdtdd
 |D rgtdtdd
 |D rttdt|tdd |D krtdt	|dd d}g }|D ]}|
|d d|d dg qd|dd|dg}|dur|d|d| n|d| | j
| | jd | S )a  Compand (compress or expand) the dynamic range of the audio.

        Parameters
        ----------
        attack_time : float, default=0.3
            The time in seconds over which the instantaneous level of the input
            signal is averaged to determine increases in volume.
        decay_time : float, default=0.8
            The time in seconds over which the instantaneous level of the input
            signal is averaged to determine decreases in volume.
        soft_knee_db : float or None, default=6.0
            The ammount (in dB) for which the points at where adjacent line
            segments on the transfer function meet will be rounded.
            If None, no soft_knee is applied.
        tf_points : list of tuples
            Transfer function points as a list of tuples corresponding to
            points in (dB, dB) defining the compander's transfer function.

        See Also
        --------
        mcompand, contrast
        r   z&attack_time must be a positive number.z%decay_time must be a positive number.zattack_time is larger than decay_time.
For most situations, attack_time should be shorter than decay time because the human ear is more sensitive to sudden loud music than sudden soft music.Nz&soft_knee_db must be a number or None.ztf_points must be a list.z'tf_points must have at least one point.c                 s       | ]	}t |t V  qd S Nr1   tupler~   pairr(   r(   r)   r         z&Transformer.compand.<locals>.<genexpr>z#elements of tf_points must be pairsc                 s       | ]	}t |d kV  qdS r   Nrg   r  r(   r(   r)   r     r  z$Tuples in tf_points must be length 2c                 s   *    | ]}t |d  ot |d  V  qdS r   r   r   r(   r(   r)   r     r   z-Tuples in tf_points must be pairs of numbers.c                 s   (    | ]}|d  d kp|d d kV  qdS r   r(   r   r(   r(   r)   r        & z,Tuple values in tf_points must be <= 0 (dB).c                 S      h | ]}|d  qS r   r(   r   r(   r(   r)   	<setcomp>  r   z&Transformer.compand.<locals>.<setcomp>z%Found duplicate x-value in tf_points.c                 S      | d S Nr   r(   )r  r(   r(   r)   <lambda>      z%Transformer.compand.<locals>.<lambda>keyrK   r	   compandr   {:f}:{})r   r3   r   r   r1   r\   rc   rg   r   r   rO   r6   r5   rv   r"   r#   )r'   r  r  r  r  transfer_listpointr   r(   r(   r)   r  X  sX   
zTransformer.compandK   c                 C   H   t |r|dk s|dkrtdd|dg}| j| | jd | S )a  Comparable with compression, this effect modifies an audio signal to
        make it sound louder.

        Parameters
        ----------
        amount : float
            Amount of enhancement between 0 and 100.

        See Also
        --------
        compand, mcompand

        r   d   z*amount must be a number between 0 and 100.contrastrK   r   )r'   amountr   r(   r(   r)   r%    s   zTransformer.contrast
sampleratebitdepthc                 C   s   g d}|dur||vrt dt| d|| jd< |dur1t|tr(|dkr,t d|| jd< |durFt|r=|dkrAt d	| | | S )
a  Converts output audio to the specified format.

        Parameters
        ----------
        samplerate : float, default=None
            Desired samplerate. If None, defaults to the same as input.
        n_channels : int, default=None
            Desired number of channels. If None, defaults to the same as input.
        bitdepth : int, default=None
            Desired bitdepth. If None, defaults to the same as input.

        See Also
        --------
        rate

        )r   r      r   r   Nzbitdepth must be one of .r;   r   r   r<   z%samplerate must be a positive number.)r3   rV   r!   r1   rG   r   r:   )r'   r'  r   r(  	bitdepthsr(   r(   r)   r}     s$   


zTransformer.convert        shiftc                 C   r#  )zApply a DC shift to the audio.

        Parameters
        ----------
        shift : float
            Amount to shift audio between -2 and 2. (Audio is between -1 and 1)

        See Also
        --------
        highpass

        r   z(shift must be a number between -2 and 2.dcshiftrK   r   )r'   r-  r   r(   r(   r)   r/    s   zTransformer.dcshiftc                 C   "   dg}| j | | jd | S )a  Apply Compact Disc (IEC 60908) de-emphasis (a treble attenuation
        shelving filter). Pre-emphasis was applied in the mastering of some
        CDs issued in the early 1980s. These included many classical music
        albums, as well as now sought-after issues of albums by The Beatles,
        Pink Floyd and others. Pre-emphasis should be removed at playback time
        by a de-emphasis filter in the playback device. However, not all modern
        CD players have this filter, and very few PC CD drives have it; playing
        pre-emphasised audio without the correct de-emphasis filter results in
        audio that sounds harsh and is far from what its creators intended.

        The de-emphasis filter is implemented as a biquad and requires the
        input audio sample rate to be either 44.1kHz or 48kHz. Maximum
        deviation from the ideal response is only 0.06dB (up to 20kHz).

        See Also
        --------
        bass, treble
        deemphr"   rO   r#   r6   r'   r   r(   r(   r)   r1    s   zTransformer.deemph	positionsc                 C   sb   t |ts	tdtdd |D stddg}|dd |D  | j| | jd | S )ai  Delay one or more audio channels such that they start at the given
        positions.

        Parameters
        ----------
        positions: list of floats
            List of times (in seconds) to delay each audio channel.
            If fewer positions are given than the number of channels, the
            remaining channels will be unaffected.

        z%positions must be a a list of numbersc                 s   s     | ]}t |o|d kV  qdS r   r   r   r(   r(   r)   r   *  s    z$Transformer.delay.<locals>.<genexpr>z"positions must be positive nubmersdelayc                 S      g | ]}|d qS rK   r(   r   r(   r(   r)   r   .  r   z%Transformer.delay.<locals>.<listcomp>r1   r\   r3   r   rO   r"   r#   r6   )r'   r4  r   r(   r(   r)   r5    s   
zTransformer.delayfactorc                 C   @   t |tr	|dk rtdd| g}| j| | jd | S )a9  Downsample the signal by an integer factor. Only the first out of
        each factor samples is retained, the others are discarded.

        No decimation filter is applied. If the input is not a properly
        bandlimited baseband signal, aliasing will occur. This may be desirable
        e.g., for frequency translation.

        For a general resampling effect with anti-aliasing, see rate.

        Parameters
        ----------
        factor : int, default=2
            Downsampling factor.

        See Also
        --------
        rate, upsample

        r	   "factor must be a positive integer.
downsampler   r'   r9  r   r(   r(   r)   r<  4  s   
zTransformer.downsamplec                 C   r0  )um  Makes audio easier to listen to on headphones. Adds ‘cues’ to 44.1kHz
        stereo audio so that when listened to on headphones the stereo image is
        moved from inside your head (standard for headphones) to outside and in
        front of the listener (standard for speakers).

        Warning: Will only work properly on 44.1kHz stereo audio!

        earwaxr2  r3  r(   r(   r)   r>  Q  s   	zTransformer.earwaxr	   r   r   n_echosc                 C   s0  t |r|dks|dkrtdt |r|dks|dkr tdt|tr)|dkr-tdt|ts6tdt||kr@tdtdd	 |D rMtd
t|tsVtdt||kr`tdtdd	 |D rmtdd|d|dg}t|D ]}|||  ||  g qz| j	| | j
d | S )a   Add echoing to the audio.

        Echoes are reflected sound and can occur naturally amongst mountains
        (and sometimes large buildings) when talking or shouting; digital echo
        effects emulate this behav- iour and are often used to help fill out
        the sound of a single instrument or vocal. The time differ- ence
        between the original signal and the reflection is the 'delay' (time),
        and the loudness of the reflected signal is the 'decay'. Multiple
        echoes can have different delays and decays.

        Parameters
        ----------
        gain_in : float, default=0.8
            Input volume, between 0 and 1
        gain_out : float, default=0.9
            Output volume, between 0 and 1
        n_echos : int, default=1
            Number of reflections
        delays : list, default=[60]
            List of delays in miliseconds
        decays : list, default=[0.4]
            List of decays, relative to gain in between 0 and 1

        See Also
        --------
        echos, reverb, chorus
        r   r	   r   r   #n_echos must be a positive integer.zdelays must be a list'the length of delays must equal n_echosc                 s   r   r   r   r   r(   r(   r)   r     r   z#Transformer.echo.<locals>.<genexpr>*the elements of delays must be numbers > 0zdecays must be a list'the length of decays must equal n_echosc                 s   r   r   r   r   r(   r(   r)   r     r   r   echorK   r   r'   r   r   r?  r   r   r   r   r(   r(   r)   rD  `  s:   !

zTransformer.echoc                 C   s4  t |r|dks|dkrtdt |r|dks|dkr tdt|tr)|dkr-tdt|ts6tdt||kr@tdtdd	 |D rMtd
t|tsVtdt||kr`tdtdd	 |D rmtdd|d|dg}t|D ]}||| d|| dg qz| j	| | j
d | S )u  Add a sequence of echoes to the audio.

        Like the echo effect, echos stand for ‘ECHO in Sequel’, that is the
        first echos takes the input, the second the input and the first echos,
        the third the input and the first and the second echos, ... and so on.
        Care should be taken using many echos; a single echos has the same
        effect as a single echo.

        Parameters
        ----------
        gain_in : float, default=0.8
            Input volume, between 0 and 1
        gain_out : float, default=0.9
            Output volume, between 0 and 1
        n_echos : int, default=1
            Number of reflections
        delays : list, default=[60]
            List of delays in miliseconds
        decays : list, default=[0.4]
            List of decays, relative to gain in between 0 and 1

        See Also
        --------
        echo, reverb, chorus
        r   r	   r   r   r@  zthe delays must be a list rA  c                 s   r   r   r   r   r(   r(   r)   r     r   z$Transformer.echos.<locals>.<genexpr>rB  zthe decays must be a list rC  c                 s   r   r   r   r   r(   r(   r)   r     r   r   echosrK   r   rE  r(   r(   r)   rF    s<   



zTransformer.echosc                 C   sx   t |r|dkrtdt |r|dkrtdt |s tdd|d|dd|dg}| j| | jd | S )a  Apply a two-pole peaking equalisation (EQ) filter to boost or
        reduce around a given frequency.
        This effect can be applied multiple times to produce complex EQ curves.

        Parameters
        ----------
        frequency : float
            The filter's central frequency in Hz.
        width_q : float
            The filter's width as a Q-factor.
        gain_db : float
            The filter's gain in dB.

        See Also
        --------
        bass, treble

        r   r   r   gain_db must be a number.	equalizerrK   r   r   )r'   r   r   r   r   r(   r(   r)   rH    s   
zTransformer.equalizerr   fade_in_lenfade_out_len
fade_shaper   hr   lr   c                 C   s   g d}||vrt dd|t|r|dk rt dt|r&|dk r*t dg }|dkr;|d| |dg |dkrL|d	d| |dd	g t|dkr^| j| | jd | S )
a  Add a fade in and/or fade out to an audio file.
        Default fade shape is 1/4 sine wave.

        Parameters
        ----------
        fade_in_len : float, default=0.0
            Length of fade-in (seconds). If fade_in_len = 0,
            no fade in is applied.
        fade_out_len : float, defaut=0.0
            Length of fade-out (seconds). If fade_out_len = 0,
            no fade in is applied.
        fade_shape : str, default='q'
            Shape of fade. Must be one of
             * 'q' for quarter sine (default),
             * 'h' for half sine,
             * 't' for linear,
             * 'l' for logarithmic
             * 'p' for inverted parabola.

        See Also
        --------
        splice

        rL  zFade shape must be one of {}rs   r   z)fade_in_len must be a nonnegative number.z*fade_out_len must be a nonnegative number.faderK   reverse)	r3   r5   rv   r   rO   rg   r"   r#   r6   )r'   rI  rJ  rK  fade_shapesr   r(   r(   r)   rO    s.   zTransformer.fadecoefficientsc                 C   sb   t |ts	tdtdd |D stddg}|dd |D  | j| | jd | S )u   Use SoX’s FFT convolution engine with given FIR filter coefficients.

        Parameters
        ----------
        coefficients : list
            fir filter coefficients

        zcoefficients must be a listc                 S   r   r(   r   r~   cr(   r(   r)   r   c  r   z#Transformer.fir.<locals>.<listcomp>zcoefficients must be numbers.firc                 S   r6  r7  r(   rS  r(   r(   r)   r   g  r   r8  )r'   rR  r   r(   r(   r)   rU  W  s   
	zTransformer.firr   G   sinelinearr5  depthregenwidthspeedrh   rW  trianglephaseinterprX  	quadraticc	           
   	   C   s.  t |r|dk s|dkrtdt |r|dk s|dkr tdt |r,|dk s,|dkr0tdt |r<|dk s<|d	kr@td
t |rL|dk sL|dkrPtd|dvrXtdt |rd|dk sd|d	krhtd|dvrptdd|d|d|d|d|d| |d| g	}	| j|	 | jd | S )a  Apply a flanging effect to the audio.

        Parameters
        ----------
        delay : float, default=0
            Base delay (in miliseconds) between 0 and 30.
        depth : float, default=2
            Added swept delay (in miliseconds) between 0 and 10.
        regen : float, default=0
            Percentage regeneration between -95 and 95.
        width : float, default=71,
            Percentage of delayed signal mixed with original between 0 and 100.
        speed : float, default=0.5
            Sweeps per second (in Hz) between 0.1 and 10.
        shape : 'sine' or 'triangle', default='sine'
            Swept wave shape
        phase : float, default=25
            Swept wave percentage phase-shift for multi-channel flange between
            0 and 100. 0 = 100 = same phase on each channel
        interp : 'linear' or 'quadratic', default='linear'
            Digital delay-line interpolation type.

        See Also
        --------
        tremolo
        r      z(delay must be a number between 0 and 30.r   z(depth must be a number between 0 and 10.i_   z*regen must be a number between -95 and 95.r$  z)width must be a number between 0 and 100.皙?z*speed must be a number between 0.1 and 10.r]  z*shape must be one of 'sine' or 'triangle'.z)phase must be a number between 0 and 100.ra  z.interp must be one of 'linear' or 'quadratic'.flangerrK   r   )
r'   r5  rY  rZ  r[  r\  rh   r_  r`  r   r(   r(   r)   rf  n  s:    zTransformer.flanger	normalizelimiterbalance)r   Br   c                 C   s   t |stdt|tstdt|tstd|dvr"tddg}|dur1|d|  |r8|d	 |r?|d
 ||d | j| | jd | S )a
  Apply amplification or attenuation to the audio signal.

        Parameters
        ----------
        gain_db : float, default=0.0
            Gain adjustment in decibels (dB).
        normalize : bool, default=True
            If True, audio is normalized to gain_db relative to full scale.
            If False, simply adjusts the audio power level by gain_db.
        limiter : bool, default=False
            If True, a simple limiter is invoked to prevent clipping.
        balance : str or None, default=None
            Balance gain across channels. Can be one of:
             * None applies no balancing (default)
             * 'e' applies gain to all channels other than that with the
                highest peak level, such that all channels attain the same
                peak level
             * 'B' applies gain to all channels other than that with the
                highest RMS level, such that all channels attain the same
                RMS level
             * 'b' applies gain with clipping protection to all channels other
                than that with the highest RMS level, such that all channels
                attain the same RMS level
            If normalize=True, 'B' and 'b' are equivalent.

        See Also
        --------
        loudness

        rG  znormalize must be a boolean.zlimiter must be a boolean.)Nr   rj  r   z.balance must be one of None, 'e', 'B', or 'b'.gainNr^   -n-lrK   )r   r3   r1   r2   r6   r"   rO   r#   )r'   r   rg  rh  ri  r   r(   r(   r)   rk    s&   #



zTransformer.gain9v?n_polesc                 C      t |r|dkrtdt |r|dkrtd|dvr tddd| |dg}|d	kr7||dd
 | j| | jd | S )a  Apply a high-pass filter with 3dB point frequency. The filter can be
        either single-pole or double-pole. The filters roll off at 6dB per pole
        per octave (20dB per pole per decade).

        Parameters
        ----------
        frequency : float
            The filter's cutoff frequency in Hz.
        width_q : float, default=0.707
            The filter's width as a Q-factor. Applies only when n_poles=2.
            The default gives a Butterworth response.
        n_poles : int, default=2
            The number of poles in the filter. Must be either 1 or 2

        See Also
        --------
        lowpass, equalizer, sinc, allpass

        r   r   r   r	   r   n_poles must be 1 or 2.highpassr^   rK   r   r   r   r3   r6   r"   rO   r#   r'   r   r   ro  r   r(   r(   r)   rs       zTransformer.highpassc                 C   rp  )a  Apply a low-pass filter with 3dB point frequency. The filter can be
        either single-pole or double-pole. The filters roll off at 6dB per pole
        per octave (20dB per pole per decade).

        Parameters
        ----------
        frequency : float
            The filter's cutoff frequency in Hz.
        width_q : float, default=0.707
            The filter's width as a Q-factor. Applies only when n_poles=2.
            The default gives a Butterworth response.
        n_poles : int, default=2
            The number of poles in the filter. Must be either 1 or 2

        See Also
        --------
        highpass, equalizer, sinc, allpass

        r   r   r   rq  rr  lowpassr^   rK   r   r   rt  ru  r(   r(   r)   rw    rv  zTransformer.lowpassnum_tapsc                 C   sp   |durt |tstd|dur|d dkrtddg}|dur*|d| g | j| | jd | S )ak  Apply an odd-tap Hilbert transform filter, phase-shifting the signal
        by 90 degrees. This is used in many matrix coding schemes and for
        analytic signal generation. The process is often written as a
        multiplication by i (or j), the imaginary unit. An odd-tap Hilbert
        transform filter has a bandpass characteristic, attenuating the lowest
        and highest frequencies.

        Parameters
        ----------
        num_taps : int or None, default=None
            Number of filter taps - must be odd. If none, it is chosen to have
            a cutoff frequency of about 75 Hz.

        Nz(num taps must be None or an odd integer.r   r   znum_taps must an odd integer.hilbertrl  )r1   rG   r3   rO   r"   r#   r6   )r'   rx  r   r(   r(   r)   ry  H  s   zTransformer.hilbert      $     @P@reference_levelc                 C   sf   t |stdt |std|dks|dk rtdd|d|dg}| j| | jd | S )aH  Loudness control. Similar to the gain effect, but provides
        equalisation for the human auditory system.

        The gain is adjusted by gain_db and the signal is equalised according
        to ISO 226 w.r.t. reference_level.

        Parameters
        ----------
        gain_db : float, default=-10.0
            Loudness adjustment amount (in dB)
        reference_level : float, default=65.0
            Reference level (in dB) according to which the signal is equalized.
            Must be between 50 and 75 (dB)

        See Also
        --------
        gain

        rG  z reference_level must be a numberr"  2   z)reference_level must be between 50 and 75loudnessrK   r   )r'   r   r|  r   r(   r(   r)   r~  g  s   zTransformer.loudnessi@  g{Gzt?g{GzD?re  g?)iir  )ir  )r  r  )ir  r  n_bandscrossover_frequenciesrk  c              	   C   sJ  t |tr	|dk rtdt |trt||d krtdtdd |D r+tdt |tr6t||kr:tdtdd |D rGtd	t |trRt||krVtd
tdd |D rctdtdd t||D rttd t |trt||krtdtdd |D rtdt |trt||krtdtdd |D rtd|D ]G}tdd |D rtdtdd |D rtdtdd |D rtdtdd |D rtdt|tdd |D krtd qt |trt||krtd!td"d |D rtd#d$g}	t	|D ]z}
|
d%kr.|	
||
d  d& ||
 d&d'||
 d&g}||
 }t|d(d) d*}g }|D ]}||d% d&|d d&g qL||
 d+uru|
d,||
 d'| n|
d'| ||
 d+ur|
||
 d& |	
d-| q| j|	 | j
d$ | S ).a\	  The multi-band compander is similar to the single-band compander but
        the audio is first divided into bands using Linkwitz-Riley cross-over
        filters and a separately specifiable compander run on each band.

        When used with n_bands=1, this effect is identical to compand.
        When using n_bands > 1, the first set of arguments applies a single
        band compander, and each subsequent set of arugments is applied on
        each of the crossover frequencies.

        Parameters
        ----------
        n_bands : int, default=2
            The number of bands.
        crossover_frequencies : list of float, default=[1600]
            A list of crossover frequencies in Hz of length n_bands-1.
            The first band is always the full spectrum, followed by the bands
            specified by crossover_frequencies.
        attack_time : list of float, default=[0.005, 0.000625]
            A list of length n_bands, where each element is the time in seconds
            over which the instantaneous level of the input signal is averaged
            to determine increases in volume over the current band.
        decay_time : list of float, default=[0.1, 0.0125]
            A list of length n_bands, where each element is the time in seconds
            over which the instantaneous level of the input signal is averaged
            to determine decreases in volume over the current band.
        soft_knee_db : list of float or None, default=[6.0, None]
            A list of length n_bands, where each element is the ammount (in dB)
            for which the points at where adjacent line segments on the
            transfer function meet will be rounded over the current band.
            If None, no soft_knee is applied.
        tf_points : list of list of tuples, default=[
                [(-47, -40), (-34, -34), (-17, -33), (0, 0)],
                [(-47, -40), (-34, -34), (-15, -33), (0, 0)]]
            A list of length n_bands, where each element is the transfer
            function points as a list of tuples corresponding to points in
            (dB, dB) defining the compander's transfer function over the
            current band.
        gain : list of floats or None
            A list of gain values for each frequency band.
            If None, no gain is applied.

        See Also
        --------
        compand, contrast

        r	   z#n_bands must be a positive integer.z9crossover_frequences must be a list of length n_bands - 1c                 S   s   g | ]}t | p|d k qS r   r   r~   rK   r(   r(   r)   r     r   z(Transformer.mcompand.<locals>.<listcomp>z7crossover_frequencies elements must be positive floats.z,attack_time must be a list of length n_bandsc                 S   r   r   r   )r~   r   r(   r(   r)   r     r   z.attack_time elements must be positive numbers.z+decay_time must be a list of length n_bandsc                 S   r   r   r   r~   dr(   r(   r)   r     r   z-decay_time elements must be positive numbers.c                 S   s   g | ]\}}||kqS r(   r(   )r~   r   r  r(   r(   r)   r     r   zElements of attack_time are larger than decay_time.
For most situations, attack_time should be shorter than decay time because the human ear is more sensitive to sudden loud music than sudden soft music.z.soft_knee_db must be a list of length n_bands.c                 S   s   g | ]}t | o|d uqS r
  r   r  r(   r(   r)   r     r   z2elements of soft_knee_db must be a number or None.z+tf_points must be a list of length n_bands.c                 S   s$   g | ]}t |t pt|d kqS r   )r1   r\   rg   )r~   r   r(   r(   r)   r     s   $ z1tf_points must be a list with at least one point.c                 s   r	  r
  r  r  r(   r(   r)   r     r  z'Transformer.mcompand.<locals>.<genexpr>z)elements of tf_points lists must be pairsc                 s   r  r  r  r  r(   r(   r)   r     r  z*Tuples in tf_points lists must be length 2c                 s   r  r   r   r   r(   r(   r)   r     r   z3Tuples in tf_points lists must be pairs of numbers.c                 s   r  r   r(   r   r(   r(   r)   r   	  r  z2Tuple values in tf_points lists must be <= 0 (dB).c                 S   r  r   r(   r   r(   r(   r)   r  	  r   z'Transformer.mcompand.<locals>.<setcomp>z*Found duplicate x-value in tf_points list.z%gain must be a list of length n_bandsc                 S   s   g | ]}t |p|d u  qS r
  r   )r~   gr(   r(   r)   r   	  r   z&gain elements must be numbers or None.mcompandr   rK   r   c                 S   r  r  r(   )tf_points_bandr(   r(   r)   r  	  r  z&Transformer.mcompand.<locals>.<lambda>r  Nr  rs   )r1   rG   r3   r\   rg   r   r   r   r   r   r6   r   rO   r5   rv   r"   r#   )r'   r  r  r  r  r  r  rk  tfpr   r   intermed_argsr  r   r!  r(   r(   r)   r    s   :


zTransformer.mcompandprofile_pathc                 C   s   t j|rtd| dt j|dkr$|dkr$t jt  |}n|}t t j|t js9t	d| dd|g}| j
|d|d dS )	a  Calculate a profile of the audio for use in noise reduction.
        Running this command does not effect the Transformer effects
        chain. When this function is called, the calculated noise profile
        file is saved to the `profile_path`.

        Parameters
        ----------
        input_filepath : str
            Path to audiofile from which to compute a noise profile.
        profile_path : str
            Path to save the noise profile file.

        See Also
        --------
        noisered

        profile_path z is a directory. z is not writeable.	noiseprofrl  )rn   N)ospathisdirr3   dirnamerv   getcwdaccessW_OKOSErrorr{   )r'   ri   r  _abs_profile_pathr   r(   r(   r)   r  4	  s   

zTransformer.noiseprofr&  c                 C   sf   t j|std| dt|r|dk s|dkrtdd||dg}| j| | j	d | S )a  Reduce noise in the audio signal by profiling and filtering.
        This effect is moderately effective at removing consistent
        background noise such as hiss or hum.

        Parameters
        ----------
        profile_path : str
            Path to a noise profile file.
            This file can be generated using the `noiseprof` effect.
        amount : float, default=0.5
            How much noise should be removed is specified by amount. Should
            be between 0 and 1.  Higher numbers will remove more noise but
            present a greater likelihood  of  removing wanted  components  of
            the  audio  signal.

        See Also
        --------
        noiseprof

        r  z does not exist.r   r	   z(amount must be a number between 0 and 1.noiseredrK   )
r  r  existsr  r   r3   r"   rO   r#   r6   )r'   r  r&  r   r(   r(   r)   r  Z	  s   
zTransformer.noisered      db_levelc                 C   s8   t |stdd|dg}| j| | jd | S )a+  Normalize an audio file to a particular db level.
        This behaves identically to the gain effect with normalize=True.

        Parameters
        ----------
        db_level : float, default=-3.0
            Output volume (db)

        See Also
        --------
        gain, loudness

        db_level must be a number.normrK   r   )r'   r  r   r(   r(   r)   r  	  s   zTransformer.normc                 C   r0  )a6  Out Of Phase Stereo effect. Mixes stereo to twin-mono where each
        mono channel contains the difference between the left and right stereo
        channels. This is sometimes known as the 'karaoke' effect as it often
        has the effect of removing most or all of the vocals from a recording.

        oopsr2  r3  r(   r(   r)   r  	  s   zTransformer.oops      4@colourc                 C   sN   t |stdt |stdd|d|dg}| j| | jd | S )a  Apply non-linear distortion.

        Parameters
        ----------
        gain_db : float, default=20
            Controls the amount of distortion (dB).
        colour : float, default=20
            Controls the amount of even harmonic content in the output (dB).

        r  zcolour must be a number.	overdriverK   r   )r'   r   r  r   r(   r(   r)   r  	  s   zTransformer.overdrivestart_durationend_durationc                 C   s^   t |r|dk rtdt |r|dk rtdd|d|dg}| j| | jd | S )a  Add silence to the beginning or end of a file.
        Calling this with the default arguments has no effect.

        Parameters
        ----------
        start_duration : float
            Number of seconds of silence to add to beginning.
        end_duration : float
            Number of seconds of silence to add to end.

        See Also
        --------
        delay

        r   z)Start duration must be a positive number.zEnd duration must be positive.padrK   r   )r'   r  r  r   r(   r(   r)   r  	  s   zTransformer.padGz?
sinusoidaldecaymodulation_shaper  
triangularc                 C   s  t |r|dks|dkrtdt |r|dks|dkr tdt |r,|dks,|dkr0tdt |r<|dk s<|dkr@td	t |rL|dk sL|d
krPtd|dvrXtdd|d|d|d|d|dg}|dkrt|d n	|dkr}|d | j| | jd | S )a  Apply a phasing effect to the audio.

        Parameters
        ----------
        gain_in : float, default=0.8
            Input volume between 0 and 1
        gain_out: float, default=0.74
            Output volume between 0 and 1
        delay : float, default=3
            Delay in miliseconds between 0 and 5
        decay : float, default=0.4
            Decay relative to gain_in, between 0.1 and 0.5.
        speed : float, default=0.5
            Modulation speed in Hz, between 0.1 and 2
        modulation_shape : str, defaul='sinusoidal'
            Modulation shpae. One of 'sinusoidal' or 'triangular'

        See Also
        --------
        flanger, tremolo
        r   r	   r   r      z delay must be a positive number.re  r   z+decay must be a number between 0.1 and 0.5.r    speed must be a positive number.r  z;modulation_shape must be one of 'sinusoidal', 'triangular'.phaserrK   r  -sr  rI   rt  )r'   r   r   r5  r  r\  r  r   r(   r(   r)   r  	  s8   	
zTransformer.phasern_semitonesquickc                 C   s~   t |std|dk s|dkrtd t|tstddg}|r(|d ||d d	 | j| | j	d | S )
a  Pitch shift the audio without changing the tempo.

        This effect uses the WSOLA algorithm. The audio is chopped up into
        segments which are then shifted in the time domain and overlapped
        (cross-faded) at points where their waveforms are most similar as
        determined by measurement of least squares.

        Parameters
        ----------
        n_semitones : float
            The number of semitones to shift. Can be positive or negative.
        quick : bool, default=False
            If True, this effect will run faster but with lower sound quality.

        See Also
        --------
        bend, speed, tempo

        z%n_semitones must be a positive numberi   z=Using an extreme pitch shift. Quality of results will be poorquick must be a boolean.pitch-qr   rK   )
r   r3   r   r   r1   r2   r6   r"   rO   r#   )r'   r  r  r   r(   r(   r)   r  #
  s   

zTransformer.pitchrM  qualityr   rN  mrM  r   c                 C   sl   g d}t |r|dkrtd||vrtdd|dd| |dg}| j| | jd | S )	a  Change the audio sampling rate (i.e. resample the audio) to any
        given `samplerate`. Better the resampling quality = slower runtime.

        Parameters
        ----------
        samplerate : float
            Desired sample rate.
        quality : str
            Resampling quality. One of:
             * q : Quick - very low quality,
             * l : Low,
             * m : Medium,
             * h : High (default),
             * v : Very high

        See Also
        --------
        upsample, downsample, convert

        r  r   z%Samplerate must be a positive number.zQuality must be one of {}.rs   r:   r^   rK   )r   r3   r5   rv   r"   rO   r#   r6   )r'   r'  r  quality_valsr   r(   r(   r)   r:   O
  s   zTransformer.rateremix_dictionarynum_output_channelsc                 C   s:  t |ts|du std|durCtdd | D s tdtdd | D s/td| D ]}tdd |D sBtd	q3t |trL|d
ksT|du sTtddg}|du ra|d n.|du rkt| }t	d|d D ]}|| v rd
dd || D }nd}|| qr| j| | jd | S )aF  Remix the channels of an audio file.

        Note: volume options are not yet implemented

        Parameters
        ----------
        remix_dictionary : dict or None
            Dictionary mapping output channel to list of input channel(s).
            Empty lists indicate the corresponding output channel should be
            empty. If None, mixes all channels down to a single mono file.
        num_output_channels : int or None
            The number of channels in the output file. If None, the number of
            output channels is equal to the largest key in remix_dictionary.
            If remix_dictionary is None, this variable is ignored.

        Examples
        --------
        Remix a 4-channel input file. The output file will have
        input channel 2 in channel 1, a mixdown of input channels 1 an 3 in
        channel 2, an empty channel 3, and a copy of input channel 4 in
        channel 4.

        >>> import sox
        >>> tfm = sox.Transformer()
        >>> remix_dictionary = {1: [2], 2: [1, 3], 4: [4]}
        >>> tfm.remix(remix_dictionary)

        Nz.remix_dictionary must be a dictionary or None.c                 S      g | ]}t |to|d kqS r   r1   rG   r~   r   r(   r(   r)   r   
  r   z%Transformer.remix.<locals>.<listcomp>z1remix dictionary must have positive integer keys.c                 S   s   g | ]}t |tqS r(   )r1   r\   r~   r   r(   r(   r)   r   
  r   z&remix dictionary values must be lists.c                 S   r  r   r  r  r(   r(   r)   r   
  r   z=elements of remix dictionary values must be positive integersr   z7num_output_channels must be a positive integer or None.remixr^   r	   r   c                 S   r   r(   )rV   r  r(   r(   r)   r   
  r   0)r1   dictr3   r   keysvaluesrG   r6   maxr   rv   r"   rO   r#   )r'   r  r  v_listr   channelout_channelr(   r(   r)   r  x
  sP   

zTransformer.remixcountc                 C   s@   t |tr	|dk rtdd| g}| j| | jd dS )zRepeat the entire audio count times.

        Parameters
        ----------
        count : int, default=1
            The number of times to repeat the audio.

        r	   z count must be a postive integer.repeatNr   )r'   r  r   r(   r(   r)   r  
  s
   	
zTransformer.repeatr}  r$  reverberancehigh_freq_damping
room_scalestereo_depth	pre_delaywet_gainwet_onlyc           	   	   C   s  t |r|dk s|dkrtdt |r|dk s|dkr tdt |r,|dk s,|dkr0tdt |r<|dk s<|dkr@tdt |rH|dk rLtdt |sTtdt|ts]td	d
g}|rg|d ||d|d|d|d|d|dg | j| | jd
 | S )u  Add reverberation to the audio using the ‘freeverb’ algorithm.
        A reverberation effect is sometimes desirable for concert halls that
        are too small or contain so many people that the hall’s natural
        reverberance is diminished. Applying a small amount of stereo reverb
        to a (dry) mono signal will usually make it sound more natural.

        Parameters
        ----------
        reverberance : float, default=50
            Percentage of reverberance
        high_freq_damping : float, default=50
            Percentage of high-frequency damping.
        room_scale : float, default=100
            Scale of the room as a percentage.
        stereo_depth : float, default=100
            Stereo depth as a percentage.
        pre_delay : float, default=0
            Pre-delay in milliseconds.
        wet_gain : float, default=0
            Amount of wet gain in dB
        wet_only : bool, default=False
            If True, only outputs the wet signal.

        See Also
        --------
        echo

        r   r$  z&reverberance must be between 0 and 100z+high_freq_damping must be between 0 and 100z$room_scale must be between 0 and 100z&stereo_depth must be between 0 and 100z#pre_delay must be a positive numberzwet_gain must be a numberzwet_only must be a boolean.reverbz-wrK   r   )	r'   r  r  r  r  r  r  r  r   r(   r(   r)   r  
  s@   %

	zTransformer.reverbc                 C   r0  )z%Reverse the audio completely
        rP  r2  r3  r(   r(   r)   rP  +  s   zTransformer.reverselocation)r   r	   r_   silence_thresholdmin_silence_durationbuffer_around_silencec                 C   s  |dvrt dt|r|dk rt d|dkrt dt|r$|dkr(t dt|ts1t dg }|dkr<|d	 |rF|d
dg n|d
 |d|d|ddg |dkrk|d|d|ddg |dkrt|d	 | j| | jd
 | S )ac  Removes silent regions from an audio file.

        Parameters
        ----------
        location : int, default=0
            Where to remove silence. One of:
             * 0 to remove silence throughout the file (default),
             * 1 to remove silence from the beginning,
             * -1 to remove silence from the end,
        silence_threshold : float, default=0.1
            Silence threshold as percentage of maximum sample amplitude.
            Must be between 0 and 100.
        min_silence_duration : float, default=0.1
            The minimum ammount of time in seconds required for a region to be
            considered non-silent.
        buffer_around_silence : bool, default=False
            If True, leaves a buffer of min_silence_duration around removed
            silent regions.

        See Also
        --------
        vad

        )r_   r   r	   z!location must be one of -1, 0, 1.r   z4silence_threshold must be a number between 0 and 100r$  z/min_silence_duration must be a positive number.z(buffer_around_silence must be a boolean.r_   rP  silencerm  1rK   %z-1)r3   r   r1   r2   r6   rO   r"   r#   )r'   r  r  r  r  r   r(   r(   r)   r  4  sL   





zTransformer.silencehigh  x   filter_typer  lowpassrejectcutoff_freqstop_band_attenuationtransition_bwphase_responsec                 C   s  g d}||vrt dd|t|st|tst d|dv r,t|tr,t d|dv r8t|r8t dt|rD|d	krDt d
t|trdt|dkrSt dtdd |D r`t dt|}t|rl|d	k rpt dt|st|ts|du st d|dv rt|trt dt|r|d	krt dt|trtdd |D rt dt|dkrt d|durt|st dt|r|d	k s|dkrt ddg}|	d|dg |dur|	d|dg |dkr|dur|	d |dg |
|d n=|d!kr"|
d"|d |dur!|	d |dg n t|r1|	d |dg nt|trB|	d |d	 dg |d#krX|
|d	 dd"|d$ d n|d%krm|
|d$ dd"|d	 d t|tr~|	d |d$ dg | j	| | j
d | S )&a-  Apply a sinc kaiser-windowed low-pass, high-pass, band-pass, or
        band-reject filter to the signal.

        Parameters
        ----------
        filter_type : str, default='high'
            Type of filter. One of:
                - 'high' for a high-pass filter
                - 'low' for a low-pass filter
                - 'pass' for a band-pass filter
                - 'reject' for a band-reject filter
        cutoff_freq : float or list, default=3000
            A scalar or length 2 list indicating the filter's critical
            frequencies. The critical frequencies are given in Hz and must be
            positive. For a high-pass or low-pass filter, cutoff_freq
            must be a scalar. For a band-pass or band-reject filter, it must be
            a length 2 list.
        stop_band_attenuation : float, default=120
            The stop band attenuation in dB
        transition_bw : float, list or None, default=None
            The transition band-width in Hz.
            If None, sox's default of 5% of the total bandwith is used.
            If a float, the given transition bandwith is used for both the
            upper and lower bands (if applicable).
            If a list, the first argument is used for the lower band and the
            second for the upper band.
        phase_response : float or None
            The filter's phase response between 0 (minimum) and 100 (maximum).
            If None, sox's default phase repsonse is used.

        See Also
        --------
        band, bandpass, bandreject, highpass, lowpass
        r  zfilter_type must be one of {}z, z&cutoff_freq must be a number or a list)r  r  zJFor filter types 'high' and 'low', cutoff_freq must be a float, not a list)r  r  zMFor filter types 'pass' and 'reject', cutoff_freq must be a list, not a floatr   z$cutoff_freq must be a postive numberr   z5If cutoff_freq is a list it may only have 2 elements.c                 S   r   r   r   r  r(   r(   r)   r     r   z$Transformer.sinc.<locals>.<listcomp>z0elements of cutoff_freq must be positive numbersz/stop_band_attenuation must be a positive numberNz/transition_bw must be a number, a list or None.zLFor filter types 'high' and 'low', transition_bw must be a float, not a listz&transition_bw must be a postive numberc                 S   r   r   r   r  r(   r(   r)   r     r   z2elements of transition_bw must be positive numbersz7If transition_bw is a list it may only have 2 elements.z(phase_response must be a number or None.r$  z(phase response must be between 0 and 100sincz-arK   -pr  rI   r  r^   r  r	   r  )r3   r5   rv   r   r1   r\   rg   r   r   rO   r6   r"   r#   )r'   r  r  r  r  r  filter_typesr   r(   r(   r)   r    s   (







zTransformer.sincc                 C   sZ   t |r|dkrtd|dk s|dkrtd d|dg}| j| | jd | S )a_  Adjust the audio speed (pitch and tempo together).

        Technically, the speed effect only changes the sample rate information,
        leaving the samples themselves untouched. The rate effect is invoked
        automatically to resample to the output sample rate, using its default
        quality/speed. For higher quality or higher speed resampling, in
        addition to the speed effect, specify the rate effect with the desired
        quality option.

        Parameters
        ----------
        factor : float
            The ratio of the new speed to the old speed.
            For ex. 1.1 speeds up the audio by 10%; 0.9 slows it down by 10%.
            Note - this argument is the inverse of what is passed to the sox
            stretch effect for consistency with speed.

        See Also
        --------
        rate, tempo, pitch
        r    factor must be a positive numberr   r   z8Using an extreme factor. Quality of results will be poorr\  rK   )r   r3   r   r   r"   rO   r#   r6   r=  r(   r(   r)   r\    s   zTransformer.speedscalermsc                 C   s   g d}|durt |r|dkrtd|d|dg |r$|d | j|d|d	d
\}}}i }|d}|D ]}	|	 }
|
sBq9|
d }d|
dd }|||d< q9|S )a  Display time and frequency domain statistical information about the
        audio. Audio is passed unmodified through the SoX processing chain.

        Unlike other Transformer methods, this does not modify the transformer
        effects chain. Instead it computes statistics on the output file that
        would be created if the build command were invoked.

        Note: The file is downmixed to mono prior to computation.

        Parameters
        ----------
        input_filepath : str
            Path to input file to compute stats on.
        scale : float or None, default=None
            If not None, scales the input by the given scale factor.
        rms : bool, default=False
            If True, scales all values by the average rms amplitude.

        Returns
        -------
        stat_dict : dict
            Dictionary of statistics.

        See Also
        --------
        stats, power_spectrum, sox.file_info
        )r<   r  statNr   z scale must be a positive number.r  rK   z-rmsrl  Trn   ro   
r_   rs   :)r   r3   rO   r6   r{   splitrv   strip)r'   ri   r  r  r   r   stat_output	stat_dictlinesline
split_linevaluer  r(   r(   r)   r  ;  s(   

zTransformer.statc                 C   sp   g d}| j |d|dd\}}}g }|d}|D ]}| }t|dkr&q|\}	}
|t|	t|
g q|S )a  Calculates the power spectrum (4096 point DFT). This method
        internally invokes the stat command with the -freq option.

        Note: The file is downmixed to mono prior to computation.

        Parameters
        ----------
        input_filepath : str
            Path to input file to compute stats on.

        Returns
        -------
        power_spectrum : list
            List of frequency (Hz), amplitude pairs.

        See Also
        --------
        stat, stats, sox.file_info
        )r<   r  r  z-freqrl  Tr  r  r   )r{   r  rg   r6   float)r'   ri   r   r   r  power_spectrumr  r  r  freqampr(   r(   r)   r  s  s   
zTransformer.power_spectrumc                 C   st   g d}| j |d|dd\}}}i }|d}|D ]}| }t|dkr&q|d }	d|d	d }
|	||
< q|S )
a  Display time domain statistical information about the audio
        channels. Audio is passed unmodified through the SoX processing chain.
        Statistics are calculated and displayed for each audio channel

        Unlike other Transformer methods, this does not modify the transformer
        effects chain. Instead it computes statistics on the output file that
        would be created if the build command were invoked.

        Note: The file is downmixed to mono prior to computation.

        Parameters
        ----------
        input_filepath : str
            Path to input file to compute stats on.

        Returns
        -------
        stats_dict : dict
            List of frequency (Hz), amplitude pairs.

        See Also
        --------
        stat, sox.file_info
        )r<   r  statsrl  Tr  r  r   r_   rs   N)r{   r  rg   rv   )r'   ri   r   r   stats_output
stats_dictr  r  r  r  r  r(   r(   r)   r    s   

zTransformer.statsr   windowc                 C   s   t |r|dkrtd|dk s|dkrtd t|d dkr&td t |r.|dkr2td	d
|d|dg}| j| | jd
 | S )a4  Change the audio duration (but not its pitch).
        **Unless factor is close to 1, use the tempo effect instead.**

        This effect is broadly equivalent to the tempo effect with search set
        to zero, so in general, its results are comparatively poor; it is
        retained as it can sometimes out-perform tempo for small factors.

        Parameters
        ----------
        factor : float
            The ratio of the new tempo to the old tempo.
            For ex. 1.1 speeds up the tempo by 10%; 0.9 slows it down by 10%.
            Note - this argument is the inverse of what is passed to the sox
            stretch effect for consistency with tempo.
        window : float, default=20
            Window size in miliseconds

        See Also
        --------
        tempo, speed, pitch

        r   r  r   r   HUsing an extreme time stretching factor. Quality of results will be poorr   re  zAFor this stretch factor, the tempo effect has better performance.z!window must be a positive number.stretchrK   )	r   r3   r   r   absr"   rO   r#   r6   )r'   r9  r   r   r(   r(   r)   r    s$   zTransformer.stretchc                 C   r0  )a  Swap stereo channels. If the input is not stereo, pairs of channels
        are swapped, and a possible odd last channel passed through.

        E.g., for seven channels, the output order will be 2, 1, 4, 3, 6, 5, 7.

        See Also
        ----------
        remix

        swapr2  r3  r(   r(   r)   r    s   zTransformer.swap
audio_type)r  r   rN  c                 C   s   t |r|dkrtd|dk s|dkrtd t|d dkr&td |d	vr.td
t|ts7tddg}|rA|d |durM|d|  ||d | j	| | j
d | S )a  Time stretch audio without changing pitch.

        This effect uses the WSOLA algorithm. The audio is chopped up into
        segments which are then shifted in the time domain and overlapped
        (cross-faded) at points where their waveforms are most similar as
        determined by measurement of least squares.

        Parameters
        ----------
        factor : float
            The ratio of new tempo to the old tempo.
            For ex. 1.1 speeds up the tempo by 10%; 0.9 slows it down by 10%.
        audio_type : str
            Type of audio, which optimizes algorithm parameters. One of:
             * m : Music,
             * s : Speech,
             * l : Linear (useful when factor is close to 1),
        quick : bool, default=False
            If True, this effect will run faster but with lower sound quality.

        See Also
        --------
        stretch, speed, pitch

        r   r  r   r   r  r   re  zCFor this stretch factor, the stretch effect has better performance.)Nr  r   rN  z1audio_type must be one of None, 'm', 's', or 'l'.r  tempor  Nr^   rK   )r   r3   r   r   r  r1   r2   r6   r"   rO   r#   )r'   r9  r  r  r   r(   r(   r)   r    s2   

zTransformer.tempo     p@c                 C   r   )	u7  Boost or cut the treble (lower) frequencies of the audio using a
        two-pole shelving filter with a response similar to that of a standard
        hi-fi’s tone-controls. This is also known as shelving equalisation.

        The filters are described in detail in
        http://musicdsp.org/files/Audio-EQ-Cookbook.txt

        Parameters
        ----------
        gain_db : float
            The gain at the Nyquist frequency.
            For a large cut use -20, for a large boost use 20.
        frequency : float, default=100.0
            The filter's cutoff frequency in Hz.
        slope : float, default=0.5
            The steepness of the filter's shelf transition.
            For a gentle slope use 0.3, and use 1.0 for a steep slope.

        See Also
        --------
        bass, equalizer

        r   r   r   r   r   treblerK   r   r   r   r(   r(   r)   r  I  s   
zTransformer.treble      D@c                 C   sf   t |r|dkrtdt |r|dks|dkrtdd|d|dg}| j| | jd | S )a>  Apply a tremolo (low frequency amplitude modulation) effect to the
        audio. The tremolo frequency in Hz is giv en by speed, and the depth
        as a percentage by depth (default 40).

        Parameters
        ----------
        speed : float
            Tremolo speed in Hz.
        depth : float
            Tremolo depth as a percentage of the total amplitude.

        See Also
        --------
        flanger

        Examples
        --------
        >>> tfm = sox.Transformer()

        For a growl-type effect

        >>> tfm.tremolo(speed=100.0)
        r   r  r$  z.depth must be a positive number less than 100.tremolorK   r   )r'   r\  rY  r   r(   r(   r)   r
  v  s   zTransformer.tremolo
start_timeend_timec                 C   s   t |r|dk rtdd|dg}|dur3t |r|dk r"td||kr*td||| d | j| | jd | S )a  Excerpt a clip from an audio file, given the start timestamp and end timestamp of the clip within the file, expressed in seconds. If the end timestamp is set to `None` or left unspecified, it defaults to the duration of the audio file.

        Parameters
        ----------
        start_time : float
            Start time of the clip (seconds)
        end_time : float or None, default=None
            End time of the clip (seconds)

        r   z%start_time must be a positive number.trimrK   Nz#end_time must be a positive number.z)start_time must be smaller than end_time.rt  )r'   r  r  r   r(   r(   r)   r    s   zTransformer.trimc                 C   r:  )a  Upsample the signal by an integer factor: zero-value samples are
        inserted between each pair of input samples. As a result, the original
        spectrum is replicated into the new frequency space (imaging) and
        attenuated. The upsample effect is typically used in combination with
        filtering effects.

        Parameters
        ----------
        factor : int, default=2
            Integer upsampling factor.

        See Also
        --------
        rate, downsample

        r	   r;  upsampler   r=  r(   r(   r)   r    s   
zTransformer.upsample      @r   r   )r	   r_   activity_thresholdmin_activity_durationinitial_search_buffermax_gapinitial_padc           	      C   s  |dvrt dt|tst dt|st dt|r!|dk r%t dt|r-|dk r1t dt|r9|dk r=t dt|rE|dk rIt d	g }|rR|d
 |dkr[|d |dd|dd|dd|dd|dd|dg |dkr~|d | j| | jd | S )a  Voice Activity Detector. Attempts to trim silence and quiet
        background sounds from the ends of recordings of speech. The algorithm
        currently uses a simple cepstral power measurement to detect voice, so
        may be fooled by other things, especially music.

        The effect can trim only from the front of the audio, so in order to
        trim from the back, the reverse effect must also be used.

        Parameters
        ----------
        location : 1 or -1, default=1
            If 1, trims silence from the beginning
            If -1, trims silence from the end
        normalize : bool, default=True
            If true, normalizes audio before processing.
        activity_threshold : float, default=7.0
            The measurement level used to trigger activity detection. This may
            need to be cahnged depending on the noise level, signal level, and
            other characteristics of the input audio.
        min_activity_duration : float, default=0.25
            The time constant (in seconds) used to help ignore short bursts of
            sound.
        initial_search_buffer : float, default=1.0
            The amount of audio (in seconds) to search for quieter/shorter
            bursts of audio to include prior to the detected trigger point.
        max_gap : float, default=0.25
            The allowed gap (in seconds) between quiteter/shorter bursts of
            audio to include prior to the detected trigger point
        initial_pad : float, default=0.0
            The amount of audio (in seconds) to preserve before the trigger
            point and any found quieter/shorter bursts.

        See Also
        --------
        silence

        Examples
        --------
        >>> tfm = sox.Transformer()

        Remove silence from the beginning of speech

        >>> tfm.vad(initial_pad=0.3)

        Remove silence from the end of speech

        >>> tfm.vad(location=-1, initial_pad=0.2)

        )r_   r	   zlocation must be -1 or 1.znormalize muse be a boolean.z$activity_threshold must be a number.r   z/min_activity_duration must be a positive numberz/initial_search_buffer must be a positive numberz"max_gap must be a positive number.z&initial_pad must be a positive number.r  r_   rP  vadrI   rK   z-Tr  z-gr  )r3   r1   r2   r   r6   rO   r"   r#   )	r'   r  rg  r  r  r  r  r  r   r(   r(   r)   r    s@   9


	
zTransformer.vadr   	gain_typer   limiter_gainc                 C   s
  t |std|durt |r|dks|dkrtd|dv r(|dk r(tddg}||d	 |d
kr<|d
 n|dkrF|d n|dkrP|d ntd|durw|dv rh|dkrh||d	 n|dkrw|dkrw||d	 | j| | jd | S )a^  Apply an amplification or an attenuation to the audio signal.

        Parameters
        ----------
        gain : float
            Interpreted according to the given `gain_type`.
            If `gain_type' = 'amplitude', `gain' is a positive amplitude ratio.
            If `gain_type' = 'power', `gain' is a power (voltage squared).
            If `gain_type' = 'db', `gain' is in decibels.
        gain_type : string, default='amplitude'
            Type of gain. One of:
                - 'amplitude'
                - 'power'
                - 'db'
        limiter_gain : float or None, default=None
            If specified, a limiter is invoked on peaks greater than
            `limiter_gain' to prevent clipping.
            `limiter_gain` should be a positive value much less than 1.

        See Also
        --------
        gain, compand

        zgain must be a number.Nr   r	   z2limiter gain must be a positive number less than 1)r   r   z9If gain_type = amplitude or power, gain must be positive.volrK   r   r   r   dBz.gain_type must be one of amplitude power or dbrt  )r'   rk  r  r  r   r(   r(   r)   r  :  s:   zTransformer.vol)FFFFr   )NNNNNF)NNNNNNT)NNNN)r   )r   F)r   r   )r   r   )r   r   r   NNNNN)r"  )NNN)r,  )r   )r,  r,  r   )r   r   r   rV  r   rW  r   rX  )r,  TFN)rn  r   r
  )rz  r{  )r   )r  )r  r  )r,  r,  )r  r  r   r   r   r  )F)rM  )NN)r	   )r}  r}  r$  r$  r   r   F)r   re  re  F)r  r  r  NN)NF)r   )r  r   )r  r	  )r	   Tr  r   r   r   r,  )r   N)X__name__
__module____qualname____doc__r*   r2   rG   r%   rH   rQ   r   rV   r  r   rS   rW   rY   r[   r]   rl   r   r   r   r{   ra   rb   r|   r   r   r   r   r   r   r   r   r<   r   r   r   r  r%  r}   r/  r1  r5  r<  r>  rD  rF  rH  rO  rU  rf  rk  rs  rw  ry  r~  r  r  r  r  r  r  r  r  r  r:   r   r  r  r  rP  r  r  r\  r  r  r  r  r  r  r  r
  r  r  r  r  r(   r(   r(   r)   r   %   s   

H*#
P-'
S7

y

S

  
*
+

1
c+




 
V
'
L
K
)

8
C

B
.
,(



 '


&'!
@-

*
R
Q
Q '
8&+2C
-"( br   )#r  r  r   pathlibr   typingr   r   r   r   r   numpyra   typing_extensionsr   r  r
   corer   r   r   r   r   r   r   logr   r4   r   r   r   r   rd   GainTyper   r(   r(   r(   r)   <module>   s.   