o
    ôjÍ-  ã                	   @   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)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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   Zarchive_pathr   r   Úfr   r   Ú	file_pathZf_split© r#   ún/var/www/html/academy/tools/voices/clone/sv-venv/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"   
€ý
ÿûr5   Úveri_test_pathc                 C   s\   g }t |dƒ�}|D ]}| ¡ \}}}| |||f¡ q
W d   ƒ |S 1 s'w   Y  |S )Nr,   )r   r-   r/   )r   r6   r1   r!   r3   ÚlabelZpath1Zpath2r#   r#   r$   Ú_get_paired_flistL   s   þ
ÿür8   Ú
_ext_audioc                 C   s8   |   d¡dd … \}}}| |d¡}d |||g¡}|S )Nú/éýÿÿÿÚ ú-)r-   Úreplacer   )r"   r9   Ú
speaker_idZ
youtube_idÚutterance_idÚfile_idr#   r#   r$   Ú_get_file_idU   s   rB   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   rD   r#   r#   r$   Ú__init__g   s   
ÿûzVoxCeleb1.__init__Únc                 C   ó   t ‚©N©ÚNotImplementedError©rJ   rL   r#   r#   r$   Úget_metadatar   ó   zVoxCeleb1.get_metadatac                 C   rM   rN   rO   rQ   r#   r#   r$   Ú__getitem__u   rS   zVoxCeleb1.__getitem__c                 C   rM   rN   rO   ©rJ   r#   r#   r$   Ú__len__x   rS   zVoxCeleb1.__len__)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r9   r   Ústrr   ÚboolrK   r.   rR   rT   rV   r#   r#   r#   r$   rC   \   s     rC   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_urlrD   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'])ÚsuperrK   Ú
ValueErrorr   r   r   r   Úexistsr   r5   rG   Ú_flist)rJ   r   r&   r^   rD   Úmeta_list_path©Ú	__class__r#   r$   rK   ˜   s   
z VoxCeleb1Identification.__init__rL   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)rb   rB   r9   r-   r.   ÚSAMPLE_RATE)rJ   rL   r"   rA   r?   r#   r#   r$   rR   ¤   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©rR   r
   rG   )rJ   rL   ÚmetadataÚwaveformr#   r#   r$   rT   ½   s   
z#VoxCeleb1Identification.__getitem__c                 C   ó
   t | jƒS rN   ©Úlenrb   rU   r#   r#   r$   rV   Ó   ó   
zVoxCeleb1Identification.__len__)rW   rX   rY   rZ   Ú_IDEN_SPLIT_URLr   r[   r   r\   rK   r.   r   rR   r   rT   rV   Ú__classcell__r#   r#   rd   r$   r]   |   s"    ÿ
ÿÿÿÿþr]   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   r^   rD   r'   Nc                    sL   t ƒ  ||¡ tj |tj |¡¡}tj |¡st||ƒ t| j	|ƒ| _
d S rN   )r_   rK   r   r   r   r   ra   r   r8   rG   rb   )rJ   r   r^   rD   rc   rd   r#   r$   rK   ô   s
   
zVoxCeleb1Verification.__init__rL   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
        )rb   r.   rB   r9   rf   )rJ   rL   r7   Zfile_path_spk1Zfile_path_spk2Zfile_id_spk1Zfile_id_spk2r#   r#   r$   rR   ü   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$   rT     s   
z!VoxCeleb1Verification.__getitem__c                 C   rj   rN   rk   rU   r#   r#   r$   rV   4  rm   zVoxCeleb1Verification.__len__)rW   rX   rY   rZ   Ú_VERI_TEST_URLr   r[   r   r\   rK   r.   r   rR   r   rT   rV   ro   r#   r#   rd   r$   rp   ×   s    ,""rp   )r   Úpathlibr   Útypingr   r   r   Útorchr   Útorch.utils.datar   Ztorchaudio._internalr   Útorchaudio.datasets.utilsr	   r
   rf   r   rn   rq   r[   r%   r5   r8   rB   rC   r]   rp   r#   r#   r#   r$   Ú<module>   s4    øýð	 [