Ë
    ÛÓ^j".  ã            	       óä   — d dl Z d dlZd dlZd dlmc mZ ddl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  ej(                  e«      Zdded	ed
edefd„Z ed¬«       G d„ de«      «       ZdgZy)é    Né   )Úmel_filter_bank)ÚSequenceFeatureExtractor)ÚBatchFeature)ÚPaddingStrategyÚ
TensorTypeÚlogging)ÚrequiresÚvalueÚnameÚ	toleranceÚreturnc                 óp   — t        | «      }t        | |z
  «      |kD  rt        |› d| › �«      ‚t        |«      S )Nz. must resolve to an integer sample count, got )ÚroundÚabsÚ
ValueErrorÚint)r   r   r   Úroundeds       ú�/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/inkling/feature_extraction_inkling.pyÚ_to_exact_intr      s>   € Ü�E‹l€GÜ
ˆ5�7‰?Ó˜iÒ'Ü˜D˜6Ð!OÐPUÈwÐWÓXÐXÜˆw‹<Ðó    )Útorch)Úbackendsc                   ó`  ‡ — e Zd ZdZddgZ	 	 	 	 	 	 ddedededed	ed
edz  fˆ fd„Zddej                  de
dej                  fd„Z	 	 	 	 	 	 	 	 ddej                  ee   z  eej                     z  eee      z  de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ˆ xZS )ÚInklingFeatureExtractoraí  
    Constructs a TML audio feature extractor, which converts raw audio waveforms into log-mel spectrogram
    features (mel filterbank energies in log10 space). The quantization of these features into discrete
    dMel bins is performed downstream by [`InklingProcessor`].

    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 80):
            The feature dimension of the extracted features, i.e. the number of mel filterbanks.
        sampling_rate (`int`, *optional*, defaults to 16000):
            The sampling rate at which the audio files should be digitized, expressed in hertz (Hz).
        padding_value (`float`, *optional*, defaults to 0.0):
            The value used to pad the log-mel spectrograms to the same length in a batch.
        audio_token_duration_s (`float`, *optional*, defaults to 0.05):
            Duration, in seconds, represented by a single audio token, i.e. the STFT hop length.
        window_size_multiplier (`float`, *optional*, defaults to 2.0):
            Multiplier applied to `audio_token_duration_s` to obtain the STFT window length.
        n_fft (`int`, *optional*):
            FFT size. Defaults to the window length (`audio_token_duration_s * window_size_multiplier *
            sampling_rate`) when not provided.
    Úinput_featuresÚinput_features_maskNÚfeature_sizeÚsampling_rateÚpadding_valueÚaudio_token_duration_sÚwindow_size_multiplierÚn_fftc           	      óz  •— t        ‰	| �  d|||dœ|¤Ž || _        || _        t	        ||z  d«      | _        t	        ||z  |z  d«      | _        |xs | j                  | _        | j
                  dk  s| j                  dk  s| j                  dk  rt        d«      ‚t        j                  | j                  dt        j                  ¬«      | _        t        | j                  dz  d	z   |d
|dz  |dd¬«      }t        j                  t        j                   |j"                  t        j                  ¬«      «      | _        y )N)r   r   r    z&audio_token_duration_s * sampling_ratez?audio_token_duration_s * window_size_multiplier * sampling_rater   z7hop_length, window_size, and n_fft must all be positiveT)ÚperiodicÚdtypeé   é   ç        ç       @Úslaney)Únum_frequency_binsÚnum_mel_filtersÚmin_frequencyÚmax_frequencyr   ÚnormÚ	mel_scale)r&   © )ÚsuperÚ__init__r!   r"   r   Ú
hop_lengthÚwindow_sizer#   r   r   Úhann_windowÚfloat32Úwindowr   Ú
from_numpyÚnpÚascontiguousarrayÚTÚmel_filters)
Úselfr   r   r    r!   r"   r#   Úkwargsr>   Ú	__class__s
            €r   r4   z InklingFeatureExtractor.__init__B   s8  ø€ ô 	‰Ñð 	
Ø%Ø'Ø'ñ	
ð ò		
ð '=ˆÔ#Ø&<ˆÔ#ä'Ø" ]Ñ2Ð4\ó
ˆŒô )Ø"Ð%;Ñ;¸mÑKØMó
ˆÔð Ò.˜d×.Ñ.ˆŒ
Ø�?‰?˜aÒ 4×#3Ñ#3°qÒ#8¸D¿J¹JÈ!ºOÜÐVÓWÐWô ×'Ñ'¨×(8Ñ(8À4ÌuÏ}É}Ô]ˆŒô &Ø#Ÿz™z¨Q™°Ñ2Ø(ØØ'¨#Ñ-Ø'ØØô
ˆô !×+Ñ+¬B×,@Ñ,@ÀÇÁÔVX×V`ÑV`Ô,aÓbˆÕr   ÚwaveformÚdevicer   c           	      óü  — t        j                  |j                  d   | j                  z  «      | j                  z  |j                  d   z
  }t	        | j
                  | j                  z
  d«      }t        j                  |||f«      }t        j                  || j
                  | j                  | j                  | j                  j                  |«      dd¬«      }t        j                  |«      }|j                  d«      j                  d«      j!                  d«      j#                  «       }| j$                  j                  |«      }||z  }|j!                  d«      j'                  «       }|j)                  dd«      S )	Néÿÿÿÿr   FT)r5   Ú
