Ë
    ÛÓ^j�1  ã                   óà   — d dl Zd dlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ  ej                  e«      Zded	ed
edededej&                  fd„Z ed¬«       G d„ de«      «       ZdgZy)é    Né   )Úhertz_to_mel)ÚSequenceFeatureExtractor)ÚBatchFeature)Ú
TensorTypeÚlogging)ÚrequiresÚnum_mel_binsÚnum_spectrogram_binsÚsample_rateÚlower_edge_hertzÚupper_edge_hertzÚreturnc                 ó‚  — t         j                  }d}|dz  }t        j                  d|||¬«      |d }	t        |	d¬«      dd…t         j                  f   }
t        j                  t        |d¬«      t        |d¬«      | dz   |¬«      }|dd	 t         j                  dd…f   |dd
 t         j                  dd…f   |dd t         j                  dd…f   }}}|
|z
  ||z
  z  }||
z
  ||z
  z  }t        j
                  dt        j                  ||«      «      }t        j                  ||dgddgg«      j                  |«      S )z.NumPy-port of the JAX mel weight matrix logic.é   g       @ç        )ÚdtypeNÚkaldi)Ú	mel_scaleé   éþÿÿÿéÿÿÿÿr   )	ÚnpÚfloat64Úlinspacer   ÚnewaxisÚmaximumÚminimumÚpadÚastype)r
   r   r   r   r   r   Úinternal_dtypeÚbands_to_zeroÚnyquist_hertzÚlinear_frequenciesÚspectrogram_bins_melÚedgesÚlower_edge_melÚ
center_melÚupper_edge_melÚlower_slopesÚupper_slopesÚmel_weights_matrixs                     ú{/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/lasr/feature_extraction_lasr.pyÚlinear_to_mel_weight_matrixr.      sJ  € ô —Z‘Z€Nð €MØ #Ñ%€MÜŸ™ S¨-Ð9MÐUcÔdÐerÐesÐtÐÜ'Ð(:ÀgÔNÊqÔRT×R\ÑR\È}Ñ]Ðä�K‰KÜÐ%°Ô9ÜÐ%°Ô9Ø�qÑØô	€Eð 	ˆcˆrˆ
”2—:‘:šq�=Ñ!Øˆa�ˆ”B—J‘J¢�MÑ"Øˆaˆbˆ	”"—*‘*ša�-Ñ ð !/�J€Nð )¨>Ñ9¸jÈ>Ñ>YÑZ€LØ"Ð%9Ñ9¸nÈzÑ>YÑZ€LÜŸ™ C¬¯©°LÀ,Ó)OÓPÐÜ�6‰6Ð$¨°qÐ'9¸A¸q¸6Ð&BÓC×JÑJÈ5ÓQÐQó    )Útorch)Úbackendsc                   ó  ‡ — e Zd ZdZddgZ	 	 	 	 	 	 dˆ fd„	Zdd„Z	 	 	 	 	 	 	 	 	 	 ddej                  e	e
   z  e	ej                     z  e	e	e
      z  ded	edz  d
eez  dz  dedz  dedz  dedz  dedz  dedz  dedz  dedz  defd„Zˆ xZS )ÚLasrFeatureExtractora7  
    Constructs a LASR feature extractor.

    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.

    This class extracts mel-filter bank features from raw speech using a custom numpy implementation of the `Short Time
    Fourier Transform` which should match pytorch's `torch.stft` equivalent.

    Args:
            feature_size (`int`, *optional*, defaults to 128):
                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).
            hop_length (`int`, *optional*, defaults to 160):
                Length of the overlapping windows for the STFT used to obtain the Mel Frequency coefficients.
            n_fft (`int`, *optional*, defaults to 512):
                Size of the Fourier transform.
            win_length (`int`, *optional*, defaults to 400):
                The window length for the STFT computation.
            padding_value (`float`, *optional*, defaults to 0.0):
                Padding value used to pad the audio. Should correspond to silences.
    Úinput_featuresÚattention_maskc           
      óÐ   •— t        ‰| �  d|||dœ|¤Ž || _        || _        || _        t        j                  t        ||dz  dz   |ddt        j                  ¬«      «      | _
        y )N)Úfeature_sizeÚsampling_rateÚpadding_valuer   r   g     @_@g     L½@)r
   r   r   r   r   r   © )ÚsuperÚ__init__Ú
