
    ujhS                         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dedee   dedeeeef      fdZ G d de      Zy)    N)Path)ListTupleUnion)Tensor)Dataset)download_url_to_file)_get_librispeech_metadata)_extract_tarlibrispeech_finetuningzIhttps://dl.fbaipublicfiles.com/librilight/data/librispeech_finetuning.tgz@5d1efdc777b548194d7e09ba89126e2188026df9fd57aa57eb14408d2b2342afz1h/0z1h/*9h)10min1h10hpathfolders
_ext_audioreturnc           	      j   t        |       } g }|D ]  }| j                  | d|       D cg c]  }|j                  |        }}||D cg c]@  }t        |j                  j                  j                        t        |j
                        fB c}z  } |j                  d        |S c c}w c c}w )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.
    z/*/*/*/*c                     | d   | d   z   S )Nr       )xs    d/var/www/html/dev/engine/venv/lib/python3.12/site-packages/torchaudio/datasets/librilight_limited.py<lambda>z$_get_fileids_paths.<locals>.<lambda>,   s    1Q4!A$;     )key)r   globrelative_tostrparentstemsort)r   r   r   files_pathsfolderppathss          r   _get_fileids_pathsr)      s    ( :DK S.2ii6((:,8W.XYt$YYERqQXX__334c!&&kBRRS ./ ZRs   B+
AB0c            
       n    e Zd ZdZdZdZ	 	 ddeeef   dede	ddfd	Z
d
edeeeeeeef   fdZdefdZy)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.flacrootsubsetdownloadr   Nc                 n   |t         vr#t        dt         j                          d|       t         |   }t        j                  |      }t        j
                  j                  |t              | _        t        j
                  j                  |t         d      }t        j
                  j                  | j                        sM|st        d      t        j
                  j                  |      st        t        |t               t        |       t!        | j                  || j"                        | _        y )Nz`subset` must be one of z	. Found: z.tgzz9Dataset not found. Please use `download=True` to download)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   archives         r   __init__zLibriLightLimited.__init__?   s     $78H8H8J7K9U[T\]^^f%yyWW\\$6
'',,td%;<ww}}TZZ("#^__77>>'*$T7	J!0WdooVr   nc                    | j                   |   \  }}t        || j                  || j                  | j                        }t        j                  t        j                  j                  | j                  |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
            int:
                Sample rate
            str:
                Transcript
            int:
                Speaker ID
            int:
                Chapter ID
            int:
                Utterance ID
        r   r   N)
r>   r
   r8   r   _ext_txt
torchaudioloadr4   r   r6   )r?   rB   	file_pathfileidmetadatawaveform_s          r   __getitem__zLibriLightLimited.__getitem__T   sv    * !//2	6,VTZZDOO]a]j]jk oobggll4::x{&KL!{Xab\))r   c                 ,    t        | j                        S )N)lenr>   )r?   s    r   __len__zLibriLightLimited.__len__n   s    4&&''r   )r   F)__name__
__module____qualname____doc__rD   r   r   r!   r   boolrA   intr   r   rL   rO   r   r   r   r+   r+   0   s    	 HJ
 	WCIW W 	W
 
W**S *U63S#s+J%K *4( (r   r+   )r4   pathlibr   typingr   r   r   rE   torchr   torch.utils.datar   torchaudio._internalr	   torchaudio.datasets.librispeechr
   torchaudio.datasets.utilsr   r7   r<   r=   r1   r!   r)   r+   r   r   r   <module>r]      s    	  % %   $ 5 E 2 )RN	&$HT DI 3 4PUVY[^V^P_K` :?( ?(r   