U
    CŒÑhS  ã                   @   sÄ   d dl Z d dlmZ d dlmZmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d	Zd
ZdZdgdgddgdœZeee eeeeef  dœdd„ZG dd„ deƒZdS )é    N)ÚPath)ÚListÚTupleÚUnion)ÚTensor)ÚDataset)Údownload_url_to_file)Ú_get_librispeech_metadata)Ú_extract_tarZlibrispeech_finetuningzIhttps://dl.fbaipublicfiles.com/librilight/data/librispeech_finetuning.tgzZ@5d1efdc777b548194d7e09ba89126e2188026df9fd57aa57eb14408d2b2342afz1h/0z1h/*Z9h)Ú10minZ1hZ10h)ÚpathÚfoldersÚ
_ext_audioÚreturnc                    s^   t ˆ ƒ‰ g }|D ]8}‡ fdd„ˆ  |› d|› �¡D ƒ}|dd„ |D ƒ7 }q|jdd„ d� |S )aî  Get the file names and the corresponding file paths without `speaker_id`
    and `chapter_id` directories.
    The format of path is like:
        {root}/{_ARCHIVE_NAME}/1h/[0-5]/[clean, other] or
        {root}/{_ARCHIVE_NAME}/9h/[clean, other]

    Args:
        path (Path): Root path to the dataset.
        folders (List[str]): Folders that contain the desired audio files.
        _ext_audio (str): Extension of audio files.

    Returns:
        List[Tuple[str, str]]:
            List of tuples where the first element is the relative path to the audio file.
            The format of relative path is like:
            1h/[0-5]/[clean, other] or 9h/[clean, other]
            The second element is the file name without audio extension.
    c                    s   g | ]}|  ˆ ¡‘qS © )Úrelative_to©Ú.0Úp©r   r   úT/tmp/user/2202/pip-unpacked-wheel-p89lpdx5/torchaudio/datasets/librilight_limited.pyÚ
<listcomp>*   s     z&_get_fileids_paths.<locals>.<listcomp>z/*/*/*/*c                 S   s$   g | ]}t |jjjƒt |jƒf‘qS r   )ÚstrÚparentÚstemr   r   r   r   r   +   s     c                 S   s   | d | d  S )Nr   é   r   )Úxr   r   r   Ú<lambda>,   ó    z$_get_fileids_paths.<locals>.<lambda>)Úkey)r   ÚglobÚsort)r   r   r   Zfiles_pathsÚfolderÚpathsr   r   r   Ú_get_fileids_paths   s    "r$   c                   @   sd   e Zd ZdZdZdZdeeef ee	ddœdd	„Z
eeeeeeeef d
œdd„Zedœdd„ZdS )ÚLibriLightLimitedaõ  Subset of Libri-light :cite:`librilight` dataset,
    which was used in HuBERT :cite:`hsu2021hubert` for supervised fine-tuning.

    Args:
        root (str or Path): Path to the directory where the dataset is found or downloaded.
        subset (str, optional): The subset to use. Options: [``"10min"``, ``"1h"``, ``"10h"``]
            (Default: ``"10min"``).
        download (bool, optional):
            Whether to download the dataset if it is not found at root path. (default: ``False``).
    z
.trans.txtz.flacr   FN)ÚrootÚsubsetÚdownloadr   c                 C   s¨   |t kr tdt  ¡ › d|› �ƒ‚t | }t |¡}tj |t¡| _tj |t› d�¡}tj 	| j¡s’|spt
dƒ‚tj |¡sŠtt|td� t|ƒ t| j|| jƒ| _d S )Nz`subset` must be one of z	. Found: z.tgzz9Dataset not found. Please use `download=True` to download)Zhash_prefix)Ú_SUBSET_MAPÚ
ValueErrorÚkeysÚosÚfspathr   ÚjoinÚ_ARCHIVE_NAMEÚ_pathÚisdirÚRuntimeErrorÚisfiler   Ú_URLÚ	_CHECKSUMr
   r$   r   Ú_fileids_paths)Úselfr&   r'   r(   r   Úarchiver   r   r   Ú__init__?   s    
zLibriLightLimited.__init__)Únr   c                 C   sT   | j | \}}t|| j|| j| jƒ}t tj 	| j|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
            int:
                Sample rate
            str:
                Transcript
            int:
                Speaker ID
            int:
                Chapter ID
            int:
                Utterance ID
        r   r   N)
r6   r	   r0   r   Ú_ext_txtÚ
torchaudioÚloadr,   r   r.   )r7   r:   Ú	file_pathZfileidÚmetadataZwaveformÚ_r   r   r   Ú__getitem__T   s    zLibriLightLimited.__getitem__)r   c                 C   s
   t | jƒS )N)Úlenr6   )r7   r   r   r   Ú__len__n   s    zLibriLightLimited.__len__)r   F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r;   r   r   r   r   Úboolr9   Úintr   r   rA   rC   r   r   r   r   r%   0   s     ü
û r%   )r,   Úpathlibr   Útypingr   r   r   r<   Ztorchr   Ztorch.utils.datar   Ztorchaudio._internalr   Ztorchaudio.datasets.librispeechr	   Ztorchaudio.datasets.utilsr
   r/   r4   r5   r)   r   r$   r%   r   r   r   r   Ú<module>   s   $