o
    Si)                     @   s$  d dl Z d dlmZ d dlmZmZmZmZ d dlm	Z	 d dl
mZmZ d dlmZ d dlmZmZ d dlmZmZ d	Zd
ZdZdedefddZdededfdedeeee f dedededefddZdeddfdedee deeee f dededeeeeeeef f f fdd ZdS )!    N)Path)DictOptionalSequenceUnion)tqdm)fix_manifests$validate_recordings_and_supervisions)RecordingSet)SupervisionSegmentSupervisionSet)Pathlikeresumable_download)traintestzShttps://docs-assets.developer.apple.com/ml-research/datasets/spatial-librispeech/v1zdhttps://docs-assets.developer.apple.com/ml-research/datasets/spatial-librispeech/v1/metadata.parquettarget_fileurlc                 C   s:   dt dtfdd}dt dtfdd}||}|| | d S )	Nr   returnc              
   S   s\   zddl }W n ty   tdw z|j| ddj}|W S  |jjy- } z|d}~ww )aA  This function downloads and returns the content of the given url
        Args:
            url (str): the url of the file to be downloaded
        Raises:
            e: The exception that is raised by the request module
        Returns:
            file_content (bytes): The file content downloaded from the url
        r   NzThe Spatial LibriSpeech recipe requires requests dependency to download the dataset. You can install the dependency using: pip install requestsT)allow_redirects)requestsImportErrorgetcontent
exceptionsRequestException)r   r   file_contente r   V/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/recipes/spatial_librispeech.py_download_file   s   
z0_download_and_save_audio.<locals>._download_filer   r   c              
   S   s\   zt | d}|| W d   W dS 1 sw   Y  W dS  ty- } z|d}~ww )aI  This function saves the downloaded content passed via `file_content' in the `target_file'
        Args:
            target_file (str): the target path for the file content to be saved to
            file_content (bytes): the content to be saved

        Raises:
            e: the IOError raised by the writing operation
        wbN)openwriteIOError)r   r   filer   r   r   r   _save_audio_content,   s   	&z5_download_and_save_audio.<locals>._save_audio_content)strbytes)r   r   r   r%   r   r   r   r   _download_and_save_audio   s   r(   .F   
target_dirdataset_partsforce_downloadbase_urlnum_jobsr   c                 C   s  zddl }W n ty   tdw 		ddtdtt d|jd	td
tdtfdd}t| } | j	ddd |dkr;t
}n
t|trC|gn|}|D ]}|t
v sTJ d| qG| d }|j	ddd |d }	|	 rstd|	 d |S |d }
|
 r}|rtt|
|d n|
 rtd|
 d ||
}z||d |||||d W n ty } z
td|  |d}~ww |	  |S )a  
    Download the Spatial-LibriSpeech dataset.

    :param target_dir: Pathlike, the path of the dir to storage the dataset.
    :param dataset_parts: "all" or a list of splits (e.g. ["train", "test"]) to download.
    :param force_download: Bool, if True, download the tars no matter if the tars exist.
    :param base_url: str, the url of the resource.
    :return: the path to downloaded and extracted directory with data.
    r   NThe Spatial LibriSpeech recipe requires pandas, pyarrow and fastparquet dependency to parse parquet formatted metadata. You can install the dependencies using: pip install pandas pyarrow fastparquetFr*   r+   r,   metadatar.   r-   r/   c                 S   s   t | } | jddd | d}ddlm} |D ]}| | }	|	jddd q||D}
tt|d |d t|d dD ])\}}||vrEq<| | |d	d
 }| d|d	d
}| r^|re|
t	|| q<W d    d S 1 sqw   Y  d S )NTparentsexist_okz/ambisonicsr   )ThreadPoolExecutor	sample_idsplit)total06z.flac/)
r   mkdirconcurrent.futures.threadr5   r   ziplenexistssubmitr(   )r+   r,   r1   r.   r-   r/   	audio_urlr5   partpart_direxr6   r7   recording_pathrecording_urlr   r   r   )_download_spatial_librispeech_audio_filesW   s*   


