




'''simple docstring'''


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 PaddingStrategy, TensorType, logging


UpperCAmelCase_ 		=      logging.get_logger(__name__)





class    __lowercase						(  __magic_name__ ):
   _a							=      ["""input_features""", """attention_mask"""]







   def __init__(	self      ,							UpperCamelCase=80      ,							UpperCamelCase=1_6000      ,							UpperCamelCase=80      ,							UpperCamelCase=0.0      ,							UpperCamelCase=True      ,							UpperCamelCase=True      ,							UpperCamelCase=True      ,							**UpperCamelCase      ,							)				->    int:
     super().__init__(feature_size=UpperCamelCase      ,							sampling_rate=UpperCamelCase      ,							padding_value=UpperCamelCase      ,							**UpperCamelCase      )
     __a			=					num_mel_bins
     __a			=					do_ceptral_normalize
     __a			=					normalize_means
     __a			=					normalize_vars
     __a			=					True







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							)				->    np.ndarray:
     __a			=					waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
     __a			=					torch.from_numpy(UpperCamelCase      ).unsqueeze(0      )
     __a			=					ta_kaldi.fbank(UpperCamelCase      ,							num_mel_bins=self.num_mel_bins      ,							sample_frequency=self.sampling_rate      )
     return features.numpy()







   @staticmethod
   def 		UpperCamelCase__			(	UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase = True      ,							UpperCamelCase = True      ,							UpperCamelCase = 0.0      ,							)				->    np.ndarray:
     # make sure we normalize float32 arrays
     if normalize_means:
       __a			=					x[:input_length].mean(axis=0      )
       __a			=					np.subtract(UpperCamelCase      ,							UpperCamelCase      )
     if normalize_vars:
       __a			=					x[:input_length].std(axis=0      )
       __a			=					np.divide(UpperCamelCase      ,							UpperCamelCase      )

     if input_length < x.shape[0]:
       __a			=					padding_value

     # make sure array is in float32
     __a			=					x.astype(np.floataa      )

     return x







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase = None      )				->    List[np.ndarray]:
     __a			=					attention_mask.sum(-1      ) if attention_mask is not None else [x.shape[0] for x in input_features]
     return [
         self.utterance_cmvn(UpperCamelCase      ,							UpperCamelCase      ,							self.normalize_means      ,							self.normalize_vars      ,							self.padding_value      )
         for x, n in zip(UpperCamelCase      ,							UpperCamelCase      )
     ]







   def __call__(	self      ,							UpperCamelCase      ,							UpperCamelCase = False      ,							UpperCamelCase = None      ,							UpperCamelCase = False      ,							UpperCamelCase = None      ,							UpperCamelCase = None      ,							UpperCamelCase = None      ,							UpperCamelCase = None      ,							**UpperCamelCase      ,							)				->    BatchFeature:

     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(UpperCamelCase      ,							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(UpperCamelCase      ,							(list, tuple)      ) and (isinstance(raw_speech[0]      ,							(np.ndarray, tuple, list)      ))
     )

     if is_batched:
       __a			=					[np.asarray(UpperCamelCase      ,							dtype=np.floataa      ) for speech in raw_speech]
     elif not is_batched and not isinstance(UpperCamelCase      ,							np.ndarray      ):
       __a			=					np.asarray(UpperCamelCase      ,							dtype=np.floataa      )
     elif isinstance(UpperCamelCase      ,							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
     __a			=					[self._extract_fbank_features(UpperCamelCase      ) for waveform in raw_speech]

     # convert into correct format for padding
     __a			=					BatchFeature({'input_features': features}      )

     __a			=					self.pad(
         UpperCamelCase      ,							padding=UpperCamelCase      ,							max_length=UpperCamelCase      ,							truncation=UpperCamelCase      ,							pad_to_multiple_of=UpperCamelCase      ,							return_attention_mask=UpperCamelCase      ,							**UpperCamelCase      ,							)

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

     __a			=					padded_inputs.get('attention_mask'      )
     if attention_mask is not None:
       __a			=					[np.asarray(UpperCamelCase      ,							dtype=np.intaa      ) for array in attention_mask]

     # Utterance-level cepstral mean and variance normalization
     if self.do_ceptral_normalize:
       __a			=					(
           np.array(UpperCamelCase      ,							dtype=np.intaa      )
           if self._get_padding_strategies(UpperCamelCase      ,							max_length=UpperCamelCase      ) is not PaddingStrategy.DO_NOT_PAD
           else None
       )
       __a			=					self.normalize(
           padded_inputs['input_features']      ,							attention_mask=UpperCamelCase      )

     if return_tensors is not None:
       __a			=					padded_inputs.convert_to_tensors(UpperCamelCase      )

     return padded_inputs









'''simple docstring'''


from ..utils import DummyObject, requires_backends





class    __lowercase						(  metaclass=__magic_name__ ):
   _a							=      ["""onnx"""]







   def __init__(	self      ,							*UpperCamelCase      ,							**UpperCamelCase      )				->    str:
     requires_backends(self      ,							['onnx']      )







   @classmethod
   def 		UpperCamelCase__			(	cls      ,							*UpperCamelCase      ,							**UpperCamelCase      )				->    Tuple:
     requires_backends(cls      ,							['onnx']      )







   @classmethod
   def 		UpperCamelCase__			(	cls      ,							*UpperCamelCase      ,							**UpperCamelCase      )				->    Tuple:
     requires_backends(cls      ,							['onnx']      )



