U
    CŒÑhÍ-  ã                   @   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	d
dddgddddgdœddddœdœZdZdZedœdd„Zeeeee dœdd„Zeed œd!d"„Zeed#œd$d%„ZG d&d'„ d'e
ƒZG d(d)„ d)eƒZG d*d+„ d+eƒZdS ),é    N)ÚPath)ÚListÚTupleÚUnion)ÚTensor)ÚDataset)Údownload_url_to_file)Ú_extract_zipÚ_load_waveformi€>  zvox1_dev_wav.zipzIhttps://thor.robots.ox.ac.uk/~vgg/data/voxceleb/vox1a/vox1_dev_wav_partaazIhttps://thor.robots.ox.ac.uk/~vgg/data/voxceleb/vox1a/vox1_dev_wav_partabzIhttps://thor.robots.ox.ac.uk/~vgg/data/voxceleb/vox1a/vox1_dev_wav_partaczIhttps://thor.robots.ox.ac.uk/~vgg/data/voxceleb/vox1a/vox1_dev_wav_partadZ@21ec6ca843659ebc2fdbe04b530baa4f191ad4b0971912672d92c158f32226a0Z@311d21e0c8cbf33573a4fce6c80e5a279d80736274b381c394319fc557159a04Z@92b64465f2b2a3dc0e4196ae8dd6828cbe9ddd1f089419a11e4cbfe2e1750df0Z@00e6190c770b27f27d2a3dd26ee15596b17066b715ac111906861a7d09a211a5)Úarchive_nameÚurlsÚ	checksumszvox1_test_wav.zipzGhttps://thor.robots.ox.ac.uk/~vgg/data/voxceleb/vox1a/vox1_test_wav.zipZ@8de57f347fe22b2c24526e9f444f689ecf5096fc2a92018cf420ff6b5b15eaea)r   ÚurlÚchecksum©ÚdevÚtestzBhttps://www.robots.ox.ac.uk/~vgg/data/voxceleb/meta/iden_split.txtzAhttps://www.robots.ox.ac.uk/~vgg/data/voxceleb/meta/veri_test.txt)Úrootc                 C   sê   dD ]à}t | d }tj | |¡}|dkr¶t | d }t | d }t|dƒ�b}t||ƒD ]P\}}tj | tj |¡¡}	t||	|d� t|	dƒ�}
| |
 	¡ ¡ W 5 Q R X qXW 5 Q R X n&t | d	 }t | d
 }t|||d� t
|ƒ qd S )Nr   r   r   r   r   Úwb)Zhash_prefixÚrbr   r   )Ú_ARCHIVE_CONFIGSÚosÚpathÚjoinÚopenÚzipÚbasenamer   ÚwriteÚreadr	   )r   Úarchiver   Úarchive_pathr   r   Úfr   r   Ú	file_pathZf_split© r#   úK/tmp/user/2202/pip-unpacked-wheel-p89lpdx5/torchaudio/datasets/voxceleb1.pyÚ_download_extract_wavs&   s     &r%   )r   r"   ÚsubsetÚreturnc           	   	   C   sn   g }|dkrd}n|dkr d}nd}t |dƒ�2}|D ]&}| ¡ \}}t|ƒ|kr4| |¡ q4W 5 Q R X t|ƒS )NÚtrainé   r   é   é   Úr)r   ÚsplitÚintÚappendÚsorted)	r   r"   r&   Úf_listÚindexr!   ÚlineÚidr   r#   r#   r$   Ú
_get_flist<   s    r5   )r   Úveri_test_pathc              	   C   sF   g }t |dƒ�.}|D ]"}| ¡ \}}}| |||f¡ qW 5 Q R X |S )Nr,   )r   r-   r/   )r   r6   r1   r!   r3   ÚlabelÚpath1Úpath2r#   r#   r$   Ú_get_paired_flistL   s    r:   )r"   Ú
_ext_audioc                 C   s8   |   d¡dd … \}}}| |d¡}d |||g¡}|S )Nú/éýÿÿÿÚ ú-)r-   Úreplacer   )r"   r;   Ú
speaker_idZ
youtube_idZutterance_idÚfile_idr#   r#   r$   Ú_get_file_idU   s    rC   c                   @   sZ   e Zd ZdZdZdeeef eddœdd„Z	e
dœd	d
„Ze
dœdd„Ze
dœdd„ZdS )Ú	VoxCeleb1a"  *VoxCeleb1* :cite:`nagrani2017voxceleb` dataset.

    Args:
        root (str or Path): Path to the directory where the dataset is found or downloaded.
        download (bool, optional):
            Whether to download the dataset if it is not found at root path. (Default: ``False``).
    z.wavFN)r   Údownloadr'   c                 C   sJ   t  |¡}t j |d¡| _t j | j¡sF|s>td| j› d�ƒ‚t|ƒ d S )NZwavzDataset not found at z5. Please set `download=True` to download the dataset.)r   Úfspathr   r   Ú_pathÚisdirÚRuntimeErrorr%   )Úselfr   rE   r#   r#   r$   Ú__init__g   s    
