o
    i-                  	   @   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	g d
g dddddddZdZdZdefddZdedededee fddZdedefddZdedef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.zip)zIhttps://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_partad)@21ec6ca843659ebc2fdbe04b530baa4f191ad4b0971912672d92c158f32226a0@311d21e0c8cbf33573a4fce6c80e5a279d80736274b381c394319fc557159a04@92b64465f2b2a3dc0e4196ae8dd6828cbe9ddd1f089419a11e4cbfe2e1750df0@00e6190c770b27f27d2a3dd26ee15596b17066b715ac111906861a7d09a211a5)archive_nameurls	checksumszvox1_test_wav.zipzGhttps://thor.robots.ox.ac.uk/~vgg/data/voxceleb/vox1a/vox1_test_wav.zip@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rot | d }t | d }t|d@}t||D ]2\}}tj| tj|}	t||	|d t|	d}
||
	  W d    n1 sYw   Y  q,W d    n1 siw   Y  nt | d	 }t | d
 }t|||d t
| qd S )Nr   r   r   r   r   wb)hash_prefixrbr   r   )_ARCHIVE_CONFIGSospathjoinopenzipbasenamer   writereadr	   )r   archiver   archive_pathr   r   fr   r   	file_pathf_split r*   Q/home/ubuntu/.local/lib/python3.10/site-packages/torchaudio/datasets/voxceleb1.py_download_extract_wavs&   s,   
r,   r(   subsetreturnc           	      C   s   g }|dkr	d}n	|dkrd}nd}t |d!}|D ]}| \}}t||kr-|| qW d    t|S 1 s;w   Y  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"   

r<   veri_test_pathc                 C   s\   g }t |d}|D ]}| \}}}||||f q
W d    |S 1 s'w   Y  |S )Nr3   )r    r4   r6   )r   r=   r8   r'   r:   labelpath1path2r*   r*   r+   _get_paired_flistL   s   
rA   
_ext_audioc                 C   s8   |  ddd  \}}}||d}d|||g}|S )N/ -)r4   replacer   )r(   rB   
speaker_id
youtube_idutterance_idfile_idr*   r*   r+   _get_file_idU   s   rL   c                   @   s^   e Zd ZdZdZddeeef deddfdd	Z	d
e
fddZd
e
fddZde
f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.wavFr   downloadr.   Nc                 C   sN   t |}t j|d| _t j| js%|std| j dt| d S d S )NwavzDataset not found at z5. Please set `download=True` to download the dataset.)r   fspathr   r   _pathisdirRuntimeErrorr,   )selfr   rN   r*   r*   r+   __init__g   s   
zVoxCeleb1.__init__nc                 C      t NNotImplementedErrorrT   rV   r*   r*   r+   get_metadatar      zVoxCeleb1.get_metadatac                 C   rW   rX   rY   r[   r*   r*   r+   __getitem__u   r]   zVoxCeleb1.__getitem__c                 C   rW   rX   rY   rT   r*   r*   r+   __len__x   r]   zVoxCeleb1.__len__)F)__name__
__module____qualname____doc__rB   r   strr   boolrU   r5   r\   r^   r`   r*   r*   r*   r+   rM   \   s     rM   c                       s   e Zd ZdZdedfdeeef dedededd	f
 fd
dZ	de
deee
e
ef fddZde
deee
e
ef fddZde
f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/   Fr   r-   meta_urlrN   r.   Nc                    s^   t  || |dvrtdtj|tj|}tj|s%t|| t	| j
||| _d S )N)r/   r   r   z0`subset` must be one of ['train', 'dev', 'test'])superrU   
ValueErrorr   r   r   r"   existsr   r<   rQ   _flist)rT   r   r-   rh   rN   meta_list_path	__class__r*   r+   rU      s   
z VoxCeleb1Identification.__init__rV   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
        rF   r   r2   N)rl   rL   rB   r4   r5   SAMPLE_RATE)rT   rV   r(   rK   rH   r*   r*   r+   r\      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   r0   Nr\   r
   rQ   )rT   rV   metadatawaveformr*   r*   r+   r^      s   
z#VoxCeleb1Identification.__getitem__c                 C   
   t | jS rX   lenrl   r_   r*   r*   r+   r`         
zVoxCeleb1Identification.__len__)ra   rb   rc   rd   _IDEN_SPLIT_URLr   re   r   rf   rU   r5   r   r\   r   r^   r`   __classcell__r*   r*   rn   r+   rg   |   s"    
rg   c                
       s   e Zd ZdZedfdeeef dededdf fdd	Z	d
e
deeee
e
eef fddZd
e
deeee
e
eef fddZde
f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.
    Fr   rh   rN   r.   Nc                    sL   t  || tj|tj|}tj|st|| t| j	|| _
d S rX   )ri   rU   r   r   r   r"   rk   r   rA   rQ   rl   )rT   r   rh   rN   rm   rn   r*   r+   rU      s
   
zVoxCeleb1Verification.__init__rV   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
        )rl   r5   rL   rB   rp   )rT   rV   r>   file_path_spk1file_path_spk2file_id_spk1file_id_spk2r*   r*   r+   r\      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   r1   r0   Nrq   )rT   rV   rr   waveform_spk1waveform_spk2r*   r*   r+   r^     s   
z!VoxCeleb1Verification.__getitem__c                 C   rt   rX   ru   r_   r*   r*   r+   r`   4  rw   zVoxCeleb1Verification.__len__)ra   rb   rc   rd   _VERI_TEST_URLr   re   r   rf   rU   r5   r   r\   r   r^   r`   ry   r*   r*   rn   r+   rz      s    ,""rz   )r   pathlibr   typingr   r   r   torchr   torch.utils.datar   torchaudio._internalr   torchaudio.datasets.utilsr	   r
   rp   r   rx   r   re   r,   r<   rA   rL   rM   rg   rz   r*   r*   r*   r+   <module>   s4    	 [