o
    Si                     @   s   d dl mZmZ d dlmZmZmZ d dlZd dl	Z	d dl
mZ d dlmZmZ eG dd deZ				
			ddejdededededededejfddZ				
			dde	jdedededededede	jfddZdS )    )asdict	dataclass)OptionalTupleUnionN)AudioTransform)Secondsis_module_availablec                   @   s   e Zd ZU dZdZeed< dZeed< dZeed< dZ	eed	< dZ
eed
< dZeed< deejejf dejfddZdedee dedeeee f fddZdS )DereverbWPEz
    Dereverberation with Weighted Prediction Error (WPE).
    The implementation and default values are borrowed from `nara_wpe` package:
    https://github.com/fgnt/nara_wpe
       n_fft   
hop_length
   taps   delay
iterationsfullstatistics_modesamplesreturnc                 O   s.   t |r|  }t|fi t| }|S N)torch	is_tensorcpunumpydereverb_wpe_numpyr   )selfr   argskwargs	augmented r"   K/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/augmentation/wpe.py__call__   s   
zDereverbWPE.__call__offsetdurationsampling_ratec                 C   s   ||fS r   r"   )r   r%   r&   r'   r"   r"   r#   reverse_timestamps"   s   zDereverbWPE.reverse_timestampsN)__name__
__module____qualname____doc__r   int__annotations__r   r   r   r   r   strr   npndarrayr   Tensorr$   r   r   r   r(   r"   r"   r"   r#   r
      s,   
 
r
   r   r   r   r   r   audior   r   r   r   r   r   r   c                 C   s   t dstdddlm} | jdksJ d| j t|}tjt	| ||d|d}	|	
d	dd }	||	||||d
}
tjt	|

d	dd|||d }|S )z
    Applies WPE-based dereverberation using nara_wpe's wpe_v8 function with numpy backend.
    The parameter defaults follow the ones in nara_wpe.
    nara_wpeVPlease install nara_wpe first using 'pip install git+https://github.com/fgnt/nara_wpe'r   )wpe_v8   zExpected 2D audio shape, got: Tr   r   return_complexwindow   r   r   r   r   r   r   r:   )r	   ImportErrornara_wpe.wper6   ndimshaper   blackman_windowstft
from_numpypermuter   istft)r3   r   r   r   r   r   r   r6   r:   YZzr"   r"   r#   r   (   s>   
r   c                 C   s   t dstdddlm} | jdksJ t|}tj| ||d|d}	|	ddd}	||	||||d	}
tj	|
ddd|||d
}|S )z
    Applies WPE-based dereverberation using nara_wpe's wpe_v6 function with PyTorch backend.
    The parameter defaults follow the ones in nara_wpe.

    .. caution:: The PyTorch backend is known to sometimes be less stable than the numpy backend.
    r4   r5   r   )wpe_v6r7   Tr8   r;   r<   r=   )
r	   r>   nara_wpe.torch_wperJ   r@   r   rB   rC   rE   rF   )r3   r   r   r   r   r   r   rJ   r:   rG   rH   rI   r"   r"   r#   dereverb_wpe_torchW   s4   
rL   )r   r   r   r   r   r   )dataclassesr   r   typingr   r   r   r   r0   r   lhotse.augmentation.transformr   lhotse.utilsr   r	   r
   r1   r-   r/   r   r2   rL   r"   r"   r"   r#   <module>   sl    
1