



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 				lowerCAmelCase_		(  a__							):
       UpperCAmelCase__				:					Optional[int]        =  ["input_features", "attention_mask"]
       def __init__(			self,			SCREAMING_SNAKE_CASE_=80,			SCREAMING_SNAKE_CASE_=1_6000,			SCREAMING_SNAKE_CASE_=80,			SCREAMING_SNAKE_CASE_=0.0,			SCREAMING_SNAKE_CASE_=True,			SCREAMING_SNAKE_CASE_=True,			SCREAMING_SNAKE_CASE_=True,			**SCREAMING_SNAKE_CASE_,			)     ->		str:
          super().__init__(feature_size=SCREAMING_SNAKE_CASE_,			sampling_rate=SCREAMING_SNAKE_CASE_,			padding_value=SCREAMING_SNAKE_CASE_,			**SCREAMING_SNAKE_CASE_       )
          UpperCamelCase    :  int							=	num_mel_bins
          UpperCamelCase    :  List[str]							=	do_ceptral_normalize
          UpperCamelCase    :  int							=	normalize_means
          UpperCamelCase    :  str							=	normalize_vars
          UpperCamelCase    :  Optional[int]							=	True
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			)     ->		np.ndarray:
          UpperCamelCase    :  Optional[int]							=	waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
          UpperCamelCase    :  str							=	torch.from_numpy(SCREAMING_SNAKE_CASE_       ).unsqueeze(0       )
          UpperCamelCase    :  Tuple							=	ta_kaldi.fbank(SCREAMING_SNAKE_CASE_,			num_mel_bins=self.num_mel_bins,			sample_frequency=self.sampling_rate       )
          return features.numpy()
       @staticmethod
       def 					snake_case_       (			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_ = True,			SCREAMING_SNAKE_CASE_ = True,			SCREAMING_SNAKE_CASE_ = 0.0,			)     ->		np.ndarray:
          # make sure we normalize float32 arrays
          if normalize_means:
             UpperCamelCase    :  Tuple							=	x[:input_length].mean(axis=0       )
             UpperCamelCase    :  Optional[Any]							=	np.subtract(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )
          if normalize_vars:
             UpperCamelCase    :  Optional[int]							=	x[:input_length].std(axis=0       )
             UpperCamelCase    :  Tuple							=	np.divide(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )

          if input_length < x.shape[0]:
             UpperCamelCase    :  Optional[Any]							=	padding_value

          # make sure array is in float32
          UpperCamelCase    :  Tuple							=	x.astype(np.floataa       )

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






       def __call__(			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_ = False,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = False,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			**SCREAMING_SNAKE_CASE_,			)     ->		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.'       )

          UpperCamelCase    :  Optional[Any]							=	isinstance(SCREAMING_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}"""       )
          UpperCamelCase    :  Tuple							=	is_batched_numpy or (
              isinstance(SCREAMING_SNAKE_CASE_,			(list, tuple)       ) and (isinstance(raw_speech[0],			(np.ndarray, tuple, list)       ))
          )

          if is_batched:
             UpperCamelCase    :  Tuple							=	[np.asarray(SCREAMING_SNAKE_CASE_,			dtype=np.floataa       ) for speech in raw_speech]
          elif not is_batched and not isinstance(SCREAMING_SNAKE_CASE_,			np.ndarray       ):
             UpperCamelCase    :  str							=	np.asarray(SCREAMING_SNAKE_CASE_,			dtype=np.floataa       )
          elif isinstance(SCREAMING_SNAKE_CASE_,			np.ndarray       ) and raw_speech.dtype is np.dtype(np.floataa       ):
             UpperCamelCase    :  Optional[Any]							=	raw_speech.astype(np.floataa       )

          # always return batch
          if not is_batched:
             UpperCamelCase    :  Optional[int]							=	[raw_speech]

          # extract fbank features
          UpperCamelCase    :  Any							=	[self._extract_fbank_features(SCREAMING_SNAKE_CASE_       ) for waveform in raw_speech]

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

          UpperCamelCase    :  Union[str, Any]							=	self.pad(
              SCREAMING_SNAKE_CASE_,			padding=SCREAMING_SNAKE_CASE_,			max_length=SCREAMING_SNAKE_CASE_,			truncation=SCREAMING_SNAKE_CASE_,			pad_to_multiple_of=SCREAMING_SNAKE_CASE_,			return_attention_mask=SCREAMING_SNAKE_CASE_,			**SCREAMING_SNAKE_CASE_,			)

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

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

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

          if return_tensors is not None:
             UpperCamelCase    :  List[Any]							=	padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE_       )

          return padded_inputs








from typing import Optional

import pyspark

from .. import Features, NamedSplit
from ..download import DownloadMode
from ..packaged_modules.spark.spark import Spark
from .abc import AbstractDatasetReader



class 				lowerCAmelCase_		(  a__							):
       def __init__(			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = True,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = False,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = True,			SCREAMING_SNAKE_CASE_ = "arrow",			**SCREAMING_SNAKE_CASE_,			)     ->		Optional[int]:
          super().__init__(
              split=SCREAMING_SNAKE_CASE_,			features=SCREAMING_SNAKE_CASE_,			cache_dir=SCREAMING_SNAKE_CASE_,			keep_in_memory=SCREAMING_SNAKE_CASE_,			streaming=SCREAMING_SNAKE_CASE_,			**SCREAMING_SNAKE_CASE_,			)
          UpperCamelCase    :  List[str]							=	load_from_cache_file
          UpperCamelCase    :  List[str]							=	file_format
          UpperCamelCase    :  Optional[int]							=	Spark(
              df=SCREAMING_SNAKE_CASE_,			features=SCREAMING_SNAKE_CASE_,			cache_dir=SCREAMING_SNAKE_CASE_,			working_dir=SCREAMING_SNAKE_CASE_,			**SCREAMING_SNAKE_CASE_,			)






       def 					snake_case_       (			self       )     ->		int:
          if self.streaming:
             return self.builder.as_streaming_dataset(split=self.split       )
          UpperCamelCase    :  Union[str, Any]							=	None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD
          self.builder.download_and_prepare(
              download_mode=SCREAMING_SNAKE_CASE_,			file_format=self._file_format,			)
          return self.builder.as_dataset(split=self.split       )



