B
    u9aG                 @   s   d dl mZ d dlZeddZdZdZdZdZd	Zd
Z	dZ
dZdZdZdZdZdZdZeeeee	egZG dd deZdd Zdd ZG dd dZG dd dZd!ddZd"dd ZdS )#    )
namedtupleN_sunau_paramsz7nchannels sampwidth framerate nframes comptype compnameidns.                                    l    c               @   s   e Zd ZdS )ErrorN)__name__
__module____qualname__ r   r   /usr/lib/python3.7/sunau.pyr      s   r   c             C   s<   d}x2t dD ]&}| d}|s$t|d t| }qW |S )Nr   r   r      )rangereadEOFErrorord)filexiZbyter   r   r   	_read_u32   s    
r   c             C   sJ   g }x2t dD ]&}t|d\}}|dt| |}qW | t| d S )Nr   r   r   )r   divmodinsertintwritebytes)r   r   datar   dmr   r   r   
_write_u32   s    r'   c               @   s   e 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)S )*Au_readc             C   s@   t |t dkr,dd l}||d}d| _nd| _| | d S )N r   rbTF)typebuiltinsopen_openedinitfp)selffr,   r   r   r   __init__   s    zAu_read.__init__c             C   s   | j r|   d S )N)_fileclose)r0   r   r   r   __del__   s    zAu_read.__del__c             C   s   | S )Nr   )r0   r   r   r   	__enter__   s    zAu_read.__enter__c             G   s   |    d S )N)r4   )r0   argsr   r   r   __exit__   s    zAu_read.__exit__c          	   C   s  || _ d| _tt|}|tkr(tdtt|| _| jdk rHtd| jdkrZtdt|| _| jtkrzt| j| _tt|| _	| j	t
krtd| j	ttfkrd| _d	| _nj| j	tkrd	 | _| _nR| j	tkrd | _| _n:| j	tkrd
 | _| _n"| j	tkrd | _| _ntdtt|| _tt|| _| jsLtd| j| j | _| jdkr|| jd | _| jd\| _}}nd| _y| | _W n  ttfk
r   d | _Y nX d S )Nr   zbad magic numberr   zheader size too smalld   zheader size ridiculously largezencoding not (yet) supportedr   r   r   r   zunknown encodingzbad # of channels        )r3   	_soundposr!   r   AUDIO_FILE_MAGICr   Z	_hdr_size
_data_sizeAUDIO_UNKNOWN_SIZE	_encoding_simple_encodingsAUDIO_FILE_ENCODING_MULAW_8AUDIO_FILE_ENCODING_ALAW_8
_sampwidth
_framesizeAUDIO_FILE_ENCODING_LINEAR_8AUDIO_FILE_ENCODING_LINEAR_16AUDIO_FILE_ENCODING_LINEAR_24AUDIO_FILE_ENCODING_LINEAR_32
_framerate
_nchannelsr   _info	partitiontell	_data_posAttributeErrorOSError)r0   r   magic_r   r   r   r/      sT    







zAu_read.initfpc             C   s   | j S )N)r3   )r0   r   r   r   getfp   s    zAu_read.getfpc             C   s   | j S )N)rK   )r0   r   r   r   getnchannels   s    zAu_read.getnchannelsc             C   s   | j S )N)rD   )r0   r   r   r   getsampwidth   s    zAu_read.getsampwidthc             C   s   | j S )N)rJ   )r0   r   r   r   getframerate   s    zAu_read.getframeratec             C   s(   | j tkrtS | jtkr$| j | j S dS )Nr   )r>   r?   r@   rA   rE   )r0   r   r   r   
getnframes   s
    

zAu_read.getnframesc             C   s$   | j tkrdS | j tkrdS dS d S )NULAWALAWNONE)r@   rB   rC   )r0   r   r   r   getcomptype   s
    

zAu_read.getcomptypec             C   s$   | j tkrdS | j tkrdS dS d S )NzCCITT G.711 u-lawzCCITT G.711 A-lawznot compressed)r@   rB   rC   )r0   r   r   r   getcompname   s
    

