
    *#h0                         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 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"y)    N)	dataclassfield)BytesIO)TYPE_CHECKINGAnyClassVarDictOptionalUnion   )config)DownloadConfig)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"y)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    T/var/www/html/sandstorm/venv/lib/python3.12/site-packages/datasets/features/audio.py__call__zAudio.__call__M   s    ||    valuereturnc                 R   	 ddl }t        |t              rd|dS t        |t              r|ddS d|v r8t               }|j                  ||d   |d   d       |j                         ddS |j                  d	      :t        j                  j                  |d	         r|d	   j                  d
      r|j                  d      t        d      |j                  d      rIt        j                  |d   t        j                         j#                  t        j$                        dz  }n;t        j&                  |d	   dd      j#                  t        j$                        dz  }t        t	                     }|j                  |||d   d       |j                         ddS d|j                  d	      dS |j                  d      |j                  d	      #|j                  d      |j                  d	      dS t)        d| d      # t        $ r}t        d      |d}~ww xY w)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_exampleP   s   	f" eS!!511u%"D11YFHHVU7^U?-CEHR#__.==YYv*rww~~eFm/LV}%%e,99_-5"#ghh99W%"$--gbhh"O"V"VWYWaWa"bej"jK"$))E&M3"O"V"VWYWaWa"bej"jK )eO.DUS!'!2DAA!%uyy/@AAYYw+uyy/@/L"YYw/69JKKghmgnnop C  	f[\bee	fs   H 	H&H!!H&token_per_repo_idc                 R   | j                   st        d      |d   |d   t        |d         fn|d   df\  }}||t        d| d      	 ddl}ddl}|t        |      d	   d	d j                         nd}t        j                  s|d
k(  rt        d      t        j                  s|dk(  rt        d      ||xs i }|j                  d      d   }	|	j                  t        j                        rt        j                  nt        j                   }
	 t#        |	|
      d   }||   }t'        |      }t)        |d|      5 }|j+                  |      \  }}ddd       n|j+                  |      \  }}j,                  }| j.                  r|j1                  |      }| j2                  r9| j2                  k7  r*|j5                  ||| j2                        }| j2                  }||dS # t        $ r}t        d      |d}~ww xY w# t        t$        f$ r d}Y w xY w# 1 sw Y   xY w)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 r6   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)tokenrb)download_config)orig_sr	target_sr)r    r/   r   )r   RuntimeErrorr   rH   librosar7   r8   r   lowerr   IS_OPUS_SUPPORTEDIS_MP3_SUPPORTEDsplit
startswithHF_ENDPOINTHUB_DATASETS_URLHUB_DATASETS_HFFS_URLr   rA   r   r   readTr   to_monor   resample)r(   r,   rN   r    filerZ   rI   rJ   audio_format
source_urlpatternrS   rT   rV   fr/   r   s                    r)   decode_examplezAudio.decode_example   sV   & {{nooAFwA[eFmWU7^%<=bghnboquav
d<DLijoippqrss	u" :>9Iyq)!"-335t''LF,Bh  ((\U-Bh 
 < 1 7RD)"-J+5+@+@ASAS+T''Z`ZvZv (W=iH)'2 -59OtT?C 2q')wwqz$}2 2 $&774= E=99OOE*E$"4"4"E$$UMTM_M_$`E ..Mu}MMS  	ujkqtt	u0 ) 2 2s6   G) H H)	H2G>>HHHH&r   c                 ^    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   )featuresrn   r   rH   )r(   rn   s     r)   flattenzAudio.flatten   s0    #;;FGG8_(O
 	
r+   storagec                    t         j                  j                  |j                        rlt        j                  dgt        |      z  t        j                               }t         j                  j                  ||gddg|j                               }nQt         j                  j                  |j                        rlt        j                  dgt        |      z  t        j                               }t         j                  j                  ||gddg|j                               }nt         j                  j                  |j                        ri|j                  j                  d      rNt        j                  |j                         D cg c]  }|t               j!                  |      nd! c}      }n*t         j                  j                  |j                        r |j                  j#                  d      dk\  r|j%                  d      }n6t        j                  dgt        |      z  t        j                               }|j                  j#                  d      dk\  r|j%                  d      }n6t        j                  dgt        |      z  t        j                               }t         j                  j                  ||gddg|j                               }t'        || j(                        S c c}w )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/   r   )patypes	is_stringrv   r/   lenro   StructArrayfrom_arraysis_null	is_binaryrp   	is_structget_all_field_indices	to_pylistr   rM   get_field_indexr   r   r!   )r(   rs   bytes_array
path_arrayxs        r)   cast_storagezAudio.cast_storage   s   $ 88gll+((D6CL#8ryy{KKnn00+w1G'SYIZahapapar0sGXX-4&3w<"7biikJJnn00':1FRXHY`g`o`o`q0rGXX-',,2T2TU\2]hh^e^o^o^qrYZQ] 6 6q 9PT TrsGXX-||++G49%mmG4 hhvG'<299;O||++F3q8$]]62
XXtfs7|&;"))+N
nn00+z1JWV\L]dkdsdsdu0vG'4<<00  ss   $K=c           	      d   t         d        }t        j                  |j                         D cg c]  }||d    ||d         n|d   nd c}t        j                               }t        j                  |j                  d      j                         D cg c]%  }|t        j                  j                  |      nd' c}t        j                               }t        j                  j                  ||gddg|j                               }t        || j                        S c c}w c c}w )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                 j    t        | d      5 }|j                         }d d d        |S # 1 sw Y   S xY w)NrU   )r   rc   )r    rk   bytes_s      r)   path_to_bytesz*Audio.embed_storage.<locals>.path_to_bytes  s3    tT" "a"M"Ms   (2Nr   r    ru   rw   )r   ry   r/   r   ro   r   r>   r    basenamerp   r}   r~   r   r   r!   )r(   rs   r   r   r   r    r   s          r)   embed_storagezAudio.embed_storage   s    
 	 
 	
 hh !**, UVTaQwZ-?qy)QwZgkk 
 XXNUmm\bNcNmNmNopdt'7RWWd#TAp

 ..,,k:-FRXHY`k`s`s`u,v'4<<00 qs    D(*D-r'   )#__name__
__module____qualname____doc__r   r
   int__annotations__r   boolr   r   r:   r   r   ry   structro   rp   r!   r   r   r%   r*   r   r   r   rM   r	   rl   rr   StringArrayr}   r   r    r+   r)   r   r      s[   )V $(M8C='D$FDB!E8C=!&RYYibiik'RSGXc]SwU?E3?/E#ud*:$; / /d ]aFNFN.6tCsDRVAW<W7X.YFN	FNP	
}d33E.FFG 	
$1E".."..*H$I $1bnn $1L1R^^ 1 1r+   r   )#r>   dataclassesr   r   ior   typingr   r   r   r	   r
   r   numpyrB   pyarrowry    r   download.download_configr   #download.streaming_download_managerr   r   tabler   utils.py_utilsr   r   rq   r   _ffmpeg_warned_librosa_warned_audioread_warnedr   r   r+   r)   <module>r      s]    	 (  F F    5 B  C % 6I 2!2 @1 @1 @1r+   