§
    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mZ ddlmZmZ  e¦   «         rd dlZ e¦   «         rd dlZd dlmc m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Úis_torch_availableÚlogging)Úis_torchaudio_availableÚrequires)Ú
torchaudio©Úbackendsc                   óÎ   ‡ — e Zd ZdZg d¢Z	 	 	 	 dˆ fd„	Z ed¬	¦  «        	 	 	 	 	 	 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 )ÚNeuCodecFeatureExtractoraù  
    Constructs a NeuCodec 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/neucodec/feature_extraction_neucodec.pyr'   z!NeuCodecFeatureExtractor.__init__@   s�   ø€ ð 	�‰ŒÔÐw lÀ-Ð_lÐwÐwÐpvÐwÐwÐwð %ˆŒÝ'?ØØ'Øð(
ñ (
ô (
ˆÔ$ð
 ;BÀ>Ð9RˆÔ$Ô6ð ˆŒØˆÔØˆÔØˆÔÐÐó    )Útorchr   r   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 �]}| j
                             t          d||         gi¦  «        | j        d¬¦  «        }|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
  t3          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Úpadr4   Ú	as_tensor)Ú.0Úas     r2   ú
<listcomp>z5NeuCodecFeatureExtractor.__call__.<locals>.<listcomp>�   s3   € ÐMÐMÐMÀ!•”•u” qÑ)Ô)¨6¸Ð=Ñ=Ô=ÐMÐMÐMr3   r"   Úpt)r7   r8   r6   Úreturn_attention_maskÚpad_to_multiple_ofr9   Úattention_mask)rH   r9   i €  iè  Úpoveyg
×£p=
ï?TFr!   é   )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   )r6   r7   r8   rH   rG   r9   r    éÿÿÿÿ)Údim)r   r   r   r   )Útensor_type)"r   Ú
ValueErrorÚloggerÚwarningr1   Ú__name__r   ÚndimÚshapeÚlenr)   rA   r   r(   ÚpopÚrangeÚtor   Ú
complianceÚkaldiÚfbankr,   r-   r.   Úmeanr4   ÚsqrtÚvarÚappendr+   ÚgetÚreshapeÚminÚvalues)r/   r"   r6   r7   r8   r9   r   r:   r0   ÚexampleÚ
batch_sizeÚpadded_inputsr   Úpadded_audioÚmel_featuresÚiÚsingle_padded_audioÚwaveformÚfeaturesÚencoded_inputsÚ
padded_melÚaudio_spectrogramÚspectrogram_maskÚtrimmed_framess                           r2   Ú__call__z!NeuCodecFeatureExtractor.__call__Z   s,  € ð^ Ð$Ø Ô 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Ø"&Ô">×"BÒ"BÝ˜g¨¨a¬ zÐ2Ñ3Ô3Ø#'¤?Ø#ð #Cñ #ô #Ðð
 +¨7Ô3×6Ò6°vÑ>Ô>ˆ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ð	ð ð 'ð
ñ 
ô 
ð 	
r3   c                 ó‚   — t          j        | j        ¦  «        }| j        j        |d<   |                     dd ¦  «         |S )NÚfeature_extractor_typer)   )ÚcopyÚdeepcopyÚ__dict__r1   r]   ra   )r/   Úoutputs     r2   Úto_dictz NeuCodecFeatureExtractor.to_dictë   s=   € Ý”˜tœ}Ñ-Ô-ˆØ+/¬>Ô+BˆÐ'Ñ(Ø�
Š
Ð,¨dÑ3Ô3Ð3Øˆr3   )r   r   r   r   )TNFNNr5   )r]   Ú
__module__Ú__qualname__Ú__doc__r*   r'   r   r   ÚboolÚstrr	   Úintr
   r   r}   Údictr   r„   Ú__classcell__)r1   s   @r2   r   r   )   sF  ø€ € € € € ðð ð& bÐaÐaÐð ØØØðð ð ð ð ð ð4 €XÐ.Ð/Ñ/Ô/ð 15Ø!%Ø Ø26Ø$(ØðN
ð N
àðN
ð ˜‘˜oÑ-ðN
ð ˜$‘Jð	N
ð
 ðN
ð ˜jÑ(¨4Ñ/ðN
ð ˜T‘zðN
ð ðN
ð 
ðN
ð N
ð N
ñ 0Ô/ðN
ð`˜˜c 3˜hœð ð ð ð ð ð ð ð r3   r   )r€   Útypingr   Úaudio_utilsr   r   Ú!feature_extraction_sequence_utilsr   Úfeature_extraction_utilsr   Úutilsr	   r
   r   r   Úutils.import_utilsr   r   r   r4   Útorch.nn.functionalÚnnÚ
functionalr@   Ú
get_loggerr]   r[   r   Ú__all__r%   r3   r2   ú<module>r˜      s^  ðð* €€€Ø Ð Ð Ð Ð Ð à 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø IÐ IÐ IÐ IÐ IÐ IØ 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø MÐ MÐ MÐ MÐ MÐ MÐ MÐ MÐ MÐ MÐ MÐ MØ CÐ CÐ CÐ CÐ CÐ CÐ CÐ Cð ÐÑÔð ØÐÐÐØÐÑÔð $Ø€L€L€LØ#Ð#Ð#Ð#Ð#Ð#Ð#Ð#Ð#ð 
ˆÔ	˜HÑ	%Ô	%€ð 
€�?Ð#Ñ#Ô#ðEð Eð Eð Eð EÐ7ñ Eô Eñ $Ô#ðEðP &Ð
&€€€r3   