zAu_read.getcompnamec             C   s*   t |  |  |  |  |  |  S )N)r   rU   rV   rW   rX   r\   r]   )r0   r   r   r   	getparams  s    zAu_read.getparamsc             C   s   d S )Nr   )r0   r   r   r   
getmarkers  s    zAu_read.getmarkersc             C   s   t dd S )Nzno marks)r   )r0   idr   r   r   getmark  s    zAu_read.getmarkc             C   sp   | j tkrl|tkr| j }n| j|| j }|  jt|| j 7  _| j tkrhdd l	}|
|| j}|S d S )Nr   )r@   rA   r?   r3   r   rE   r<   lenrB   audioopZulaw2linrD   )r0   nframesr$   rc   r   r   r   
readframes  s    

zAu_read.readframesc             C   s*   | j d krtd| j| j  d| _d S )Nzcannot seekr   )rO   rQ   r3   seekr<   )r0   r   r   r   rewind  s    
zAu_read.rewindc             C   s   | j S )N)r<   )r0   r   r   r   rN   !  s    zAu_read.tellc             C   sP   |dk s||   krtd| jd kr.td| j| j|| j   || _d S )Nr   zposition not in rangezcannot seek)rX   r   rO   rQ   r3   rf   rE   r<   )r0   posr   r   r   setpos$  s    
zAu_read.setposc             C   s"   | j }|rd | _ | jr|  d S )N)r3   r.   r4   )r0   r   r   r   r   r4   ,  s
    zAu_read.closeN)r   r   r   r2   r5   r6   r8   r/   rT   rU   rV   rW   rX   r\   r]   r^   r_   ra   re   rg   rN   ri   r4   r   r   r   r   r(      s(   	.r(   c               @   s   e 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d/d0 Zd1d2 Zd3S )4Au_writec             C   s@   t |t dkr,dd l}||d}d| _nd| _| | d S )Nr)   r   wbTF)r+   r,   r-   r.   r/   )r0   r1   r,   r   r   r   r2   5  s    zAu_write.__init__c             C   s   | j r|   d | _ d S )N)r3   r4   )r0   r   r   r   r5   >  s    zAu_write.__del__c             C   s   | S )Nr   )r0   r   r   r   r6   C  s    zAu_write.__enter__c             G   s   |    d S )N)r4   )r0   r7   r   r   r   r8   F  s    zAu_write.__exit__c             C   sF   || _ d| _d| _d| _d| _t| _d| _d| _d| _	d| _
d| _d S )Nr   r;   rY   )r3   rJ   rK   rD   rE   r?   _nframes_nframeswritten_datawritten_datalengthrL   	_comptype)r0   r   r   r   r   r/   I  s    zAu_write.initfpc             C   s(   | j rtd|dkrtd|| _d S )Nz0cannot change parameters after starting to write)r   r   r   z"only 1, 2, or 4 channels supported)rm   r   rK   )r0   	nchannelsr   r   r   setnchannelsV  s
    zAu_write.setnchannelsc             C   s   | j std| j S )Nznumber of channels not set)rK   r   )r0   r   r   r   rU   ]  s    zAu_write.getnchannelsc             C   s(   | j rtd|dkrtd|| _d S )Nz0cannot change parameters after starting to write)r   r   r   r   zbad sample width)rm   r   rD   )r0   	sampwidthr   r   r   setsampwidthb  s
    zAu_write.setsampwidthc             C   s   | j std| jS )Nzsample width not specified)rJ   r   rD   )r0   r   r   r   rV   i  s    zAu_write.getsampwidthc             C   s   | j rtd|| _d S )Nz0cannot change parameters after starting to write)rm   r   rJ   )r0   	framerater   r   r   setframeraten  s    zAu_write.setframeratec             C   s   | j std| j S )Nzframe rate not set)rJ   r   )r0   r   r   r   rW   s  s    zAu_write.getframeratec             C   s(   | j rtd|dk rtd|| _d S )Nz0cannot change parameters after starting to writer   z# of frames cannot be negative)rm   r   rl   )r0   rd   r   r   r   
setnframesx  s
    zAu_write.setnframesc             C   s   | j S )N)rm   )r0   r   r   r   rX     s    zAu_write.getnframesc             C   s   |dkr|| _ ntdd S )N)r[   rY   zunknown compression type)rp   r   )r0   r+   namer   r   r   setcomptype  s    zAu_write.setcomptypec             C   s   | j S )N)rp   )r0   r   r   r   r\     s    zAu_write.getcomptypec             C   s$   | j dkrdS | j dkrdS dS d S )NrY   zCCITT G.711 u-lawrZ   zCCITT G.711 A-lawznot compressed)rp   )r0   r   r   r   r]     s
    

