o
    ({jz                     @  s\   d Z ddlmZ ddlZddlZddlZddlmZmZ dd	d
Z	ddddZ
dddZdS )z=Split WAV files into chunks for Sarvam sync REST (30s limit).    )annotationsN)ListTuplepathstrreturnfloatc                 C  sb   t | d!}| }|dkr	 W d    dS | t| W  d    S 1 s*w   Y  d S )Nrbr   g        )waveopengetframerate
getnframesr   )r   wfrate r   H/home/aiteam/pcaa-dev/call-proccessing/stt_pipeline/stt/audio_chunker.pywav_duration_seconds
   s   $r         9@max_secondsTuple[List[str], List[float]]c              	   C  sb  t | d}| }| }| }| }|dks|dkr*g g fW  d   S tdt|| }g }g }	d}
|
|k rt|||
 }|	|
 |
|}tjddd\}}t| t |d}|| || || || W d   n1 sw   Y  || |	|
t|  |
|7 }
|
|k s=||	fW  d   S 1 sw   Y  dS )	zm
    Split a PCM WAV into <= max_seconds chunks.
    Returns (chunk_paths, chunk_start_offsets_seconds).
    r	   r   N   z.wavz/tmp)suffixdirwb)r
   r   getnchannelsgetsampwidthr   r   maxintminsetpos
readframestempfilemkstemposclosesetnchannelssetsampwidthsetframeratewriteframesappendr   )r   r   r   channelssample_widthr   total_framesframes_per_chunkchunk_pathsoffsetsstart_framechunk_framesframeschunk_fd
chunk_pathoutr   r   r   	split_wav   s<   






$r6   paths	List[str]Nonec              	   C  s>   | D ]}z|rt j|rt | W q ty   Y qw d S )N)r#   r   existsunlinkOSError)r7   r   r   r   r   remove_paths8   s   
r=   )r   r   r   r   )r   )r   r   r   r   r   r   )r7   r8   r   r9   )__doc__
__future__r   r#   r!   r
   typingr   r   r   r6   r=   r   r   r   r   <module>   s    
&