§
    9²¸j‚*  ã                   ó  — d dl 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mZmZ ddlmZmZmZ  e¦   «         rd dlZd dlmc mZ  e¦   «         rd dlZ ej        e¦  «        Z ed	¬
¦  «         G d„ de¦  «        ¦   «         ZdgZdS )é    N)ÚAnyé   )Ú
AudioInputÚmake_list_of_audio)ÚSequenceFeatureExtractor)ÚBatchFeature)ÚPaddingStrategyÚ
TensorTypeÚlogging)Úis_torch_availableÚis_torchaudio_availableÚrequires)Ú
torchaudio)Úbackendsc                   óª   ‡ — e Zd ZdZg d¢Z	 	 	 	 dˆ fd„	Z	 	 	 	 	 	 ddedeez  e	z  de
d	z  dedeez  d	z  de
d	z  dedefd„Zdeeef         fd„Zˆ xZS )ÚXcodec2FeatureExtractoraø  
    Constructs a Xcodec2 feature extractor, which computes mel-filter bank features for the semantic encoder and padded
    audio for the acoustic encoder.

    This feature extractor inherits from [`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 80):
            The feature dimension of the extracted features.
        sampling_rate (`int`, *optional*, defaults to 16000):
            The sample rate at which the audio files should be digitalized expressed in hertz (Hz).
        padding_value (`float`, *optional*, defaults to 1.0):
            The value that is used to fill the padding vectors for the mel spectrogram.
        hop_length (`int`, *optional*, defaults to 320):
            Number of audio samples encoded per frame. Equivalent to product of downsampling ratios.
            Needed for acoustic encoder input padding.
    )Úinput_featuresÚinput_valuesÚpadding_maskÚinput_features_maskéP   é€>  ç      ð?é@  c                 óÒ   •—  t          ¦   «         j        d
|||dœ|¤Ž || _        t          d|d¬¦  «        | _        ddg| j        _        d| _        d| _        d| _        d	| _	        d S )N)Úfeature_sizeÚsampling_rateÚpadding_valueé   ç        Úaudior   é   r   i�  é    © )
ÚsuperÚ__init__Ú
hop_lengthr   Úacoustic_encoder_padderÚmodel_input_namesÚstrideÚnum_mel_binsÚframe_lengthÚframe_shift)Úselfr   r   r   r'   ÚkwargsÚ	__class__s         €ú†/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/xcodec2/feature_extraction_xcodec2.pyr&   z Xcodec2FeatureExtractor.__init__;   s�   ø€ ð 	�‰ŒÔÐw lÀ-Ð_lÐwÐwÐpvÐwÐwÐwð %ˆŒÝ'?ØØ'Øð(
ñ (
ô (
ˆÔ$ð
 ;BÀ>Ð9RˆÔ$Ô6ð ˆŒØˆÔØˆÔØˆÔÐÐó    TNFÚcpur!   ÚpaddingÚ
max_lengthÚ
truncationÚreturn_tensorsr   ÚdeviceÚreturnc                 ó~  — |�2|| j         k    r&t          d| › d| j         › d| j         › d|› d�	¦  «        ‚n(t                               d| j        j        › d�¦  «         t          |¦  «        }|D ]$}	|	j        d	k    rt          d
|	j        › �¦  «        ‚Œ%t          |¦  «        }
d„ |D ¦   «         }| j
                             t          d|i¦  «        ||||| j        d¬¦  «        }|                     d¦  «        }|d         dd…ddd…f         }g }t          |
¦  «        D �]‘}|�9t!          ||                              ¦   «                              ¦   «         ¦  «        n|j        d         }|| j        z   dz
  | j        z  | j        z  }t'          ||j        d         ¦  «        }||dd…d|…f         }t)          j        || j        d	z  | j        d	z  fd¬¦  «        }|                     |¦  «        }t,          j        j                             |dz  | j        | j        | j         z  dz  | j        | j         z  dz  | j         dddddddd| j         d	z  ¬¦  «        }||                     d¦  «        z
  t=          j        |                      dd¬¦  «        dz   ¦  «        z  }| !                    |¦  «         �Œ“t          d|i¦  «        }|                      ||||| j"        |d¬ ¦  «        }|d         }| #                    d¦  «        }|j        d         |j        d         | j"        z  z
  }|dd…d|…dd…f          $                    |
|| j"        z  | j        | j"        z  ¦  «        }|�I|dd…d|…f          $                    |
|| j"        z  | j"        ¦  «                             d¬!¦  «        j%        }t          ||||d"œ|¬#¦  «        S )$aÄ  
        Args:
            audio (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`):
                Numpy array or torch tensor with shape (num_channels, sequence_length). A list of such arrays or
                tensors can also be provided for a batch of inputs.
            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).
            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*.
            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 sample rate at which the `audio` input was sampled. It is strongly recommended to pass
                `sampling_rate` at the forward call to prevent silent errors.
            device (`str`, *optional*, defaults to `"cpu"`):
                Device for PyTorch tensors during mel-filter bank feature extraction.
            kwargs (*optional*):
                Remaining dictionary of keyword arguments that will be passed to the tokenizer or the feature
                extractor.
        Nz3The model corresponding to this feature extractor: z& was trained using a sampling rate of zD. 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.r"   z6Expected input shape (channels, length) but got shape c                 ó`   — g | ]+}t          j        t          j        |¦  «        d d¬¦  «        ‘Œ,S ))r   r   r    ©Úvalue)ÚFÚpadÚtorchÚ	as_tensor)Ú.0Úas     r1   ú
