U
    ,:%e-                     @   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rt | 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#   \/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/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   labelZpath1Zpath2r#   r#   r$   _get_paired_flistL   s    r8   )r"   
_ext_audioc                 C   s8   |  ddd  \}}}||d}d|||g}|S )N/ -)r-   replacer   )r"   r9   
speaker_idZ
youtube_idZutterance_idfile_idr#   r#   r$   _get_file_idU   s    rA   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   rC   r#   r#   r$   __init__g   s    
zVoxCeleb1.__init__)nc                 C   s   t d S NNotImplementedErrorrH   rJ   r#   r#   r$   get_metadatar   s    zVoxCeleb1.get_metadatac                 C   s   t d S rK   rL   rN   r#   r#   r$   __getitem__u   s    zVoxCeleb1.__getitem__r'   c                 C   s   t d S rK   rL   rH   r#   r#   r$   __len__x   s    zVoxCeleb1.__len__)F)__name__
__module____qualname____doc__r9   r   strr   boolrI   r.   rO   rP   rS   r#   r#   r#   r$   rB   \   s   rB   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_urlrC   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'])superrI   
ValueErrorr   r   r   r   existsr   r5   rE   _flist)rH   r   r&   r[   rC   meta_list_path	__class__r#   r$   rI      s    
z VoxCeleb1Identification.__init__rJ   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)r_   rA   r9   r-   r.   SAMPLE_RATE)rH   rJ   r"   r@   r?   r#   r#   r$   rO      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rO   r
   rE   )rH   rJ   metadataZwaveformr#   r#   r$   rP      s    
z#VoxCeleb1Identification.__getitem__rQ   c                 C   s
   t | jS rK   lenr_   rR   r#   r#   r$   rS      s    zVoxCeleb1Identification.__len__)rT   rU   rV   rW   _IDEN_SPLIT_URLr   rX   r   rY   rI   r.   r   rO   r   rP   rS   __classcell__r#   r#   ra   r$   rZ   |   s     
   rZ   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[   rC   r'   c                    sL   t  || tj|tj|}tj|s:t|| t| j	|| _
d S rK   )r\   rI   r   r   r   r   r^   r   r8   rE   r_   )rH   r   r[   rC   r`   ra   r#   r$   rI      s
    
zVoxCeleb1Verification.__init__rc   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
        )r_   r.   rA   r9   rd   )rH   rJ   r7   Zfile_path_spk1Zfile_path_spk2Zfile_id_spk1Zfile_id_spk2r#   r#   r$   rO      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)   Nre   )rH   rJ   rf   Zwaveform_spk1Zwaveform_spk2r#   r#   r$   rP     s    
z!VoxCeleb1Verification.__getitem__rQ   c                 C   s
   t | jS rK   rg   rR   r#   r#   r$   rS   4  s    zVoxCeleb1Verification.__len__)rT   rU   rV   rW   _VERI_TEST_URLr   rX   r   rY   rI   r.   r   rO   r   rP   rS   rj   r#   r#   ra   r$   rk      s
   &  rk   )r   pathlibr   typingr   r   r   Ztorchr   Ztorch.utils.datar   Ztorchaudio._internalr   Ztorchaudio.datasets.utilsr	   r
   rd   r   ri   rl   rX   r%   r5   r8   rA   rB   rZ   rk   r#   r#   r#   r$   <module>   sB   	 [