B
    u9a.              
   @   s@  d dl Z d dlZd dlZdddgZG dd deZdZdd Zd	d
 Zdd Z	dd Z
d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 lmZ d d!lmZ ed"d#Zd$ej_d%ej_d&ej_d'ej_d(ej_d)ej_G d*d+ d+Z G d,d- d-Z!d@d.dZ"dAd/dZ#e$d0kr<d dl%Z%e%j&d1d sJe%j&'d2 e%j&d1 Z(e"e(d3Z)e*d4e( e*d5e)+  e*d6e),  e*d7e)-  e*d8e).  e*d9e)/  e*d:e)0  e%j&d;d r2e%j&d; Z1e*d<e1 e"e1d=8Z2e23e)4  x"e)5d>Z6e6sP e27e6 qW W dQ R X e*d? W dQ R X dS )B    NErroropenopenfpc               @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   /usr/lib/python3.7/aifc.pyr      s   l   @Q E c             C   s8   yt d| dd S  t jk
r2   td Y nX d S )Nz>l   r   )structunpackreaderrorEOFError)filer   r   r	   
_read_long   s    r   c             C   s8   yt d| dd S  t jk
r2   td Y nX d S )Nz>Lr
   r   )r   r   r   r   r   )r   r   r   r	   _read_ulong   s    r   c             C   s8   yt d| dd S  t jk
r2   td Y nX d S )Nz>h   r   )r   r   r   r   r   )r   r   r   r	   _read_short   s    r   c             C   s8   yt d| dd S  t jk
r2   td Y nX d S )Nz>Hr   r   )r   r   r   r   r   )r   r   r   r	   _read_ushort   s    r   c             C   s@   t | d}|dkrd}n
| |}|d@ dkr<| d}|S )N   r       )ordr   )r   lengthdatadummyr   r   r	   _read_string   s    

r   gc             C   s   t | }d}|dk r d}|d }t| }t| }||  krN|  krNdkrXn nd} n0|dkrft} n"|d }|d | td	|d
  } ||  S )Nr   r   i   g        i  i?  l        g       @?   )r   r   	_HUGE_VALpow)fexponsignhimantlomantr   r   r	   _read_float   s    "r&   c             C   s   |  td| d S )Nz>h)writer   pack)r!   xr   r   r	   _write_short   s    r*   c             C   s   |  td| d S )Nz>H)r'   r   r(   )r!   r)   r   r   r	   _write_ushort   s    r+   c             C   s   |  td| d S )Nz>l)r'   r   r(   )r!   r)   r   r   r	   _write_long   s    r,   c             C   s   |  td| d S )Nz>L)r'   r   r(   )r!   r)   r   r   r	   _write_ulong   s    r-   c             C   sR   t |dkrtd| tdt | | | t |d@ dkrN| d d S )N   z%string exceeds maximum pstring lengthBr   r       )len
ValueErrorr'   r   r(   )r!   sr   r   r	   _write_string   s    
r4   c       	      C   s   dd l }|dk rd}|d }nd}|dkr8d}d}d}n||\}}|dks^|dks^||krp|dB }d}d}nh|d }|dk r|||}d}||B }||d}||}t|}||| d}||}t|}t| | t| | t| | d S )	Nr   i   r   i @  r   i  i?      )mathZfrexpZldexpZfloorintr+   r-   )	r!   r)   r6   r#   r"   r$   r%   ZfmantZfsmantr   r   r	   _write_float   s8    




r8   )Chunk)
namedtuple_aifc_paramsz7nchannels sampwidth framerate nframes comptype compnamez3Number of audio channels (1 for mono, 2 for stereo)zSample width in byteszSampling frequencyzNumber of audio framesz(Compression type ("NONE" for AIFF files)zRA human-readable version of the compression type
('not compressed' for AIFF files)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dS )2	Aifc_readNc             C   s8  d| _ d | _g | _d| _|| _t|}| dkr:td|d}|dkrTd| _	n|dkrdd| _	ntdd| _
d | _xd| _yt| j}W n tk
r   P Y nX | }|d	kr| | d| _
nH|d
kr|| _|d}d| _n(|dkrt|| _ n|dkr| | |  qzW | j
r,| js4tdd S )Nr   s   FORMz file does not start with FORM idr
   s   AIFFs   AIFCr   znot an AIFF or AIFF-C files   COMMs   SSND   s   FVERs   MARKz$COMM chunk and/or SSND chunk missing)_version_convert_markers	_soundpos_filer9   Zgetnamer   r   _aifcZ_comm_chunk_read_ssnd_chunk_ssnd_seek_neededr   _read_comm_chunkr   	_readmarkskip)selfr   chunkZformdataZ	chunknamer   r   r   r	   initfp4  sJ    