win_lengthr9   ÚcenterÚreturn_complexr'   g»½×Ùß|Û=r(   )ÚmathÚceilÚshaper5   Úmaxr#   ÚFÚpadr   Ústftr6   r9   ÚtoÚview_as_realÚpowÚsumÚ	clamp_minÚsqrtr>   Úlog10Ú	transpose)	r?   rB   rC   Ú	right_padÚleft_padrO   Ú
magnitudesr>   Úmel_specs	            r   Ú_torch_extract_fbank_featuresz5InklingFeatureExtractor._torch_extract_fbank_featureso   s6  € Ü—I‘I˜hŸn™n¨RÑ0°4·?±?ÑBÓCÀdÇoÁoÑUÐX`×XfÑXfÐgiÑXjÑjˆ	Ü�t—z‘z D§O¡OÑ3°QÓ7ˆÜ—5‘5˜ H¨iÐ#8Ó9ˆä�z‰zØØ�J‰JØ—‘Ø×'Ñ'Ø—;‘;—>‘> &Ó)ØØô
ˆô ×'Ñ'¨Ó-ˆ
Ø—^‘^ AÓ&×*Ñ*¨2Ó.×8Ñ8¸Ó?×DÑDÓFˆ
à×&Ñ&×)Ñ)¨&Ó1ˆØ Ñ+ˆØ×%Ñ% eÓ,×2Ñ2Ó4ˆð ×!Ñ! ! QÓ'Ð'r   Ú
raw_speechÚpaddingÚ
max_lengthÚ
truncationÚpad_to_multiple_ofÚreturn_attention_maskÚreturn_tensorsc
           	      ób  ‡— |�8|| j                   k7  r>t        d| j                   › d| j                   › d|› d�«      ‚t        j                  d«       | j                  j
                  Šddd	t        j                  fˆfd
„}t        |t        j                  «      rt        j                  |«      }t        |t        j                  «      r,|j                  dkD  rt        d|j                  › d�«      ‚|g}n�t        |t        t        f«      r`t        |«      dk(  rt        d«      ‚t        |d   t         t"        t        j$                  t        j&                  f«      r|g}n&t        |«      }nt)        d‰› dt+        |«      › �«      ‚|D �cg c]  } ||«      dd…df   ‘Œ }}|D �cg c]  }t        |«      ‘Œ }}t-        ||dœ«      }| j/                  |||||d¬«      }|j0                  j3                  d«      }| j5                  ||	«      }t        j6                  |j8                  | j:                  z   dz
  | j:                  d¬«      }t        j<                  |j>                  d   |	¬«      ddd…f   |dd…df   k  }||jA                  d«      z  }d|i}|r||d<   t-        ||¬«      S c c}w c c}w )aá  
        Extract log-mel spectrogram features from one or several audio clip(s).

        Args:
            raw_speech (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`):
                The sequence or batch of sequences to be padded. 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. Must be mono
                channel audio at `self.sampling_rate`, not stereo, i.e. single float per timestep. Decoding
                and resampling of raw audio (bytes / paths / URLs) is handled upstream by the processor's
                `apply_chat_template`, not here.
            sampling_rate (`int`, *optional*):
                The sampling rate of `raw_speech`, used only to validate against `self.sampling_rate`.
            device (`str`, *optional*, defaults to `"cpu"`):
                The device on which the log-mel spectrogram is computed in `_torch_extract_fbank_features`.
        NzWThe model corresponding to this feature extractor was trained using a sampling rate of zB. Please make sure that the provided audio input was sampled with z	 and not ú.z›It is strongly recommended to pass the `sampling_rate` argument to this function. Failing to do so can result in silent errors that might be hard to debug.Úclipz np.ndarray | torch.Tensor | listr   c                 ó¨  •— t        | t        j                  «      r| n't        j                  t	        j
                  | «      «      }|j                  t        j                  «      }|j                  dk(  r-t        j                  d‰› d�«       |j                  d¬«      }|S |j                  dk7  r"t        dt        |j                  «      › d�«      ‚|S )	Nr'   z2Only mono-channel audio is supported for input to zB. Taking the mean over the channel (last) axis to convert to mono.rE   )Údimr(   zDEach audio clip must be 1-D (mono) or 2-D (multichannel), got shape re   )Ú
isinstancer   ÚTensorÚ	as_tensorr;   ÚasarrayrP   r8   ÚndimÚloggerÚwarning_onceÚmeanr   ÚtuplerK   )rf   ÚtensorÚcls_names     €r   Ú_to_monoz2InklingFeatureExtractor.__call__.<locals>._to_mono²   sÃ   ø€ Ü'¨¬e¯l©lÔ;‘TÄÇÁÔQS×Q[ÑQ[Ð\`ÓQaÓAbˆFØ—Y‘YœuŸ}™}Ó-ˆFØ�{‰{˜aÒÜ×#Ñ#ØHÈÈ
ð SWð Wôð  Ÿ™¨˜Ó,�ð
 ˆMð	 —‘ Ò!Ü ØZÔ[`Ðag×amÑamÓ[nÐZoÐopÐqóð ð ˆMr   r'   zCA single array input must be 1-D (mono) or 2-D (multichannel); got z2 dims. Pass a list of arrays for a batch of clips.r   zReceived an empty audio input.z!Unsupported audio input type for z: )r   Úaudio_lengthsÚpt)r^   r_   r`   ra   rc   rE   r(   Úfloor)Úrounding_mode)rC   r   r   )ÚdataÚtensor_type)!r   r   rn   ro   rA   Ú__name__r   rj   ri   r;   Úndarrayr:   rm   Úlistrq   Úlenr   ÚfloatÚintegerÚfloatingÚ	TypeErrorÚtyper   rN   r   Úsqueezer\   Údivru   r5   ÚarangerK   Ú	unsqueeze)r?   r]   r   r^   r_   r`   ra   rb   rc   rC   r@   rt   Úclipsrf   Úspeechru   Úbatched_speechÚpadded_inputsÚinput_waveformsr   Ú
num_framesr   ry   rs   s                          @r   Ú__call__z InklingFeatureExtractor.__call__‡   sÄ  ø€ ð8 Ð$Ø × 2Ñ 2Ò2Ü ðØ#×1Ñ1Ð2ð 3(Ø(,×(:Ñ(:Ð';¸9À]ÀOÐSTðVóð ô ×Ñð\ôð
 —>‘>×*Ñ*ˆð	Ð=ð 	Ä%Ç,Á,õ 	ô �j¤"§*¡*Ô-Ü×)Ñ)¨*Ó5ˆJÜ�j¤%§,¡,Ô/à�‰ Ò"Ü ØYÐZd×ZiÑZiÐYjð kBð Bóð ð  �L‰EÜ˜
¤T¬5 MÔ2Ü�:‹ !Ò#Ü Ð!AÓBÐBä˜* Q™-¬#¬u´b·j±jÄ"Ç+Á+Ð)NÔOØ#˜‘ä˜ZÓ(‘äÐ?À¸zÈÌDÐQ[ÓL\ÐK]Ð^Ó_Ð_à:?Ö@°$‘h˜t“n¢Q¨ WÓ-Ð@ˆ
Ð@ð 4>Ö>¨œ˜V�Ð>ˆÐ>Ü%¸ÐVcÑ&dÓeˆØŸ™ØØØ!Ø!Ø1Øð !ó 
ˆð (×6Ñ6×>Ñ>¸rÓBˆà×;Ñ;¸OÈVÓTˆô —Y‘YØ×'Ñ'¨$¯/©/Ñ9¸AÑ=¸t¿¹Ð^eô
ˆ
ô $Ÿl™l¨>×+?Ñ+?ÀÑ+BÈ6ÔRÐSWÒYZÐSZÑ[Ð^hÒijÐlpÐipÑ^qÑqÐØ'Ð*=×*GÑ*GÈÓ*KÑKˆà  .Ð1ˆÙ ð +>ˆDÐ&Ñ'Ü °>ÔBÐBùò? Aùò ?s   ÆJ'Æ$J,)éP   i€>  r)   gš™™™™™©?r*   N)Úcpu)NTNFNTNr�   )r{   Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesr   r   r4   r   rj   Ústrr\   r;   r|   r}   Úboolr   r   r   rŽ   Ú__classcell__)rA   s   @r   r   r   %   s�  ø„ ñð2 *Ð+@ÐAÐð Ø#Ø"Ø(,Ø(+Ø ñ+càð+cð ð+cð ð	+cð
 !&ð+cð !&ð+cð �T‰zõ+cñZ(°e·l±lð (ÈCð (Ð\a×\hÑ\hó (ð6 %)Ø04Ø!%Ø Ø)-Ø-1Ø26Ø"ñnCà—J‘J  e¡Ñ,¨t°B·J±JÑ/?Ñ?À$ÀtÈEÁ{ÑBSÑSðnCð ˜T‘zðnCð ˜‘˜oÑ-ð	nCð
 ˜$‘JðnCð ðnCð   $™JðnCð  $ d™{ðnCð ˜jÑ(¨4Ñ/ðnCð �d‘
ðnCð 
÷nCr   r   )g�íµ ÷Æ°>)rI   Únumpyr;   r   Útorch.nn.functionalÚnnÚ
functionalrM   Úaudio_utilsr   Ú!feature_extraction_sequence_utilsr   Úfeature_extraction_utilsr   Úutilsr   r   r	   Úutils.import_utilsr
   Ú
get_loggerr{   rn   r   r•   r   r   r   Ú__all__r2   r   r   ú<module>r£      s�   ðó ã Û ß Ð å *Ý IÝ 4ß 9Ñ 9Ý *ð 
ˆ×	Ñ	˜HÓ	%€ñ˜ð  cð °eð Àsó ñ 
�:ÔôOCÐ6ó OCó ðOCðd %Ð
%�r   