o
    Si                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlmZmZmZ ddlmZmZ dZd	d
d
ddZdedfdede	e de	e defddZ		ddede	e de
e deeeeeeef f f fddZdS )aY	  
This script creates the Room Impulse Response and Noises data directory.
This data includes all the room impulse responses (RIRs) and noises used in the
paper "A Study on Data Augmentation of Reverberant Speech for Robust Speech Recognition"
submitted to ICASSP 2017. It includes the real RIRs and isotropic noises from the RWCP
sound scene database, the 2014 REVERB challenge database and the Aachen impulse response
database (AIR); the simulated RIRs generated by ourselves and also the point-source
noises that extracted from the MUSAN corpus.
The required dataset is freely available at http://www.openslr.org/17/

The dataset consists of 3 types of RIRs or noises:

1. pointsource_noises:

The point-source noises are sampled from the Freesound portion of the MUSAN corpus.
This portion of the corpus contains 843 noise recordings and each of them is manually
classified as either a foreground or a background noise. The MUSAN corpus can be downloaded by
wget http://www.openslr.org/resources/17/musan.tar.gz


2. real_rirs_isotropic_noises:

The set of real RIRs is composed of three databases: the RWCP sound scene database,
the 2014 REVERB challenge database and the Aachen impulse response database (AIR).
Overall there are 325 real RIRs. The isotropic noises available in the real RIR databases are
used along with the associated RIRs. Here are the links to download the individual databases:

  - AIR `wget http://www.openslr.org/resources/20/air_database_release_1_4.zip`
  - RWCP `wget http://www.openslr.org/resources/13/RWCP.tar.gz`
  - 2014 REVERB challenge,
    `wget http://reverb2014.dereverberation.com/tools/reverb_tools_for_Generate_mcTrainData.tgz`
    `wget http://reverb2014.dereverberation.com/tools/reverb_tools_for_Generate_SimData.tgz`


3. simulated_rirs

This folder contains the simulated RIRs. Please go to simulated_rirs/README for the details of the data.
This simulated RIR data set can also be downloaded from wget http://www.openslr.org/resources/26/sim_rir.zip

The corpus can be cited as follows:
@article{Ko2017ASO,
  title={A study on data augmentation of reverberant speech for robust speech recognition},
  author={Tom Ko and Vijayaditya Peddinti and Daniel Povey and Michael L. Seltzer and Sanjeev Khudanpur},
  journal={2017 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP)},
  year={2017},
  pages={5220-5224}
}
    N)defaultdict)Path)DictOptionalSequenceUnion)CutSet	RecordingRecordingSet)Pathlikeresumable_downloadz4https://www.openslr.org/resources/28/rirs_noises.zippointsource_noisesreal_rirs_isotropic_noisessimulated_rirspoint_noise	iso_noisereal_rirsim_rir.F
target_dirurlforce_downloadreturnc                 C   s   t | } | jddd d}| | }| r!|s!td| d nt|||d | d }| sTtd| d	 t|}||  W d
   |S 1 sOw   Y  |S )a#  
    Download and untar the RIR Noise corpus.

    :param target_dir: Pathlike, the path of the dir to store the dataset.
    :param url: str, the url that downloads file called "rirs_noises.zip".
    :param force_download: bool, if True, download the archive even if it already exists.
    Tparentsexist_okzrirs_noises.zipz	Skipping z because file exists.)r   RIRS_NOISESz
Unzipping r   N)	r   mkdirexistslogginginfor   zipfileZipFile
extractall)r   r   r   zip_namezip_pathzip_dirzf r)   L/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/recipes/rir_noise.pydownload_rir_noiseD   s    
r+   
corpus_dir
output_dirpartsc           
   	      s  t | } |  sJ d|  |stdt|tr|g}tt}|D ]}td| d | t	|  }| s?J d| |dkrig }dD ] |  d }| fdd	|
d
D 7 }qGt||| d< q#|dkrtdd |
d
D || d< q#|dkrtdd |
d
D || d< q#|dkrtdd |
d
D || d< q#|durt |}|jddd |D ]}||  D ]\}}	|	||dd d| d  qq|S )a  
    Prepare the RIR Noise corpus.

    :param corpus_dir: Pathlike, the path of the dir to store the dataset.
    :param output_dir: Pathlike, the path of the dir to write the manifests.
    :param parts: Sequence[str], the parts of the dataset to prepare.

    The corpus contains 4 things: point-source noises (point_noise), isotropic noises (iso_noise),
    real RIRs (real_rir), and simulated RIRs (sim_rir). We will prepare these parts
    in the corresponding dict keys.
    zNo such directory: z,No parts specified for manifest preparation.z
Preparing z...r   )smallmediumlargeroomc                    s&   g | ]}t j|  d |j dqS )-)recording_id)r	   	from_filestem.0file	room_typer)   r*   
<listcomp>   s    z%prepare_rir_noise.<locals>.<listcomp>z*.wav
recordingsr   c                 s   s    | ]}t |V  qd S )N)r	   r5   r7   r)   r)   r*   	<genexpr>   s    

z$prepare_rir_noise.<locals>.<genexpr>r   c                 s   $    | ]}d |j v rt|V  qdS )noiseNr6   r	   r5   r7   r)   r)   r*   r>          
r   c                 s   r?   )rirNrA   r7   r)   r)   r*   r>      rB   NTr   _r3   z_all.jsonl.gz)r   is_dir
ValueError
isinstancestrr   dictr    r!   PARTSrglobr
   from_recordingsr   itemsto_filereplace)
r,   r-   r.   	manifestspart	audio_dirr=   room_dirkeymanifestr)   r:   r*   prepare_rir_noisea   sT   




rV   )Nr   )__doc__r    r"   collectionsr   pathlibr   typingr   r   r   r   lhotser   r	   r
   lhotse.utilsr   r   RIR_NOISE_ZIP_URLrJ   rH   boolr+   rV   r)   r)   r)   r*   <module>   sJ    0	
