a
    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	g d
g d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.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)Z@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rt | d }t | d }t|d}t||D ]d\}}tj| tj|}	t||	|d t|	d}
||
	  W d    qZ1 s0    Y  qZW d    n1 s0    Y  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/var/www/auris/lib/python3.9/site-packages/torchaudio/datasets/voxceleb1.py_download_extract_wavs&   s     
Nr%   )r   r"   subsetreturnc           	      C   s   g }|dkrd}n|dkr d}nd}t |d<}|D ]&}| \}}t||kr4|| q4W d    n1 sp0    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    *r5   )r   veri_test_pathc                 C   sZ   g }t |d8}|D ]"}| \}}}||||f qW d    n1 sL0    Y  |S )Nr,   )r   r-   r/   )r   r6   r1   r!   r3   labelpath1path2r#   r#   r$   _get_paired_flistL   s    0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v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>   s2   	 [