o
    eif
                     @   s*   d Z ddlZddlmZ G dd dZdS )zPreprocessors for audio    N)Resamplec                   @   s*   e Zd ZdZdddZdd Zdd	 Zd
S )AudioNormalizeraU  Normalizes audio into a standard format

    Arguments
    ---------
    sample_rate : int
        The sampling rate to which the incoming signals should be converted.
    mix : {"avg-to-mono", "keep"}
        "avg-to-mono" - add all channels together and normalize by number of
        channels. This also removes the channel dimension, resulting in [time]
        format tensor.
        "keep" - don't normalize channel information

    Example
    -------
    >>> import torchaudio
    >>> example_file = 'tests/samples/multi-mic/speech_-0.82918_0.55279_-0.082918.flac'
    >>> signal, sr = torchaudio.load(example_file, channels_first = False)
    >>> normalizer = AudioNormalizer(sample_rate=8000)
    >>> normalized = normalizer(signal, sr)
    >>> signal.shape
    torch.Size([160000, 4])
    >>> normalized.shape
    torch.Size([80000])

    NOTE
    ----
    This will also upsample audio. However, upsampling cannot produce meaningful
    information in the bandwidth which it adds. Generally models will not work
    well for upsampled data if they have not specifically been trained to do so.
    >  avg-to-monoc                 C   s,   || _ |dvrtd| || _i | _d S )N)r   keepz Unexpected mixing configuration )sample_rate
ValueErrormix_cached_resamplers)selfr   r	    r   [/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/speechbrain/dataio/preprocess.py__init__(   s
   
zAudioNormalizer.__init__c                 C   sD   || j vrt|| j| j |< | j | }||dd}| |S )ay  Perform normalization

        Arguments
        ---------
        audio : torch.Tensor
            The input waveform torch tensor. Assuming [time, channels],
            or [time].
        sample_rate : int
            Rate the audio was sampled at.

        Returns
        -------
        audio : torch.Tensor
            Channel- and sample-rate-normalized audio.
        r   )r
   r   r   	unsqueezesqueeze_mix)r   audior   	resampler	resampledr   r   r   __call__/   s   



zAudioNormalizer.__call__c                 C   s<   |  dk}| jdkr|r|S t|dS | jdkr|S dS )zHandle channel mixing   r   r   N)dimr	   torchmean)r   r   
flat_inputr   r   r   r   H   s   

zAudioNormalizer._mixN)r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s
    
r   )r   r   speechbrain.augment.time_domainr   r   r   r   r   r   <module>   s    