§
    9²¸jTA  ã                   ó¤   — d Z ddlmZ ddlZddlmZmZmZm	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 SpeechT5.é    )ÚAnyNé   )Úmel_filter_bankÚoptimal_fft_lengthÚspectrogramÚwindow_function)ÚSequenceFeatureExtractor)ÚBatchFeature)ÚPaddingStrategyÚ
TensorTypeÚloggingc                   ó  ‡ — e Zd ZdZddgZ	 	 	 	 	 	 	 	 	 	 	 	 d-dedededededededededededefˆ fd„Z	e
	 d.deej                 deej                 dedeej                 fd„¦   «         Zdej        dej        fd„Z	 	 	 	 	 	 	 	 	 d/d!ej        ee         z  eej                 z  eee                  z  d z  d"ej        ee         z  eej                 z  eee                  z  d z  d#eez  ez  d$ed z  d%ed&ed z  ded z  d'eez  d z  ded z  defd(„Z	 	 	 	 	 	 	 d0d)ej        ee         z  eej                 z  eee                  z  d*ed#eez  ez  d$ed z  d%ed&ed z  ded z  d'eez  d z  defd+„Zdeeef         fˆ fd,„Zˆ xZS )1ÚSpeechT5FeatureExtractoraË  
    Constructs a SpeechT5 feature extractor.

    This class can pre-process a raw speech signal by (optionally) normalizing to zero-mean unit-variance, for use by
    the SpeechT5 speech encoder prenet.

    This class can also extract log-mel filter bank features from raw speech, for use by the SpeechT5 speech decoder
    prenet.

    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.
        sampling_rate (`int`, *optional*, defaults to 16000):
            The sampling rate at which the audio files 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.
        do_normalize (`bool`, *optional*, defaults to `False`):
            Whether or not to zero-mean unit-variance normalize the input. Normalizing can help to significantly
            improve the performance for some models.
        num_mel_bins (`int`, *optional*, defaults to 80):
            The number of mel-frequency bins in the extracted spectrogram features.
        hop_length (`int`, *optional*, defaults to 16):
            Number of ms between windows. Otherwise referred to as "shift" in many papers.
        win_length (`int`, *optional*, defaults to 64):
            Number of ms per window.
        win_function (`str`, *optional*, defaults to `"hann_window"`):
            Name for the window function used for windowing, must be accessible via `torch.{win_function}`
        fmin (`float`, *optional*, defaults to 80):
            Minimum mel frequency in Hz.
        fmax (`float`, *optional*, defaults to 7600):
            Maximum mel frequency in Hz.
        mel_floor (`float`, *optional*, defaults to 1e-10):
            Minimum value of mel frequency banks..
        return_attention_mask (`bool`, *optional*, defaults to `True`):
            Whether or not [`~SpeechT5FeatureExtractor.__call__`] should return `attention_mask`.
    Úinput_valuesÚattention_maské   é€>  ç        FéP   é   é@   Úhann_windowé°  ç»½×Ùß|Û=TÚfeature_sizeÚsampling_rateÚpadding_valueÚdo_normalizeÚnum_mel_binsÚ
hop_lengthÚ
win_lengthÚwin_functionÚfminÚfmaxÚ	mel_floorÚreturn_attention_maskc           	      óô  •—  t          ¦   «         j        d	|||dœ|¤Ž || _        || _        || _        || _        || _        || _        |	| _        |
| _	        || _
        ||z  dz  | _        ||z  dz  | _        t          | j        ¦  «        | _        | j        dz  dz   | _        t!          | j        | j        d¬¦  «        | _        t%          | j        | j        | j        | j	        | j        dd¬¦  «        | _        d S )
