Your IP : 3.145.42.140


Current Path : /opt/alt/python35/lib64/python3.5/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/__pycache__/aifc.cpython-35.opt-2.pyc



���]�|�@s�ddlZddlZddlZdddgZGdd�de�ZdZdd�Zd	d
�Zdd�Z	d
d�Z
dd�ZdZdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd lmZdd!lmZed"d#�ZGd$d%�d%�ZGd&d'�d'�Zdd(d�ZeZed)kr�ddlZejd*d�r�ejj d+�ejd*Z!ee!d,��Z"e#d-e!�e#d.e"j$��e#d/e"j%��e#d0e"j&��e#d1e"j'��e#d2e"j(��e#d3e"j)��ejd4d�r�ejd4Z*e#d5e*�ee*d6��EZ+e+j,e"j-��x(e"j.d7�Z/e/r�Pe+j0e/�q~WWdQRXe#d8�WdQRXdS)9�N�Error�open�openfpc@seZdZdS)rN)�__name__�
__module__�__qualname__�rr�)/opt/alt/python35/lib64/python3.5/aifc.pyr�sl@QEcCsCy!tjd|jd��dSWntjk
r>t�YnXdS)Nz>l�r)�struct�unpack�read�error�EOFError)�filerrr	�
_read_long�s!rcCsCy!tjd|jd��dSWntjk
r>t�YnXdS)Nz>Lr
r)rrr
rr)rrrr	�_read_ulong�s!rcCsCy!tjd|jd��dSWntjk
r>t�YnXdS)Nz>h�r)rrr
rr)rrrr	�_read_short�s!rcCsCy!tjd|jd��dSWntjk
r>t�YnXdS)Nz>Hrr)rrr
rr)rrrr	�_read_ushort�s!rcCs\t|jd��}|dkr*d}n|j|�}|d@dkrX|jd�}|S)N�r�)�ordr
)r�length�data�dummyrrr	�_read_string�s	rg�����cCs�t|�}d}|dkr.d
}|d}t|�}t|�}||koh|kohdknrvd}n>|dkr�t}n)|d}|d|td|d	�}||S)Nrri�gi�i�?lg@�?���)rr�	_HUGE_VAL�pow)�f�expon�sign�himant�lomantrrr	�_read_float�s
'		
r&cCs|jtjd|��dS)Nz>h)�writer�pack)r!�xrrr	�_write_short�sr*cCs|jtjd|��dS)Nz>H)r'rr()r!r)rrr	�
_write_ushort�sr+cCs|jtjd|��dS)Nz>l)r'rr()r!r)rrr	�_write_long�sr,cCs|jtjd|��dS)Nz>L)r'rr()r!r)rrr	�_write_ulong�sr-cCsqt|�dkrtd��|jtjdt|���|j|�t|�d@dkrm|jd�dS)N�z%string exceeds maximum pstring length�Brrs)�len�
ValueErrorr'rr()r!�srrr	�
_write_string�s
r3c	Cseddl}|dkr+d}|d}nd}|dkrRd}d}d}n�|j|�\}}|dks�|dks�||kr�|dB}d}d}n�|d}|dkr�|j||�}d}||B}|j|d�}|j|�}t|�}|j||d�}|j|�}t|�}t||�t||�t||�dS)	Nri�ri@i�i�?� r)�math�frexp�ldexp�floor�intr+r-)	r!r)r5r#r"r$r%ZfmantZfsmantrrr	�_write_float�s8
	$
	