hop_lengthÚn_fftÚ
win_lengthr0   Ú
from_numpyr.   r   r   Úmel_filters)	Úselfr7   r8   r=   r>   r?   r9   ÚkwargsÚ	__class__s	           €r-   r<   zLasrFeatureExtractor.__init__^   sn   ø€ ô 	‰ÑÐw lÀ-Ð_lÑwÐpvÒwà$ˆŒØˆŒ
Ø$ˆŒÜ ×+Ñ+Ü'Ø)Ø%*¨a¡Z°!¡^Ø)Ø!&Ø!'Ü—j‘jôó	
ˆÕr/   c                 ó  — t        j                  | j                  d|t         j                  ¬«      }|j	                  t         j                  «      }|j                  d| j                  | j                  «      }t         j                  j                  ||z  | j                  ¬«      }t        j                  |«      dz  }| j                  j	                  |«      }t        j                  ||z  d¬«      }t        j                  |«      }|S )NF)ÚperiodicÚdevicer   r   )Únr   gñhãˆµøä>)Úmin)r0   Úhann_windowr?   r   ÚtoÚunfoldr=   ÚfftÚrfftr>   ÚabsrA   ÚclampÚlog)	rB   ÚwaveformrG   ÚwindowÚframesÚstftÚ
power_specrA   Úmel_specs	            r-   Ú_torch_extract_fbank_featuresz2LasrFeatureExtractor._torch_extract_fbank_featuresx   sÁ   € ä×"Ñ" 4§?¡?¸UÈ6ÔY^×YfÑYfÔgˆØ—;‘;œuŸ}™}Ó-ˆð
 —‘  T§_¡_°d·o±oÓFˆÜ�y‰y�~‰~˜f v™o°·±ˆ~Ó<ˆÜ—Y‘Y˜t“_¨Ñ)ˆ
ð ×&Ñ&×)Ñ)¨&Ó1ˆÜ—;‘;˜z¨KÑ7¸TÔBˆÜ—9‘9˜XÓ&ˆàˆr/   NÚ
raw_speechÚ
truncationÚpad_to_multiple_ofÚreturn_tensorsÚreturn_attention_maskÚpaddingÚ
max_lengthr8   Údo_normalizerG   Úreturn_token_timestampsr   c                 ó¸  — |�O|| j                   k7  rmt        d| j                  j                  › d| j                   › d| j                   › d|› d�	«      ‚t        j                  d| j                  j                  › d�«       t        |t        j                  «      rt        j                  |«      }nqt        |t        t        f«      r[t        |d	   t        t        j                  f«      r#|D �cg c]  }t        j                  |«      ‘Œ }}nt        j                  |«      }t        |t        j                  «      xr t        |j                  «      d
kD  }|rVt        |j                  «      dkD  r>t        j                  d| j                  j                  › d�«       |j!                  d«      }t        |t        t        f«      }|r^|D ]Y  }t        |j                  «      d
kD  sŒt        j                  d| j                  j                  › d�«       |j!                  d«      }Œ[ |s|r4|D �cg c](  }|dd…df   j#                  t        j$                  «      ‘Œ* }}n'|dd…df   j#                  t        j$                  «      g}t'        d|i«      }| j)                  ||||||d¬«      }|j*                  j-                  d«      }| j/                  ||
«      }d|j#                  t        j$                  «      i}|rO|j0                  dd…| j2                  d
z
  d| j4                  …f   }|j#                  t        j6                  «      |d<   t'        ||¬«      S c c}w c c}w )aö  
        Main method to featurize and prepare for the model one or several sequence(s). Implementation uses PyTorch for
        the STFT computation if available, otherwise a slower NumPy based one.

        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, not
                stereo, i.e. single float per timestep.
            truncation (`bool`, *optional*, default to `True`):
                Activates truncation to cut input sequences longer than *max_length* to *max_length*.
            pad_to_multiple_of (`int`, *optional*, defaults to None):
                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)

                <Tip>

                For Parakeet models, `attention_mask` should always be passed for batched inference, to avoid subtle
                bugs.

                </Tip>

            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 sampling rate at which the `raw_speech` input was sampled. It is strongly recommended to pass
                `sampling_rate` at the forward call to prevent silent errors and allow automatic speech recognition
                pipeline.
            padding_value (`float`, *optional*, defaults to 0.0):
                The value that is used to fill the padding values / vectors.
            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 of the model.
            device (`str`, *optional*, defaults to `'cpu'`):
                Specifies the device for computation of the log-mel spectrogram of audio signals in the
                `_torch_extract_fbank_features` method. (e.g., "cpu", "cuda")
            return_token_timestamps (`bool`, *optional*, defaults to `None`):
                Deprecated. Use `return_attention_mask` instead from which the number of frames can be inferred.

                Whether or not to return the number of frames of the input raw_speech.
                These num_frames can be used by the model to compute word level timestamps.
        Nz3The model corresponding to this feature extractor: z& was trained using a sampling rate of zI. Please make sure that the provided `raw_speech` 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   r   r   z2Only mono-channel audio is supported for input to z;. We will take the mean of the channels to convert to mono.r   r4   Úpt)r^   r_   rZ   r[   r]   r\   r5   )ÚdataÚtensor_type)r8   Ú