N)r   r   r   iè  é   r   T)Úwindow_lengthÚnameÚperiodicÚslaney)Únum_frequency_binsÚnum_mel_filtersÚmin_frequencyÚmax_frequencyr   ÚnormÚ	mel_scale© )ÚsuperÚ__init__r   r&   r   r    r!   r"   r#   r$   r%   Úsample_sizeÚsample_strider   Ún_fftÚn_freqsr   Úwindowr   r   Úmel_filters)Úselfr   r   r   r   r   r    r!   r"   r#   r$   r%   r&   ÚkwargsÚ	__class__s                 €úˆ/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/speecht5/feature_extraction_speecht5.pyr5   z!SpeechT5FeatureExtractor.__init__H   s  ø€ ð  	�‰ŒÔÐw lÀ-Ð_lÐwÐwÐpvÐwÐwÐwØ(ˆÔØ%:ˆÔ"à(ˆÔØ$ˆŒØ$ˆŒØ(ˆÔØˆŒ	ØˆŒ	Ø"ˆŒà%¨Ñ5¸Ñ=ˆÔØ'¨-Ñ7¸4Ñ?ˆÔÝ'¨Ô(8Ñ9Ô9ˆŒ
Øœ
 a™¨1Ñ,ˆŒå%°DÔ4DÈ4ÔK\ÐgkÐlÑlÔlˆŒå*Ø#œ|Ø Ô-Øœ)Øœ)ØÔ,ØØð
ñ 
ô 
ˆÔÐÐó    Úreturnc                 ó´  — |�Ét          j        |t           j        ¦  «        }g }t          | |                     d¦  «        ¦  «        D ]ƒ\  }}||d|…                              ¦   «         z
  t          j        |d|…                              ¦   «         dz   ¦  «        z  }||j        d         k     r|||d…<   | 	                    |¦  «         Œ„nd„ | D ¦   «         }|S )z[
        Every array in the list is normalized to have zero mean and unit variance
        NéÿÿÿÿçH¯¼šò×z>r   c                 ó�   — g | ]C}||                      ¦   «         z
  t          j        |                     ¦   «         d z   ¦  «        z  ‘ŒDS )rD   )ÚmeanÚnpÚsqrtÚvar)Ú.0Úxs     r?   ú
<listcomp>zDSpeechT5FeatureExtractor.zero_mean_unit_var_norm.<locals>.<listcomp>ˆ   s@   € Ð"bÐ"bÐ"bÐPQ A¨¯ª©¬¡LµB´G¸A¿EºE¹G¼GÀd¹NÑ4KÔ4KÑ#KÐ"bÐ"bÐ"br@   )
rG   ÚarrayÚint32ÚzipÚsumrF   rH   rI   ÚshapeÚappend)r   r   r   Únormed_input_valuesÚvectorÚlengthÚnormed_slices          r?   Úzero_mean_unit_var_normz0SpeechT5FeatureExtractor.zero_mean_unit_var_normu   só   € ð Ð%ÝœX nµb´hÑ?Ô?ˆNØ"$Ðå"% l°N×4FÒ4FÀrÑ4JÔ4JÑ"KÔ"Kð 9ð 9‘�˜Ø &¨°°°¬×)=Ò)=Ñ)?Ô)?Ñ ?Å2Ä7È6ÐRYÐSYÐRYÌ?×K^ÒK^ÑK`ÔK`ÐcgÑKgÑChÔChÑh�Ø˜LÔ.¨qÔ1Ò1Ð1Ø,9�L   Ñ)à#×*Ò*¨<Ñ8Ô8Ð8Ð8ð9ð #cÐ"bÐUaÐ"bÑ"bÔ"bÐà"Ð"r@   Úone_waveformc           
      óz   — t          || j        | j        | j        | j        | j        | j        d¬¦  «        }|j        S )zZ
        Extracts log-mel filterbank features for one waveform array (unbatched).
        Úlog10)r:   Úframe_lengthr    Ú
