





from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, logging


a__			:    List[str]			      =		logging.get_logger(__name__)





class UpperCAmelCase_							(   _SCREAMING_SNAKE_CASE   ):
  __lowerCAmelCase      :	Union[str, Any]  	=      ["""input_values""", """attention_mask"""]


  def __init__(       self   ,__snake_case=1   ,__snake_case=1_6_0_0_0   ,__snake_case=1_2_8   ,__snake_case=1_0_2_4   ,__snake_case=0.0   ,__snake_case=True   ,__snake_case=-4.267_7393   ,__snake_case=4.568_9974   ,__snake_case=False   ,**__snake_case   ,):



       """simple docstring"""





       super().__init__(feature_size=__snake_case   ,sampling_rate=__snake_case   ,padding_value=__snake_case   ,**__snake_case   )
       A_					      =					num_mel_bins
       A_					      =					max_length
       A_					      =					do_normalize
       A_					      =					mean
       A_					      =					std
       A_					      =					return_attention_mask


  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case   ,):



       """simple docstring"""





       A_					      =					torch.from_numpy(__snake_case   ).unsqueeze(0   )
       A_					      =					ta_kaldi.fbank(
           __snake_case   ,htk_compat=__snake_case   ,sample_frequency=self.sampling_rate   ,use_energy=__snake_case   ,window_type='''hanning'''   ,num_mel_bins=self.num_mel_bins   ,dither=0.0   ,frame_shift=1_0   ,)

       A_					      =					fbank.shape[0]
       A_					      =					max_length - n_frames

       # pad or truncate, depending on difference
       if difference > 0:
            A_					      =					torch.nn.ZeroPadad((0, 0, 0, difference)   )
            A_					      =					pad_module(__snake_case   )
       elif difference < 0:
            A_					      =					fbank[0:max_length, :]

       A_					      =					fbank.numpy()

       return fbank


  def 	__UpperCAmelCase    (       self   ,__snake_case   ):



       """simple docstring"""





       return (input_values - (self.mean)) / (self.std * 2)




  def __call__(       self   ,__snake_case   ,__snake_case = None   ,__snake_case = None   ,**__snake_case   ,):



       """simple docstring"""





       if sampling_rate is not None:
            if sampling_rate != self.sampling_rate:
                 raise ValueError(
                     f'The model corresponding to this feature extractor: {self} was trained using a sampling rate of'
                     f' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'
                     f' {self.sampling_rate} and not {sampling_rate}.'   )
       else:
            logger.warning(
                '''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.'''   )

       A_					      =					isinstance(__snake_case   ,np.ndarray   ) and len(raw_speech.shape   ) > 1
       if is_batched_numpy and len(raw_speech.shape   ) > 2:
            raise ValueError(f'Only mono-channel audio is supported for input to {self}'   )
       A_					      =					is_batched_numpy or (
           isinstance(__snake_case   ,(list, tuple)   ) and (isinstance(raw_speech[0]   ,(np.ndarray, tuple, list)   ))
       )

       if is_batched:
            A_					      =					[np.asarray(__snake_case   ,dtype=np.floataa   ) for speech in raw_speech]
       elif not is_batched and not isinstance(__snake_case   ,np.ndarray   ):
            A_					      =					np.asarray(__snake_case   ,dtype=np.floataa   )
       elif isinstance(__snake_case   ,np.ndarray   ) and raw_speech.dtype is np.dtype(np.floataa   ):
            A_					      =					raw_speech.astype(np.floataa   )

       # always return batch
       if not is_batched:
            A_					      =					[raw_speech]

       # extract fbank features and pad/truncate to max_length
       A_					      =					[self._extract_fbank_features(__snake_case   ,max_length=self.max_length   ) for waveform in raw_speech]

       # convert into BatchFeature
       A_					      =					BatchFeature({'''input_values''': features}   )

       # make sure list is in array format
       A_					      =					padded_inputs.get('''input_values'''   )
       if isinstance(input_values[0]   ,__snake_case   ):
            A_					      =					[np.asarray(__snake_case   ,dtype=np.floataa   ) for feature in input_values]

       # normalization
       if self.do_normalize:
            A_					      =					[self.normalize(__snake_case   ) for feature in input_values]

       if return_tensors is not None:
            A_					      =					padded_inputs.convert_to_tensors(__snake_case   )

       return padded_inputs










from __future__ import annotations

def     UpperCAmelCase_						(						_UpperCAmelCase       :list[int | float]							)  ->							int | float:
     '''simple docstring'''


     if len(_UpperCAmelCase							) == 0:
          raise ValueError('''find_max() arg is an empty sequence'''							)
     A_					      =					nums[0]
     for x in nums:
          if x > max_num:
               A_					      =					x
     return max_num


if __name__ == "__main__":
      import doctest

      doctest.testmod(verbose=True)



