



import warnings
from typing import Any, Dict, List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


__snake_case    :		Optional[int]  =logging.get_logger(__name__)



class 	lowerCamelCase__      (							lowerCamelCase__):




		'''simple docstring'''



		snake_case_     =["""input_values""", """attention_mask"""]



		def __init__(self		,__lowerCamelCase = 1		,__lowerCamelCase = 1_60_00		,__lowerCamelCase = 0.0		,__lowerCamelCase = False		,__lowerCamelCase = 80		,__lowerCamelCase = 16		,__lowerCamelCase = 64		,__lowerCamelCase = "hann_window"		,__lowerCamelCase = 1.0		,__lowerCamelCase = 80		,__lowerCamelCase = 76_00		,__lowerCamelCase = 1e-10		,__lowerCamelCase = 2		,__lowerCamelCase = True		,**__lowerCamelCase		,)					->      Any:

						"""simple docstring"""






						super().__init__(feature_size=__lowerCamelCase		,sampling_rate=__lowerCamelCase		,padding_value=__lowerCamelCase		,**__lowerCamelCase			)
						lowerCAmelCase__					:    int												=					do_normalize
						lowerCAmelCase__					:    Any												=					return_attention_mask

						lowerCAmelCase__					:    Tuple												=					num_mel_bins
						lowerCAmelCase__					:    Union[str, Any]												=					hop_length
						lowerCAmelCase__					:    int												=					win_length
						lowerCAmelCase__					:    Any												=					win_function
						lowerCAmelCase__					:    List[Any]												=					frame_signal_scale
						lowerCAmelCase__					:    List[str]												=					fmin
						lowerCAmelCase__					:    Any												=					fmax
						lowerCAmelCase__					:    Any												=					mel_floor
						lowerCAmelCase__					:    Optional[Any]												=					reduction_factor

						lowerCAmelCase__					:    Union[str, Any]												=					win_length * sampling_rate // 10_00
						lowerCAmelCase__					:    Any												=					hop_length * sampling_rate // 10_00
						lowerCAmelCase__					:    int												=					optimal_fft_length(self.sample_size			)
						lowerCAmelCase__					:    Dict												=					(self.n_fft // 2) + 1

						lowerCAmelCase__					:    Dict												=					window_function(window_length=self.sample_size		,name=self.win_function		,periodic=__lowerCamelCase			)

						lowerCAmelCase__					:    int												=					mel_filter_bank(
						    num_frequency_bins=self.n_freqs		,num_mel_filters=self.num_mel_bins		,min_frequency=self.fmin		,max_frequency=self.fmax		,sampling_rate=self.sampling_rate		,norm='''slaney'''		,mel_scale='''slaney'''		,)

						if frame_signal_scale != 1.0:
										warnings.warn(
										    '''The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers'''		,__lowerCamelCase		,)
						if reduction_factor != 2.0:
										warnings.warn(
										    '''The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers'''		,__lowerCamelCase		,)



		@staticmethod
		# Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
		def 		lowerCAmelCase__				(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase = 0.0			)					->      List[np.ndarray]:

						"""simple docstring"""






						if attention_mask is not None:
										lowerCAmelCase__					:    Tuple												=					np.array(__lowerCamelCase		,np.intaa			)
										lowerCAmelCase__					:    List[Any]												=					[]

										for vector, length in zip(__lowerCamelCase		,attention_mask.sum(-1			)			):
														lowerCAmelCase__					:    List[str]												=					(vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7			)
														if length < normed_slice.shape[0]:
																		lowerCAmelCase__					:    str												=					padding_value

														normed_input_values.append(__lowerCamelCase			)
						else:
										lowerCAmelCase__					:    Union[str, Any]												=					[(x - x.mean()) / np.sqrt(x.var() + 1e-7			) for x in input_values]

						return normed_input_values



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,)					->      np.ndarray:

						"""simple docstring"""






						lowerCAmelCase__					:    str												=					spectrogram(
						    __lowerCamelCase		,window=self.window		,frame_length=self.sample_size		,hop_length=self.sample_stride		,fft_length=self.n_fft		,mel_filters=self.mel_filters		,mel_floor=self.mel_floor		,log_mel='''log10'''		,)
						return log_mel_spec.T



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

						"""simple docstring"""






						if audio is None and audio_target is None:
										raise ValueError('''You must provide either `audio` or `audio_target` values.'''			)

						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 audio 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.'''			)

						if audio is not None:
										lowerCAmelCase__					:    Dict												=					self._process_audio(
										    __lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,**__lowerCamelCase		,)
						else:
										lowerCAmelCase__					:    List[str]												=					None

						if audio_target is not None:
										lowerCAmelCase__					:    List[str]												=					self._process_audio(
										    __lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,**__lowerCamelCase		,)

										if inputs is None:
														return inputs_target
										else:
														lowerCAmelCase__					:    Optional[int]												=					inputs_target['''input_values''']
														lowerCAmelCase__					:    List[Any]												=					inputs_target.get('''attention_mask'''			)
														if decoder_attention_mask is not None:
																		lowerCAmelCase__					:    Any												=					decoder_attention_mask

						return inputs



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase = False		,__lowerCamelCase = False		,__lowerCamelCase = None		,__lowerCamelCase = False		,__lowerCamelCase = None		,__lowerCamelCase = None		,__lowerCamelCase = None		,**__lowerCamelCase		,)					->      BatchFeature:

						"""simple docstring"""






						lowerCAmelCase__					:    Optional[int]												=					isinstance(__lowerCamelCase		,np.ndarray			) and len(speech.shape			) > 1
						if is_batched_numpy and len(speech.shape			) > 2:
										raise ValueError(f"""Only mono-channel audio is supported for input to {self}"""			)
						lowerCAmelCase__					:    str												=					is_batched_numpy or (
						    isinstance(__lowerCamelCase		,(list, tuple)			) and (isinstance(speech[0]		,(np.ndarray, tuple, list)			))
						)

						if is_batched:
										lowerCAmelCase__					:    Optional[int]												=					[np.asarray(__lowerCamelCase		,dtype=np.floataa			) for speech in speech]
						elif not is_batched and not isinstance(__lowerCamelCase		,np.ndarray			):
										lowerCAmelCase__					:    Optional[int]												=					np.asarray(__lowerCamelCase		,dtype=np.floataa			)
						elif isinstance(__lowerCamelCase		,np.ndarray			) and speech.dtype is np.dtype(np.floataa			):
										lowerCAmelCase__					:    List[str]												=					speech.astype(np.floataa			)

						# always return batch
						if not is_batched:
										lowerCAmelCase__					:    Optional[Any]												=					[speech]

						# needed to make pad() work on spectrogram inputs
						lowerCAmelCase__					:    List[Any]												=					self.feature_size

						# convert into correct format for padding
						if is_target:
										lowerCAmelCase__					:    Optional[int]												=					[self._extract_mel_features(__lowerCamelCase			) for waveform in speech]
										lowerCAmelCase__					:    List[Any]												=					BatchFeature({'''input_values''': features}			)
										lowerCAmelCase__					:    Tuple												=					self.num_mel_bins
						else:
										lowerCAmelCase__					:    Dict												=					BatchFeature({'''input_values''': speech}			)

						lowerCAmelCase__					:    Optional[Any]												=					self.pad(
						    __lowerCamelCase		,padding=__lowerCamelCase		,max_length=__lowerCamelCase		,truncation=__lowerCamelCase		,pad_to_multiple_of=__lowerCamelCase		,return_attention_mask=__lowerCamelCase		,**__lowerCamelCase		,)

						lowerCAmelCase__					:    List[Any]												=					feature_size_hack

						# convert input values to correct format
						lowerCAmelCase__					:    str												=					padded_inputs['''input_values''']
						if not isinstance(input_values[0]		,np.ndarray			):
										lowerCAmelCase__					:    str												=					[np.asarray(__lowerCamelCase		,dtype=np.floataa			) for array in input_values]
						elif (
						    not isinstance(__lowerCamelCase		,np.ndarray			)
						    and isinstance(input_values[0]		,np.ndarray			)
						    and input_values[0].dtype is np.dtype(np.floataa			)
						):
										lowerCAmelCase__					:    Union[str, Any]												=					[array.astype(np.floataa			) for array in input_values]
						elif isinstance(__lowerCamelCase		,np.ndarray			) and input_values.dtype is np.dtype(np.floataa			):
										lowerCAmelCase__					:    List[Any]												=					input_values.astype(np.floataa			)

						# convert attention_mask to correct format
						lowerCAmelCase__					:    str												=					padded_inputs.get('''attention_mask'''			)
						if attention_mask is not None:
										lowerCAmelCase__					:    Optional[int]												=					[np.asarray(__lowerCamelCase		,dtype=np.intaa			) for array in attention_mask]

						# zero-mean and unit-variance normalization
						if not is_target and self.do_normalize:
										lowerCAmelCase__					:    int												=					(
										    attention_mask
										    if self._get_padding_strategies(__lowerCamelCase		,max_length=__lowerCamelCase			) is not PaddingStrategy.DO_NOT_PAD
										    else None
										)
										lowerCAmelCase__					:    Dict												=					self.zero_mean_unit_var_norm(
										    padded_inputs['''input_values''']		,attention_mask=__lowerCamelCase		,padding_value=self.padding_value			)

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

						return padded_inputs



		def 		lowerCAmelCase__				(self			)					->      Dict[str, Any]:

						"""simple docstring"""






						lowerCAmelCase__					:    Dict												=					super().to_dict()

						# Don't serialize these as they are derived from the other properties.
						lowerCAmelCase__					:    List[Any]												=					['''window''', '''mel_filters''', '''sample_size''', '''sample_stride''', '''n_fft''', '''n_freqs''']
						for name in names:
										if name in output:
														del output[name]

						return output






import flax.linen as nn
import jax
import jax.numpy as jnp



class 	lowerCamelCase__      (							nn.Module):




		'''simple docstring'''



		snake_case_     =42
		snake_case_     =jnp.floataa



		def 		lowerCAmelCase__				(self			)					->      List[str]:

						"""simple docstring"""






						lowerCAmelCase__					:    Dict												=					nn.Conv(
						    self.out_channels		,kernel_size=(3, 3)		,strides=(1, 1)		,padding=((1, 1), (1, 1))		,dtype=self.dtype		,)



		def __call__(self		,__lowerCamelCase			)					->      Union[str, Any]:

						"""simple docstring"""






						lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__					:    Dict												=					hidden_states.shape
						lowerCAmelCase__					:    Dict												=					jax.image.resize(
						    __lowerCamelCase		,shape=(batch, height * 2, width * 2, channels)		,method='''nearest'''		,)
						lowerCAmelCase__					:    Dict												=					self.conv(__lowerCamelCase			)
						return hidden_states



class 	lowerCamelCase__      (							nn.Module):




		'''simple docstring'''



		snake_case_     =42
		snake_case_     =jnp.floataa



		def 		lowerCAmelCase__				(self			)					->      int:

						"""simple docstring"""






						lowerCAmelCase__					:    List[Any]												=					nn.Conv(
						    self.out_channels		,kernel_size=(3, 3)		,strides=(2, 2)		,padding=((1, 1), (1, 1))		,dtype=self.dtype		,)



		def __call__(self		,__lowerCamelCase			)					->      Any:

						"""simple docstring"""






						lowerCAmelCase__					:    Optional[int]												=					self.conv(__lowerCamelCase			)
						return hidden_states



class 	lowerCamelCase__      (							nn.Module):




		'''simple docstring'''



		snake_case_     =42
		snake_case_     =None
		snake_case_     =0.0
		snake_case_     =None
		snake_case_     =jnp.floataa



		def 		lowerCAmelCase__				(self			)					->      int:

						"""simple docstring"""






						lowerCAmelCase__					:    List[str]												=					self.in_channels if self.out_channels is None else self.out_channels

						lowerCAmelCase__					:    Union[str, Any]												=					nn.GroupNorm(num_groups=32		,epsilon=1e-5			)
						lowerCAmelCase__					:    Union[str, Any]												=					nn.Conv(
						    __lowerCamelCase		,kernel_size=(3, 3)		,strides=(1, 1)		,padding=((1, 1), (1, 1))		,dtype=self.dtype		,)

						lowerCAmelCase__					:    int												=					nn.Dense(__lowerCamelCase		,dtype=self.dtype			)

						lowerCAmelCase__					:    List[Any]												=					nn.GroupNorm(num_groups=32		,epsilon=1e-5			)
						lowerCAmelCase__					:    Union[str, Any]												=					nn.Dropout(self.dropout_prob			)
						lowerCAmelCase__					:    Optional[int]												=					nn.Conv(
						    __lowerCamelCase		,kernel_size=(3, 3)		,strides=(1, 1)		,padding=((1, 1), (1, 1))		,dtype=self.dtype		,)

						lowerCAmelCase__					:    str												=					self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut

						lowerCAmelCase__					:    Union[str, Any]												=					None
						if use_nin_shortcut:
										lowerCAmelCase__					:    Optional[Any]												=					nn.Conv(
										    __lowerCamelCase		,kernel_size=(1, 1)		,strides=(1, 1)		,padding='''VALID'''		,dtype=self.dtype		,)



		def __call__(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase=True			)					->      Union[str, Any]:

						"""simple docstring"""






						lowerCAmelCase__					:    Tuple												=					hidden_states
						lowerCAmelCase__					:    Dict												=					self.norma(__lowerCamelCase			)
						lowerCAmelCase__					:    Union[str, Any]												=					nn.swish(__lowerCamelCase			)
						lowerCAmelCase__					:    List[Any]												=					self.conva(__lowerCamelCase			)

						lowerCAmelCase__					:    List[Any]												=					self.time_emb_proj(nn.swish(__lowerCamelCase			)			)
						lowerCAmelCase__					:    Dict												=					jnp.expand_dims(jnp.expand_dims(__lowerCamelCase		,1			)		,1			)
						lowerCAmelCase__					:    Optional[int]												=					hidden_states + temb

						lowerCAmelCase__					:    Optional[int]												=					self.norma(__lowerCamelCase			)
						lowerCAmelCase__					:    Optional[int]												=					nn.swish(__lowerCamelCase			)
						lowerCAmelCase__					:    int												=					self.dropout(__lowerCamelCase		,__lowerCamelCase			)
						lowerCAmelCase__					:    List[Any]												=					self.conva(__lowerCamelCase			)

						if self.conv_shortcut is not None:
										lowerCAmelCase__					:    Optional[int]												=					self.conv_shortcut(__lowerCamelCase			)

						return hidden_states + residual

