Ë
    ÜÓ^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  e«       rd dlZd dlmc mZ  e«       rd dlZ ej.                  e«      Z G d	„ d
e«      Zd
gZy)é    N)ÚAnyé   )Ú
AudioInputÚmake_list_of_audio)ÚSequenceFeatureExtractor)ÚBatchFeature)ÚPaddingStrategyÚ
TensorTypeÚlogging)Úis_torch_availableÚis_torchaudio_availablec                   ó¤   ‡ — 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_maskc                 ó¾   •— t        ‰| �  d
|||dœ|¤Ž || _        t        d|d¬«      | _        ddg| j                  _        d| _        d| _        d| _        d	| _	        y )N)Úfeature_sizeÚsampling_rateÚpadding_valueé   ç        Úaudior   é   éP   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         €ú�/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/xcodec2/feature_extraction_xcodec2.pyr    z Xcodec2FeatureExtractor.__init__:   sv   ø€ ô 	‰ÑÐw lÀ-Ð_lÑwÐpvÒwð %ˆŒÜ'?ØØ'Øô(
ˆÔ$ð
 ;BÀ>Ð9Rˆ×$Ñ$Ô6ð ˆŒØˆÔØˆÔØˆÕó    Nr   ÚpaddingÚ
max_lengthÚ
truncationÚreturn_tensorsr   ÚdeviceÚreturnc                 ó`  — t        «       st        d«      ‚|�;|| j                  k7  rYt        d| › d| j                  › d| j                  › d|› d�	«      ‚t        j                  d| j                  j                  › d	�«       t        |«      }|D ])  }	|	j                  d
kD  sŒt        d|	j                  › �«      ‚ t        |«      }
|D �cg c]-  }t        j                  t        j                  |«      dd¬«      ‘Œ/ }}| j                   j                  t#        d|i«      ||||| j$                  d¬«      }|j'                  d«      }|d   dd…ddd…f   }g }t)        |
«      D �]²  }|�*t+        ||   j-                  «       j/                  «       «      n|j                  d   }|| j$                  z   dz
  | j$                  z  | j$                  z  }t1        ||j                  d   «      }||dd…d|…f   }t        j                  || j$                  d
z  | j$                  d
z  fd¬«      }|j3                  |«      }t4        j6                  j8                  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  ¬«      }||jC                  d«      z
  t        jD                  |jG                  dd¬«      dz   «      z  }|jI                  |«       �Œµ t#        d |i«      }| j                  ||||| jJ                  |d¬!«      }|d    }|jM                  d«      }|j                  d   |j                  d   | jJ                  z  z
  }|dd…d|…dd…f   jO                  |
|| jJ                  z  | j<                  | jJ                  z  «      }|�M|dd…d|…f   jO                  |
|| jJ                  z  | jJ                  «      j1                  d¬"«      jP                  }t#        ||||d#œ|¬$«      S c c}w )%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.
        z;PyTorch is required for mel-filter bank feature extraction.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 )r   r   r   )Úvaluer   Úpt)r.   r/   r-   Úreturn_attention_maskÚpad_to_multiple_ofr0   Úattention_maskéÿÿÿÿ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   )r-   r.   r/   r8   r7   r0   )Údim)r   r   r   r   )Útensor_type))r   ÚImportErrorr   Ú
ValueErrorÚloggerÚwarningr*   Ú__name__r   ÚndimÚshapeÚlenÚFÚpadÚtorchÚ	as_tensorr"   r   r!   ÚpopÚrangeÚintÚsumÚitemÚminÚtoÚ
torchaudioÚ
complianceÚkaldiÚfbankr%   r&   r'   ÚmeanÚsqrtÚvarÚappendr$   ÚgetÚreshapeÚvalues)r(   r   r-   r.   r/   r0   r   r1   r)   ÚexampleÚ
batch_sizeÚaÚ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                              r+   Ú__call__z Xcodec2FeatureExtractor.__call__T   st  € ôZ "Ô#ÜÐ[Ó\Ð\àÐ$Ø × 2Ñ 2Ò2Ü ØIÈ$Èð PØ×*Ñ*Ð+ð ,Ø×*Ñ*Ð+¨9°]°OÀ1ðFóð ô �N‰NØVÐW[×WeÑWe×WnÑWnÐVoð p\ð \ôô
 # 5Ó)ˆØò 	kˆGØ�|‰|˜aÓÜ Ð#YÐZa×ZgÑZgÐYhÐ!iÓjÐjð	kô ˜“Zˆ
ð HMÖMÀ!”—‘”u—‘ qÓ)¨6¸Ö=ÐMˆÐMØ×4Ñ4×8Ñ8Ü˜' 5Ð)Ó*Ø!Ø!ØØ")Ø#Ÿ™Øð 9ó 
ˆð %×(Ñ(Ð)9Ó:ˆØ$ WÑ-ªa°²q¨jÑ9ˆð
 ˆÜ�zÓ"ó 	*ˆAØ<HÐ<T”s˜<¨™?×.Ñ.Ó0×5Ñ5Ó7Ô8ÐZf×ZlÑZlÐmoÑZpˆHØ'¨$¯/©/Ñ9¸AÑ=À$Ç/Á/ÑQÐUY×UdÑUdÑdˆNÜ˜N¨L×,>Ñ,>¸rÑ,BÓCˆIØ# A¢q¨*¨9¨*Ð$4Ñ5ˆHÜ—u‘u˜X¨¯©¸1Ñ(<¸d¿o¹oÐQRÑ>RÐ'SÐ[^Ô_ˆHØ—{‘{ 6Ó*ˆHÜ!×,Ñ,×2Ñ2×8Ñ8Ø˜EÑ"Ø!×.Ñ.Ø!×.Ñ.°×1CÑ1CÑCÀdÑJØ ×,Ñ,¨t×/AÑ/AÑAÀDÑHØ!%×!3Ñ!3Ø#Ø(,Ø!%Ø"Ø ØØØØ×,Ñ,°Ñ1ð 9ó ˆHð  ! 8§=¡=°Ó#3Ñ3´u·z±zÀ(Ç,Á,ÈqÐ[_À,ÓB`ÐcgÑBgÓ7hÑhˆHØ×Ñ Ö)ð1	*ô2 &Ð'7¸Ð&FÓGˆØ—X‘XØØØ!Ø!Ø#Ÿ{™{Ø")Øð ó 
