o
    wi4[                     @   s   d Z 	 ddlmZ ddlZddlZddlZg dZG dd deZdZ	dZ
d	Zd
ZeddZdd ZG dd dZG dd dZG dd dZdddZdS )zGCopiped from https://github.com/python/cpython/blob/v3.13.2/Lib/wave.py    )
namedtupleN)openError	Wave_read
Wave_writec                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r
   r
   P/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/kaldiio/python_wave.pyr   T   s    r      i  s            8q)NbhNi_wave_paramsz7nchannels sampwidth framerate nframes comptype compnamec                 C   sT   t t| }tdt| |D ]}t|D ]}| ||  ||| d | < qqt|S )Nr   r   )	bytearraylenrangebytes)datawidthswapped_datar   jr
   r
   r   	_byteswapb   s   r   c                   @   sJ   e Zd ZdddZdd Zdd Zdd
dZdd ZdddZdd Z	dS )_ChunkTFc              	   C   s   d| _ || _|rd}nd}|| _|d| _t| jdk rtzt|d |dd | _	W n tj
y:   td w |rC| j	d | _	d| _z| j | _W n ttfy]   d| _Y d S w d| _d S )	NF><   Lr      T)closedalignfileread	chunknamer   EOFErrorstructunpack_from	chunksizeerror	size_readtelloffsetAttributeErrorOSErrorseekable)selfr"   r!   	bigendian
inclheaderstrflagr
   r
   r   __init__m   s.    
z_Chunk.__init__c                 C      | j S )z*Return the name (ID) of the current chunk.)r$   r0   r
   r
   r   getname   s   z_Chunk.getnamec                 C   s(   | j sz
|   W d| _ d S d| _ w d S )NT)r    skipr6   r
   r
   r   close   s
   
z_Chunk.closer   c                 C   sv   | j rtd| jstd|dkr|| j }n	|dkr!|| j }|dk s*|| jkr,t| j| j	| d || _dS )zSeek to specified position into the chunk.
        Default position is 0 (start of chunk).
        If the file is not seekable, this will result in an error.
        I/O operation on closed filezcannot seekr      r   N)
r    
ValueErrorr/   r.   r*   r(   RuntimeErrorr"   seekr,   )r0   poswhencer
   r
   r   r>      s   

z_Chunk.seekc                 C   s   | j rtd| jS )Nr:   )r    r<   r*   r6   r
   r
   r   r+         z_Chunk.tellc                 C   s   | j rtd| j| jkrdS |dk r| j| j }|| j| j kr'| j| j }| j|}| jt| | _| j| jkrQ| jrQ| jd@ rQ| jd}| jt| | _|S )zRead at most size bytes from the chunk.
        If size is omitted or negative, read until the end
        of the chunk.
        r:       r   r   )r    r<   r*   r(   r"   r#   r   r!   )r0   sizer   dummyr
   r
   r   r#      s$   z_Chunk.readc                 C   s   | j rtd| jr6z"| j| j }| jr| jd@ r|d }| j|d | j| | _W dS  ty5   Y nw | j| jk rVt	d| j| j }| 
|}|sNt| j| jk s<dS dS )zSkip the rest of the chunk.
        If you are not interested in the contents of the chunk,
        this method should be called so that the file points to
        the start of the next chunk.
        r:   r   Ni    )r    r<   r/   r(   r*   r!   r"   r>   r.   minr#   r%   )r0   nrE   r
   r
   r   r8      s&   
z_Chunk.skipN)TTF)r   )rB   )
r   r   r	   r4   r7   r9   r>   r+   r#   r8   r
   r
   r
   r   r   l   s    


r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,S )-r   aP  Variables used in this class:

    These variables are available to the user though appropriate
    methods of this class:
    _file -- the open file with methods read(), close(), and seek()
              set through the __init__() method
    _nchannels -- the number of audio channels
              available through the getnchannels() method
    _nframes -- the number of audio frames
              available through the getnframes() method
    _sampwidth -- the number of bytes per audio sample
              available through the getsampwidth() method
    _framerate -- the sampling frequency
              available through the getframerate() method
    _comptype -- the AIFF-C compression type ('NONE' if AIFF)
              available through the getcomptype() method
    _compname -- the human-readable AIFF-C compression type
              available through the getcomptype() method
    _soundpos -- the position in the audio stream
              available through the tell() method, set through the
              setpos() method

    These variables are used internally only:
    _fmt_chunk_read -- 1 iff the FMT chunk has been read
    _data_seek_needed -- 1 iff positioned correctly in audio
              file for readframes()
    _data_chunk -- instantiation of a chunk class for the DATA chunk
    _framesize -- size of one frame in the file
    c                 C   s   d | _ d| _t|dd| _| j dkrtd| jddkr$tdd| _d | _	 d| _	z	t| jdd}W n	 t
