o
    Siq                     @   s   d dl Z d dlZd dlmZmZ d dlmZmZ d dlZ	d dl
Z
d dlZd dlmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZ d dlmZmZ e
 d	d
 Z ddefddZ!G dd dZ"dS )    N)NamedTemporaryFileTemporaryDirectory)DictList)	AudioSourceCutSetFbankFbankConfigLilcomChunkyWriterMonoCutNumpyFilesWriter	RecordingSupervisionSegment)seconds_to_frames)
save_audioAlignmentItem)Secondsuuid4c                   C   s   dt jd< d S )N1LHOTSE_DILL_ENABLED)osenviron r   r   K/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/testing/fixtures.pywith_dill_enabled   s   r   d   returnc                    s    d t  fddt| D S )N>  c                 3   sX    | ]'}t t td d    td  d    d tt g dddddV  qd	S )
r         
   r   i j g      Y@idsourcessampling_ratenum_samplesdurationr#   startr'   channel	recordingN)r   r   randomrandintr   ).0_srr   r   	<genexpr>"   s"    
z!random_cut_set.<locals>.<genexpr>)r   	from_cutsrange)n_cutsr   r0   r   random_cut_set    s   r6   c                   @   s   e Zd Zdd Zdd Zdd Z	d!ded	ed
edefddZ							d"ded	ededededede
dedefddZdede
dedefddZdededeeee f fddZdede
ddfdd ZdS )#RandomCutTestCasec                 C   s   g | _ g | _d S N)filesdirsselfmethodr   r   r   setup_method5   s   
zRandomCutTestCase.setup_methodc                 C   s   |    d S r8   )cleanupr;   r   r   r   teardown_method9   s   z!RandomCutTestCase.teardown_methodc                 C   s8   | j D ]}|  qg | _ | jD ]}|  qg | _d S r8   )r9   closer:   r?   )r<   fdr   r   r   r?   <   s   




zRandomCutTestCase.cleanupFr%   r&   	use_zerosr   c                 C   s   t ddd}| j| || }|rtd|f}ntd|f}t|j||d |  t	
| ttt tddg|jdg|||d	S )
Nwbz.wav)suffix   )r%   filer   )typechannelssourcer"   )r   r9   appendtorchzerosrandr   nameflushr   fsyncr   strr   r   )r<   r%   r&   rD   rB   r'   samplesr   r   r   with_recordingD   s    
z RandomCutTestCase.with_recordingT{Gz?featuressupervision	alignmentcustom_fieldframe_shift
use_zeroesc	                 C   s   || }	t tt d|	d| j|||dd}
|r | j|
||d}
|r>|
jtd|
j |
j	d|
j
d|r9| |
dnd d |rG| j|
|d |
S )	Nr   )r%   r&   rD   r(   )r[   r%   zsup-
irrelevant)r#   recording_idr)   r'   textrY   )cutr[   )r   rS   r   rU   _with_featuressupervisionsrL   r   r#   r^   r'   _with_alignment_with_custom_temporal_array)r<   r%   r&   rW   rX   rY   rZ   r[   r\   r'   r`   r   r   r   with_cutY   s@   
zRandomCutTestCase.with_cutr`   c                 C   s`   t  }| j| tt||dd}t|j}|j||dW  d    S 1 s)w   Y  d S )N)r%   r[   )config)storage)r   r:   rL   r   r	   r
   rP   compute_and_store_features)r<   r`   r[   r%   rC   	extractorrg   r   r   r   ra      s   
$z RandomCutTestCase._with_featuresr_   c                    sJ   fddt dtdD }|jt|   fddt|D }d|iS )Nc                    s   g | ]
} ||d   qS )r    r   )r.   i)r_   r   r   
<listcomp>   s    z5RandomCutTestCase._with_alignment.<locals>.<listcomp>r   r    c                    s"   g | ]\}}t ||   d qS ))symbolr)   r'   r   )r.   rj   sub)durr   r   rk      s    subword)r4   lenr'   	enumerate)r<   r`   r_   subwordsrY   r   )rn   r_   r   rc      s   

z!RandomCutTestCase._with_alignmentNc                 C   sv   t  }| j| t|j|d}tjjd|fd}t|j	}|j
d||dd|_W d    d S 1 s4w   Y  d S )N)r[      )sizeali1r   )keyvaluer[   temporal_dim)r   r:   rL   r   r'   npr,   r-   r   rP   store_arraycodebook_indices)r<   r`   r[   rC   
num_framesarrayrg   r   r   r   rd      s   
"z-RandomCutTestCase._with_custom_temporal_array)F)TFFFrV   F)__name__
__module____qualname__r>   r@   r?   intboolr   rU   r   r   re   ra   rS   r   r   r   rc   rd   r   r   r   r   r7   4   sp    	
	

,

r7   )r   )#r   r,   tempfiler   r   typingr   r   numpyry   pytestrM   lhotser   r   r   r	   r
   r   r   r   r   lhotse.arrayr   lhotse.audior   lhotse.supervisionr   lhotse.utilsr   r   fixturer   r6   r7   r   r   r   r   <module>   s     ,