"zOdownload_spatial_librispeech.<locals>._download_spatial_librispeech_audio_filesTr2   allUnknown dataset part: zSpatial-LibriSpeechz
.completedzSkipping download, found r)   metadata.parquet)r-   audio_files)r+   r,   r1   r.   r-   r/   z Failed to download audio files: )Fr*   )pandasr   r   r   r&   	DataFrameboolintr   r;   SPATIAL_LIBRISPEECH
isinstanceis_filelogginginfor   META_DATA_URLread_parquet	Exceptionerrortouch)r+   r,   r-   r.   r/   pdrG   rB   
corpus_dircompleted_detectormetadata_pathr1   r   r   r   r   download_spatial_librispeech?   st   



r^   noner[   
output_dirnormalize_textc                    sT  zddl }W n ty   tdw t| } |durt|n| }|jddd |  s2J d|  |dkr9t}n
t|trA|gn|}|D ]}|tv sRJ d| qE| d	 }| sbJ | d
|	|}i }	|D ]}|tv sxJ d| t
d| d | d | }
tj|
d|dd d}g }||d |k }t| t|d d| dD ]3\}}|d d}d}|| j}|| j}|d }|d }|d }t||||||||d}|| qt|}|dkrdd  t fdd|D }t||\}}t|| |dur ||d| d   ||d!| d   ||d"|	|< qk|	S )#a6  
    Returns the manifests which consist of the Recordings and Supervisions.
    When all the manifests are available in the ``output_dir``, it will simply read and return them.

    :param corpus_dir: Pathlike, the path of the data dir.
    :param output_dir: Pathlike, the path where to write the manifests.
    :param dataset_parts: string or sequence of strings representing dataset part names, e.g. 'train', 'test'.
        By default we will infer which parts are available in ``corpus_dir``.
    :param normalize_text: str, "none" or "lower",
        for "lower" the transcripts are converted to lower-case.
    :param num_jobs: int, number of parallel threads used for 'parse_utterance' calls.
    :return: a Dict whose key is the dataset part, and the value is Dicts with the keys 'audio' and 'supervisions'.
    r   Nr0   Tr2   zNo such directory: rH   rI   rJ   z
 not foundzProcessing z	 split...rK   z*.flacc                 S   s   | j S N)stem)xr   r   r   <lambda>   s    z-prepare_spatial_librispeech.<locals>.<lambda>)patternr/   recording_idr7   r6   z+Processing supervision segments for split: )r8   descr9   z)speech/librispeech_metadata/transcriptionz%speech/librispeech_metadata/reader_idz&speech/librispeech_metadata/reader_sex)idrg   startdurationchanneltextgenderspeakerlowerc                 S   s   |   S rb   )rp   )rm   r   r   r   re      s    c                    s   g | ]}|  qS r   )transform_text).0sto_lowerr   r   
<listcomp>   s    z/prepare_spatial_librispeech.<locals>.<listcomp>zspatial-librispeech_recordings_z	.jsonl.gzz!spatial-librispeech_supervisions_)
recordingssupervisions)rL   r   r   r;   is_dirrP   rQ   r&   rR   rV   rS   rT   r
   from_dirr   iterrowsr>   rk   channel_idsr   appendr   from_segmentsr   r	   to_file)r[   r`   r,   ra   r/   rZ   rB   r]   r1   	manifestsrC   recording_setsupervision_segmentspart_metadata_rowrg   rj   rk   rl   rm   ro   rn   segmentsupervision_setr   rt   r   prepare_spatial_librispeech   s   







r   )rS   pathlibr   typingr   r   r   r   	tqdm.autor   lhotser   r	   lhotse.audior
   lhotse.supervisionr   r   lhotse.utilsr   r   rP   BASE_URLrU   r&   r(   rN   rO   r^   r   r   r   r   r   <module>   s^    /
c