ˆ
ð 'Ð'7Ñ8ÐØ%Ÿ>™>Ð*:Ó;ÐØ*×0Ñ0°Ñ3Ð7H×7NÑ7NÈqÑ7QÐTX×T_ÑT_Ñ7_Ñ`ˆØ-ªa°°.°Â!Ð.CÑD×LÑLØ˜¨$¯+©+Ñ5°t×7HÑ7HÈ4Ï;É;Ñ7Vó
Ðð Ð'à ¢ O ^ OÐ!3Ñ4ß‘˜ ^°t·{±{Ñ%BÀDÇKÁKÓPß‘˜�“ß‘ð	 ô à ,Ø ,Ø"3Ø'7ñ	ð 'ô
ð 	
ùòE Ns   Ã2P+c                 óš   — t        j                  | j                  «      }| j                  j                  |d<   |j                  dd «       |S )NÚfeature_extractor_typer"   )ÚcopyÚdeepcopyÚ__dict__r*   rN   rV   )r(   Úoutputs     r+   Úto_dictzXcodec2FeatureExtractor.to_dictä   s=   € Ü—‘˜tŸ}™}Ó-ˆØ+/¯>©>×+BÑ+BˆÐ'Ñ(Ø�
‰
Ð,¨dÔ3Øˆr,   )r   i€>  g      ð?i@  )TNFNNÚcpu)rN   Ú
__module__Ú__qualname__Ú__doc__r#   r    r   ÚboolÚstrr	   rX   r
   r   ry   Údictr   r€   Ú__classcell__)r*   s   @r+   r   r   $   sÄ   ø„ ñò& bÐð ØØØõð: 15Ø!%Ø Ø26Ø$(ØñN
àðN
ð ˜‘˜oÑ-ðN
ð ˜$‘Jð	N
ð
 ðN
ð ˜jÑ(¨4Ñ/ðN
ð ˜T‘zðN
ð ðN
ð 
óN
ð`˜˜c 3˜h™÷ r,   r   )r|   Útypingr   Úaudio_utilsr   r   Ú!feature_extraction_sequence_utilsr   Úfeature_extraction_utilsr   Úutilsr	   r
   r   Úutils.import_utilsr   r   rT   Útorch.nn.functionalÚnnÚ
functionalrR   r]   Ú
get_loggerrN   rL   r   Ú__all__r   r,   r+   ú<module>r”      sg   ðó Ý ç 9Ý IÝ 4ß 9Ñ 9ß Mñ ÔÛß#Ð#áÔÛð 
ˆ×	Ñ	˜HÓ	%€ôDÐ6ô DðN %Ð
%�r,   