o
    Zh                     @   sp   d Z ddl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mZ eeZG dd	 d	eZd	gZdS )
zFeature extractor class for DAC    )ListOptionalUnionN   )SequenceFeatureExtractor)BatchFeature)PaddingStrategy
TensorTypeloggingc                       s   e Zd ZdZddgZ				dded	ed
edef fddZ					ddee	j
ee ee	j
 eee  f deeeeef  dee dee deeeef  d	ee defddZ  ZS )DacFeatureExtractora>  
    Constructs an Dac feature extractor.

    This feature extractor inherits from [`~feature_extraction_sequence_utils.SequenceFeatureExtractor`] which contains
    most of the main methods. Users should refer to this superclass for more information regarding those methods.

    Args:
        feature_size (`int`, *optional*, defaults to 1):
            The feature dimension of the extracted features. Use 1 for mono, 2 for stereo.
        sampling_rate (`int`, *optional*, defaults to 16000):
            The sampling rate at which the audio waveform should be digitalized, expressed in hertz (Hz).
        padding_value (`float`, *optional*, defaults to 0.0):
            The value that is used for padding.
        hop_length (`int`, *optional*, defaults to 512):
            Overlap length between successive windows.
    input_valuesZn_quantizers   >             feature_sizesampling_ratepadding_value
hop_lengthc                    s$   t  jd|||d| || _d S )N)r   r   r    )super__init__r   )selfr   r   r   r   kwargs	__class__r   ]/var/www/auris/lib/python3.10/site-packages/transformers/models/dac/feature_extraction_dac.pyr   1   s   
zDacFeatureExtractor.__init__NF	raw_audiopadding
truncation
max_lengthreturn_tensorsreturnc              
   C   s  |dur|| j krtd|  d| j  d| j  d| d	ntd| jj d |r0|r0td	|du r6d
}tt|tt	foHt|d t
jt	tf}|rTdd |D }n&|set|t
jset
j|t
jd}nt|t
jrz|jt
t
ju rz|t
j}|st
|jg}t|D ]/\}}	|	jdkrtd|	j | jdkr|	jdkrtd|	jd  d| jdkrtdqtd|i}
| j|
|||d| jd}|r|jddt
jddf |_g }
|dD ]}	| jdkr|	d }	|
|	j q|
|d< |dur||}|S )a  
        Main method to featurize and prepare for the model one or several sequence(s).

        Args:
            raw_audio (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`):
                The sequence or batch of sequences to be processed. Each sequence can be a numpy array, a list of float
                values, a list of numpy arrays or a list of list of float values. The numpy array must be of shape
                `(num_samples,)` for mono audio (`feature_size = 1`), or `(2, num_samples)` for stereo audio
                (`feature_size = 2`).
            padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `True`):
                Select a strategy to pad the returned sequences (according to the model's padding side and padding
                index) among:

                - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
                  sequence if provided).
                - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum
                  acceptable input length for the model if that argument is not provided.
                - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different
                  lengths).
            truncation (`bool`, *optional*, defaults to `False`):
                Activates truncation to cut input sequences longer than `max_length` to `max_length`.
            max_length (`int`, *optional*):
                Maximum length of the returned list and optionally padding length (see above).
            return_tensors (`str` or [`~utils.TensorType`], *optional*, default to 'pt'):
                If set, will return tensors instead of list of python integers. Acceptable values are:

                - `'tf'`: Return TensorFlow `tf.constant` objects.
                - `'pt'`: Return PyTorch `torch.Tensor` objects.
                - `'np'`: Return Numpy `np.ndarray` objects.
            sampling_rate (`int`, *optional*):
                The sampling rate at which the `audio` input was sampled. It is strongly recommended to pass
                `sampling_rate` at the forward call to prevent silent errors.
        Nz3The model corresponding to this feature extractor: z& was trained using a sampling rate of zB. Please make sure that the provided audio input was sampled with z	 and not .zDIt is strongly recommended to pass the `sampling_rate` argument to `zN()`. Failing to do so can result in silent errors that might be hard to debug.zABoth padding and truncation were set. Make sure you only set one.Tr   c                 S   s   g | ]}t j|t jd jqS )dtype)npasarrayfloat32T).0Zaudior   r   r   
<listcomp>~   s    z0DacFeatureExtractor.__call__.<locals>.<listcomp>r$      z6Expected input shape (channels, length) but got shape r   z$Expected mono audio but example has z	 channelsz$Stereo audio isn't supported for nowr   F)r    r   r   Zreturn_attention_maskZpad_to_multiple_of).N)r   
ValueErrorloggerwarningr   __name__bool
isinstancelisttupler&   ndarrayr'   r(   r%   Zfloat64Zastyper)   	enumeratendimshaper   r   padr   r   ZnewaxispopappendZconvert_to_tensors)r   r   r   r   r    r!   r   Z
is_batchedidxZexampler   Zpadded_inputsr   r   r   __call__<   st   *
"

	

zDacFeatureExtractor.__call__)r   r   r   r   )NFNNN)r1   
__module____qualname____doc__Zmodel_input_namesintfloatr   r   r&   r6   r   r   r2   strr   r	   r   r>   __classcell__r   r   r   r   r      sH    "r   )rA   typingr   r   r   numpyr&   Z!feature_extraction_sequence_utilsr   Zfeature_extraction_utilsr   utilsr   r	   r
   Z
get_loggerr1   r/   r   __all__r   r   r   r   <module>   s   
 