fft_lengthr;   r%   Úlog_mel)r   r:   r6   r7   r8   r;   r%   ÚT)r<   rX   Úlog_mel_specs      r?   Ú_extract_mel_featuresz.SpeechT5FeatureExtractor._extract_mel_featuresŒ   sI   € õ #ØØ”;ØÔ)ØÔ)Ø”zØÔ(Ø”nØð	
ñ 	
ô 	
ˆð Œ~Ðr@   NÚaudioÚaudio_targetÚpaddingÚ
max_lengthÚ
truncationÚpad_to_multiple_ofÚreturn_tensorsc
                 óœ  — |€|€t          d¦  «        ‚|	�2|	| j        k    r&t          d| › d| j        › d| j        › d|	› d�	¦  «        ‚n(t                               d| j        j        › d	�¦  «         |� | j        |d
||||||fi |
¤Ž}nd}|�@ | j        |d||||||fi |
¤Ž}|€|S |d         |d<   |                     d¦  «        }|�||d<   |S )aþ  
        Main method to featurize and prepare for the model one or several sequence(s).

        Pass in a value for `audio` to extract waveform features. Pass in a value for `audio_target` to extract log-mel
        spectrogram features.

        Args:
            audio (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`, *optional*):
                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. This outputs waveform features. Must
                be mono channel audio, not stereo, i.e. single float per timestep.
            audio_target (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`, *optional*):
                The sequence or batch of sequences to be processed as targets. 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. This outputs log-mel
                spectrogram features.
            padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):
                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).
            max_length (`int`, *optional*):
                Maximum length of the returned list and optionally padding length (see above).
            truncation (`bool`):
                Activates truncation to cut input sequences longer than *max_length* to *max_length*.
            pad_to_multiple_of (`int`, *optional*):
                If set will pad the sequence to a multiple of the provided value.

                This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability
                `>= 7.5` (Volta), or on TPUs which benefit from having sequence lengths be a multiple of 128.
            return_attention_mask (`bool`, *optional*):
                Whether to return the attention mask. If left to the default, will return the attention mask according
                to the specific feature_extractor's default.

                [What are attention masks?](../glossary#attention-mask)

            return_tensors (`str` or [`~utils.TensorType`], *optional*):
                If set, will return tensors instead of list of python integers. Acceptable values are:

                - `'pt'`: Return PyTorch `torch.Tensor` objects.
                - `'np'`: Return Numpy `np.ndarray` objects.
            sampling_rate (`int`, *optional*):
                The sampling rate at which the `audio` or `audio_target` input was sampled. It is strongly recommended
                to pass `sampling_rate` at the forward call to prevent silent errors.
        Nz9You must provide either `audio` or `audio_target` values.z3The 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.FTr   Úlabelsr   Údecoder_attention_mask)Ú
ValueErrorr   ÚloggerÚwarningr>   Ú__name__Ú_process_audioÚget)r<   ra   rb   rc   rd   re   rf   r&   rg   r   r=   ÚinputsÚinputs_targetrk   s                 r?   Ú__call__z!SpeechT5FeatureExtractor.__call__Ÿ   s£  € ð| ˆ=˜\Ð1ÝÐXÑYÔYÐYàÐ$Ø Ô 2Ò2Ð2Ý ðFÈ$ð Fð FØÔ*ðFð FàÔ*ðFð Fà5BðFð Fð Fñô ð ð 3õ �NŠNð\ÐW[ÔWeÔWnð \ð \ð \ñô ð ð
 ÐØ(�TÔ(ØØØØØØ"Ø%Øð
ð 
ð ð
ð 
ˆFˆFð ˆFàÐ#Ø/˜DÔ/ØØØØØØ"Ø%Øð
ð 
ð ð
ð 
ˆMð ˆ~Ø$Ð$à#0°Ô#@��xÑ Ø)6×):Ò):Ð;KÑ)LÔ)LÐ&Ø)Ð5Ø7M�FÐ3Ñ4àˆr@   ÚspeechÚ	is_targetc	           	      ó¼  ‡ — t          |t          j        ¦  «        ot          |j        ¦  «        dk    }
|
r*t          |j        ¦  «        dk    rt          d‰ › �¦  «        ‚|
pHt          |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|g}‰ j        }|r,ˆ fd„|D ¦   «         }t          d|i¦  «        }‰ j        ‰ _        nt          d|i¦  «        } ‰ j        |f|||||d	œ|	¤Ž}|‰ _        |d         }t          |d         t          j        ¦  «        sd
„ |D ¦   «         |d<   nÖt          |t          j        ¦  «        s[t          |d         t          j        ¦  «        r;|d         j
        t          j
        t          j        ¦  «        u rd„ |D ¦   «         |d<   nat          |t          j        ¦  «        rG|j
        t          j
        t          j        ¦  «        u r"|                     t          j	        ¦  «        |d<   |                     d¦  «        }|�d„ |D ¦   «         |d<   |sT‰ j        rM‰                      ||¬¦  «        t(          j        ur|nd }‰                      |d         |‰ j        ¬¦  «        |d<   |�|                     |¦  «        }|S )Nr   r(   z2Only mono-channel audio is supported for input to r   c                 óN   — g | ]"}t          j        |t           j        ¬ ¦  «        ‘Œ#S ©©Údtype©rG   ÚasarrayÚfloat32)rJ   ru   s     r?   rL   z;SpeechT5FeatureExtractor._process_audio.<locals>.<listcomp>'  s)   € ÐPÐPÐP¸v•b”j ­r¬zÐ:Ñ:Ô:ÐPÐPÐPr@   rz   c                 ó:   •— g | ]}‰                      |¦  «        ‘ŒS r3   )r`   )rJ   Úwaveformr<   s     €r?   rL   z;SpeechT5FeatureExtractor._process_audio.<locals>.<listcomp>6  s'   ø€ ÐTÐTÐTÀ˜×2Ò2°8Ñ<Ô<ÐTÐTÐTr@   r   )rc   rd   re   rf   r&   c                 óN   — g | ]"}t          j        |t           j        ¬ ¦  «        ‘Œ#S ry   r|   ©rJ   rM   s     r?   rL   z;SpeechT5FeatureExtractor._process_audio.<locals>.<listcomp>K  s*   € Ð,kÐ,kÐ,kÐUZ­R¬Z¸ÅRÄZÐ-PÑ-PÔ-PÐ,kÐ,kÐ,kr@   c                 óL   — g | ]!}|                      t          j        ¦  «        ‘Œ"S r3   )ÚastyperG   r~   r‚   s     r?   rL   z;SpeechT5FeatureExtractor._process_audio.<locals>.<listcomp>Q  s&   € Ð,`Ð,`Ð,`È%¨U¯\ª\½"¼*Ñ-EÔ-EÐ,`Ð,`Ð,`r@   r   c                 óN   — g | ]"}t          j        |t           j        ¬ ¦  «        ‘Œ#S ry   )rG   r}   rN   r‚   s     r?   rL   z;SpeechT5FeatureExtractor._process_audio.<locals>.<listcomp>X  s*   € Ð.mÐ.mÐ.mÐUZ­r¬z¸%ÅrÄxÐ/PÑ/PÔ/PÐ.mÐ.mÐ.mr@   )rd   )r   r   )Ú
