


'''simple docstring'''






from typing import List, Optional, Union

import numpy as np

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 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Tuple     						=				["input_values", "padding_mask"]







							def __init__(		self ,   lowerCamelCase_ = 1 ,   lowerCamelCase_ = 2_40_00 ,   lowerCamelCase_ = 0.0 ,   lowerCamelCase_ = None ,   lowerCamelCase_ = None ,   **lowerCamelCase_ ,   )	->							Any:
														super().__init__(feature_size=lowerCamelCase_ ,   sampling_rate=lowerCamelCase_ ,   padding_value=lowerCamelCase_ ,   **lowerCamelCase_       )
														lowerCAmelCase__	       =     chunk_length_s
														lowerCAmelCase__	       =     overlap







							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[int]:
														if self.chunk_length_s is None:
																					return None
														else:
																					return int(self.chunk_length_s * self.sampling_rate       )







							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[int]:
														if self.chunk_length_s is None or self.overlap is None:
																					return None
														else:
																					return max(1 ,   int((1.0 - self.overlap) * self.chunk_length       )       )







							def __call__(		self ,   lowerCamelCase_ ,   lowerCamelCase_ = None ,   lowerCamelCase_ = False ,   lowerCamelCase_ = None ,   lowerCamelCase_ = None ,   lowerCamelCase_ = None ,   )	->							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 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 padding and truncation:
																					raise ValueError('''Both padding and truncation were set. Make sure you only set one.'''       )
														elif padding is None:
																					# by default let's pad the inputs
																					lowerCAmelCase__	       =     True

														lowerCAmelCase__	       =     bool(
														    isinstance(lowerCamelCase_ ,   (list, tuple)       ) and (isinstance(raw_audio[0] ,   (np.ndarray, tuple, list)       ))       )

														if is_batched:
																					lowerCAmelCase__	       =     [np.asarray(lowerCamelCase_ ,   dtype=np.floataa       ).T for audio in raw_audio]
														elif not is_batched and not isinstance(lowerCamelCase_ ,   np.ndarray       ):
																					lowerCAmelCase__	       =     np.asarray(lowerCamelCase_ ,   dtype=np.floataa       )
														elif isinstance(lowerCamelCase_ ,   np.ndarray       ) and raw_audio.dtype is np.dtype(np.floataa       ):
																					lowerCAmelCase__	       =     raw_audio.astype(np.floataa       )

														# always return batch
														if not is_batched:
																					lowerCAmelCase__	       =     [np.asarray(lowerCamelCase_       ).T]

														# verify inputs are valid
														for idx, example in enumerate(lowerCamelCase_       ):
																					if example.ndim > 2:
																												raise ValueError(F"""Expected input shape (channels, length) but got shape {example.shape}"""       )
																					if self.feature_size == 1 and example.ndim != 1:
																												raise ValueError(F"""Expected mono audio but example has {example.shape[-1]} channels"""       )
																					if self.feature_size == 2 and example.shape[-1] != 2:
																												raise ValueError(F"""Expected stereo audio but example has {example.shape[-1]} channels"""       )

														lowerCAmelCase__	       =     None
														lowerCAmelCase__	       =     BatchFeature({'''input_values''': raw_audio}       )
														if self.chunk_stride is not None and self.chunk_length is not None and max_length is None:
																					if truncation:
																												lowerCAmelCase__	       =     min(array.shape[0] for array in raw_audio       )
																												lowerCAmelCase__	       =     int(np.floor(max_length / self.chunk_stride       )       )
																												lowerCAmelCase__	       =     (nb_step - 1) * self.chunk_stride + self.chunk_length
																					elif padding:
																												lowerCAmelCase__	       =     max(array.shape[0] for array in raw_audio       )
																												lowerCAmelCase__	       =     int(np.ceil(max_length / self.chunk_stride       )       )
																												lowerCAmelCase__	       =     (nb_step - 1) * self.chunk_stride + self.chunk_length
																												lowerCAmelCase__	       =     '''max_length'''
																					else:
																												lowerCAmelCase__	       =     input_values

        # normal padding on batch
														if padded_inputs is None:
																					lowerCAmelCase__	       =     self.pad(
																					    lowerCamelCase_ ,   max_length=lowerCamelCase_ ,   truncation=lowerCamelCase_ ,   padding=lowerCamelCase_ ,   return_attention_mask=lowerCamelCase_ ,   )
																					if padding:
																												lowerCAmelCase__	       =     padded_inputs.pop('''attention_mask'''       )

														lowerCAmelCase__	       =     []
														for example in padded_inputs.pop('''input_values'''       ):
																					if self.feature_size == 1:
																												lowerCAmelCase__	       =     example[..., None]
																					input_values.append(example.T       )

														lowerCAmelCase__	       =     input_values
														if return_tensors is not None:
																					lowerCAmelCase__	       =     padded_inputs.convert_to_tensors(lowerCamelCase_       )

														return padded_inputs








'''simple docstring'''






import os
from typing import Optional