r:)�Chunk)�
namedtuple�_aifc_paramsz7nchannels sampwidth framerate nframes comptype compnamec@s6eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�ZdS)2�	Aifc_readNcCs�d|_d|_g|_d|_||_t|�}|j�dkrWtd��|jd�}|dkr~d|_	n$|dkr�d|_	ntd��d|_
x�d|_yt|j�}Wntk
r�PYnX|j�}|d	kr|j
|�d|_
ng|d
krA||_|jd�}d|_n7|dkr_t|�|_n|d
krx|j|�|j�q�W|j
s�|jr�td��dS)NrsFORMz file does not start with FORM idr
sAIFFsAIFCrznot an AIFF or AIFF-C filesCOMMsSSND�sFVERsMARKz$COMM chunk and/or SSND chunk missing)�_version�_convert�_markers�	_soundpos�_filer;Zgetnamerr
�_aifcZ_comm_chunk_read�_ssnd_seek_neededr�_read_comm_chunk�_ssnd_chunkr�	_readmark�skip)�selfr�chunkZformdataZ	chunknamerrrr	�initfp+sH							

	
zAifc_read.initfpc	Cs^t|t�rMtj|d�}y|j|�WqZ|j��YqZXn
|j|�dS)N�rb)�
isinstance�str�builtinsrrM�close)rKr!�file_objectrrr	�__init__Rs
zAifc_read.__init__cCs|S)Nr)rKrrr	�	__enter__^szAifc_read.__enter__cGs|j�dS)N)rR)rK�argsrrr	�__exit__aszAifc_read.__exit__cCs|jS)N)rD)rKrrr	�getfpgszAifc_read.getfpcCsd|_d|_dS)Nrr)rFrC)rKrrr	�rewindjs	zAifc_read.rewindcCs,|j}|dk	r(d|_|j�dS)N)rDrR)rKrrrr	rRns		zAifc_read.closecCs|jS)N)rC)rKrrr	�telltszAifc_read.tellcCs|jS)N)�
_nchannels)rKrrr	�getnchannelswszAifc_read.getnchannelscCs|jS)N)�_nframes)rKrrr	�
getnframeszszAifc_read.getnframescCs|jS)N)�
_sampwidth)rKrrr	�getsampwidth}szAifc_read.getsampwidthcCs|jS)N)�
_framerate)rKrrr	�getframerate�szAifc_read.getframeratecCs|jS)N)�	_comptype)rKrrr	�getcomptype�szAifc_read.getcomptypecCs|jS)N)�	_compname)rKrrr	�getcompname�szAifc_read.getcompnamecCs=t|j�|j�|j�|j�|j�|j��S)N)r=r\r`rbr^rdrf)rKrrr	�	getparams�szAifc_read.getparamscCs t|j�dkrdS|jS)Nr)r0rB)rKrrr	�
getmarkers�szAifc_read.getmarkerscCsAx%|jD]}||dkr
|Sq
Wtdj|���dS)Nrzmarker {0!r} does not exist)rBr�format)rK�id�markerrrr	�getmark�szAifc_read.getmarkcCs=|dks||jkr'td��||_d|_dS)Nrzposition not in ranger)r]rrCrF)rK�posrrr	�setpos�s	zAifc_read.setposcCs�|jr^|jjd�|jjd�}|j|j}|rU|jj|d�d|_|dkrndS|jj||j�}|jr�|r�|j|�}|jt|�|j|j	|_|S)Nrr?r)
rFrH�seekr
rC�
_framesizerAr0r[r_)rK�nframesrrmrrrr	�
readframes�s		zAifc_read.readframescCsddl}|j|d�S)Nrr)�audioopZalaw2lin)rKrrsrrr	�	_alaw2lin�szAifc_read._alaw2lincCsddl}|j|d�S)Nrr)rsZulaw2lin)rKrrsrrr	�	_ulaw2lin�szAifc_read._ulaw2lincCsIddl}t|d�s$d|_|j|d|j�\}|_|S)Nr�_adpcmstater)rs�hasattrrvZ	adpcm2lin)rKrrsrrr	�
_adpcm2lin�s
	!zAifc_read._adpcm2lincCs�t|�|_t|�|_t|�dd|_tt|��|_|j|j|_|j	r�d}|j
dkr�d}tjd�d|_
|j
d�|_|rt|jj
d��}|d@dkr�|d}|j
||_
|jjdd�t|�|_|jd	kr�|jd
kr@|j|_nH|jdkr^|j|_n*|jdkr||j|_ntd��d|_nd	|_d|_dS)N�r?r�rzWarning: bad COMM chunk size�r
sNONEsG722�ulaw�ULAW�alaw�ALAWzunsupported compression typersnot compressedr)r|r})r~r)rr[rr]r_r9r&rarprE�	chunksize�warnings�warnr
rcrrrorrerxrArurtr)rKrLZkludgerrrr	rG�s<	
	
	zAifc_read._read_comm_chunkcCs�t|�}ydx]t|�D]O}t|�}t|�}t|�}|sR|r|jj|||f�qWWnVtk
r�dt|j�t|j�dkr�dnd|f}tj	|�YnXdS)Nz;Warning: MARK chunk contains only %s marker%s instead of %sr�r2)
r�rangerrrB�appendrr0r�r�)rKrLZnmarkers�irjrm�name�wrrr	rI�s!
*
zAifc_read._readmark)rrrrDrMrTrUrWrXrYrRrZr\r^r`rbrdrfrgrhrlrnrrrtrurxrGrIrrrr	r>s2$'&r>c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Z d<d=�Z!d>d?�Z"d@dA�Z#dBdC�Z$dDdE�Z%dFdG�Z&dHdI�Z'dS)J�
Aifc_writeNc	Csvt|t�retj|d�}y|j|�Wn|j��YnX|jd�rrd|_n
|j|�dS)N�wbz.aiffr)rOrPrQrrMrR�endswithrE)rKr!rSrrr	rT!s
zAifc_write.__init__cCs�||_t|_d|_d|_d|_d|_d|_d|_d|_	d|_
d|_d|_g|_
d|_d|_dS)NsNONEsnot compressedrr)rD�
_AIFC_versionr@rcrerAr[r_rar]�_nframeswritten�_datawritten�_datalengthrB�_marklengthrE)rKrrrr	rM1s														zAifc_write.initfpcCs|j�dS)N)rR)rKrrr	�__del__BszAifc_write.__del__cCs|S)Nr)rKrrr	rUEszAifc_write.__enter__cGs|j�dS)N)rR)rKrVrrr	rWHszAifc_write.__exit__cCs"|jrtd��d|_dS)Nz0cannot change parameters after starting to writer)r�rrE)rKrrr	�aiffNs	zAifc_write.aiffcCs"|jrtd��d|_dS)Nz0cannot change parameters after starting to writer)r�rrE)rKrrr	�aifcSs	zAifc_write.aifccCs:|jrtd��|dkr-td��||_dS)Nz0cannot change parameters after starting to writerzbad # of channels)r�rr[)rK�	nchannelsrrr	�setnchannelsXs
	zAifc_write.setnchannelscCs|jstd��|jS)Nznumber of channels not set)r[r)rKrrr	r\_s	zAifc_write.getnchannelscCsF|jrtd��|dks-|dkr9td��||_dS)Nz0cannot change parameters after starting to writerr
