o
    ôj9  ã                   @   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dededeeeeeef f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              	      s^   g }|D ](}t j ˆ |¡}t|ƒ�}|‡ fdd„|D ƒ7 }W d   ƒ n1 s'w   Y  q|S )Nc              	      s&   g | ]}t j t j ˆ | ¡ ¡¡‘qS © )ÚosÚpathÚnormpathÚjoinÚstrip)Ú.0Úline©Úrootr   úu/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/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ÚspeakerÚ
speaker_idZutterance_numberr   r   r   Ú_get_speechcommands_metadata   s   r+   c                   @   s–   e Zd ZdZeeddfdeeef dedede	de
e d	dfd
d„Zded	eeeeeef fdd„Zded	eeeeeef fdd„Zd	ef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``)
    FNr   ÚurlÚfolder_in_archiveÚdownloadÚsubsetr   c                    s¸  |d ur|dvrt dƒ‚|dv rd}d}tj ||| ¡}t |¡}tj ||¡| _tj |¡}tj ||¡}	| dd¡d }tj ||¡}tj ||¡| _|rqtj 	| j¡sptj 
|	¡sjt |d ¡}
t||	|
d	� t|	| jƒ ntj | j¡s�td
| j› d�ƒ‚|dkrŽt| jdƒ| _d S |dkr›t| jdƒ| _d S |dkrÃtt| jddƒƒ‰ tdd„ t| jƒ d¡D ƒƒ}‡ fdd„|D ƒ| _d S 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   )Úhash_prefixz	The path zT doesn't exist. Please check the ``root`` path or set `download=True` to download itr2   zvalidation_list.txtr3   ztesting_list.txtr1   c                 s   ó   � | ]}t |ƒV  qd S r    ©Ústr©r   Úpr   r   r   Ú	<genexpr>|   ó   € z*SPEECHCOMMANDS.__init__.<locals>.<genexpr>z*/*.wavc                    s0   g | ]}t |v rt|vrtj |¡ˆ vr|‘qS r   )r$   ÚEXCEPT_FOLDERr   r   r   ©r   Úw©Zexcludesr   r   r   }   s
     þz+SPEECHCOMMANDS.__init__.<locals>.<listcomp>c                 s   r7   r    r8   r:   r   r   r   r<   ƒ   r=   c                 S   s    g | ]}t |v rt|v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/   r0   Úbase_urlÚext_archiverE   ÚarchiveÚchecksumÚwalkerr   rA   r   Ú__init__H   sH   	
€ÿ
þzSPEECHCOMMANDS.__init__Únc                 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
        )rN   r+   rD   )rR   rY   Ú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)r[   r
   rD   )rR   rY   ÚmetadataÚwaveformr   r   r   Ú__getitem__ž   s   
zSPEECHCOMMANDS.__getitem__c                 C   s
   t | jƒS r    )ÚlenrN   )rR   r   r   r   Ú__len__¶   s   
zSPEECHCOMMANDS.__len__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚURLÚFOLDER_IN_ARCHIVEr   r9   r   Úboolr   rX   r%   r   r[   r   r_   ra   r   r   r   r   r,   2   s,    ú
þýüûú
ù > r,   )r   Úpathlibr   Útypingr   r   r   Útorchr   Útorch.utils.datar   Ztorchaudio._internalr   Útorchaudio.datasets.utilsr	   r
   rg   rf   r$   r>   r&   rJ   r   r9   r%   r+   r,   r   r   r   r   Ú<module>   s$    þ$	