a
    âñh�*  ã                   @   sÆ   d dl Z d dlZd dlmZmZ d dlZd dlmZ d dlm	Z	 d dl
mZmZmZ dZdZddiZdeeeeeeeeeeeeef d
œdd„ZG dd„ de	ƒZeeeeeef ZG dd„ de	ƒZdS )é    N)ÚAnyÚTuple)ÚTensor)ÚDataset)Údownload_urlÚextract_archiveÚ
walk_fileszNhttps://datashare.is.ed.ac.uk/bitstream/handle/10283/3443/VCTK-Corpus-0.92.zipzVCTK-CorpusZ 8a6ba2946b36fcbef0212cad601f4bfaF)ÚfileidÚpathÚ	ext_audioÚext_txtÚfolder_audioÚ
folder_txtÚ
downsampleÚreturnc                 C   sÀ   |   d¡\}}tj |||| | ¡}	t|	ƒ�}
|
 ¡ d }W d   ƒ n1 sN0    Y  tj |||| | ¡}t |¡\}}|r²tj}tj	}|j
|ddd�}||ƒ}|j|dd�}|||||fS )NÚ_r   i€>  Zsinc_interpolation)Zresampling_methodT)Znoise_shaping)ÚsplitÚosr
   ÚjoinÚopenÚ	readlinesÚ
torchaudioÚloadZ
functionalZ
transformsZResampleZdither)r	   r
   r   r   r   r   r   Ú
speaker_idÚutterance_idZfile_txtZ	file_textÚ	utteranceZ
file_audioÚwaveformÚsample_rateÚFÚTÚsample© r!   úX/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torchaudio/datasets/vctk.pyÚload_vctk_item   s    
*r#   c                
   @   sz   e Zd ZdZdZdZdZdZdZe	e
ddddfeeeeeeedd	œd
d„Zeeeeeeef dœdd„Zedœdd„ZdS )ÚVCTKap  Create a Dataset for VCTK.

    Note:
        * **This dataset is no longer publicly available.** Please use :py:class:`VCTK_092`
        * Directory ``p315`` is ignored because there is no corresponding text files.
          For more information about the dataset visit: https://datashare.is.ed.ac.uk/handle/10283/3443

    Args:
        root (str): Path to the directory where the dataset is found or downloaded.
        url (str, optional): Not used as the dataset is no longer publicly available.
        folder_in_archive (str, optional):
            The top-level directory of the dataset. (default: ``"VCTK-Corpus"``)
        download (bool, optional):
            Whether to download the dataset if it is not found at root path. (default: ``False``).
            Giving ``download=True`` will result in error as the dataset is no longer
            publicly available.
        downsample (bool, optional): Not used.
        transform (callable, optional): Optional transform applied on waveform. (default: ``None``)
        target_transform (callable, optional): Optional transform applied on utterance. (default: ``None``)
    ÚtxtZwav48ú.txtz.wavZp315FN)ÚrootÚurlÚfolder_in_archiveÚdownloadr   Ú	transformÚtarget_transformr   c           
         sº   |rt  d¡ |d us|d ur(t  d¡ |ˆ _|ˆ _|ˆ _tj |¡}tj ||¡}tj ||¡ˆ _	|rpt