ÿzVoxCeleb1.__init__)Únc                 C   s   t ‚d S ©N©ÚNotImplementedError©rJ   rL   r#   r#   r$   Úget_metadatar   s    zVoxCeleb1.get_metadatac                 C   s   t ‚d S rM   rN   rP   r#   r#   r$   Ú__getitem__u   s    zVoxCeleb1.__getitem__©r'   c                 C   s   t ‚d S rM   rN   ©rJ   r#   r#   r$   Ú__len__x   s    zVoxCeleb1.__len__)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r;   r   Ústrr   ÚboolrK   r.   rQ   rR   rU   r#   r#   r#   r$   rD   \   s   rD   c                       s„   e Zd ZdZdedfeeef eeeddœ‡ fdd„Z	e
eee
e
ef dœd	d
„Ze
eee
e
ef dœdd„Ze
dœdd„Z‡  ZS )ÚVoxCeleb1Identificationuý  *VoxCeleb1* :cite:`nagrani2017voxceleb` dataset for speaker identification task.

    Each data sample contains the waveform, sample rate, speaker id, and the file id.

    Args:
        root (str or Path): Path to the directory where the dataset is found or downloaded.
        subset (str, optional): Subset of the dataset to use. Options: ["train", "dev", "test"]. (Default: ``"train"``)
        meta_url (str, optional): The url of meta file that contains the list of subset labels and file paths.
            The format of each row is ``subset file_path". For example: ``1 id10006/nLEBBc9oIFs/00003.wav``.
            ``1``, ``2``, ``3`` mean ``train``, ``dev``, and ``test`` subest, respectively.
            (Default: ``"https://www.robots.ox.ac.uk/~vgg/data/voxceleb/meta/iden_split.txt"``)
        download (bool, optional):
            Whether to download the dataset if it is not found at root path. (Default: ``False``).

    Note:
        The file structure of `VoxCeleb1Identification` dataset is as follows:

        â””â”€ root/

         â””â”€ wav/

         â””â”€ speaker_id folders

        Users who pre-downloaded the ``"vox1_dev_wav.zip"`` and ``"vox1_test_wav.zip"`` files need to move
        the extracted files into the same ``root`` directory.
    r(   FN)r   r&   Úmeta_urlrE   r'   c                    s^   t ƒ  ||¡ |dkrtdƒ‚tj |tj |¡¡}tj |¡sJt||ƒ t	| j
||ƒ| _d S )N)r(   r   r   z0`subset` must be one of ['train', 'dev', 'test'])ÚsuperrK   Ú
ValueErrorr   r   r   r   Úexistsr   r5   rG   Ú_flist)rJ   r   r&   r]   rE   Úmeta_list_path©Ú	__class__r#   r$   rK   ˜   s    
z VoxCeleb1Identification.__init__©rL   r'   c                 C   s@   | j | }t|| jƒ}| d¡d }t|dd… ƒ}|t||f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

        Returns:
            Tuple of the following items;

            str:
                Path to audio
            int:
                Sample rate
            int:
                Speaker ID
            str:
                File ID
        r?   r   r+   N)ra   rC   r;   r-   r.   ÚSAMPLE_RATE)rJ   rL   r"   rB   rA   r#   r#   r$   rQ   ¤   s
    
z$VoxCeleb1Identification.get_metadatac                 C   s2   |   |¡}t| j|d |d ƒ}|f|dd…  S )ad  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
            int:
                Speaker ID
            str:
                File ID
        r   r)   N©rQ   r
   rG   )rJ   rL   ÚmetadataZwaveformr#   r#   r$   rR   ½   s    