zbad sample width)r�rr_)rK�	sampwidthrrr	�setsampwidthds
	zAifc_write.setsampwidthcCs|jstd��|jS)Nzsample width not set)r_r)rKrrr	r`ks	zAifc_write.getsampwidthcCs:|jrtd��|dkr-td��||_dS)Nz0cannot change parameters after starting to writerzbad frame rate)r�rra)rK�	frameraterrr	�setframerateps
	zAifc_write.setframeratecCs|jstd��|jS)Nzframe rate not set)rar)rKrrr	rbws	zAifc_write.getframeratecCs"|jrtd��||_dS)Nz0cannot change parameters after starting to write)r�rr])rKrqrrr	�
setnframes|s	zAifc_write.setnframescCs|jS)N)r�)rKrrr	r^�szAifc_write.getnframescCsC|jrtd��|d	kr-td��||_||_dS)
Nz0cannot change parameters after starting to write�NONE�ulaw�ULAW�alaw�ALAW�G722zunsupported compression type)r�r�r�r�r�r�)r�rrcre)rK�comptype�compnamerrr	�setcomptype�s			zAifc_write.setcomptypecCs|jS)N)rc)rKrrr	rd�szAifc_write.getcomptypecCs|jS)N)re)rKrrr	rf�szAifc_write.getcompnamecCs�|\}}}}}}|jr-td��|d	krEtd��|j|�|j|�|j|�|j|�|j||�dS)
Nz0cannot change parameters after starting to write�NONE�ulaw�ULAW�alaw�ALAW�G722zunsupported compression type)r�r�r�r�r�r�)r�rr�r�r�r�r�)rK�paramsr�r�r�rqr�r�rrr	�	setparams�s		



zAifc_write.setparamscCsU|js|js|jr*td��t|j|j|j|j|j|j�S)Nznot all parameters set)r[r_rarr=r]rcre)rKrrr	rg�szAifc_write.getparamscCs�|dkrtd��|dkr0td��t|t�sKtd��xNtt|j��D]7}||j|dkra|||f|j|<dSqaW|jj|||f�dS)Nrzmarker ID must be > 0zmarker position must be >= 0zmarker name must be bytes)rrO�bytesr�r0rBr�)rKrjrmr�r�rrr	�setmark�szAifc_write.setmarkcCsAx%|jD]}||dkr
|Sq
Wtdj|���dS)Nrzmarker {0!r} does not exist)rBrri)rKrjrkrrr	rl�szAifc_write.getmarkcCs t|j�dkrdS|jS)Nr)r0rB)rKrrr	rh�szAifc_write.getmarkerscCs|jS)N)r�)rKrrr	rZ�szAifc_write.tellcCs�t|ttf�s*t|�jd�}|jt|��t|�|j|j}|j	ro|j	|�}|j
j|�|j||_|j
t|�|_
dS)Nr/)rOr��	bytearray�
memoryview�cast�_ensure_header_writtenr0r_r[rArDr'r�r�)rKrrqrrr	�writeframesraw�s	zAifc_write.writeframesrawcCs?|j|�|j|jks1|j|jkr;|j�dS)N)r�r�r]r�r��_patchheader)rKrrrr	�writeframes�s
zAifc_write.writeframescCs�|jdkrdSz|jd�|jd@rP|jjd�|jd|_|j�|j|jks�|j|jks�|jr�|j	�Wdd|_
|j}d|_|j�XdS)Nrrs)rDr�r�r'�
_writemarkersr�r]r�r�r�rArR)rKr!rrr	rR�s 


				zAifc_write.closecCsddl}|j|d�S)Nrr)rsZlin2alaw)rKrrsrrr	�	_lin2alaw�szAifc_write._lin2alawcCsddl}|j|d�S)Nrr)rsZlin2ulaw)rKrrsrrr	�	_lin2ulaw�szAifc_write._lin2ulawcCsIddl}t|d�s$d|_|j|d|j�\}|_|S)Nrrvr)rsrwrvZ	lin2adpcm)rKrrsrrr	�
_lin2adpcm�s
	!zAifc_write._lin2adpcmcCs�|js�|jdkrE|js*d|_|jdkrEtd��|jsZtd��|jsotd	��|js�td
��|j|�dS)N�ULAW�ulaw�ALAW�alaw�G722rzRsample width must be 2 when compressing with ulaw/ULAW, alaw/ALAW or G7.22 (ADPCM)z# channels not specifiedzsample width not specifiedzsampling rate not specified)r�r�r�r�r�)r�rcr_rr[ra�
_write_header)rKZdatasizerrr	r��s						z!Aifc_write._ensure_header_writtencCs[|jdkr|j|_n9|jdkr<|j|_n|jdkrW|j|_dS)NsG722�ulaw�ULAW�alaw�ALAW)r�r�)r�r�)rcr�rAr�r�)rKrrr	�_init_compressionszAifc_write._init_compressioncCs
|jr"|jdkr"|j�|jjd�|jsR||j|j|_|j|j|j|_|jd@r�|jd|_|jr|jdkr�|jd|_|jd@r|jd|_n@|jd	kr|jd
d|_|jd@r|jd|_y|jj	�|_
Wn!ttfk
rJd|_
YnX|j
|j�}|jr�|jjd�|jjd
�t|jd�t|j|j�n|jjd�|jjd�t|j|�t|j|j�|j
dk	r|jj	�|_t|j|j�|jdkrEt|jd�nt|j|jd�t|j|j�|jr�|jj|j�t|j|j�|jjd�|j
dk	r�|jj	�|_t|j|jd�t|jd�t|jd�dS)NsNONEsFORMr�ulaw�ULAW�alaw�ALAWr�G722�r
sAIFCsFVERsAIFFsCOMMr?sSSNDr)r�r�r�r�)r�r�r�r�r�)rErcr�rDr'r]r[r_r�rZ�_form_length_pos�AttributeError�OSError�_write_form_lengthr-r@r*�_nframes_posr:rar3re�_ssnd_length_pos)rKZ
initlength�
commlengthrrr	r�s^
	
	

		zAifc_write._write_headercCst|jr9d	t|j�}|d@r0|d}d}nd}d}t|jd||jd|d|�|S)
Nrz�r�rr
r?�r{)rEr0rer-rDr�)rK�
datalengthr�Z
verslengthrrr	r�Js	

		"zAifc_write._write_form_lengthcCs0|jj�}|jd@r<|jd}|jjd�n	|j}||jkr�|j|jkr�|jdkr�|jj|d�dS|jj|j	d�|j
|�}|jj|jd�t|j|j�|jj|j
d�t|j|d�|jj|d�|j|_||_dS)Nrsrr?)rDrZr�r'r�r]r�r�ror�r�r�r-r�)rKZcurposr�rrrr	r�Ws&

	zAifc_write._patchheadercCst|j�dkrdS|jjd�d}xX|jD]M}|\}}}|t|�dd}t|�d@dkr9|d}q9Wt|j|�|d|_t|jt|j��xP|jD]E}|\}}}t|j|�t|j|�t|j|�q�WdS)NrsMARKrr�r?)r0rBrDr'r-r�r*r3)rKrrkrjrmr�rrr	r�ms"
zAifc_write._writemarkers)(rrrrDrTrMr�rUrWr�r�r�r\r�r`r�rbr�r^r�rdrfr�rgr�rlrhrZr�r�rRr�r�r�r�r�r�r�r�r�rrrr	r�sJ	

3
r�cCsi|dkr-t|d�r'|j}nd}|dkrCt|�S|dkrYt|�Std��dS)	N�moderN�rr�r�z$mode must be 'r', 'rb', 'w', or 'wb')r�rN)r�r�)rwr�r>r�r)r!r�rrr	r�s

�__main__rz/usr/demos/data/audio/bach.aiffr�ZReadingznchannels =znframes   =zsampwidth =zframerate =zcomptype  =zcompname  =rZWritingr�izDone.)1rrQr��__all__�	Exceptionrr�rrrrrrr&r*r+r,r-r3r:rLr;�collectionsr<r=r>r�rrr�sys�argvr��fnr!�printr\r^r`rbrdrfZgn�gr�rgrrrr�rrrr	�<module>�sf
!	���






?>