dƒ‚tj ˆ j	¡s†t
dƒ‚tˆ j	ˆ jddd�}	t‡ fdd	„|	ƒ}	t|	ƒˆ _d S )
Nz–In the next version, transforms will not be part of the dataset. Please use `downsample=False` to enable this behavior now, and suppress this warning.z In the next version, transforms will not be part of the dataset. Please remove the option `transform=True` and `target_transform=True` to suppress this warning.z`This Dataset is no longer available. Please use `VCTK_092` class to download the latest version.zcDataset not found. Please use `VCTK_092` class with `download=True` to donwload the latest version.FT)ÚsuffixÚprefixZremove_suffixc                    s
   ˆ j | vS ©N)Ú_except_folder)Úw©Úselfr!   r"   Ú<lambda>}   ó    zVCTK.__init__.<locals>.<lambda>)ÚwarningsÚwarnr   r+   r,   r   r
   Úbasenamer   Ú_pathÚRuntimeErrorÚisdirr   Ú
_ext_audioÚfilterÚlistÚ_walker)
r3   r'   r(   r)   r*   r   r+   r,   ÚarchiveÚwalkerr!   r2   r"   Ú__init__O   s6    	ÿÿÿÿÿzVCTK.__init__©Únr   c           	      C   sj   | j | }t|| j| j| j| j| jƒ}|\}}}}}| jdurH|  |¡}| jdur\|  |¡}|||||fS )úÞLoad the n-th sample from the dataset.

        Args:
            n (int): The index of the sample to be loaded

        Returns:
            tuple: ``(waveform, sample_rate, utterance, speaker_id, utterance_id)``
        N)	r?   r#   r9   r<   Ú_ext_txtÚ_folder_audioÚ_folder_txtr+   r,   )	r3   rD   r	   Úitemr   r   r   r   r   r!   r!   r"   Ú__getitem__€   s    	
ú



zVCTK.__getitem__©r   c                 C   s
   t | jƒS r/   )Úlenr?   r2   r!   r!   r"   Ú__len__�   s    zVCTK.__len__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rH   rG   rF   r<   r0   ÚURLÚFOLDER_IN_ARCHIVEÚstrÚboolr   rB   Úintr   r   rJ   rM   r!   r!   r!   r"   r$   3   s.   ùù1r$   c                   @   s„   e Zd ZdZddedfeeeedœdd„Zedœd	d
„Ze	e
ef dœdd„Zeeeedœdd„Zeedœdd„Zedœdd„ZdS )ÚVCTK_092a  Create VCTK 0.92 Dataset

    Args:
        root (str): Root directory where the dataset's top level directory is found.
        mic_id (str): Microphone ID. Either ``"mic1"`` or ``"mic2"``. (default: ``"mic2"``)
        download (bool, optional):
            Whether to download the dataset if it is not found at root path. (default: ``False``).
        url (str, optional): The URL to download the dataset from.
            (default: ``"https://datashare.is.ed.ac.uk/bitstream/handle/10283/3443/VCTK-Corpus-0.92.zip"``)
        audio_ext (str, optional): Custom audio extension if dataset is converted to non-default audio format.

    Note:
        * All the speeches from speaker ``p315`` will be skipped due to the lack of the corresponding text files.
        * All the speeches from ``p280`` will be skipped for ``mic_id="mic2"`` due to the lack of the audio files.
        * Some of the speeches from speaker ``p362`` will be skipped due to the lack of  the audio files.
        * See Also: https://datashare.is.ed.ac.uk/handle/10283/3443
    Úmic2Fz.flac)r'   Úmic_idr*   r(   c              
   C   sŠ  |dvrt d|› �ƒ‚tj |d¡}tj |d¡| _tj | jd¡| _tj | jd¡| _|| _|| _|rªtj 	| j¡sªtj 
|¡sžt |d ¡}t|||dd� t|| jƒ tj 	| j¡sÀt d	ƒ‚tt | j¡ƒ| _g | _| jD ]¦}|d
krô|dkrôqÞtj | j|¡}	tdd„ t |	¡D ƒƒD ]f}
tj |
¡d }tj | j||› d|› | j› �¡}|dk�rntj 
|¡�sn�q| j | d¡¡ �qqÞd S )N)Zmic1rX   z3`mic_id` has to be either "mic1" or "mic2". Found: zVCTK-Corpus-0.92.zipzVCTK-Corpus-0.92r%   Zwav48_silence_trimmedÚmd5)Z
hash_valueZ	hash_typez=Dataset not found. Please use `download=True` to download it.Zp280rX   c                 s   s   | ]}|  d ¡r|V  qdS )r&   N)Úendswith)Ú.0Úfr!   r!   r"   Ú	<genexpr>ê   s   z$VCTK_092.__init__.<locals>.<genexpr>r   r   Zp362)r:   r   r
   r   r9   Ú_txt_dirÚ
_audio_dirÚ_mic_idÚ
_audio_extr;   ÚisfileÚ
_CHECKSUMSÚgetr   r   ÚsortedÚlistdirZ_speaker_idsÚ_sample_idsÚsplitextÚappendr   )r3   r'   rY   r*   r(   Z	audio_extr@   Zchecksumr   Zutterance_dirZutterance_filer   Zaudio_path_micr!   r!   r"   rB   ·   sL    ÿÿ
ÿýzVCTK_092.__init__rK   c                 C   s8   t |ƒ�}| ¡ d W  d   ƒ S 1 s*0    Y  d S )Nr   )r   r   ©r3   Ú	file_pathr!   r!   r"   Ú
_load_text÷   s    
zVCTK_092._load_textc                 C   s
   t  |¡S r/   )r   r   rk   r!   r!   r"   Ú_load_audioû   s    zVCTK_092._load_audio)r   r   rY   r   c           	   
   C   sl   t j | j||› d|› d�¡}t j | j||› d|› d|› | j› �¡}|  |¡}|  |¡\}}|||||fS )Nr   r&   )r   r
   r   r_   r`   rb   rm   rn   )	r3   r   r   rY   Zutterance_pathZ
audio_pathr   r   r   r!   r!   r"   Ú_load_sampleþ   s    ÿý
zVCTK_092._load_samplerC   c                 C   s   | j | \}}|  ||| j¡S )rE   )rh   ro   ra   )r3   rD   r   r   r!   r!   r"   rJ     s    	zVCTK_092.__getitem__c                 C   s
   t | jƒS r/   )rL   rh   r2   r!   r!   r"   rM     s    zVCTK_092.__len__N)rN   rO   rP   rQ   rR   rT   rU   rB   rm   r   r   rV   rn   Ú
SampleTypero   rJ   rM   r!   r!   r!   r"   rW   ¤   s    úû@rW   )F)r   r6   Útypingr   r   r   Ztorchr   Ztorch.utils.datar   Ztorchaudio.datasets.utilsr   r   r   rR   rS   rd   rT   rU   rV   r#   r$   rp   rW   r!   r!   r!   r"   Ú<module>   s,   ÿ úún