o
    Si&                     @   s   d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	Z
ddlZddlmZmZ defd	d
ZG dd deZG dd deZG dd deZdS )a  
Silero VAD integration module

This module integrates Silero VAD (Voice Activity Detector),
a high-precision pre-trained model for voice activity detection in audio streams.

GitHub Repository: https://github.com/snakers4/silero-vad

Citations:

@misc{Silero VAD,
  author = {Silero Team},
  title = {
      Silero VAD: pre-trained enterprise-grade Voice Activity Detector (VAD),
      Number Detector and Language Classifier
  },
  year = {2021},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/snakers4/silero-vad}},
  commit = {insert_some_commit_here},
  email = {hello@silero.ai}
}

Please cite Silero VAD when used in your projects. Details can be found in the repository.
    N)suppress)Path)DictList   )ActivityActivityDetectorsampling_ratec                    s"   dt ttf dtf fdd}|S )Nstatereturnc                    s*   | d | d }}t |  ||   dS )Nstartend)r   duration)r   )r
   r   r   r	    b/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/workflows/activity_detection/silero_vad.pymake_activity(   s
   
z)_to_activity_maker.<locals>.make_activity)r   strfloatr   )r	   r   r   r   r   _to_activity_maker'   s   r   c                       s   e Zd ZdZdddddededef fd	d
Zede	e
 fddZedd ZedddefddZdejde	e fddZedd Z  ZS )	SileroVADz,Silero Voice Activity Detector model wrappercpu>  F)devicer	   force_downloadr   r	   r   c                   st   |dvrdd| f}t |t jd|d  d||d | j|d\| _}|d	 | _| j| j t|| _	d S )
N)@  r   z,Sampling rate must be either 8000 or 16000, zbut got silero_vad_i  k)detector_namer   r	   r   r   )

ValueErrorsuper__init__
_get_model_model_predicttor   r   _to_activity)selfr   r	   r   msgutils	__class__r   r   r"   5   s   
zSileroVAD.__init__r   c                 C   s4   t j }t|tstd| tt|dS )NzBad cache directory path. Got zsnakers4_silero-vad_*)	torchhubget_dir
isinstancer   	TypeErrorlistr   glob)cls	cache_dirr   r   r   _cache_dirsO   s   

zSileroVAD._cache_dirsc                 C   s$   |   D ]}| rt| qdS )z#Remove Silero VAD models from cacheN)r6   is_dirshutilrmtree)r4   	directoryr   r   r   _clear_cacheV   s
   
zSileroVAD._clear_cacher   c                C   s   |r|    |  sd}ddd|d}tt ddlm} |tj|dkr+d|d	< W d    n1 s5w   Y  tjj	d
i |S )NTzsnakers4/silero-vad
silero_vadF)repo_or_dirmodelonnxforce_reloadr   )parse_versionz1.12
trust_repor   )
r;   r6   r   	Exceptionpkg_resourcesrA   r-   __version__r.   load)r4   r   configrA   r   r   r   r#   ]   s    
zSileroVAD._get_modeltrackc                 C   sr   t || j}t   | j|| j| jdtdddddddd}W d   n1 s,w   Y  t	t
| j|S )	z Predict voice activity for audio   infd   i      FN)audior>   r	   min_speech_duration_msmax_speech_duration_smin_silence_duration_mswindow_size_samplesspeech_pad_msreturn_secondsvisualize_probsprogress_tracking_callback)r-   Tensorr&   r   no_gradr%   r$   _sampling_rater   r2   mapr'   )r(   rH   rM   murkupr   r   r   forwardr   s"   
zSileroVAD.forwardc                 C   s   | j dd d S )NTr   )r#   )r4   r   r   r   r      s   zSileroVAD.force_download)__name__
__module____qualname____doc__r   intboolr"   classmethodr   r   r6   r;   r#   npndarrayr   r[   r   __classcell__r   r   r+   r   r   2   s*    
r   c                       .   e Zd Zddddedef fddZ  ZS )	SileroVAD8kr   Fr   r   r   c                      t  jd||d d S )Nr   r	   r   r   r!   r"   r(   r   r   r+   r   r   r"      
   
zSileroVAD8k.__init__r   r\   r]   r^   r   ra   r"   re   r   r   r+   r   rg          &rg   c                       rf   )	SileroVAD16kr   Fr   r   r   c                   rh   )Nr   ri   rj   rk   r+   r   r   r"      rl   zSileroVAD16k.__init__rm   rn   r   r   r+   r   rp      ro   rp   )r_   r8   
contextlibr   pathlibr   typingr   r   numpyrc   r-   baser   r   r`   r   r   rg   rp   r   r   r   r   <module>   s    Z	