o
    iu"                     @   s   d dl mZ d dlmZ d dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZ 						d!d
edededededefddZG dd dZdefddZG dd deZd
edeeef fddZG dd deZG dd deZG dd  d eZdS )"    )Path)DictN)assert_scipy_wav_style)SoundHDF5FilematF   wav
wspecifierfiletypewrite_num_framescompresscompression_method
pcm_formatc                 C   sh   |dkrt | |||dS |dkrt| ||dS |dkr"t| ||dS |dkr-t| ||dS td| )	au  Write matrices in kaldi style

    Args:
        wspecifier: e.g. ark,scp:out.ark,out.scp
        filetype: "mat" is kaldi-martix, "hdf5": HDF5
        write_num_frames: e.g. 'ark,t:num_frames.txt'
        compress: Compress or not
        compression_method: Specify compression level

    Write in kaldi-matrix-ark with "kaldi-scp" file:

    >>> with file_writer_helper('ark,scp:out.ark,out.scp') as f:
    >>>     f['uttid'] = array

    This "scp" has the following format:

        uttidA out.ark:1234
        uttidB out.ark:2222

    where, 1234 and 2222 points the strating byte address of the matrix.
    (For detail, see official documentation of Kaldi)

    Write in HDF5 with "scp" file:

    >>> with file_writer_helper('ark,scp:out.h5,out.scp', 'hdf5') as f:
    >>>     f['uttid'] = array

    This "scp" file is created as:

        uttidA out.h5:uttidA
        uttidB out.h5:uttidB

    HDF5 can be, unlike "kaldi-ark", accessed to any keys,
    so originally "scp" is not required for random-reading.
    Nevertheless we create "scp" for HDF5 because it is useful
    for some use-case. e.g. Concatenation, Splitting.

    r   )r   r   r   hdf5)r   r   z
sound.hdf5)r   r   soundz	filetype=)KaldiWriter
HDF5WriterSoundHDF5WriterSoundWriterNotImplementedError)r	   r
   r   r   r   r    r   L/home/ubuntu/.local/lib/python3.10/site-packages/espnet/utils/cli_writers.pyfile_writer_helper   s(   .r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )

BaseWriterc                 C   s   t N)r   selfkeyvaluer   r   r   __setitem__S      zBaseWriter.__setitem__c                 C   s   | S r   r   r   r   r   r   	__enter__V   r    zBaseWriter.__enter__c                 C   s   |    d S r   )close)r   exc_typeexc_valexc_tbr   r   r   __exit__Y   s   zBaseWriter.__exit__c                 C   s   z| j   W n	 ty   Y nw | jd ur'z| j  W n	 ty&   Y nw | jd ur?z| j  W d S  ty>   Y d S w d S r   )writerr#   	Exception
writer_scpwriter_nframer!   r   r   r   r#   \   s$   

zBaseWriter.closeN)__name__
__module____qualname__r   r"   r'   r#   r   r   r   r   r   R   s
    r   c                 C   sR   | dur"d| vrt d| | dd\}}|dkr"t d|t|ddd	S )
zcget_num_frames_writer

    Examples:
        >>> get_num_frames_writer('ark,t:num_frames.txt')
    N:z%Must include ":", write_num_frames={}   zark,tzDOnly supporting text mode. e.g. --write-num-frames=ark,t:foo.txt :{}wutf-8encoding)
ValueErrorformatsplitopen)r   nframes_typenframes_filer   r   r   get_num_frames_writero   s   r;   c                   @   s    e Zd Z	dddZdd ZdS )	r   NFr   c                 C   sH   |rt j||d| _nt || _d | _|d urt|| _d S d | _d S )N)r   )kaldiioWriteHelperr(   r*   r;   r+   )r   r	   r   r   r   r   r   r   __init__   s   

zKaldiWriter.__init__c                 C   s8   || j |< | jd ur| j| dt| d d S d S )N 
)r(   r+   writelenr   r   r   r   r      s   

 zKaldiWriter.__setitem__)NFr   )r,   r-   r.   r>   r   r   r   r   r   r      s    