ValueErrorrD   Ú__name__ÚloggerÚwarningÚ
isinstancer   Úndarrayr0   ÚtensorÚlistÚtupleÚTensorÚlenÚshapeÚmeanrK   Úfloat32r   r   r4   ÚsqueezerX   r5   r?   r=   Úbool)rB   rY   rZ   r[   r\   r]   r^   r_   r8   r`   rG   ra   rC   ÚspeechÚis_batched_torchÚis_batched_sequenceÚbatched_speechÚpadded_inputsr4   re   r5   s                        r-   Ú__call__zLasrFeatureExtractor.__call__‹   s!  € ðH Ð$Ø × 2Ñ 2Ò2Ü ØIÈ$Ï.É.×JaÑJaÐIbð c)Ø)-×);Ñ);Ð(<ð =)Ø)-×);Ñ);Ð(<¸IÀmÀ_ÐTUðWóð ô �N‰NØVÐW[×WeÑWe×WnÑWnÐVoð p\ð \ôô �j¤"§*¡*Ô-ÜŸ™ jÓ1‰JÜ˜
¤T¬5 MÔ2Ü˜* Q™-¬$´·
±
Ð);Ô<ØAKÖL°vœeŸl™l¨6Õ2ÐL�
ÑLä"Ÿ\™\¨*Ó5�
ä% j´%·,±,Ó?Ò]ÄCÈ
×HXÑHXÓDYÐ\]ÑD]ÐÙ¤ J×$4Ñ$4Ó 5¸Ò 9Ü�N‰NØDÀTÇ^Á^×E\ÑE\ÐD]ð ^Lð Lôð $Ÿ™¨Ó,ˆJä(¨´d¼E°]ÓCÐÙØ$ò -�Ü�v—|‘|Ó$ qÓ(Ü—N‘NØLÈTÏ^É^×MdÑMdÐLeð fTð Tôð $Ÿ[™[¨›_‘Fð-ñ Ñ2ØJTÖUÀ˜&¢ D ™/×,Ñ,¬U¯]©]Õ;ÐUˆJÑUà$¢Q¨ WÑ-×0Ñ0´·±Ó?Ð@ˆJä%Ð'7¸Ð&DÓEˆØŸ™ØØØ!Ø!Ø1Ø"7Øð !ó 
ˆð '×5Ñ5×=Ñ=¸bÓAˆØ×;Ñ;¸NÈFÓSˆà˜n×/Ñ/´·±Ó>ð
ˆñ !Ø*×9Ñ9º!¸T¿_¹_ÈqÑ=PÐ=cÐTX×TcÑTcÐ=cÐ:cÑdˆNØ%3×%6Ñ%6´u·z±zÓ%BˆDÐ!Ñ"ä °>ÔBÐBùò_ Mùò. Vs   Ã,MÈ*-M)é€   i€>  é    i   i�  r   )Úcpu)
FNNNÚlongestNNNr   N)rh   Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesr<   rX   r   rl   rn   Úfloatrv   ÚintÚstrr   r   r|   Ú__classcell__)rD   s   @r-   r3   r3   B   s<  ø„ ñð0 *Ð+;Ð<Ðð ØØØØØõ
ó4ð, !Ø)-Ø26Ø-1Ø'Ø!%Ø$(Ø$(Ø"Ø/3ñECà—J‘J  e¡Ñ,¨t°B·J±JÑ/?Ñ?À$ÀtÈEÁ{ÑBSÑSðECð ðECð   $™Jð	ECð
 ˜jÑ(¨4Ñ/ðECð  $ d™{ðECð �t‘ðECð ˜$‘JðECð ˜T‘zðECð ˜T‘kðECð �d‘
ðECð "&¨¡ðECð 
÷ECr/   r3   )Únumpyr   r0   Úaudio_utilsr   Ú!feature_extraction_sequence_utilsr   Úfeature_extraction_utilsr   Úutilsr   r   Úutils.import_utilsr	   Ú
get_loggerrh   ri   r†   r…   rl   r.   r3   Ú__all__r:   r/   r-   ú<module>r‘      s«   ðó Û å 'Ý IÝ 4ß (Ý *ð 
ˆ×	Ñ	˜HÓ	%€ð"RØð"Ràð"Rð ð"Rð ð	"Rð
 ð"Rð ‡Z�Zó"RñJ 
�:ÔôMCÐ3ó MCó ðMCð` "Ð
"�r/   