
    +gd2/                         d dl Z d dlmZmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlZd dlZddlmZ ddlmZmZ ddlmZ dd	lmZmZ erd
dlmZ d\  ZZZe G d d                      Z dS )    N)	dataclassfield)BytesIO)TYPE_CHECKINGAnyClassVarDictOptionalUnion   )config)xopen	xsplitext)
array_cast)no_op_if_value_is_nullstring_to_dict   )FeatureType)FFFc                      e Zd ZU dZdZee         ed<   dZe	ed<   dZ
e	ed<   dZee         ed<   dZee         ed	<    ej         ej                     ej                    d
          Zee         ed<    ed dd          Zeed<   d Zdeeeef         defdZ	 ddedeeeeee	df         f                  defdZdedeedf         f         fdZdeej        ej         f         dej         fdZ!dej         dej         fdZ"dS )Audioa1  Audio [`Feature`] to extract audio data from an audio file.

    Input: The Audio feature accepts as input:
    - A `str`: Absolute path to the audio file (i.e. random access is allowed).
    - A `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 `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 sampling rate of the audio sample.

      This is useful for archived files with sequential access.

    Args:
        sampling_rate (`int`, *optional*):
            Target sampling rate. If `None`, the native sampling rate is used.
        mono (`bool`, defaults to `True`):
            Whether to convert the audio signal to mono by averaging samples across
            channels.
        decode (`bool`, defaults to `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}
    ```
    Nsampling_rateTmonodecodeiddictdtypebytespathpa_typeF)defaultinitrepr_typec                     | j         S N)r    )selfs    7lib/python3.11/site-packages/datasets/features/audio.py__call__zAudio.__call__L   s
    |    valuereturnc                 "   	 ddl }n"# t          $ r}t          d          |d}~ww xY wt          |t                    rd|dS t          |t                    r|ddS d|v rJt                      }|                    ||d         |d         d           |                                ddS |                    d	          ht          j
                            |d	                   rB|d	                             d
          r|                    d          t          d          |                    d          rGt          j        |d         t          j                                      t          j                  dz  }n=t          j        |d	         dd                              t          j                  dz  }t          t	                                }|                    |||d         d           |                                ddS d|                    d	          dS |                    d          |                    d	          +|                    d          |                    d	          dS t)          d| d          )zEncode example into a format for Arrow.

        Args:
            value (`str` or `dict`):
                Data passed as input to Audio feature.

        Returns:
            `dict`
        r   Nz;To support encoding audio data, please install 'soundfile'.r   arrayr   wav)formatr   pcmzBTo use PCM files, please specify a 'sampling_rate' in Audio objectr   )r   i  hr)r   modezUAn audio sample should have one of 'path' or 'bytes' but they are missing or None in .)	soundfileImportError
isinstancestrr   r   writegetvaluegetosr   isfileendswithKeyErrornp
frombufferint16astypefloat32memmap
ValueError)r'   r+   sferrbufferbytes_values         r(   encode_examplezAudio.encode_exampleO   s   	f""""" 	f 	f 	f[\\bee	feS!! !	!5111u%% 	"D111YYFHHVU7^U?-CEHRRR#__..===YYv*rw~~eFm/L/L*V}%%e,, B99_--5"#ghhh99W%% k"$-gbh"O"O"O"V"VWYWa"b"bej"jKK"$)E&M3"O"O"O"V"VWYWa"b"bej"jK ))eO.DUSSS!'!2!2DAAA!%uyy/@/@AAAYYw+uyy/@/@/L"YYw//69J9JKKKphmppp  s    
&!&token_per_repo_idc                 >   | j         st          d          |d         |d         t          |d                   fn	|d         df\  }}||t          d| d          	 ddl}ddl}n"# t          $ r}t          d          |d}~ww xY w|/t          |          d	         d	d                                         nd}t          j
        s|d