import fsspec
from fsspec.archive import AbstractArchiveFileSystem
from fsspec.utils import DEFAULT_BLOCK_SIZE




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Dict     						=				""
							lowercase__      :       str     						=				(
							    None  # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz
							)
							lowercase__      :       str     						=				None  # compression type in fsspec. ex: "gzip"
							lowercase__      :       str     						=				None  # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz







							def __init__(		self ,   lowerCamelCase_ = "" ,   lowerCamelCase_ = None ,   lowerCamelCase_ = None ,   **lowerCamelCase_       )	->							Any:
														super().__init__(self ,   **lowerCamelCase_       )
														# always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode
														lowerCAmelCase__	       =     fsspec.open(
														    lowerCamelCase_ ,   mode='''rb''' ,   protocol=lowerCamelCase_ ,   compression=self.compression ,   client_kwargs={
														        '''requote_redirect_url''': False,  # see https://github.com/huggingface/datasets/pull/5459
														        '''trust_env''': True,  # Enable reading proxy env variables.
														        **(target_options or {}).pop('''client_kwargs''' ,   {}       ),  # To avoid issues if it was already passed.
														    } ,   **(target_options or {}) ,   )
														lowerCAmelCase__	       =     os.path.basename(self.file.path.split('''::'''       )[0]       )
														lowerCAmelCase__	       =     (
														    self.compressed_name[: self.compressed_name.rindex('''.'''       )]
														    if '''.''' in self.compressed_name
														    else self.compressed_name
														)
														lowerCAmelCase__	       =     None







							@classmethod
							def   __SCREAMING_SNAKE_CASE							(		cls ,   lowerCamelCase_       )	->							Any:
														# compressed file paths are always relative to the archive root
														return super()._strip_protocol(lowerCamelCase_       ).lstrip('''/'''       )







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[int]:
														if self.dir_cache is None:
																					lowerCAmelCase__	       =     {**self.file.fs.info(self.file.path       ), '''name''': self.uncompressed_name}
																					lowerCAmelCase__	       =     {f['''name''']: f}







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_       )	->							List[Any]:
														return self.file.open().read()







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_ = "rb" ,   lowerCamelCase_=None ,   lowerCamelCase_=True ,   lowerCamelCase_=None ,   **lowerCamelCase_ ,   )	->							List[str]:
														lowerCAmelCase__	       =     self._strip_protocol(lowerCamelCase_       )
														if mode != "rb":
																					raise ValueError(F"""Tried to read with mode {mode} on file {self.file.path} opened with mode 'rb'"""       )
														return self.file.open()




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Optional[Any]     						=				"bz2"
							lowercase__      :       str     						=				"bz2"
							lowercase__      :       Optional[int]     						=				".bz2"




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Any     						=				"gzip"
							lowercase__      :       int     						=				"gzip"
							lowercase__      :       int     						=				".gz"




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Tuple     						=				"lz4"
							lowercase__      :       Optional[Any]     						=				"lz4"
							lowercase__      :       int     						=				".lz4"




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Optional[int]     						=				"xz"
							lowercase__      :       str     						=				"xz"
							lowercase__      :       List[Any]     						=				".xz"




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Union[str, Any]     						=				"zstd"
							lowercase__      :       Union[str, Any]     						=				"zstd"
							lowercase__      :       Dict     						=				".zst"







							def __init__(		self ,   lowerCamelCase_ ,   lowerCamelCase_ = "rb" ,   lowerCamelCase_ = None ,   lowerCamelCase_ = None ,   lowerCamelCase_ = DEFAULT_BLOCK_SIZE ,   **lowerCamelCase_ ,   )	->							List[Any]:
														super().__init__(
														    fo=lowerCamelCase_ ,   mode=lowerCamelCase_ ,   target_protocol=lowerCamelCase_ ,   target_options=lowerCamelCase_ ,   block_size=lowerCamelCase_ ,   **lowerCamelCase_ ,   )
														# We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2:
														#
														# File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open
														#     out.close = close
														# AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only
														#
														# see https://github.com/intake/filesystem_spec/issues/725
														lowerCAmelCase__	       =     self.file.__enter__




														class 		a__    :





																					'''simple docstring'''






																					def __init__(		self ,   lowerCamelCase_       )	->							List[str]:
																												lowerCAmelCase__	       =     file_







																					def __enter__(		self       )	->							Tuple:
																												self._file.__enter__()
																												return self







																					def __exit__(		self ,   *lowerCamelCase_ ,   **lowerCamelCase_       )	->							str:
																												self._file.__exit__(*lowerCamelCase_ ,   **lowerCamelCase_       )







																					def __iter__(		self       )	->							Any:
																												return iter(self._file       )







																					def   __SCREAMING_SNAKE_CASE							(		self       )	->							Union[str, Any]:
																												return next(self._file       )







																					def __getattr__(		self ,   lowerCamelCase_       )	->							str:
																												return getattr(self._file ,   lowerCamelCase_       )

														def fixed_enter(*lowerCamelCase_ ,   **lowerCamelCase_       ):
																					return WrappedFile(_enter(*lowerCamelCase_ ,   **lowerCamelCase_       )       )

														lowerCAmelCase__	       =     fixed_enter