U
    ,:%e9                     @   s   d dl Z d dlmZ d dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ dZd	Zd
ZdZdZdddZdd Zeeeeeeeef dddZG dd de
ZdS )    N)Path)OptionalTupleUnion)Tensor)Dataset)download_url_to_file)_extract_tar_load_waveformZSpeechCommandsspeech_commands_v0.02Z_nohash_Z_background_noise_i>  Z@743935421bb51cccdb6bdd152e04c5c70274e935c82119ad7faeec31780d811dZ@af14739ee7dc311471de98f5f9d2c9191b18aedfe957f4a6ff791c709868ff58)z@http://download.tensorflow.org/data/speech_commands_v0.01.tar.gzz@http://download.tensorflow.org/data/speech_commands_v0.02.tar.gzc              
      sJ   g }|D ]<}t j |}t|}| fdd|D 7 }W 5 Q R X q|S )Nc              	      s&   g | ]}t jt j | qS  )ospathnormpathjoinstrip).0linerootr   a/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torchaudio/datasets/speechcommands.py
<listcomp>   s     z_load_list.<locals>.<listcomp>)r   r   r   open)r   	filenamesoutputfilenamefilepathfileobjr   r   r   
_load_list   s    
"r   )r   r   returnc           
      C   sr   t j| |}t j|\}}t j|\}}t j|\}}t j|\}}|t\}}	t|	}	|t|||	fS N)r   r   relpathsplitsplitextHASH_DIVIDERintSAMPLE_RATE)
r   r   r!   Zreldirr   _labelspeakerZ
speaker_idZutterance_numberr   r   r   _get_speechcommands_metadata   s    r*   c                   @   s   e Zd ZdZeeddfeeef eee	e
e ddddZeeeeeeef ddd	Zeeeeeeef dd
dZedddZdS )SPEECHCOMMANDSa,  *Speech Commands* :cite:`speechcommandsv2` dataset.

    Args:
        root (str or Path): Path to the directory where the dataset is found or downloaded.
        url (str, optional): The URL to download the dataset from,
            or the type of the dataset to dowload.
            Allowed type values are ``"speech_commands_v0.01"`` and ``"speech_commands_v0.02"``
            (default: ``"speech_commands_v0.02"``)
        folder_in_archive (str, optional):
            The top-level directory of the dataset. (default: ``"SpeechCommands"``)
        download (bool, optional):
            Whether to download the dataset if it is not found at root path. (default: ``False``).
        subset (str or None, optional):
            Select a subset of the dataset [None, "training", "validation", "testing"]. None means
            the whole dataset. "validation" and "testing" are defined in "validation_list.txt" and
            "testing_list.txt", respectively, and "training" is the rest. Details for the files
            "validation_list.txt" and "testing_list.txt" are explained in the README of the dataset
            and in the introduction of Section 7 of the original paper and its reference 12. The
            original paper can be found `here <https://arxiv.org/pdf/1804.03209.pdf>`_. (Default: ``None``)
    FN)r   urlfolder_in_archivedownloadsubsetr   c                    s  |d k	r|dkrt d|dkr:d}d}tj||| }t|}tj||| _tj|}tj||}	|ddd }tj||}tj||| _|rtj	| jstj
|	st|d }
t||	|
d	 t|	| j n"tj| jstd
| j d|dkrt| jd| _n|dkr8t| jd| _n~|dkrtt| jdd tdd t| jdD } fdd|D | _n.tdd t| jdD }dd |D | _d S )N)training
validationtestingzSWhen `subset` is not None, it must be one of ['training', 'validation', 'testing'].)zspeech_commands_v0.01r   z$http://download.tensorflow.org/data/z.tar.gz.   r   )Zhash_prefixz	The path zT doesn't exist. Please check the ``root`` path or set `download=True` to download itr1   zvalidation_list.txtr2   ztesting_list.txtr0   c                 s   s   | ]}t |V  qd S r    strr   pr   r   r   	<genexpr>|   s     z*SPEECHCOMMANDS.__init__.<locals>.<genexpr>z*/*.wavc                    s0   g | ](}t |krt|krtj| kr|qS r   )r$   EXCEPT_FOLDERr   r   r   r   wexcludesr   r   r   }   s
     z+SPEECHCOMMANDS.__init__.<locals>.<listcomp>c                 s   s   | ]}t |V  qd S r    r5   r7   r   r   r   r9      s     c                 S   s    g | ]}t |krt|kr|qS r   )r$   r:   r;   r   r   r   r      s       )
ValueErrorr   r   r   fspath_archivebasenamersplit_pathisdirisfile
_CHECKSUMSgetr   r	   existsRuntimeErrorr   _walkersetsortedr   glob)selfr   r,   r-   r.   r/   base_urlZext_archiverB   archiveZchecksumZwalkerr   r=   r   __init__H   sF    	





zSPEECHCOMMANDS.__init__)nr   c                 C   s   | j | }t|| jS )a  Get metadata for the n-th sample from the dataset. Returns filepath instead of waveform,
        but otherwise returns the same fields as :py:func:`__getitem__`.

        Args:
            n (int): The index of the sample to be loaded

        Returns:
            Tuple of the following items;

            str:
                Path to the audio
            int:
                Sample rate
            str:
                Label
            str:
                Speaker ID
            int:
                Utterance number
        )rK   r*   rA   )rO   rS   Zfileidr   r   r   get_metadata   s    
zSPEECHCOMMANDS.get_metadatac                 C   s2   |  |}t| j|d |d }|f|dd  S )a  Load the n-th sample from the dataset.

        Args:
            n (int): The index of the sample to be loaded

        Returns:
            Tuple of the following items;

            Tensor:
                Waveform
            int:
                Sample rate
            str:
                Label
            str:
                Speaker ID
            int:
                Utterance number
        r      N)rT   r
   rA   )rO   rS   metadataZwaveformr   r   r   __getitem__   s    
zSPEECHCOMMANDS.__getitem__)r   c                 C   s
   t | jS r    )lenrK   )rO   r   r   r   __len__   s    zSPEECHCOMMANDS.__len__)__name__
__module____qualname____doc__URLFOLDER_IN_ARCHIVEr   r6   r   boolr   rR   r%   r   rT   r   rW   rY   r   r   r   r   r+   2   s    
>r+   )r   pathlibr   typingr   r   r   Ztorchr   Ztorch.utils.datar   Ztorchaudio._internalr   Ztorchaudio.datasets.utilsr	   r
   r_   r^   r$   r:   r&   rG   r   r6   r%   r*   r+   r   r   r   r   <module>   s"   	 