
     `i&                         d Z ddl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j        e          Z G d d	e          Zd	gZdS )
z$Feature extractor class for EnCodec.    )OptionalUnionN   )SequenceFeatureExtractor)BatchFeature)PaddingStrategy
TensorTypeloggingc                       e Zd ZdZddgZ	 	 	 	 	 dded	ed
edee         dee         f
 fdZe	dee         fd            Z
e	dee         f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Z xZS )EncodecFeatureExtractora  
    Constructs an EnCodec 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.

    Instantiating a feature extractor with the defaults will yield a similar configuration to that of the
    [facebook/encodec_24khz](https://huggingface.co/facebook/encodec_24khz) architecture.

    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 24000):
            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 to fill the padding values.
        chunk_length_s (`float`, *optional*):
            If defined the audio is pre-processed into chunks of lengths `chunk_length_s` and then encoded.
        overlap (`float`, *optional*):
            Defines the overlap between each chunk. It is used to compute the `chunk_stride` using the following
            formulae : `int((1.0 - self.overlap) * self.chunk_length)`.
    input_valuespadding_mask   ]          Nfeature_sizesampling_ratepadding_valuechunk_length_soverlapc                 ^     t                      j        d|||d| || _        || _        d S )N)r   r   r    )super__init__r   r   )selfr   r   r   r   r   kwargs	__class__s          /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/encodec/feature_extraction_encodec.pyr   z EncodecFeatureExtractor.__init__7   s@     	wl-_lwwpvwww,    returnc                 L    | j         d S t          | j         | j        z            S )N)r   intr   r   s    r   chunk_lengthz$EncodecFeatureExtractor.chunk_lengthE   s)    &4t*T-??@@@r   c                 |    | j         | j        d S t          dt          d| j        z
  | j        z                      S )Nr   g      ?)r   r   maxr"   r$   r#   s    r   chunk_stridez$EncodecFeatureExtractor.chunk_strideM   s>    &$,*>4q#sT\1T5FFGGHHHr   F	raw_audiopadding
truncation
max_lengthreturn_tensorsc                 V   |2|| j         k    r&t          d|  d| j          d| j          d| d	          n(t                              d| j        j         d           |r|rt          d	          |d
}t          t          |t          t          f          o,t          |d         t          j        t          t          f                    }|rd |D             }n|s;t          |t          j                  s!t          j        |t          j                  }n^t          |t          j                  rD|j        t          j        t          j                  u r|                    t          j                  }|st          j        |          j        g}t%          |          D ]\  }}	|	j        dk    rt          d|	j                   | j        dk    r)|	j        dk    rt          d|	j        d          d          | j        dk    r/|	j        d         dk    rt          d|	j        d          d          d}
t-          d|i          }| j        | j        ||rXt3          d |D                       }t5          t          j        || j        z                      }|dz
  | j        z  | j        z   }n^|rZt9          d |D                       }t5          t          j        || j        z                      }|dz
  | j        z  | j        z   }d}n|}
|
4|                     |||||          }
|r|
                    d          |
d<   g }|
                    d          D ]/}	| j        dk    r|	d         }	|                     |	j                   0||
d<   ||
!                    |          }
|
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*):
                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                 X    g | ]'}t          j        |t           j                   j        (S )dtype)npasarrayfloat32T).0audios     r   
<listcomp>z4EncodecFeatureExtractor.__call__.<locals>.<listcomp>   s,    VVV5E<<<>VVVr   r0      z6Expected input shape (channels, length) but got shape r   z$Expected mono audio but example has z	 channelsz&Expected stereo audio but example has r   c              3   0   K   | ]}|j         d          V  dS r   Nshaper6   arrays     r   	<genexpr>z3EncodecFeatureExtractor.__call__.<locals>.<genexpr>   (       G GEQ G G G G G Gr   c              3   0   K   | ]}|j         d          V  dS r<   r=   r?   s     r   rA   z3EncodecFeatureExtractor.__call__.<locals>.<genexpr>   rB   r   r+   )r+   r*   r)   return_attention_maskattention_maskr   ).N)"r   
ValueErrorloggerwarningr   __name__bool
isinstancelisttupler2   ndarrayr3   r4   r1   float64astyper5   	enumeratendimr>   r   r   r'   r$   minr"   floorr&   ceilpadpopappendconvert_to_tensors)r   r(   r)   r*   r+   r,   r   
is_batchedidxexamplepadded_inputsr   nb_steps                r   __call__z EncodecFeatureExtractor.__call__T   sY   T $ 222 F$ F F*F F*F F5BF F F   3 NN\W[WeWn \ \ \  
  	z 	`aaa_Gy4-00jj1PRPZ\acgOh6i6i
 

  	5VVIVVVII 	5Jy"*$E$E 	5
9BJ???II	2:.. 	59?bhrzFZFZ3Z3Z!((44I  	2I..01I &i00 	h 	hLC|a !iZaZg!i!ijjj A%%',!*;*; !dVXHY!d!d!deee A%%'-*;q*@*@ !f'-XZJ[!f!f!fggg#^Y$?@@(T->-JzOa 
-  G GY G G GGG
bhzD4E'EFFGG%kT->>ARR

 -  G GY G G GGG
bgj43D&DEEFF%kT->>ARR
& ,   HH%%&- %  M  T0=0A0ABR0S0Sn-$((88 	+ 	+G A%%!),	****(4n%%)<<^LLMr   )r   r   r   NN)NFNNN)rI   
__module____qualname____doc__model_input_namesr"   floatr   r   propertyr$   r'   r   r2   rN   rL   rJ   strr   r	   r   r_   __classcell__)r   s   @r   r   r      s        . (8 ""*.#'   	
 ! %      Ahsm A A A XA Ihsm I I I XI @D%*$(;?'+z zT%[$rz2BDeDUUVz %c? :;<z TN	z
 SMz !sJ!78z  }z 
z z z z z z z zr   r   )rb   typingr   r   numpyr2   !feature_extraction_sequence_utilsr   feature_extraction_utilsr   utilsr   r	   r
   
get_loggerrI   rG   r   __all__r   r   r   <module>ro      s    + * " " " " " " " "     I I I I I I 4 4 4 4 4 4 9 9 9 9 9 9 9 9 9 9 
	H	%	%q q q q q6 q q qh %
%r   