o
    Si                     @   s   d Z ddlZddlZddl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 ddlmZ dd	lmZmZ dd
lmZmZ dZ	ddededefddZdededeeeeeeef f f fddZdS )uA  
About the AudioMNIST corpus

The AudioMNIST dataset consists of 30000 audio recordings
(ca. 9.5 hours) of spoken digits (0-9) in English with 50 repetitions
per digit for each of the 60 different speakers. Recordings were
collected in quiet offices with a RØDE NT-USB microphone as
mono channel signal at a sampling frequency of 48kHz and were
saved in 16 bit integer format. In addition to audio recordings, meta
information including age (range: 22 - 61 years), gender (12 female
/ 48 male), origin and accent of all speakers were collected as well.
All speakers were informed about the intent of the data collection
and have given written declarations of consent for their participation prior
to their recording session.


The data is publicly available at the following github repo:

    https://github.com/soerenab/AudioMNIST
    N)Path)DictUnion)$validate_recordings_and_supervisions)	RecordingRecordingSet)fix_manifests)	load_json)SupervisionSegmentSupervisionSet)Pathlikeresumable_downloadz<https://github.com/soerenab/AudioMNIST/archive/master.tar.gzF
target_dirforce_downloadreturnc                 C   s   t | } | jddd d}| | }| r |s td| d tt||d | d }| s_td| d	 t|}|j	| d
 W d   n1 sNw   Y  t
t| d t| |S )a$  
    Download and untar the AudioMNIST dataset.

    :param target_dir: Pathlike, the path of the dir to store the dataset.
    :param force_download: bool, if True, download the archive even if it
      already exists.
    :return: Return the directory containing the extracted dataset.
    Tparentsexist_okzmaster.tar.gzz	Skipping z because file exists.)r   
AudioMNISTz
Untarring .)pathNzAudioMNIST-master)r   mkdirexistslogginginfor   AUDIO_MNIST_URLtarfileopen
extractallosrenamestr)r   r   tgz_nametgz_pathtgz_dirtar r&   N/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/recipes/audio_mnist.pydownload_audio_mnist'   s&   r(   
corpus_dir
output_dirc                    sT  t | d  t  d  sJ t d }t|dks"J t|tddD ]}|d|v s3J |q't fddtddD }d	d
ddddddddd
}g }|D ]"}|jd\}}	}
|	t
|j|jd|jd|| d|	||	 d	 qTt|}t||\}}t|| |durt |}|jddd ||d  ||d  ||dS )a  Prepare manifests for the AudioMNIST corpus.

    :param: corpus_dir: We assume it is the github repo directory and it
      contains the following directories: data/{01,02,03,...,60}
    :param: output_dir: Directory where the manifests should be written.
    datazaudioMNIST_meta.txt<      =   02c                 3   s8    | ]} |d   dD ]}tj||jdV  qqdS )r/   z*.wav)r   recording_idN)rglobr   	from_filestem).0ir   in_data_dirr&   r'   	<genexpr>[   s    	
z&prepare_audio_mnist.<locals>.<genexpr>zeroonetwothreefourfivesixseveneightnine)
0123456789_r   English)	idr0   startdurationchanneltextlanguagespeakercustomNTr   z!audio_mnist_supervisions.jsonl.gzzaudio_mnist_recordings.jsonl.gz)
recordingssupervisions)r   is_filer	   lenranger   from_recordingsrO   splitappendr
   rQ   r   from_segmentsr   r   r   to_file)r)   r*   metadatar5   rW   id2textrX   rdigit
speaker_idrM   r&   r6   r'   prepare_audio_mnistI   sZ   
	


rf   )F)__doc__r   r   r   pathlibr   typingr   r   lhotser   lhotse.audior   r   	lhotse.qar   lhotse.serializationr	   lhotse.supervisionr
   r   lhotse.utilsr   r   r   boolr(   r!   rf   r&   r&   r&   r'   <module>   s8    
"