isinstancerG   ÚndarrayÚlenrQ   rl   ÚlistÚtupler}   r~   r{   Úfloat64r„   r   r
   r   Úpadrq   r   Ú_get_padding_strategiesr   Ú
DO_NOT_PADrW   r   Úconvert_to_tensors)r<   ru   rv   rc   rd   re   rf   r&   rg   r=   Úis_batched_numpyÚ
is_batchedÚfeature_size_hackÚfeaturesÚencoded_inputsÚpadded_inputsr   r   s   `                 r?   rp   z'SpeechT5FeatureExtractor._process_audio  s¨  ø€ õ & f­b¬jÑ9Ô9ÐS½cÀ&Ä,Ñ>OÔ>OÐRSÒ>SÐØð 	Z¥ F¤LÑ 1Ô 1°AÒ 5Ð 5ÝÐXÐRVÐXÐXÑYÔYÐYØ%ð 
Ý�v¥¥e˜}Ñ-Ô-Ðdµ:¸fÀQ¼iÍ"Ì*ÕV[Õ]aÐIbÑ3cÔ3cð 	ð ð 	/ØPÐPÈÐPÑPÔPˆFˆFØð 	/¥J¨vµr´zÑ$BÔ$Bð 	/Ý”Z ­b¬jÐ9Ñ9Ô9ˆFˆFÝ˜¥¤
Ñ+Ô+ð 	/°´ÅÄÍÌÑ@TÔ@TÐ0TÐ0TØ—]’]¥2¤:Ñ.Ô.ˆFð ð 	Ø�XˆFð !Ô-Ðð ð 	DØTÐTÐTÐTÈVÐTÑTÔTˆHÝ)¨>¸8Ð*DÑEÔEˆNØ $Ô 1ˆDÔÐå)¨>¸6Ð*BÑCÔCˆNà ˜œØð
àØ!Ø!Ø1Ø"7ð
ð 
ð ð
ð 
ˆð .ˆÔð % ^Ô4ˆÝ˜, qœ/­2¬:Ñ6Ô6ð 		LØ,kÐ,kÐ^jÐ,kÑ,kÔ,kˆM˜.Ñ)Ð)å˜<­¬Ñ4Ô4ð	Lå˜<¨œ?­B¬JÑ7Ô7ð	Lð ˜Q”Ô%­¬µ"´*Ñ)=Ô)=Ð=Ð=à,`Ð,`ÐS_Ð,`Ñ,`Ô,`ˆM˜.Ñ)Ð)Ý˜¥b¤jÑ1Ô1ð 	L°lÔ6HÍBÌHÕUWÔU_ÑL`ÔL`Ð6`Ð6`Ø,8×,?Ò,?ÅÄ
Ñ,KÔ,KˆM˜.Ñ)ð '×*Ò*Ð+;Ñ<Ô<ˆØÐ%Ø.mÐ.mÐ^lÐ.mÑ.mÔ.mˆMÐ*Ñ+ð ð 	˜TÔ.ð 	ð ×/Ò/°ÀJÐ/ÑOÔOÕWfÔWqÐqÐqð �àð ð
 -1×,HÒ,HØ˜nÔ-¸nÐ\`Ô\nð -Iñ -ô -ˆM˜.Ñ)ð Ð%Ø)×<Ò<¸^ÑLÔLˆMàÐr@   c                 óh   •— t          ¦   «                              ¦   «         }g d¢}|D ]	}||v r||= Œ
|S )N)r:   r;   r6   r7   r8   r9   )r4   Úto_dict)r<   ÚoutputÚnamesr*   r>   s       €r?   r—   z SpeechT5FeatureExtractor.to_dictj  sI   ø€ Ý‘”—’Ñ"Ô"ˆð ^Ð]Ð]ˆØð 	!ð 	!ˆDØ�vˆ~ˆ~Ø˜4�Løàˆr@   )r   r   r   Fr   r   r   r   r   r   r   T)r   )	NNFNFNNNN)FFNFNNN)ro   Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesÚintÚfloatÚboolÚstrr5   Ústaticmethodr‰   rG   r‡   rW   r`   r   r   r
   rt   rp   Údictr   r—   Ú__classcell__)r>   s   @r?   r   r      s®  ø€ € € € € ð&ð &ðP (Ð)9Ð:Ðð Ø"Ø"Ø"ØØØØ)ØØØ Ø&*ð+
