/usr/local/lib/python3.6/site-packages/datasets/features/__pycache__
Edit: /usr/local/lib/python3.6/site-packages/datasets/features/__pycache__/audio.cpython-36.pyc (11473B)
3
<%Eg5 @ s d dl Z d dlmZmZ d dlmZ d dlmZmZm Z m
Z
mZmZ d dl
Zd dlZd dlmZ ddlmZ ddlmZ dd lmZ dd
lmZmZ erddlmZ eG d
d dZdS ) N) dataclassfield)BytesIO)
TYPE_CHECKINGAnyClassVarDictOptionalUnion)version )config)xopen)
array_cast)no_op_if_value_is_nullstring_to_dict )FeatureTypec @ sN e Zd ZU dZdZee dZedZ e dZ
ee
dZe
e ejej ej dZe
e ed dddZedd Zeeef ed
ddZd!eeeeeeedf f ed
ddZedeedf f dddZeejejf ejdddZd"ejeejdddZ d#eeeeeedf f dddZ!d$ddZ"dd Z#dS )%Audioa Audio Feature to extract audio data from an audio file.
Input: The Audio feature accepts as input:
- A :obj:`str`: Absolute path to the audio file (i.e. random access is allowed).
- A :obj:`dict` with the keys:
- path: String with relative path of the audio file to the archive file.
- bytes: Bytes content of the audio file.
This is useful for archived files with sequential access.
- A :obj:`dict` with the keys:
- path: String with relative path of the audio file to the archive file.
- array: Array containing the audio sample
- sampling_rate: Integer corresponding to the samping rate of the audio sample.
This is useful for archived files with sequential access.
Args:
sampling_rate (:obj:`int`, optional): Target sampling rate. If `None`, the native sampling rate is used.
mono (:obj:`bool`, default ``True``): Whether to convert the audio signal to mono by averaging samples across
channels.
decode (:obj:`bool`, default ``True``): Whether to decode the audio data. If `False`,
returns the underlying dictionary in the format {"path": audio_path, "bytes": audio_bytes}.
Example:
```py
>>> from datasets import load_dataset, Audio
>>> ds = load_dataset("PolyAI/minds14", name="en-US", split="train")
>>> ds = ds.cast_column("audio", Audio(sampling_rate=16000))
>>> ds[0]["audio"]
{'array': array([ 2.3443763e-05, 2.1729663e-04, 2.2145823e-04, ...,
3.8356509e-05, -7.3497440e-06, -2.1754686e-05], dtype=float32),
'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav',
'sampling_rate': 16000}
```
NTdict)bytespathF)defaultinitreprc C s | j S )N)pa_type)self r A/usr/local/lib/python3.6/site-packages/datasets/features/audio.py__call__G s zAudio.__call__)valuereturnc C s yddl }W n, tk
r8 } ztd|W Y dd}~X nX t|trNd|dS d|krt }|j||d |d dd |j ddS |jd dk otj j
|d rV|d jd
rD|jddkrtd|jdrt
j|d t
jd
jt
jd }n t
j|d dddjt
jd }tt }|j|||d dd |j ddS d|jd dS nF|jddk sv|jd dk r|jd|jd dS td| ddS )zEncode example into a format for Arrow.
Args:
value (:obj:`str` or :obj:`dict`): Data passed as input to Audio feature.
Returns:
:obj:`dict`
r Nz;To support encoding audio data, please install 'soundfile'.)r r array
sampling_rateZwav)formatr ZpcmzBTo use PCM files, please specify a 'sampling_rate' in Audio objectr )dtypei hr)r% modezUAn audio sample should have one of 'path' or 'bytes' but they are missing or None in .) soundfileImportError
isinstancestrr writegetvaluegetosr isfileendswithKeyErrornpZ
frombufferZint16ZastypeZfloat32Zmemmapr
ValueError)r r sferrbufferZbytes_valuer r r encode_exampleJ s2
"
zAudio.encode_example)r token_per_repo_idr! c C s | j std|d dk r.|d t|d fn
|d df\}}|dkr`|dkr`td| dn|dk r|jdr| j|r~|n|\}}nd|dk r|jdr|r| j|d\}}q| j|d|d \}}n&|r| j|\}}n| j||d \}}|||d
S )a Decode example audio file into audio data.
Args:
value (:obj:`dict`): a dictionary with keys:
- path: String with relative audio file path.
- bytes: Bytes of the audio file.
token_per_repo_id (:obj:`dict`, optional): To access and decode
audio files from private repositories on the Hub, you can pass
a dictionary repo_id (str) -> token (bool or str)
Returns:
dict
zMDecoding is disabled for this feature. Please use Audio(decode=True) instead.r Nr zJAn audio sample should have one of 'path' or 'bytes' but both are None in r) mp3opus)r; )r r" r# )decodeRuntimeErrorr r6 r3 _decode_mp3_decode_non_mp3_file_like_decode_non_mp3_path_like)r r r; r filer" r# r r r decode_examplex s 0zAudio.decode_exampler )r! c C s, ddl m} | jrtd|d|ddS )z[If in the decodable state, raise an error, otherwise flatten the feature into a dictionary.r )Valuez'Cannot flatten a decoded Audio feature.binarystring)r r )featuresrE r> r6 )r rE r r r flatten s
z
Audio.flatten)storager! c C s( t jj|jrJt jdgt| t j d}t jj||gddg|j d}nt jj
|jr~|jjdr~t jdd |j D }nt jj
|jr|jj
dd kr|jd}nt jdgt| t j d}|jj
dd kr|jd}nt jdgt| t j d}t jj||gddg|j d}t|| jS )
a Cast an Arrow array to the Audio arrow storage type.
The Arrow types that can be converted to the Audio pyarrow storage type are:
- pa.string() - it must contain the "path" data
- pa.struct({"bytes": pa.binary()})
- pa.struct({"path": pa.string()})
- pa.struct({"bytes": pa.binary(), "path": pa.string()}) - order doesn't matter
Args:
storage (Union[pa.StringArray, pa.StructArray]): PyArrow array to cast.
Returns:
pa.StructArray: Array in the Audio arrow storage type, that is
pa.struct({"bytes": pa.binary(), "path": pa.string()})
N)typer r )maskr" c S s$ g | ]}|d k rt j|nd qS )N)r r: ).0xr r r