y@   Y n0w | }|d	krR| | d| _n|d
krk| js]td|| _|j| j | _d| _	n|  q+| jrv| jsztdd S )Nr   )r1      RIFFz file does not start with RIFF idr      WAVEznot a WAVE filer      fmt    datazdata chunk before fmt chunkz#fmt chunk and/or data chunk missing)_convert	_soundposr   _filer7   r   r#   _fmt_chunk_read_data_chunk_data_seek_neededr%   _read_fmt_chunkr(   
_framesize_nframesr8   )r0   r"   chunkr$   r
   r
   r   initfp   s@   
zWave_read.initfpc                 C   J   d | _ t|trt|d}|| _ z| | W d S    | j r$|   )Nrb_i_opened_the_file
isinstancestrbuiltinsr   rV   r9   r0   fr
   r
   r   r4     s   
zWave_read.__init__c                 C      |    d S Nr9   r6   r
   r
   r   __del__&     zWave_read.__del__c                 C      | S ra   r
   r6   r
   r
   r   	__enter__)     zWave_read.__enter__c                 G   r`   ra   rb   r0   argsr
   r
   r   __exit__,  rd   zWave_read.__exit__c                 C   r5   ra   )rN   r6   r
   r
   r   getfp2     zWave_read.getfpc                 C   s   d| _ d| _d S )Nr   r   )rQ   rM   r6   r
   r
   r   rewind5  s   
zWave_read.rewindc                 C   s&   d | _ | j}|rd | _|  d S d S ra   )rN   rZ   r9   r0   r"   r
   r
   r   r9   9  s   zWave_read.closec                 C   r5   ra   )rM   r6   r
   r
   r   r+   @  rl   zWave_read.tellc                 C   r5   ra   )
_nchannelsr6   r
   r
   r   getnchannelsC  rl   zWave_read.getnchannelsc                 C   r5   ra   )rT   r6   r
   r
   r   
getnframesF  rl   zWave_read.getnframesc                 C   r5   ra   )
_sampwidthr6   r
   r
   r   getsampwidthI  rl   zWave_read.getsampwidthc                 C   r5   ra   )
_framerater6   r
   r
   r   getframerateL  rl   zWave_read.getframeratec                 C   r5   ra   	_comptyper6   r
   r
   r   getcomptypeO  rl   zWave_read.getcomptypec                 C   r5   ra   	_compnamer6   r
   r
   r   getcompnameR  rl   zWave_read.getcompnamec                 C   s*   t |  |  |  |  |  |  S ra   )r   rp   rs   ru   rq   rx   r{   r6   r
   r
   r   	getparamsU  s   zWave_read.getparamsc                 C      dd l }|jddd d S )Nr   Wave_read.getmarkers      removewarnings_deprecatedr0   r   r
   r
   r   
getmarkersZ     r~   c                 C      dd l }|jddd td)Nr   Wave_read.getmarkr   r   no marksr   r   r   r0   idr   r
   r
   r   getmark_     r   c                 C   s*   |dk s	|| j krtd|| _d| _d S )Nr   zposition not in ranger   )rT   r   rM   rQ   )r0   r?   r
   r
   r   setposd  s   
zWave_read.setposc                 C   s   | j r| jdd | j| j }|r| j|d d| _ |dkr"dS | j|| j }| jdkr;tjdkr;t	|| j}| j
rE|rE| 
|}| jt|| j| j   | _|S )Nr   rC   r   big)rQ   rP   r>   rM   rS   r#   rr   sys	byteorderr   rL   r   ro   )r0   nframesr?   r   r
   r
   r   
readframesj  s   

zWave_read.readframesc                 C   sV  zt d|d\}| _| _}}W n t jy   td w zt d|dd }W n t jy6   td w |tkrzt d|d\}}}|d}	t|	dk rUtW n t jya   td w |	t	krzdd l
}
d	|
j|	d
 }W t| ty   d}Y t|w |d d | _| jstd| jstd| j| j | _d| _d| _d S )Nz<HHLLH   z<Hr;   r   z<HHLr      zunknown extended format: )bytes_lezunknown extended format   bad sample widthbad # of channelsNONEznot compressed)r&   r'   r#   ro   rt   r)   r%   WAVE_FORMAT_EXTENSIBLEr   KSDATAFORMAT_SUBTYPE_PCMuuidUUID	Exceptionr   rr   rS   rw   rz   )r0   rU   
wFormatTagdwAvgBytesPerSecwBlockAlign	sampwidthcbSizewValidBitsPerSampledwChannelMask	SubFormatr   subformat_msgr
   r
   r   rR     sJ   $

zWave_read._read_fmt_chunkN)r   r   r	   __doc__rV   r4   rc   rf   rj   rk   rm   r9   r+   rp   rq   rs   ru   rx   r{   r|   r   r   r   r   rR   r
   r
   r
   r   r      s.    r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:S );r   a  Variables used in this class:

    These variables are user settable through appropriate methods
    of this class:
    _file -- the open file with methods write(), close(), tell(), seek()
              set through the __init__() method
    _comptype -- the AIFF-C compression type ('NONE' in AIFF)
              set through the setcomptype() or setparams() method
    _compname -- the human-readable AIFF-C compression type
              set through the setcomptype() or setparams() method
    _nchannels -- the number of audio channels
              set through the setnchannels() or setparams() method
    _sampwidth -- the number of bytes per audio sample
              set through the setsampwidth() or setparams() method
    _framerate -- the sampling frequency
              set through the setframerate() or setparams() method
    _nframes -- the number of audio frames written to the header
              set through the setnframes() or setparams() method

    These variables are used internally only:
    _datalength -- the size of the audio samples written to the header
    _nframeswritten -- the number of frames actually written
    _datawritten -- the size of the audio samples actually written
    c                 C   rW   )NwbrY   r^   r
   r
   r   r4     s   
zWave_write.__init__c                 C   s@   || _ d | _d| _d| _d| _d| _d| _d| _d| _d| _	d S )Nr   F)
rN   rL   ro   rr   rt   rT   _nframeswritten_datawritten_datalength_headerwrittenrn   r
   r
   r   rV     s   
zWave_write.initfpc                 C   r`   ra   rb   r6   r
   r
   r   rc     rd   zWave_write.__del__c                 C   re   ra   r
   r6   r
   r
   r   rf     rg   zWave_write.__enter__c                 G   r`   ra   rb   rh   r
   r
   r   rj     rd   zWave_write.__exit__c                 C   s(   | j rtd|dk rtd|| _d S )N0cannot change parameters after starting to writer   r   )r   r   ro   )r0   	nchannelsr
   r
   r   setnchannels  s
   
zWave_write.setnchannelsc                 C      | j std| j S )Nznumber of channels not set)ro   r   r6   r
   r
   r   rp     rA   zWave_write.getnchannelsc                 C   s0   | j rtd|dk s|dkrtd|| _d S )Nr   r   r   r   )r   r   rr   )r0   r   r
   r
   r   setsampwidth  s
   
zWave_write.setsampwidthc                 C   r   )Nzsample width not set)rr   r   r6   r
   r
   r   rs     rA   zWave_write.getsampwidthc                 C   s0   | j rtd|dkrtdtt|| _d S )Nr   r   zbad frame rate)r   r   introundrt   )r0   	framerater
   r
   r   setframerate  s
   zWave_write.setframeratec                 C   r   )Nzframe rate not set)rt   r   r6   r
   r
   r   ru     rA   zWave_write.getframeratec                 C   s   | j rtd|| _d S Nr   )r   r   rT   )r0   r   r
   r
   r   
setnframes  s   
zWave_write.setnframesc                 C   r5   ra   r   r6   r
   r
   r   rq     rl   zWave_write.getnframesc                 C   s.   | j rtd|dvrtd|| _|| _d S )Nr   )r   zunsupported compression type)r   r   rw   rz   )r0   comptypecompnamer
   r
   r   setcomptype  s   
zWave_write.setcomptypec                 C   r5   ra   rv   r6   r
   r
   r   rx     rl   zWave_write.getcomptypec                 C   r5   ra   ry   r6   r
   r
   r   r{     rl   zWave_write.getcompnamec                 C   sV   |\}}}}}}| j rtd| | | | | | | | | || d S r   )r   r   r   r   r   r   r   )r0   paramsr   r   r   r   r   r   r
   r
   r   	setparams  s   



zWave_write.setparamsc                 C   s8   | j r	| jr	| jstdt| j | j| j| j| j| jS )Nznot all parameters set)ro   rr   rt   r   r   rT   rw   rz   r6   r
   r
   r   r|   '  s
   zWave_write.getparamsc                 C   s   dd l }|jddd td)Nr   Wave_write.setmarkr   r   zsetmark() not supportedr   )r0   r   r?   namer   r
   r
   r   setmark-  r   r   c                 C   r   )Nr   Wave_write.getmarkr   r   r   r   r   r
   r
   r   r   2  r   r   c                 C   r}   )Nr   Wave_write.getmarkersr   r   r   r   r
   r
   r   r   7  r   r   c                 C   r5   ra   r   r6   r
   r
   r   r+   <  rl   zWave_write.tellc                 C   s   t |ttfst|d}| t| t|| j| j  }| j	r'| 	|}| jdkr7t
jdkr7t|| j}| j| |  jt|7  _| j| | _d S )NBr   r   )r[   r   r   
memoryviewcast_ensure_header_writtenr   rr   ro   rL   r   r   r   rN   writer   r   )r0   r   r   r
   r
   r   writeframesraw?  s   
zWave_write.writeframesrawc                 C   s&   |  | | j| jkr|   d S d S ra   )r   r   r   _patchheader)r0   r   r
   r
   r   writeframesL  s   
zWave_write.writeframesc                 C   sz   z+| j r| d | j| jkr|   | j   W d | _ | j}|r*d | _|  d S d S d | _ | j}|r<d | _|  w w )Nr   )rN   r   r   r   r   flushrZ   r9   rn   r
   r
   r   r9   Q  s&   


zWave_write.closec                 C   sB   | j s| js
td| jstd| jstd| | d S d S )Nz# channels not specifiedzsample width not specifiedzsampling rate not specified)r   ro   r   rr   rt   _write_header)r0   datasizer
   r
   r   r   c  s   z!Wave_write._ensure_header_writtenc                 C   s   | j rJ | jd | js|| j| j  | _| j| j | j | _z| j | _W n t	t
fy7   d | _Y nw | jtdd| j dddt| j| j| j| j | j | j| j | jd d | jd urj| j | _| jtd	| j d
| _ d S )NrH   z<L4s4sLHHLLHH4s$   rI   rJ   r   r   rK   <LT)r   rN   r   rT   ro   rr   r   r+   _form_length_posr-   r.   r&   packWAVE_FORMAT_PCMrt   _data_length_pos)r0   
initlengthr
   r
   r   r   m  s*   






zWave_write._write_headerc                 C   s   | j sJ | j| jkrd S | j }| j| jd | jt	dd| j  | j| j
d | jt	d| j | j|d | j| _d S )Nr   r   r   )r   r   r   rN   r+   r>   r   r   r&   r   r   )r0   curposr
   r
   r   r     s   

zWave_write._patchheaderN) r   r   r	   r   r4   rV   rc   rf   rj   r   rp   r   rs   r   ru   r   rq   r   rx   r{   r   r|   r   r   r   r+   r   r   r9   r   r   r   r
   r
   r
   r   r     s<    

r   c                 C   sF   |d u rt | dr| j}nd}|dv rt| S |dv rt| S td)NmoderX   )rrX   )wr   z$mode must be 'r', 'rb', 'w', or 'wb')hasattrr   r   r   r   )r_   r   r
   r
   r   r     s   
r   ra   )r   collectionsr   r]   r&   r   __all__r   r   r   r   r   _array_fmtsr   r   r   r   r   r   r
   r
   r
   r   <module>   s,    I
o K k