<listcomp>z4Xcodec2FeatureExtractor.__call__.<locals>.<listcomp>–   s3   € ÐMÐMÐMÀ!•”•u” qÑ)Ô)¨6¸Ð=Ñ=Ô=ÐMÐMÐMr2   r!   Úpt)r5   r6   r4   Úreturn_attention_maskÚpad_to_multiple_ofr7   Úattention_maskéÿÿÿÿr   r    r=   i €  iè  Úpoveyg
×£p=
ï?TFé   )r+   r,   r-   Úsample_frequencyÚwindow_typeÚpreemphasis_coefficientÚremove_dc_offsetÚuse_log_fbankÚ
use_energyÚditherÚ
snip_edgesÚlow_freqÚ	high_freqr   )ÚunbiasedgH¯¼šò×z>r   )r4   r5   r6   rH   rG   r7   )Údim)r   r   r   r   )Útensor_type)&r   Ú
ValueErrorÚloggerÚwarningr0   Ú__name__r   ÚndimÚshapeÚlenr(   r@   r   r'   ÚpopÚrangeÚintÚsumÚitemÚminr?   Útor   Ú
complianceÚkaldiÚfbankr+   r,   r-   ÚmeanrA   ÚsqrtÚvarÚappendr*   ÚgetÚreshapeÚvalues)r.   r!   r4   r5   r6   r7   r   r8   r/   ÚexampleÚ
batch_sizeÚpadded_inputsr   Úpadded_audioÚmel_featuresÚiÚorig_lenÚper_sample_lenÚ	valid_lenÚwaveformÚfeaturesÚencoded_inputsÚ
padded_melÚaudio_spectrogramÚspectrogram_maskÚtrimmed_framess                             r1   Ú__call__z Xcodec2FeatureExtractor.__call__U   sŸ  € ðZ Ð$Ø Ô 2Ò2Ð2Ý ðFÈ$ð Fð FØÔ*ðFð FàÔ*ðFð Fà5BðFð Fð Fñô ð ð 3õ �NŠNð\ÐW[ÔWeÔWnð \ð \ð \ñô ð õ
 # 5Ñ)Ô)ˆØð 	kð 	kˆGØŒ|˜aÒÐÝ Ð!iÐZaÔZgÐ!iÐ!iÑjÔjÐjð  å˜‘Z”Zˆ
ð NÐMÀuÐMÑMÔMˆØÔ4×8Ò8Ý˜' 5Ð)Ñ*Ô*Ø!Ø!ØØ")Ø#œØð 9ñ 
ô 
ˆð %×(Ò(Ð)9Ñ:Ô:ˆØ$ WÔ-¨a¨a¨a°°q°q°q¨jÔ9ˆð
 ˆÝ�zÑ"Ô"ð 	*ñ 	*ˆAØ<HÐ<T•s˜<¨œ?×.Ò.Ñ0Ô0×5Ò5Ñ7Ô7Ñ8Ô8Ð8ÐZfÔZlÐmoÔZpˆHØ'¨$¬/Ñ9¸AÑ=À$Ä/ÑQÐUYÔUdÑdˆNÝ˜N¨LÔ,>¸rÔ,BÑCÔCˆIØ# A q q q¨*¨9¨*Ð$4Ô5ˆHÝ”u˜X¨¬¸1Ñ(<¸d¼oÐQRÑ>RÐ'SÐ[^Ð_Ñ_Ô_ˆHØ—{’{ 6Ñ*Ô*ˆHÝ!Ô,Ô2×8Ò8Ø˜EÑ"Ø!Ô.Ø!Ô.°Ô1CÑCÀdÑJØ Ô,¨tÔ/AÑAÀDÑHØ!%Ô!3Ø#Ø(,Ø!%Ø"Ø ØØØØÔ,°Ñ1ð 9ñ ô ˆHð  ! 8§=¢=°Ñ#3Ô#3Ñ3µu´zÀ(Ç,Â,ÈqÐ[_À,ÑB`ÔB`ÐcgÑBgÑ7hÔ7hÑhˆHØ×Ò Ñ)Ô)Ð)Ñ)Ý%Ð'7¸Ð&FÑGÔGˆØ—X’XØØØ!Ø!Ø#œ{Ø")Øð ñ 
ô 
ˆ
ð 'Ð'7Ô8ÐØ%Ÿ>š>Ð*:Ñ;Ô;ÐØ*Ô0°Ô3Ð7HÔ7NÈqÔ7QÐTXÔT_Ñ7_Ñ`ˆØ-¨a¨a¨a°°.°À!À!À!Ð.CÔD×LÒLØ˜¨$¬+Ñ5°tÔ7HÈ4Ì;Ñ7Vñ
ô 
Ðð Ð'à     O ^ OÐ!3Ô4ß’˜ ^°t´{Ñ%BÀDÄKÑPÔPß’˜�‘”Üð	 õ à ,Ø ,Ø"3Ø'7ð	ð ð 'ð
ñ 
ô 
ð 	
r2   c                 ó‚   — t          j        | j        ¦  «        }| j        j        |d<   |                     dd ¦  «         |S )NÚfeature_extractor_typer(   )ÚcopyÚdeepcopyÚ__dict__r0   r]   ra   )r.   Úoutputs     r1   Úto_dictzXcodec2FeatureExtractor.to_dictâ   s=   € Ý”˜tœ}Ñ-Ô-ˆØ+/¬>Ô+BˆÐ'Ñ(Ø�
Š
Ð,¨dÑ3Ô3Ð3Øˆr2   )r   r   r   r   )TNFNNr3   )r]   Ú
__module__Ú__qualname__Ú__doc__r)   r&   r   ÚboolÚstrr	   rc   r
   r   r‚   Údictr   r‰   Ú__classcell__)r0   s   @r1   r   r   $   s+  ø€ € € € € ðð ð& bÐaÐaÐð ØØØðð ð ð ð ð ð: 15Ø!%Ø Ø26Ø$(ØðK
ð K
àðK
ð ˜‘˜oÑ-ðK
ð ˜$‘Jð	K
ð
 ðK
ð ˜jÑ(¨4Ñ/ðK
ð ˜T‘zðK
ð ðK
ð 
ðK
ð K
ð K
ð K
ðZ˜˜c 3˜hœð ð ð ð ð ð ð ð r2   r   )r…   Útypingr   Úaudio_utilsr   r   Ú!feature_extraction_sequence_utilsr   Úfeature_extraction_utilsr   Úutilsr	   r
   r   Úutils.import_utilsr   r   r   rA   Útorch.nn.functionalÚnnÚ
functionalr?   r   Ú
get_loggerr]   r[   r   Ú__all__r$   r2   r1   ú<module>rœ      s^  ðð €€€Ø Ð Ð Ð Ð Ð à 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø IÐ IÐ IÐ IÐ IÐ IØ 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ WÐ Wð ÐÑÔð $Ø€L€L€LØ#Ð#Ð#Ð#Ð#Ð#Ð#Ð#Ð#àÐÑÔð ØÐÐÐð 
ˆÔ	˜HÑ	%Ô	%€ð 
€�?Ð#Ñ#Ô#ðAð Að Að Að AÐ6ñ Aô Añ $Ô#ðAðH %Ð
%€€€r2   