"""simple docstring"""





from math import ceil
from typing import List, Optional, Union

import numpy as np

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


lowerCAmelCase_     =	logging.get_logger(__name__)




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						["audio_values", "audio_mask"]





				def __init__(      self							:							Optional[Any]      ,    _A							:							Any=2_0_4_8      ,    _A							:							Dict=1      ,    _A							:							Union[str, Any]=[1_6, 1_6]      ,    _A							:							List[str]=1_2_8      ,    _A							:							Tuple=4_4_1_0_0      ,    _A							:							Tuple=8_6      ,    _A							:							Dict=2_0_4_8      ,    _A							:							Optional[Any]=0.0      ,    **_A							:							Optional[Any]      ,    ):



											"""simple docstring"""







											super().__init__(
											    feature_size=_A      ,    sampling_rate=_A      ,    padding_value=_A      ,    **_A      ,    )

											_SCREAMING_SNAKE_CASE		:						Any			  =    spectrogram_length
											_SCREAMING_SNAKE_CASE		:						int			  =    num_channels
											_SCREAMING_SNAKE_CASE		:						Dict			  =    patch_size
											_SCREAMING_SNAKE_CASE		:						Dict			  =    feature_size // self.patch_size[1]
											_SCREAMING_SNAKE_CASE		:						Any			  =    n_fft
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    sampling_rate // hop_length_to_sampling_rate
											_SCREAMING_SNAKE_CASE		:						str			  =    sampling_rate
											_SCREAMING_SNAKE_CASE		:						Any			  =    padding_value
											_SCREAMING_SNAKE_CASE		:						Dict			  =    mel_filter_bank(
											    num_frequency_bins=1 + n_fft // 2      ,    num_mel_filters=_A      ,    min_frequency=0.0      ,    max_frequency=22_050.0      ,    sampling_rate=_A      ,    norm="""slaney"""      ,    mel_scale="""slaney"""      ,    ).T





				def 		_lowerCAmelCase		(      self							:							Any      ,    _A							:							np.array):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						str			  =    spectrogram(
											    _A      ,    window_function(self.n_fft      ,    """hann""")      ,    frame_length=self.n_fft      ,    hop_length=self.hop_length      ,    power=2.0      ,    mel_filters=self.mel_filters.T      ,    log_mel="""dB"""      ,    db_range=80.0      ,    )
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    log_spec[:, :-1]
											_SCREAMING_SNAKE_CASE		:						str			  =    log_spec - 20.0
											_SCREAMING_SNAKE_CASE		:						Dict			  =    np.clip(log_spec / 40.0      ,    -2.0      ,    0.0) + 1.0
											return log_spec





				def __call__(      self							:							Dict      ,    _A							:							Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]      ,    _A							:							Optional[Union[str, TensorType]] = None      ,    _A							:							Optional[bool] = True      ,    _A							:							Optional[int] = None      ,    _A							:							bool = False      ,    _A							:							bool = False      ,    **_A							:							Tuple      ,    ):



											"""simple docstring"""







											if sampling_rate is not None:
																		if sampling_rate != self.sampling_rate:
																									raise ValueError(
																									    """This feature extractor is set to support sampling rate"""
																									    f""" of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled"""
																									    f""" with {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.""")

											_SCREAMING_SNAKE_CASE		:						Tuple			  =    isinstance(_A      ,    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}""")
											_SCREAMING_SNAKE_CASE		:						Dict			  =    is_batched_numpy or (
											    isinstance(_A      ,    (list, tuple)) and (isinstance(raw_speech[0]      ,    (np.ndarray, tuple, list)))
											)
											if is_batched:
																		_SCREAMING_SNAKE_CASE		:						Dict			  =    [np.asarray([speech]      ,    dtype=np.floataa).T for speech in raw_speech]
											elif not is_batched and not isinstance(_A      ,    np.ndarray):
																		_SCREAMING_SNAKE_CASE		:						Dict			  =    np.asarray(_A      ,    dtype=np.floataa)
											elif isinstance(_A      ,    np.ndarray) and raw_speech.dtype is np.dtype(np.floataa):
																		_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    raw_speech.astype(np.floataa)
											# always return batch
											if not is_batched:
																		_SCREAMING_SNAKE_CASE		:						str			  =    [np.asarray([raw_speech]).T]

											# Convert audio signals to log mel spectrograms, truncate by time axis
											_SCREAMING_SNAKE_CASE		:						str			  =    [
											    self._np_extract_fbank_features(waveform.squeeze()).T[: self.spectrogram_length] for waveform in raw_speech
											]
											if isinstance(audio_features[0]      ,    _A):
																		_SCREAMING_SNAKE_CASE		:						Dict			  =    [np.asarray(_A      ,    dtype=np.floataa) for feature in audio_features]

											# Create audio attention mask
											_SCREAMING_SNAKE_CASE		:						int			  =    max(
											    [ceil(feature.shape[0] / self.patch_size[0]) * self.freq_len for feature in audio_features])  # The maximum number of audio patches in a batch
											if return_attention_mask:
																		_SCREAMING_SNAKE_CASE		:						Dict			  =    [
																		    (ceil(feature.shape[0] / self.patch_size[0]) * self.freq_len) * [1]
																		    + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]) * self.freq_len) * [0]
																		    for feature in audio_features
																		]
																		_SCREAMING_SNAKE_CASE		:						List[str]			  =    np.array(_A).astype(np.floataa)

											# convert into correct format for padding
											_SCREAMING_SNAKE_CASE		:						Any			  =    max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    np.ones([len(_A), 1, max_time_len, self.feature_size]).astype(np.floataa)
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    padded_audio_features * self.padding_value
											for i in range(len(_A)):
																		_SCREAMING_SNAKE_CASE		:						Tuple			  =    audio_features[i]
																		_SCREAMING_SNAKE_CASE		:						int			  =    feature

											# return as BatchFeature
											if return_attention_mask:
																		_SCREAMING_SNAKE_CASE		:						Dict			  =    {"""audio_values""": padded_audio_features, """audio_mask""": audio_mask}
											else:
																		_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    {"""audio_values""": padded_audio_features}

											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    BatchFeature(data=_A      ,    tensor_type=_A)
											return encoded_inputs



"""simple docstring"""





from __future__ import annotations



def      lowerCamelCase_(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	)->    tuple:
							if (electron_conc, hole_conc, intrinsic_conc).count(0  ) != 1:
														raise ValueError("""You cannot supply more or less than 2 values"""  )
							elif electron_conc < 0:
														raise ValueError("""Electron concentration cannot be negative in a semiconductor"""  )
							elif hole_conc < 0:
														raise ValueError("""Hole concentration cannot be negative in a semiconductor"""  )
							elif intrinsic_conc < 0:
														raise ValueError(
														    """Intrinsic concentration cannot be negative in a semiconductor"""  )
							elif electron_conc == 0:
														return (
														    "electron_conc",
														    intrinsic_conc**2 / hole_conc,
														)
							elif hole_conc == 0:
														return (
														    "hole_conc",
														    intrinsic_conc**2 / electron_conc,
														)
							elif intrinsic_conc == 0:
														return (
														    "intrinsic_conc",
														    (electron_conc * hole_conc) ** 0.5,
														)
							else:
														return (-1, -1)


if __name__ == "__main__":
			import doctest

			doctest.testmod()


