




"""simple docstring"""





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

import numpy as np
import torch

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


A   =       logging.get_logger(__name__)



class      a__				(	__magic_name__   ):
			lowercase_    =	["input_features", "is_longer"]


			def __init__(      self      :    Any      ,  UpperCamelCase_      :    List[Any]=64      ,  UpperCamelCase_      :    int=48000      ,  UpperCamelCase_      :    Optional[int]=480      ,  UpperCamelCase_      :    Optional[int]=10      ,  UpperCamelCase_      :    Union[str, Any]=1024      ,  UpperCamelCase_      :    Optional[int]=0.0      ,  UpperCamelCase_      :    Optional[int]=False      ,  UpperCamelCase_      :    float = 0      ,  UpperCamelCase_      :    float = 14000      ,  UpperCamelCase_      :    int = None      ,  UpperCamelCase_      :    str = "fusion"      ,  UpperCamelCase_      :    str = "repeatpad"      ,  **UpperCamelCase_      :    Optional[Any]      ,  ):


						"""simple docstring"""




						super().__init__(
						    feature_size=UpperCamelCase_      ,  sampling_rate=UpperCamelCase_      ,  padding_value=UpperCamelCase_      ,  return_attention_mask=UpperCamelCase_      ,  **UpperCamelCase_      ,  )
						__UpperCAmelCase	: Union[str, Any]		=					top_db
						__UpperCAmelCase	: str		=					truncation
						__UpperCAmelCase	: str		=					padding
						__UpperCAmelCase	: Union[str, Any]		=					fft_window_size
						__UpperCAmelCase	: str		=					(fft_window_size >> 1) + 1
						__UpperCAmelCase	: Optional[int]		=					hop_length
						__UpperCAmelCase	: int		=					max_length_s
						__UpperCAmelCase	: List[Any]		=					max_length_s * sampling_rate
						__UpperCAmelCase	: Optional[Any]		=					sampling_rate
						__UpperCAmelCase	: Tuple		=					frequency_min
						__UpperCAmelCase	: str		=					frequency_max
						__UpperCAmelCase	: Dict		=					mel_filter_bank(
						    num_frequency_bins=self.nb_frequency_bins      ,  num_mel_filters=UpperCamelCase_      ,  min_frequency=UpperCamelCase_      ,  max_frequency=UpperCamelCase_      ,  sampling_rate=UpperCamelCase_      ,  norm=UpperCamelCase_      ,  mel_scale="htk"      ,  )
						__UpperCAmelCase	: Union[str, Any]		=					mel_filter_bank(
						    num_frequency_bins=self.nb_frequency_bins      ,  num_mel_filters=UpperCamelCase_      ,  min_frequency=UpperCamelCase_      ,  max_frequency=UpperCamelCase_      ,  sampling_rate=UpperCamelCase_      ,  norm="slaney"      ,  mel_scale="slaney"      ,  )


			def       a_     (      self      :    int):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					copy.deepcopy(self.__dict__)
						__UpperCAmelCase	: str		=					self.__class__.__name__
						if "mel_filters" in output:
									del output["mel_filters"]
						if "mel_filters_slaney" in output:
									del output["mel_filters_slaney"]
						return output


			def       a_     (      self      :    Dict      ,  UpperCamelCase_      :    np.array      ,  UpperCamelCase_      :    Optional[np.array] = None):


						"""simple docstring"""




						__UpperCAmelCase	: Any		=					spectrogram(
						    UpperCamelCase_      ,  window_function(self.fft_window_size      ,  "hann")      ,  frame_length=self.fft_window_size      ,  hop_length=self.hop_length      ,  power=2.0      ,  mel_filters=UpperCamelCase_      ,  log_mel="dB"      ,  )
						return log_mel_spectrogram.T


			def       a_     (      self      :    List[str]      ,  UpperCamelCase_      :    List[str]      ,  UpperCamelCase_      :    Any      ,  UpperCamelCase_      :    Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase	: str		=					np.array_split(list(range(0      ,  total_frames - chunk_frames + 1))      ,  3)
						if len(ranges[1]) == 0:
									# if the audio is too short, we just use the first chunk
									__UpperCAmelCase	: Any		=					[0]
						if len(ranges[2]) == 0:
									# if the audio is too short, we just use the first chunk
									__UpperCAmelCase	: int		=					[0]
						# randomly choose index for each part
						__UpperCAmelCase	: Union[str, Any]		=					np.random.choice(ranges[0])
						__UpperCAmelCase	: List[str]		=					np.random.choice(ranges[1])
						__UpperCAmelCase	: Any		=					np.random.choice(ranges[2])

						__UpperCAmelCase	: Any		=					mel[idx_front : idx_front + chunk_frames, :]
						__UpperCAmelCase	: List[Any]		=					mel[idx_middle : idx_middle + chunk_frames, :]
						__UpperCAmelCase	: List[Any]		=					mel[idx_back : idx_back + chunk_frames, :]

						__UpperCAmelCase	: Union[str, Any]		=					torch.tensor(mel[None, None, :])
						__UpperCAmelCase	: List[Any]		=					torch.nn.functional.interpolate(
						    UpperCamelCase_      ,  size=[chunk_frames, 64]      ,  mode="bilinear"      ,  align_corners=UpperCamelCase_)
						__UpperCAmelCase	: Dict		=					mel_shrink[0][0].numpy()
						__UpperCAmelCase	: str		=					np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back]      ,  axis=0)
						return mel_fusion


			def       a_     (      self      :    str      ,  UpperCamelCase_      :    np.array      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    Optional[Any]      ,  UpperCamelCase_      :    Optional[Any]):


						"""simple docstring"""




						if waveform.shape[0] > max_length:
									if truncation == "rand_trunc":
												__UpperCAmelCase	: Dict		=					True
												# random crop to max_length (for compatibility) -> this should be handled by self.pad
												__UpperCAmelCase	: Optional[Any]		=					len(UpperCamelCase_) - max_length
												__UpperCAmelCase	: Optional[int]		=					np.random.randint(0      ,  overflow + 1)
												__UpperCAmelCase	: str		=					waveform[idx : idx + max_length]
												__UpperCAmelCase	: Union[str, Any]		=					self._np_extract_fbank_features(UpperCamelCase_      ,  self.mel_filters_slaney)[None, :]
									elif truncation == "fusion":
												__UpperCAmelCase	: Dict		=					self._np_extract_fbank_features(UpperCamelCase_      ,  self.mel_filters)
												__UpperCAmelCase	: List[str]		=					max_length // self.hop_length + 1  # the +1 related to how the spectrogram is computed
												__UpperCAmelCase	: List[str]		=					mel.shape[0]
												if chunk_frames == total_frames:
															# there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length.
															# In this case, we just use the whole audio.
															__UpperCAmelCase	: List[Any]		=					np.stack([mel, mel, mel, mel]      ,  axis=0)
															__UpperCAmelCase	: str		=					False
												else:
															__UpperCAmelCase	: List[Any]		=					self._random_mel_fusion(UpperCamelCase_      ,  UpperCamelCase_      ,  UpperCamelCase_)
															__UpperCAmelCase	: str		=					True
									else:
												raise NotImplementedError(F"data_truncating {truncation} not implemented")

						else:
									__UpperCAmelCase	: int		=					False
									# only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding
									if waveform.shape[0] < max_length:
												if padding == "repeat":
															__UpperCAmelCase	: Any		=					int(max_length / len(UpperCamelCase_))
															__UpperCAmelCase	: Optional[int]		=					np.stack(np.tile(UpperCamelCase_      ,  n_repeat + 1))[:max_length]
												if padding == "repeatpad":
															__UpperCAmelCase	: Any		=					int(max_length / len(UpperCamelCase_))
															__UpperCAmelCase	: Any		=					np.stack(np.tile(UpperCamelCase_      ,  UpperCamelCase_))
												__UpperCAmelCase	: Optional[Any]		=					np.pad(UpperCamelCase_      ,  (0, max_length - waveform.shape[0])      ,  mode="constant"      ,  constant_values=0)

									if truncation == "fusion":
												__UpperCAmelCase	: Dict		=					self._np_extract_fbank_features(UpperCamelCase_      ,  self.mel_filters)
												__UpperCAmelCase	: Union[str, Any]		=					np.stack([input_mel, input_mel, input_mel, input_mel]      ,  axis=0)
									else:
												__UpperCAmelCase	: Optional[Any]		=					self._np_extract_fbank_features(UpperCamelCase_      ,  self.mel_filters_slaney)[None, :]

						return input_mel, longer


			def __call__(      self      :    int      ,  UpperCamelCase_      :    Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]      ,  UpperCamelCase_      :    str = None      ,  UpperCamelCase_      :    Optional[str] = None      ,  UpperCamelCase_      :    Optional[int] = None      ,  UpperCamelCase_      :    Optional[int] = None      ,  UpperCamelCase_      :    Optional[Union[str, TensorType]] = None      ,  **UpperCamelCase_      :    str      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Any		=					truncation if truncation is not None else self.truncation
						__UpperCAmelCase	: Optional[int]		=					padding if padding else self.padding

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

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

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

						# always return batch
						if not is_batched:
									__UpperCAmelCase	: int		=					[np.asarray(UpperCamelCase_)]

						# convert to mel spectrogram, truncate and pad if needed.
						__UpperCAmelCase	: Any		=					[
						    self._get_input_mel(UpperCamelCase_      ,  max_length if max_length else self.nb_max_samples      ,  UpperCamelCase_      ,  UpperCamelCase_)
						    for waveform in raw_speech
						]

						__UpperCAmelCase	: Optional[Any]		=					[]
						__UpperCAmelCase	: Optional[Any]		=					[]
						for mel, longer in padded_inputs:
									input_mel.append(UpperCamelCase_)
									is_longer.append(UpperCamelCase_)

						if truncation == "fusion" and sum(UpperCamelCase_) == 0:
									# if no audio is longer than 10s, then randomly select one audio to be longer
									__UpperCAmelCase	: Union[str, Any]		=					np.random.randint(0      ,  len(UpperCamelCase_))
									__UpperCAmelCase	: int		=					True

						if isinstance(input_mel[0]      ,  UpperCamelCase_):
									__UpperCAmelCase	: Tuple		=					[np.asarray(UpperCamelCase_      ,  dtype=np.floataa) for feature in input_mel]

						# is_longer is a list of bool
						__UpperCAmelCase	: Dict		=					[[longer] for longer in is_longer]

						__UpperCAmelCase	: Dict		=					{"input_features": input_mel, "is_longer": is_longer}
						__UpperCAmelCase	: List[str]		=					BatchFeature(UpperCamelCase_)

						if return_tensors is not None:
									__UpperCAmelCase	: Optional[Any]		=					input_features.convert_to_tensors(UpperCamelCase_)

						return input_features








