
    ujh-              	          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Zdedededee   fdZdedefdZdedefdZ G d de
      Z G d  d!e      Z G d" d#e      Zy)$    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           	         dD ]'  }t         |   d   }t        j                  j                  | |      }|dk(  rt         |   d   }t         |   d   }t	        |d      5 }t        ||      D ]  \  }}t        j                  j                  | t        j                  j                  |            }	t        ||	|       t	        |	d      5 }
|j                  |
j                                d d d         	 d d d        n&t         |   d	   }t         |   d
   }t        |||       t        |       * y # 1 sw Y   xY w# 1 sw Y   $xY w)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_splits              [/var/www/html/dev/engine/venv/lib/python3.12/site-packages/torchaudio/datasets/voxceleb1.py_download_extract_wavsr-   &   s7   " #'0@ww||D,7 e#G,V4D(1+>IlD) 0Q%(y%9 0MC "T2773C3CC3H II(iXNi. 0'/0 000 0 #7+E2C'0<H lI\"%#0 0	0 0s%   A*D; D/	&
D;/D84D;;E	r*   subsetreturnc                    g }|dk(  rd}n
|dk(  rd}nd}t        |d      5 }|D ]5  }|j                         \  }}t        |      |k(  s%|j                  |       7 	 d d d        t	        |      S # 1 sw Y   t	        |      S xY w)Ntrain   r         r)r"   splitintappendsorted)	r   r*   r.   f_listindexr)   lineidr    s	            r,   
_get_flistr>   <   s    F	5	i	 $ 	$Dzz|HB2w%d#	$$
 &>$
 &>s   'A0	A00Bveri_test_pathc                     g }t        |d      5 }|D ]*  }|j                         \  }}}|j                  |||f       , 	 d d d        |S # 1 sw Y   |S xY w)Nr5   )r"   r6   r8   )r   r?   r:   r)   r<   labelpath1path2s           r,   _get_paired_flistrD   L   se    F	nc	" 1a 	1D"&**,E5%MM5%/0	11 M	1 Ms   0A

A
_ext_audioc                     | j                  d      dd  \  }}}|j                  |d      }dj                  |||g      }|S )N/ -)r6   replacer!   )r*   rE   
speaker_id
youtube_idutterance_idfile_ids         r,   _get_file_idrP   U   sJ    +4??3+?+D(J
L''
B7Lhh
J=>GN    c                   X    e Zd ZdZdZddeeef   deddfdZ	de
fd	Zde
fd
Zde
fdZy)	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.wavr   downloadr/   Nc                    t        j                  |      }t         j                  j                  |d      | _        t         j                  j                  | j                        s'|st        d| j                   d      t        |       y y )NwavzDataset not found at z5. Please set `download=True` to download the dataset.)r   fspathr    r!   _pathisdirRuntimeErrorr-   )selfr   rT   s      r,   __init__zVoxCeleb1.__init__g   si    yyWW\\$.
ww}}TZZ("+DJJ<7lm  #4( )rQ   nc                     t         NNotImplementedErrorr[   r]   s     r,   get_metadatazVoxCeleb1.get_metadatar       !!rQ   c                     t         r_   r`   rb   s     r,   __getitem__zVoxCeleb1.__getitem__u   rd   rQ   c                     t         r_   r`   r[   s    r,   __len__zVoxCeleb1.__len__x   rd   rQ   )F)__name__
__module____qualname____doc__rE   r   strr   boolr\   r7   rc   rf   ri    rQ   r,   rS   rS   \   sU     J	)U39- 	) 	)$ 	)"c ""S "" "rQ   rS   c                        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
Z	de
deee
e
ef   fdZde
deee
e
ef   fdZde
fdZ x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.
    r1   Fr   r.   meta_urlrT   r/   Nc                 L   t         |   ||       |dvrt        d      t        j                  j                  |t        j                  j                  |            }t        j                  j                  |      st        ||       t        | j                  ||      | _        y )N)r1   r   r   z0`subset` must be one of ['train', 'dev', 'test'])superr\   
ValueErrorr   r    r!   r$   existsr	   r>   rX   _flist)r[   r   r.   rs   rT   meta_list_path	__class__s         r,   r\   z VoxCeleb1Identification.__init__   sz     	x(11OPPdBGG,<,<X,FGww~~n- >: ^VDrQ   r]   c                     | j                   |   }t        || j                        }|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
        rJ   r   r4   N)rx   rP   rE   r6   r7   SAMPLE_RATE)r[   r]   r*   rO   rL   s        r,   rc   z$VoxCeleb1Identification.get_metadata   sS    & KKN	y$//:]]3'*
AB(
+z7::rQ   c                 p    | j                  |      }t        | j                  |d   |d         }|f|dd z   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   r2   Nrc   r   rX   )r[   r]   metadatawaveforms       r,   rf   z#VoxCeleb1Identification.__getitem__   sA    $ $$Q'!$**hqk8A;G{Xab\))rQ   c                 ,    t        | j                        S r_   lenrx   rh   s    r,   ri   zVoxCeleb1Identification.__len__       4;;rQ   )rj   rk   rl   rm   _IDEN_SPLIT_URLr   rn   r   ro   r\   r7   r   rc   r   rf   ri   __classcell__rz   s   @r,   rr   rr   |   s    8 5<_ot
E#t)$
E.1
EGJ
Ehl
E	
E;c ;eCc3,>&? ;2*S *U63S+@%A *,   rQ   rr   c            
            e Zd ZdZedfdeeef   dededdf fdZ	d	e
deeee
e
eef   fd
Zd	e
deeee
e
eef   fdZde
fdZ x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   rs   rT   r/   Nc                 ,   t         |   ||       t        j                  j	                  |t        j                  j                  |            }t        j                  j                  |      st        ||       t        | j                  |      | _
        y r_   )ru   r\   r   r    r!   r$   rw   r	   rD   rX   rx   )r[   r   rs   rT   ry   rz   s        r,   r\   zVoxCeleb1Verification.__init__   sa    x(dBGG,<,<X,FGww~~n- >:'

NCrQ   r]   c                     | 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
        )rx   r7   rP   rE   r|   )r[   r]   rA   file_path_spk1file_path_spk2file_id_spk1file_id_spk2s          r,   rc   z"VoxCeleb1Verification.get_metadata   sU    . 15A-~~E
#NDOOD#NDOOD~{E<Q]]]rQ   c                     | j                  |      }t        | j                  |d   |d         }t        | j                  |d   |d         }||f|dd z   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   r3   r2   Nr~   )r[   r]   r   waveform_spk1waveform_spk2s        r,   rf   z!VoxCeleb1Verification.__getitem__  s^    , $$Q'&tzz8A;L&tzz8A;L}-<<rQ   c                 ,    t        | j                        S r_   r   rh   s    r,   ri   zVoxCeleb1Verification.__len__4  r   rQ   )rj   rk   rl   rm   _VERI_TEST_URLr   rn   r   ro   r\   r7   r   rc   r   rf   ri   r   r   s   @r,   r   r      s    8 @N`e DU39- D DY] Djn D^c ^eCc3S,H&I ^:=S =U663S#+M%N =6   rQ   r   )r   pathlibr   typingr   r   r   torchr   torch.utils.datar   torchaudio._internalr	   torchaudio.datasets.utilsr
   r   r|   r   r   r   rn   r-   r>   rD   rP   rS   rr   r   rp   rQ   r,   <module>r      s    	  % %  $ 5 B  +

  ,XV! , WT# #,S S # $s)  C  C S " "@X i X v^ I ^ rQ   