ð +
àð+
ð ð+
ð ð	+
ð
 ð+
ð ð+
ð ð+
ð ð+
ð ð+
ð ð+
ð ð+
ð ð+
ð  $ð+
ð +
ð +
ð +
ð +
ð +
ðZ ð beð#ð #Ø˜2œ:Ô&ð#Ø8<¸R¼ZÔ8Hð#ØY^ð#à	ˆbŒjÔ	ð#ð #ð #ñ „\ð#ð*à”jðð 
Œðð ð ð ð* Y]Ø_cØ05Ø!%Ø Ø)-Ø-1Ø26Ø$(ðrð ràŒz˜D œKÑ'¨$¨r¬zÔ*:Ñ:¸TÀ$ÀuÄ+Ô=NÑNÐQUÑUðrð ”j 4¨¤;Ñ.°°b´jÔ1AÑAÀDÈÈeÌÔDUÑUÐX\Ñ\ðrð ˜‘˜oÑ-ð	rð
 ˜$‘Jðrð ðrð   $™Jðrð  $ d™{ðrð ˜jÑ(¨4Ñ/ðrð ˜T‘zðrð 
ðrð rð rð rðn  Ø05Ø!%Ø Ø)-Ø-1Ø26ðUð Uà”
˜T %œ[Ñ(¨4°´
Ô+;Ñ;¸dÀ4ÈÄ;Ô>OÑOðUð ðUð ˜‘˜oÑ-ð	Uð
 ˜$‘JðUð ðUð   $™JðUð  $ d™{ðUð ˜jÑ(¨4Ñ/ðUð 
ðUð Uð Uð Uðn	˜˜c 3˜hœð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	r@   r   )rœ   Útypingr   ÚnumpyrG   Úaudio_utilsr   r   r   r   Ú!feature_extraction_sequence_utilsr	   Úfeature_extraction_utilsr
   Úutilsr   r   r   Ú
get_loggerro   rm   r   Ú__all__r3   r@   r?   ú<module>r­      sç   ðð ,Ð +à Ð Ð Ð Ð Ð à Ð Ð Ð à \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ø IÐ IÐ IÐ IÐ IÐ IØ 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9ð 
ˆÔ	˜HÑ	%Ô	%€ðVð Vð Vð Vð VÐ7ñ Vô Vð Vðr
 &Ð
&€€€r@   