zAifc_read.initfpc             C   sL   t |tr>t|d}y| | W qH   |   Y qHX n
| | d S )Nrb)
isinstancestrbuiltinsr   rK   close)rI   r!   file_objectr   r   r	   __init__\  s    

zAifc_read.__init__c             C   s   | S )Nr   )rI   r   r   r	   	__enter__h  s    zAifc_read.__enter__c             G   s   |    d S )N)rP   )rI   argsr   r   r	   __exit__k  s    zAifc_read.__exit__c             C   s   | j S )N)rB   )rI   r   r   r	   getfpq  s    zAifc_read.getfpc             C   s   d| _ d| _d S )Nr   r   )rE   rA   )rI   r   r   r	   rewindt  s    zAifc_read.rewindc             C   s    | j }|d k	rd | _ |  d S )N)rB   rP   )rI   r   r   r   r	   rP   x  s    zAifc_read.closec             C   s   | j S )N)rA   )rI   r   r   r	   tell~  s    zAifc_read.tellc             C   s   | j S )N)
_nchannels)rI   r   r   r	   getnchannels  s    zAifc_read.getnchannelsc             C   s   | j S )N)_nframes)rI   r   r   r	   
getnframes  s    zAifc_read.getnframesc             C   s   | j S )N)
_sampwidth)rI   r   r   r	   getsampwidth  s    zAifc_read.getsampwidthc             C   s   | j S )N)
_framerate)rI   r   r   r	   getframerate  s    zAifc_read.getframeratec             C   s   | j S )N)	_comptype)rI   r   r   r	   getcomptype  s    zAifc_read.getcomptypec             C   s   | j S )N)	_compname)rI   r   r   r	   getcompname  s    zAifc_read.getcompnamec             C   s*   t |  |  |  |  |  |  S )N)r;   rZ   r^   r`   r\   rb   rd   )rI   r   r   r	   	getparams  s    zAifc_read.getparamsc             C   s   t | jdkrd S | jS )Nr   )r1   r@   )rI   r   r   r	   
getmarkers  s    zAifc_read.getmarkersc             C   s2   x| j D ]}||d kr|S qW td|d S )Nr   zmarker {0!r} does not exist)r@   r   format)rI   idmarkerr   r   r	   getmark  s    zAifc_read.getmarkc             C   s*   |dk s|| j krtd|| _d| _d S )Nr   zposition not in ranger   )r[   r   rA   rE   )rI   posr   r   r	   setpos  s    zAifc_read.setposc             C   s   | j rD| jd | jd}| j| j }|r>| j|d  d| _ |dkrPdS | j|| j }| jrv|rv| |}| jt|| j| j	   | _|S )Nr   r=   r   )
rE   rD   seekr   rA   
_framesizer?   r1   rY   r]   )rI   nframesr   rk   r   r   r   r	   
readframes  s    

zAifc_read.readframesc             C   s   dd l }||dS )Nr   r   )audioopZalaw2lin)rI   r   rq   r   r   r	   	_alaw2lin  s    zAifc_read._alaw2linc             C   s   dd l }||dS )Nr   r   )rq   Zulaw2lin)rI   r   rq   r   r   r	   	_ulaw2lin  s    zAifc_read._ulaw2linc             C   s2   dd l }t| dsd | _||d| j\}| _|S )Nr   _adpcmstater   )rq   hasattrrt   Z	adpcm2lin)rI   r   rq   r   r   r	   
_adpcm2lin  s
    
zAifc_read._adpcm2linc             C   sV  t || _t|| _t |d d | _tt|| _| jdkrFtd| jdkrXtd| j| j | _	| j
rFd}|jdkrd}td d	|_|d
| _|rt|jd}|d@ dkr|d }|j| |_|jdd t|| _| jdkrR| jdkr
| j| _n4| jdkr | j| _n| jdkr6| j| _ntdd| _nd| _d| _d S )N   r=   r   zbad sample widthzbad # of channels   r   zWarning: bad COMM chunk size   r
   r   s   NONEs   G722)s   ulaws   ULAW)s   alaws   ALAWzunsupported compression typer   s   not compressed)r   rY   r   r[   r]   r7   r&   r_   r   rn   rC   Z	chunksizewarningswarnr   ra   r   r   rm   r   rc   rv   r?   rs   rr   )rI   rJ   Zkludger   r   r   r	   rF     sD    









zAifc_read._read_comm_chunkc             C   s   t |}yHxBt|D ]6}t |}t|}t|}|s8|r| j|||f qW W nD tk
r   dt| jt| jdkr|dnd|f }t	| Y nX d S )Nz;Warning: MARK chunk contains only %s marker%s instead of %sr    r3   )
r   ranger   r   r@   appendr   r1   rz   r{   )rI   rJ   Znmarkersirh   rk   namewr   r   r	   rG     s    zAifc_read._readmark)r   r   r   rB   rK   rR   rS   rU   rV   rW   rP   rX   rZ   r\   r^   r`   rb   rd   re   rf   rj   rl   rp   rr   rs   rv   rF   rG   r   r   r   r	   r<     s2   $(*r<   c               @   s0  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:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dS )J
Aifc_writeNc             C   s\   t |trNt|d}y| | W n   |   Y nX |drXd| _n
| | d S )Nwbz.aiffr   )rM   rN   rO   r   rK   rP   endswithrC   )rI   r!   rQ   r   r   r	   rR   /  s    