z#VoxCeleb1Identification.__getitem__rS   c                 C   s
   t | jƒS rM   ©Úlenra   rT   r#   r#   r$   rU   Ó   s    zVoxCeleb1Identification.__len__)rV   rW   rX   rY   Ú_IDEN_SPLIT_URLr   rZ   r   r[   rK   r.   r   rQ   r   rR   rU   Ú__classcell__r#   r#   rc   r$   r\   |   s     ÿ
   þr\   c                       sˆ   e Zd ZdZedfeeef eeddœ‡ fdd„Z	e
eeee
e
eef dœdd	„Ze
eeee
e
eef dœd
d„Ze
dœdd„Z‡  ZS )ÚVoxCeleb1Verificationuö  *VoxCeleb1* :cite:`nagrani2017voxceleb` dataset for speaker verification task.

    Each data sample contains a pair of waveforms, sample rate, the label indicating if they are
    from the same speaker, and the file ids.

    Args:
        root (str or Path): Path to the directory where the dataset is found or downloaded.
        meta_url (str, optional): The url of meta file that contains a list of utterance pairs
            and the corresponding labels. The format of each row is ``label file_path1 file_path2".
            For example: ``1 id10270/x6uYqmx31kE/00001.wav id10270/8jEAjG6SegY/00008.wav``.
            ``1`` means the two utterances are from the same speaker, ``0`` means not.
            (Default: ``"https://www.robots.ox.ac.uk/~vgg/data/voxceleb/meta/veri_test.txt"``)
        download (bool, optional):
            Whether to download the dataset if it is not found at root path. (Default: ``False``).

    Note:
        The file structure of `VoxCeleb1Verification` dataset is as follows:

        â””â”€ root/

         â””â”€ wav/

         â””â”€ speaker_id folders

        Users who pre-downloaded the ``"vox1_dev_wav.zip"`` and ``"vox1_test_wav.zip"`` files need to move
        the extracted files into the same ``root`` directory.
    FN)r   r]   rE   r'   c                    sL   t ƒ  ||¡ tj |tj |¡¡}tj |¡s:t||ƒ t| j	|ƒ| _
d S rM   )r^   rK   r   r   r   r   r`   r   r:   rG   ra   )rJ   r   r]   rE   rb   rc   r#   r$   rK   ô   s
    
zVoxCeleb1Verification.__init__re   c                 C   s@   | j | \}}}t|ƒ}t|| jƒ}t|| jƒ}||t|||fS )ag  Get metadata for the n-th sample from the dataset. Returns filepaths instead of waveforms,
        but otherwise returns the same fields as :py:func:`__getitem__`.

        Args:
            n (int): The index of the sample

        Returns:
            Tuple of the following items;

            str:
                Path to audio file of speaker 1
            str:
                Path to audio file of speaker 2
            int:
                Sample rate
            int:
                Label
            str:
                File ID of speaker 1
            str:
                File ID of speaker 2
        )ra   r.   rC   r;   rf   )rJ   rL   r7   Zfile_path_spk1Zfile_path_spk2Zfile_id_spk1Zfile_id_spk2r#   r#   r$   rQ   ü   s
    z"VoxCeleb1Verification.get_metadatac                 C   sJ   |   |¡}t| j|d |d ƒ}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 of speaker 1
            Tensor:
                Waveform of speaker 2
            int:
                Sample rate
            int:
                Label
            str:
                File ID of speaker 1
            str:
                File ID of speaker 2
        r   r*   r)   Nrg   )rJ   rL   rh   Zwaveform_spk1Zwaveform_spk2r#   r#   r$   rR     s    
z!VoxCeleb1Verification.__getitem__rS   c                 C   s
   t | jƒS rM   ri   rT   r#   r#   r$   rU   4  s    zVoxCeleb1Verification.__len__)rV   rW   rX   rY   Ú_VERI_TEST_URLr   rZ   r   r[   rK   r.   r   rQ   r   rR   rU   rl   r#   r#   rc   r$   rm   ×   s
   &  rm   )r   Úpathlibr   Útypingr   r   r   Ztorchr   Ztorch.utils.datar   Ztorchaudio._internalr   Ztorchaudio.datasets.utilsr	   r
   rf   r   rk   rn   rZ   r%   r5   r:   rC   rD   r\   rm   r#   r#   r#   r$   Ú<module>   sB   üüøýð	 [