zAu_write.getcompnamec             C   sH   |\}}}}}}|  | | | | | | | | || d S )N)rr   rt   rv   rw   ry   )r0   paramsrq   rs   ru   rd   ZcomptypeZcompnamer   r   r   	setparams  s    



zAu_write.setparamsc             C   s*   t |  |  |  |  |  |  S )N)r   rU   rV   rW   rX   r\   r]   )r0   r   r   r   r^     s    zAu_write.getparamsc             C   s   | j S )N)rm   )r0   r   r   r   rN     s    zAu_write.tellc             C   s~   t |ttfst|d}|   | jdkrDdd l}||| j	}t
|| j }| j| | j| | _| jt
| | _d S )NBrY   r   )
isinstancer#   	bytearray
memoryviewcast_ensure_header_writtenrp   rc   Zlin2ulawrD   rb   rE   r3   r"   rm   rn   )r0   r$   rc   rd   r   r   r   writeframesraw  s    
zAu_write.writeframesrawc             C   s.   |  | | j| jks"| j| jkr*|   d S )N)r   rm   rl   ro   rn   _patchheader)r0   r$   r   r   r   writeframes  s    
zAu_write.writeframesc             C   s^   | j rZz6|   | j| jks(| j| jkr0|   | j   W d | j }d | _ | jrX|	  X d S )N)
r3   r   rm   rl   ro   rn   r   flushr.   r4   )r0   r   r   r   r   r4     s    zAu_write.closec             C   s<   | j s8| jstd| js"td| js0td|   d S )Nz# of channels not specifiedzsample width not specifiedzframe rate not specified)rm   rK   r   rD   rJ   _write_header)r0   r   r   r   r     s    zAu_write._ensure_header_writtenc          	   C   s  | j dkrl| jdkr t}d| _q| jdkr6t}d| _q| jdkrLt}d| _q| jdkrbt}d| _qtdn| j dkrt}d| _ntd| j| j	 | _t
| jt dt| j }|d	 d
@ }t
| j| | jtkrt}n| j| j }y| j | _W n  ttfk
r   d | _Y nX t
| j| || _t
| j| t
| j| j t
| j| j	 | j| j | jd|t| j d   d S )Nr[   r   r   r   r   zinternal errorrY   r   r
   ir:   r   )rp   rD   rF   rE   rG   rH   rI   r   rB   rK   r'   r3   r=   rb   rL   rl   r?   rN   _form_length_posrP   rQ   ro   rJ   r"   )r0   encodingZheader_sizelengthr   r   r   r     sJ    







zAu_write._write_headerc             C   sH   | j d krtd| j| j  t| j| j | j| _| jdd d S )Nzcannot seekr   r   )r   rQ   r3   rf   r'   rn   ro   )r0   r   r   r   r     s    
zAu_write._patchheaderN)r   r   r   r2   r5   r6   r8   r/   rr   rU   rt   rV   rv   rW   rw   rX   ry   r\   r]   r{   r^   rN   r   r   r4   r   r   r   r   r   r   r   rj   3  s2   	
*rj   c             C   sJ   |d krt | dr| j}nd}|dkr.t| S |dkr>t| S tdd S )Nmoder*   )rr*   )wrk   z$mode must be 'r', 'rb', 'w', or 'wb')hasattrr   r(   rj   r   )r1   r   r   r   r   r-     s    
r-   c             C   s   t jdtdd t| |dS )NzDsunau.openfp is deprecated since Python 3.7. Use sunau.open instead.r   )
stacklevel)r   )warningswarnDeprecationWarningr-   )r1   r   r   r   r   openfp  s    
r   )N)N)collectionsr   r   r   r=   rB   rF   rG   rH   rI   ZAUDIO_FILE_ENCODING_FLOATZAUDIO_FILE_ENCODING_DOUBLEZAUDIO_FILE_ENCODING_ADPCM_G721ZAUDIO_FILE_ENCODING_ADPCM_G722Z AUDIO_FILE_ENCODING_ADPCM_G723_3Z AUDIO_FILE_ENCODING_ADPCM_G723_5rC   r?   rA   	Exceptionr   r   r'   r(   rj   r-   r   r   r   r   r   <module>j   s@   	  Q