zAifc_write.__init__c             C   s^   || _ t| _d| _d| _d | _d| _d| _d| _d| _	d| _
d| _d| _g | _d| _d| _d S )Ns   NONEs   not compressedr   r   )rB   _AIFC_versionr>   ra   rc   r?   rY   r]   r_   r[   _nframeswritten_datawritten_datalengthr@   _marklengthrC   )rI   r   r   r   r	   rK   ?  s    zAifc_write.initfpc             C   s   |    d S )N)rP   )rI   r   r   r	   __del__P  s    zAifc_write.__del__c             C   s   | S )Nr   )rI   r   r   r	   rS   S  s    zAifc_write.__enter__c             G   s   |    d S )N)rP   )rI   rT   r   r   r	   rU   V  s    zAifc_write.__exit__c             C   s   | j rtdd| _d S )Nz0cannot change parameters after starting to writer   )r   r   rC   )rI   r   r   r	   aiff\  s    zAifc_write.aiffc             C   s   | j rtdd| _d S )Nz0cannot change parameters after starting to writer   )r   r   rC   )rI   r   r   r	   aifca  s    zAifc_write.aifcc             C   s(   | j rtd|dk rtd|| _d S )Nz0cannot change parameters after starting to writer   zbad # of channels)r   r   rY   )rI   	nchannelsr   r   r	   setnchannelsf  s
    zAifc_write.setnchannelsc             C   s   | j std| j S )Nznumber of channels not set)rY   r   )rI   r   r   r	   rZ   m  s    zAifc_write.getnchannelsc             C   s0   | j rtd|dk s|dkr&td|| _d S )Nz0cannot change parameters after starting to writer   r
   zbad sample width)r   r   r]   )rI   	sampwidthr   r   r	   setsampwidthr  s
    zAifc_write.setsampwidthc             C   s   | j std| j S )Nzsample width not set)r]   r   )rI   r   r   r	   r^   y  s    zAifc_write.getsampwidthc             C   s(   | j rtd|dkrtd|| _d S )Nz0cannot change parameters after starting to writer   zbad frame rate)r   r   r_   )rI   	framerater   r   r	   setframerate~  s
    zAifc_write.setframeratec             C   s   | j std| j S )Nzframe rate not set)r_   r   )rI   r   r   r	   r`     s    zAifc_write.getframeratec             C   s   | j rtd|| _d S )Nz0cannot change parameters after starting to write)r   r   r[   )rI   ro   r   r   r	   
setnframes  s    zAifc_write.setnframesc             C   s   | j S )N)r   )rI   r   r   r	   r\     s    zAifc_write.getnframesc             C   s.   | j rtd|dkrtd|| _|| _d S )Nz0cannot change parameters after starting to write)s   NONEs   ulaws   ULAWs   alaws   ALAWs   G722zunsupported compression type)r   r   ra   rc   )rI   comptypecompnamer   r   r	   setcomptype  s    zAifc_write.setcomptypec             C   s   | j S )N)ra   )rI   r   r   r	   rb     s    zAifc_write.getcomptypec             C   s   | j S )N)rc   )rI   r   r   r	   rd     s    zAifc_write.getcompnamec             C   sf   |\}}}}}}| j rtd|dkr.td| | | | | | | | | || d S )Nz0cannot change parameters after starting to write)s   NONEs   ulaws   ULAWs   alaws   ALAWs   G722zunsupported compression type)r   r   r   r   r   r   r   )rI   paramsr   r   r   ro   r   r   r   r   r	   	setparams  s    



zAifc_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)rY   r]   r_   r   r;   r[   ra   rc   )rI   r   r   r	   re     s    zAifc_write.getparamsc             C   s   |dkrt d|dk r t dt|ts2t dx<tt| jD ]*}|| j| d krB|||f| j|< d S qBW | j|||f d S )Nr   zmarker ID must be > 0zmarker position must be >= 0zmarker name must be bytes)r   rM   bytesr}   r1   r@   r~   )rI   rh   rk   r   r   r   r   r	   setmark  s    
zAifc_write.setmarkc             C   s2   x| j D ]}||d kr|S qW td|d S )Nr   zmarker {0!r} does not exist)r@   r   rg   )rI   rh   ri   r   r   r	   rj     s    zAifc_write.getmarkc             C   s   t | jdkrd S | jS )Nr   )r1   r@   )rI   r   r   r	   rf     s    zAifc_write.getmarkersc             C   s   | j S )N)r   )rI   r   r   r	   rX     s    zAifc_write.tellc             C   sz   t |ttfst|d}| t| t|| j| j  }| j	rN| 	|}| j
| | j| | _| jt| | _d S )Nr/   )rM   r   	bytearray
memoryviewcast_ensure_header_writtenr1   r]   rY   r?   rB   r'   r   r   )rI   r   ro   r   r   r	   writeframesraw  s    
zAifc_write.writeframesrawc             C   s.   |  | | j| jks"| j| jkr*|   d S )N)r   r   r[   r   r   _patchheader)rI   r   r   r   r	   writeframes  s    
zAifc_write.writeframesc             C   s   | j d krd S z^| d | jd@ r<| j d | jd | _|   | j| jksb| j| jksb| jrj| 	  W d d | _
| j }d | _ |  X d S )Nr   r   r0   )rB   r   r   r'   _writemarkersr   r[   r   r   r   r?   rP   )rI   r!   r   r   r	   rP     s     


zAifc_write.closec             C   s   dd l }||dS )Nr   r   )rq   Zlin2alaw)rI   r   rq   r   r   r	   	_lin2alaw  s    zAifc_write._lin2alawc             C   s   dd l }||dS )Nr   r   )rq   Zlin2ulaw)rI   r   rq   r   r   r	   	_lin2ulaw  s    zAifc_write._lin2ulawc             C   s2   dd l }t| dsd | _||d| j\}| _|S )Nr   rt   r   )rq   ru   rt   Z	lin2adpcm)rI   r   rq   r   r   r	   
_lin2adpcm  s
    
zAifc_write._lin2adpcmc             C   sf   | j sb| jdkr.| jsd| _| jdkr.td| js<td| jsJtd| jsXtd| | d S )N)s   ULAWs   ulaws   ALAWs   alaws   G722r   zRsample width must be 2 when compressing with ulaw/ULAW, alaw/ALAW or G7.22 (ADPCM)z# channels not specifiedzsample width not specifiedzsampling rate not specified)r   ra   r]   r   rY   r_   _write_header)rI   Zdatasizer   r   r	   r     s    

z!Aifc_write._ensure_header_writtenc             C   s>   | j dkr| j| _n&| j dkr(| j| _n| j dkr:| j| _d S )Ns   G722)s   ulaws   ULAW)s   alaws   ALAW)ra   r   r?   r   r   )rI   r   r   r	   _init_compression  s    




zAifc_write._init_compressionc          	   C   sJ  | j r| jdkr|   | jd | js<|| j| j  | _| j| j | j | _| jd@ rf| jd | _| j r| jdkr| jd | _| jd@ r| jd | _n0| jdkr| jd d | _| jd@ r| jd | _y| j	 | _
W n ttfk
r   d | _
Y nX | | j}| j rB| jd	 | jd
 t| jd t| j| j n| jd | jd t| j| t| j| j | j
d k	r| j	 | _t| j| j | jdkrt| jd nt| j| jd  t| j| j | j r| j| j t| j| j | jd | j
d k	r| j	 | _t| j| jd  t| jd t| jd d S )Ns   NONEs   FORMr   )s   ulaws   ULAWs   alaws   ALAWr   s   G722   r
   s   AIFCs   FVERs   AIFFs   COMM)s   ULAWs   ulaws   ALAWs   alaws   G722r=   s   SSNDr   )rC   ra   r   rB   r'   r[   rY   r]   r   rX   _form_length_posAttributeErrorOSError_write_form_lengthr-   r>   r*   _nframes_posr8   r_   r4   rc   _ssnd_length_pos)rI   Z
initlength
commlengthr   r   r	   r   %  s^    




zAifc_write._write_headerc             C   s\   | j r*dt| j }|d@ r$|d }d}nd}d}t| jd| | j d | d |  |S )	Nry   r      rx   r   r
   r=      )rC   r1   rc   r-   rB   r   )rI   
datalengthr   Z
verslengthr   r   r	   r   X  s     zAifc_write._write_form_lengthc             C   s   | j  }| jd@ r,| jd }| j d n| j}|| jkrd| j| jkrd| jdkrd| j |d d S | j | j	d | 
|}| j | jd t| j | j | j | jd t| j |d  | j |d | j| _|| _d S )Nr   r0   r   r=   )rB   rX   r   r'   r   r[   r   r   rm   r   r   r   r-   r   )rI   Zcurposr   r   r   r   r	   r   e  s&    





zAifc_write._patchheaderc             C   s   t | jdkrd S | jd d}xD| jD ]:}|\}}}|t | d d }t |d@ dkr*|d }q*W t| j| |d | _t| jt | j x<| jD ]2}|\}}}t| j| t| j| t| j| qW d S )Nr   s   MARKr   r      r=   )r1   r@   rB   r'   r-   r   r*   r4   )rI   r   ri   rh   rk   r   r   r   r	   r   {  s"    


zAifc_write._writemarkers)(r   r   r   rB   rR   rK   r   rS   rU   r   r   r   rZ   r   r^   r   r`   r   r\   r   rb   rd   r   re   r   rj   rf   rX   r   r   rP   r   r   r   r   r   r   r   r   r   r   r   r   r	   r     sJ   	3r   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moderL   )rrL   )r   r   z$mode must be 'r', 'rb', 'w', or 'wb')ru   r   r<   r   r   )r!   r   r   r   r	   r     s    
c             C   s   t jdtdd t| |dS )NzBaifc.openfp is deprecated since Python 3.7. Use aifc.open instead.r   )
stacklevel)r   )rz   r{   DeprecationWarningr   )r!   r   r   r   r	   r     s    
__main__r   z/usr/demos/data/audio/bach.aiffr   ZReadingznchannels =znframes   =zsampwidth =zframerate =zcomptype  =zcompname  =r   ZWritingr   i   zDone.)N)N)8r   rO   rz   __all__	Exceptionr   r   r   r   r   r   r   r   r&   r*   r+   r,   r-   r4   r8   rJ   r9   collectionsr:   r;   r   __doc__r   r   ro   r   r   r<   r   r   r   r   sysargvr~   fnr!   printrZ   r\   r^   r`   rb   rd   Zgngr   re   rp   r   r   r   r   r   r	   <module>   sz   

!     