k    rt          d          t          j        s|dk    rt          d          ||pi }|                    d          d         }		 t          |	t          j                  d         }
||
         }n# t          t          f$ r d}Y nw xY wt!          |d|          5 }|                    |          \  }}ddd           n# 1 swxY w Y   n|                    |          \  }}|j        }| j        r|                    |          }| j        r/| j        |k    r$|                    ||| j                  }| j        }|||dS )a  Decode example audio file into audio data.

        Args:
            value (`dict`):
                A dictionary with keys:

                - `path`: String with relative audio file path.
                - `bytes`: Bytes of the audio file.
            token_per_repo_id (`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 r5   r   zJTo support decoding audio files, please install 'librosa' and 'soundfile'.r   opuszDecoding 'opus' files requires system library 'libsndfile'>=1.0.31, You can try to update `soundfile` python library: `pip install "soundfile>=0.12.1"`. mp3zDecoding 'mp3' files requires system library 'libsndfile'>=1.1.0, You can try to update `soundfile` python library: `pip install "soundfile>=0.12.1"`. z::repo_idrb)use_auth_token)orig_sr	target_sr)r   r.   r   )r   RuntimeErrorr   rG   librosar6   r7   r   lowerr   IS_OPUS_SUPPORTEDIS_MP3_SUPPORTEDsplitr   HUB_DATASETS_URLr@   r   readTr   to_monor   resample)r'   r+   rM   r   filerX   rH   rI   audio_format
source_urlrR   rT   fr.   r   s                  r(   decode_examplezAudio.decode_example   s   & { 	pnoooAFwA[eFmWU7^%<%<==bghnboquav
d<DLrjorrrsss	uNNN""""" 	u 	u 	ujkkqtt	u :>9Iyq)!""-33555t' 		LF,B,Bh   ( 	\U-B-Bh  
 < 1 7RD))"-J&(V5LMMiX!27!;) & & &!%& tT.AAA 2Q')wwqzz$}2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 $&774== E=9 	+OOE**E 	/$"4"E"E$$UMTM_$``E .Mu}MMMs<   !A* *
B	4BB	#(E E"!E"7FF #F r   c                 h    ddl m} | j        rt          d           |d           |d          dS )z[If in the decodable state, raise an error, otherwise flatten the feature into a dictionary.r   )Valuez'Cannot flatten a decoded Audio feature.binarystringr   )featuresrh   r   rG   )r'   rh   s     r(   flattenzAudio.flatten   sS    ######; 	HFGGGU8__E(OO
 
 	
r*   storagec                    t           j                            |j                  rrt          j        dgt          |          z  t          j                              }t           j                            ||gddg|	                                          }n;t           j        
                    |j                  rrt          j        dgt          |          z  t          j                              }t           j                            ||gddg|	                                          }nt           j                            |j                  rL|j                            d          r2t          j        d |                                D                       }n5t           j                            |j                  r|j                            d          dk    r|                    d          }n8t          j        dgt          |          z  t          j                              }|j                            d          dk    r|                    d          }n8t          j        dgt          |          z  t          j                              }t           j                            ||gddg|	                                          }t#          || j                  S )	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.binary()` - it must contain the audio bytes
        - `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                 X    g | ]'}|!t                                          |          nd (S r&   )r   rL   ).0xs     r(   
<listcomp>z&Audio.cast_storage.<locals>.<listcomp>   s5    rrrYZQ] 6 6q 9 9 9PTrrrr*   r   )patypes	is_stringrp   r.   lenri   StructArrayfrom_arraysis_null	is_binaryrj   	is_structget_all_field_indices	to_pylistget_field_indexr   r   r    )r'   rm   bytes_array
path_arrays       r(   cast_storagezAudio.cast_storage   s`   $ 8gl++ 	w(D6CLL#8ry{{KKKKn00+w1G'SYIZahapaparar0ssGGX-- 	w4&3w<<"7bikkJJJJn00':1FRXHY`g`o`o`q`q0rrGGX-- 	w',2T2TU\2]2] 	whrr^e^o^o^q^qrrrssGGX-- 		w|++G4499%mmG44 hvG'<29;;OOO|++F33q88$]]622

Xtfs7||&;")++NNN
n00+z1JWV\L]dkdsdsdudu0vvG'4<000r*   c                    t           d             t          j        fd|                                D             t          j                              }t          j        d |                    d                                          D             t          j                              }t          j                            ||gddg|	                                          }t          || j                  S )a8  Embed audio files into the Arrow array.

        Args:
            storage (`pa.StructArray`):
                PyArrow array to embed.

        Returns:
            `pa.StructArray`: Array in the Audio arrow storage type, that is
                `pa.struct({"bytes": pa.binary(), "path": pa.string()})`.
        c                 ~    t          | d          5 }|                                }d d d            n# 1 swxY w Y   |S )NrS   )r   r^   )r   re   bytes_s      r(   path_to_bytesz*Audio.embed_storage.<locals>.path_to_bytes  sv    tT"" "a" " " " " " " " " " " " " " "Ms   266c                 Z    g | ]'}|!|d          |d                   n	|d         nd (S )Nr   r    )rt   ru   r   s     r(   rv   z'Audio.embed_storage.<locals>.<listcomp>  sU        UVTaQwZ-?qy)))QwZZgk  r*   ro   c                 T    g | ]%}|t           j                            |          nd &S r&   )r=   r   basename)rt   r   s     r(   rv   z'Audio.embed_storage.<locals>.<listcomp>  s2    pppdt'7RWd###Tpppr*   r   r   rq   )r   rw   r.   r   ri   r   rj   r{   r|   r}   r   r    )r'   rm   r   r   r   s       @r(   embed_storagezAudio.embed_storage   s    
 	 	 
 		
 h    **,,   
 
 
 Xppgmm\bNcNcNmNmNoNoppp
 
 

 .,,k:-FRXHY`k`s`s`u`u,vv'4<000r*   r&   )#__name__
__module____qualname____doc__r   r
   int__annotations__r   boolr   r   r9   r   r   rw   structri   rj   r    r   r   r$   r)   r   r   r   rL   r	   rf   rl   StringArrayr{   r   r   r   r*   r(   r   r      s        ) )V $(M8C='''D$FDB!E8C=!!!&RYibikk'R'RSSGXc]SSSwU???E3???  /E#ud*:$; / / / / /d ]aBN BNBN.6tCsDRVAW<W7X.YBN	BN BN BN BNH	
}d33E.FFG 	
 	
 	
 	
$1E".".*H$I $1bn $1 $1 $1 $1L1R^ 1 1 1 1 1 1 1r*   r   )!r=   dataclassesr   r   ior   typingr   r   r   r	   r
   r   numpyrA   pyarrowrw    r   #download.streaming_download_managerr   r   tabler   utils.py_utilsr   r   rk   r   _ffmpeg_warned_librosa_warned_audioread_warnedr   r   r*   r(   <module>r      sH   				 ( ( ( ( ( ( ( (       F F F F F F F F F F F F F F F F               B B B B B B B B       C C C C C C C C  &%%%%%% 6I 2!2 |1 |1 |1 |1 |1 |1 |1 |1 |1 |1r*   