r   returnc                 C   sn   |  dd\}}|dvrtd|| | d}| d}t|t|kr.td||tt||}|S )zParse wspecifier to dict

    Examples:
        >>> parse_wspecifier('ark,scp:out.ark,out.scp')
        {'ark': 'out.ark', 'scp': 'out.scp'}

    r/   r0   )arkzscp,arkzark,scpz{} is not allowed: {},zMismatch: {} and {})r7   r5   r6   rB   dictzip)r	   ark_scpfilepathark_scps	filepaths	spec_dictr   r   r   parse_wspecifier   s   

rM   c                   @   "   e Zd ZdZdddZdd ZdS )	r   z~HDF5Writer

    Examples:
        >>> with HDF5Writer('ark:out.h5', compress=True) as f:
        ...     f['key'] = array
    NFc                 C   s~   t |}|d | _|rddi| _ni | _t|d d| _d|v r,t|d ddd| _nd | _|d ur:t|| _	d S d | _	d S )NrD   compressiongzipr1   scpr2   r3   )
rM   filenamekwargsh5pyFiler(   r8   r*   r;   r+   )r   r	   r   r   rL   r   r   r   r>      s   

zHDF5Writer.__init__c                 C   sr   | j j|fd|i| j | jd ur"| j| d| j d| d | jd ur7| j| dt| d d S d S )Ndatar?   r/   r@   )r(   create_datasetrS   r*   rA   rR   r+   rB   r   r   r   r   r      s   
 
 zHDF5Writer.__setitem__)NFr,   r-   r.   __doc__r>   r   r   r   r   r   r      s    
r   c                   @   rN   )	r   zSoundHDF5Writer

    Examples:
        >>> fs = 16000
        >>> with SoundHDF5Writer('ark:out.h5') as f:
        ...     f['key'] = fs, array
    Nr   c                 C   sr   || _ t|}|d | _t|d d| j d| _d|v r&t|d ddd| _nd | _|d ur4t|| _d S d | _d S )NrD   r1   )r6   rQ   r2   r3   )	r   rM   rR   r   r(   r8   r*   r;   r+   r   r	   r   r   rL   r   r   r   r>      s   

zSoundHDF5Writer.__init__c                 C   s   t | |d |d f}| jj||d | jd ur)| j| d| j d| d | jd ur@| j| dt|d  d d S d S )Nr0   r   )rV   r?   r/   r@   )r   r(   rW   r*   rA   rR   r+   rB   r   r   r   r   r      s   
 
$zSoundHDF5Writer.__setitem__Nr   rX   r   r   r   r   r      s    
r   c                   @   rN   )	r   zSoundWriter

    Examples:
        >>> fs = 16000
        >>> with SoundWriter('ark,scp:outdir,out.scp') as f:
        ...     f['key'] = fs, array
    Nr   c                 C   sv   || _ t|}|d | _t| jjddd d | _d|v r(t|d ddd| _nd | _|d ur6t|| _	d S d | _	d S )NrD   T)parentsexist_okrQ   r1   r2   r3   )
r   rM   dirnamer   mkdirr(   r8   r*   r;   r+   rZ   r   r   r   r>      s   

zSoundWriter.__init__c                 C   s   t | |\}}t| j|d | j  }t||tj| | j	d ur0| j	| d| d | j
d urE| j
| dt| d d S d S )N.r?   r@   )r   r   r^   r   	soundfilerA   astypenumpyint16r*   r+   rB   )r   r   r   ratesignalwavfiler   r   r   r     s   

 zSoundWriter.__setitem__r[   rX   r   r   r   r   r      s    
r   )r   NFr   r   )pathlibr   typingr   rT   r<   rc   ra   espnet.utils.cli_utilsr   espnet.utils.io_utilsr   strboolintr   r   r;   r   rM   r   r   r   r   r   r   r   <module>   sB    
E#$