"""simple docstring"""





from ...processing_utils import ProcessorMixin



class      a__				(	__magic_name__   ):
			lowercase_    =	["image_processor", "feature_extractor"]
			lowercase_    =	"TvltImageProcessor"
			lowercase_    =	"TvltFeatureExtractor"


			def __init__(      self      :    Tuple      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    Dict):


						"""simple docstring"""




						super().__init__(image_processor=UpperCamelCase_      ,  feature_extractor=UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					image_processor
						__UpperCAmelCase	: Dict		=					feature_extractor


			def __call__(      self      :    Tuple      ,  UpperCamelCase_      :    str=None      ,  UpperCamelCase_      :    Optional[int]=None      ,  UpperCamelCase_      :    Optional[int]=None      ,  UpperCamelCase_      :    str=None      ,  UpperCamelCase_      :    List[str]=False      ,  UpperCamelCase_      :    Optional[int]=False      ,  *UpperCamelCase_      :    List[str]      ,  **UpperCamelCase_      :    str      ,  ):


						"""simple docstring"""




						if images is None and audio is None:
									raise ValueError("You need to specify either an `images` or `audio` input to process.")

						__UpperCAmelCase	: Optional[Any]		=					None
						if images is not None:
									__UpperCAmelCase	: int		=					self.image_processor(UpperCamelCase_      ,  mask_pixel=UpperCamelCase_      ,  *UpperCamelCase_      ,  **UpperCamelCase_)
						if images_mixed is not None:
									__UpperCAmelCase	: int		=					self.image_processor(UpperCamelCase_      ,  is_mixed=UpperCamelCase_      ,  *UpperCamelCase_      ,  **UpperCamelCase_)
						if audio is not None:
									__UpperCAmelCase	: List[Any]		=					self.feature_extractor(
									    UpperCamelCase_      ,  *UpperCamelCase_      ,  sampling_rate=UpperCamelCase_      ,  mask_audio=UpperCamelCase_      ,  **UpperCamelCase_)

						__UpperCAmelCase	: List[str]		=					{}
						if audio is not None:
									output_dict.update(UpperCamelCase_)
						if images is not None:
									output_dict.update(UpperCamelCase_)
						if images_mixed_dict is not None:
									output_dict.update(UpperCamelCase_)
						return output_dict


			@property
			def       a_     (      self      :    str):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					self.image_processor.model_input_names
						__UpperCAmelCase	: List[Any]		=					self.feature_extractor.model_input_names
						return list(dict.fromkeys(image_processor_input_names + feature_extractor_input_names))


