�`^c@s`dZddlZddlZdddgZdefd��YZdZd�Zd �Zd
�Z d�Z
d�Zd
Zd�Z
d�Zd�Zd�Zd�Zd�Zd�ZddlmZdfd��YZdfd��YZed�ZeZedkr\ddlZejdr<ejjd�nejdZeed�Z z�GeGHd Ge j!�GHd!Ge j"�GHd"Ge j#�GHd#Ge j$�GHd$Ge j%�GHd%Ge j&�GHejd&rJejd&Z'd'Ge'GHee'd(�Z(zDe(j)e j*��x*e j+d)�Z,e,r#Pne(j-e,�q Wde(j.�Xd*GHnWde j.�XndS(+sDStuff to parse AIFF-C and AIFF files.
Unless explicitly stated otherwise, the description below is true
both for AIFF-C files and AIFF files.
An AIFF-C file has the following structure.
+-----------------+
| FORM |
+-----------------+
| <size> |
+----+------------+
| | AIFC |
| +------------+
| | <chunks> |
| | . |
| | . |
| | . |
+----+------------+
An AIFF file has the string "AIFF" instead of "AIFC".
A chunk consists of an identifier (4 bytes) followed by a size (4 bytes,
big endian order), followed by the data. The size field does not include
the size of the 8 byte header.
The following chunk types are recognized.
FVER
<version number of AIFF-C defining document> (AIFF-C only).
MARK
<# of markers> (2 bytes)
list of markers:
<marker ID> (2 bytes, must be > 0)
<position> (4 bytes)
<marker name> ("pstring")
COMM
<# of channels> (2 bytes)
<# of sound frames> (4 bytes)
<size of the samples> (2 bytes)
<sampling frequency> (10 bytes, IEEE 80-bit extended
floating point)
in AIFF-C files only:
<compression type> (4 bytes)
<human-readable version of compression type> ("pstring")
SSND
<offset> (4 bytes, not used by this program)
<blocksize> (4 bytes, not used by this program)
<sound data>
A pstring consists of 1 byte length, a string of characters, and 0 or 1
byte pad to make the total length even.
Usage.
Reading AIFF files:
f = aifc.open(file, 'r')
where file is either the name of a file or an open file pointer.
The open file pointer must have methods read(), seek(), and close().
In some types of audio files, if the setpos() method is not used,
the seek() method is not necessary.
This returns an instance of a class with the following public methods:
getnchannels() -- returns number of audio channels (1 for
mono, 2 for stereo)
getsampwidth() -- returns sample width in bytes
getframerate() -- returns sampling frequency
getnframes() -- returns number of audio frames
getcomptype() -- returns compression type ('NONE' for AIFF files)
getcompname() -- returns human-readable version of
compression type ('not compressed' for AIFF files)
getparams() -- returns a tuple consisting of all of the
above in the above order
getmarkers() -- get the list of marks in the audio file or None
if there are no marks
getmark(id) -- get mark with the specified id (raises an error
if the mark does not exist)
readframes(n) -- returns at most n frames of audio
rewind() -- rewind to the beginning of the audio stream
setpos(pos) -- seek to the specified position
tell() -- return the current position
close() -- close the instance (make it unusable)
The position returned by tell(), the position given to setpos() and
the position of marks are all compatible and have nothing to do with
the actual position in the file.
The close() method is called automatically when the class instance
is destroyed.
Writing AIFF files:
f = aifc.open(file, 'w')
where file is either the name of a file or an open file pointer.
The open file pointer must have methods write(), tell(), seek(), and
close().
This returns an instance of a class with the following public methods:
aiff() -- create an AIFF file (AIFF-C default)
aifc() -- create an AIFF-C file
setnchannels(n) -- set the number of channels
setsampwidth(n) -- set the sample width
setframerate(n) -- set the frame rate
setnframes(n) -- set the number of frames
setcomptype(type, name)
-- set the compression type and the
human-readable compression type
setparams(tuple)
-- set all parameters at once
setmark(id, pos, name)
-- add specified mark to the list of marks
tell() -- return current position in output file (useful
in combination with setmark())
writeframesraw(data)
-- write audio frames without pathing up the
file header
writeframes(data)
-- write audio frames and patch up the file header
close() -- patch up the file header and close the
output file
You should set the parameters before the first writeframesraw or
writeframes. The total number of frames does not need to be set,
but when it is set to the correct value, the header does not have to
be patched up.
It is best to first set all parameters, perhaps possibly the
compression type, and then write audio frames using writeframesraw.
When all frames have been written, either call writeframes('') or
close() to patch up the sizes in the header.
Marks can be added anytime. If there are any marks, you must call
close() after all frames have been written.
The close() method is called automatically when the class instance
is destroyed.
When a file is opened with the extension '.aiff', an AIFF file is
written, otherwise an AIFF-C file is written. This default can be
changed by calling aiff() or aifc() before the first writeframes or
writeframesraw.
i�NtErrortopentopenfpcBseZRS((t__name__t
__module__(((s/sys/lib/python2.7/aifc.pyR�sl@QEcCsBy!tjd|jd��dSWntjk
r=t�nXdS(Ns>lii(tstructtunpacktreadterrortEOFError(tfile((s/sys/lib/python2.7/aifc.pyt
_read_long�s!cCsBy!tjd|jd��dSWntjk
r=t�nXdS(Ns>Lii(RRRRR (R
((s/sys/lib/python2.7/aifc.pyt_read_ulong�s!cCsBy!tjd|jd��dSWntjk
r=t�nXdS(Ns>hii(RRRRR (R
((s/sys/lib/python2.7/aifc.pyt_read_short�s!cCsBy!tjd|jd��dSWntjk
r=t�nXdS(Ns>Hii(RRRRR (R
((s/sys/lib/python2.7/aifc.pyt_read_ushort�s!cCs_t|jd��}|dkr*d}n|j|�}|d@dkr[|jd�}n|S(Niit(tordR(R
tlengthtdatatdummy((s/sys/lib/python2.7/aifc.pyt_read_string�s g��cCs�t|�}d}|dkr1d}|d}nt|�}t|�}||kok|kokdknryd}n>|dkr�t}n)|d}|d|td |d
�}||S(Niii�i�gi�i�lg@i?(R
Rt _HUGE_VALtpow(tftexpontsignthimanttlomant((s/sys/lib/python2.7/aifc.pyt_read_float�s
'
cCs|jtjd|��dS(Ns>h(twriteRtpack(Rtx((s/sys/lib/python2.7/aifc.pyt_write_short�scCs|jtjd|��dS(Ns>H(RRR(RR((s/sys/lib/python2.7/aifc.pyt
_write_ushort�scCs|jtjd|��dS(Ns>l(RRR(RR((s/sys/lib/python2.7/aifc.pyt_write_long�scCs|jtjd|��dS(Ns>L(RRR(RR((s/sys/lib/python2.7/aifc.pyt_write_ulong�scCs}t|�dkr!td��n|jtjdt|���|j|�t|�d@dkry|jtd��ndS(Ni�s%string exceeds maximum pstring lengthtBii(tlent
ValueErrorRRRtchr(Rts((s/sys/lib/python2.7/aifc.pyt
_write_string�s
c Cshddl}|dkr+d}|d}nd}|dkrRd}d}d}n�j|�\}}|dks�|dks�||kr�|dB}d}d}n�|d}|dkr�|j||�}d}n||B}|j|d�}|j|�}t|�}|j||d�}|j|�}t|�}t||�t||�t||�dS( Ni�ii�i@ii�i�i (tmathtfrexptldexptfloortlongR!R#( RRR*RRRRtfmanttfsmant((s/sys/lib/python2.7/aifc.pyt_write_float�s8
$
(tChunkt Aifc_readcBs�eZd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z
d �Zd
�Zd�Z
d�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZRS(cCsTd|_d|_d|_g|_d|_||_t|�}|j�dkr`t d�n|j
d�}|dkr�d|_n!|dkr�d|_n t d�d|_x�d|_
yt|j�}Wntk
r�PnX|j�}|d kr|j|�d|_nj|d
krF||_|j
d�}d|_
n:|dkrdt|�|_n|d
kr�|j|�n|j�q�|js�|jr�t d�n|jrP|jrPddl}|jd|j|jd|j|jg}|jdkr|j|d<n(|jdkr4|j|d<n t d�|jj|�ndS(NitFORMs file does not start with FORM iditAIFFtAIFCisnot an AIFF or AIFF-C filetCOMMtSSNDitFVERtMARKs$COMM chunk and/or SSND chunk missingi�is$cannot compress more than 2 channels(t_versiontNonet_decompt_convertt_markerst _soundpost_fileR2tgetnameRRt_aifct_comm_chunk_readt_ssnd_seek_neededR t_read_comm_chunkt_ssnd_chunkRt _readmarktskiptcltORIGINAL_FORMATtBITS_PER_COMPONENTt
_sampwidtht
FRAME_RATEt
_frameratet
_nchannelstMONOtSTEREO_INTERLEAVEDt SetParams(tselfR
tchunktformdatat chunknameRRJtparams((s/sys/lib/python2.7/aifc.pytinitfp#s`
cCs>t|�td�kr-tj|d�}n|j|�dS(NRtrb(ttypet__builtin__RRY(RTR((s/sys/lib/python2.7/aifc.pyt__init__WscCs|jS(N(RA(RT((s/sys/lib/python2.7/aifc.pytgetfp`scCsd|_d|_dS(Nii(RER@(RT((s/sys/lib/python2.7/aifc.pytrewindcs cCs3|jr"|jj�d|_n|jj�dS(N(R=tCloseDecompressorR<RAtclose(RT((s/sys/lib/python2.7/aifc.pyRags
cCs|jS(N(R@(RT((s/sys/lib/python2.7/aifc.pyttellmscCs|jS(N(RP(RT((s/sys/lib/python2.7/aifc.pytgetnchannelspscCs|jS(N(t_nframes(RT((s/sys/lib/python2.7/aifc.pyt
getnframessscCs|jS(N(RM(RT((s/sys/lib/python2.7/aifc.pytgetsampwidthvscCs|jS(N(RO(RT((s/sys/lib/python2.7/aifc.pytgetframerateyscCs|jS(N(t _comptype(RT((s/sys/lib/python2.7/aifc.pytgetcomptype|scCs|jS(N(t _compname(RT((s/sys/lib/python2.7/aifc.pytgetcompnamescCs:|j�|j�|j�|j�|j�|j�fS(N(RcRfRgReRiRk(RT((s/sys/lib/python2.7/aifc.pyt getparams�scCs t|j�dkrdS|jS(Ni(R%R?R<(RT((s/sys/lib/python2.7/aifc.pyt
getmarkers�scCs<x%|jD]}||dkr
|Sq
Wtd|f�dS(Nismarker %r does not exist(R?R(RTtidtmarker((s/sys/lib/python2.7/aifc.pytgetmark�scCs=|dks||jkr'td�n||_d|_dS(Nisposition not in rangei(RdRR@RE(RTtpos((s/sys/lib/python2.7/aifc.pytsetpos�s cCs�|jrd|jjd�|jjd�}|j|j}|rX|jj|d�nd|_n|dkrtdS|jj||j�}|jr�|r�|j|�}n|jt|�|j|j |_|S(NiiR(
RERGtseekRR@t
_framesizeR>R%RPRM(RTtnframesRRqR((s/sys/lib/python2.7/aifc.pyt
readframes�s $cCsNddl}|jj|jt|�d�}|jjt|�|j|�S(Ni�i(RJR=tSetParamtFRAME_BUFFER_SIZER%t
DecompressRP(RTRRJR((s/sys/lib/python2.7/aifc.pyt_decomp_data�s
cCsddl}|j|d�S(Ni�i(taudiooptulaw2lin(RTRR{((s/sys/lib/python2.7/aifc.pyt _ulaw2lin�scCsLddl}t|d�s'd|_n|j|d|j�\}|_|S(Ni�t_adpcmstatei(R{thasattrR<R~t adpcm2lin(RTRR{((s/sys/lib/python2.7/aifc.pyt
_adpcm2lin�scCspt|�|_t|�|_t|�dd|_tt|��|_|j|j|_|j rZd}|j
dkr�d}dGHd|_
n|jd�|_|rt
|jjd��}|d@dkr�|d}n|j
||_
|jjd d�nt|�|_|jd
krl|jdkrryd dl}Wntk
rUqrX|j|_d|_dSnyd dl}Wnitk
r�|jdkr�)d dl}|j|_d|_dSWq�tk
r�q�ntd�nX|jdkr |j}n$|jdkr$|j}n td�|j|�|_|j|_d|_qlnd
|_d|_dS(NiiiiisWarning: bad COMM chunk sizeiii�tNONEtG722itULAWtulaws#cannot read compressed AIFF-C filestALAWtalawsunsupported compression typesnot compressed(R�R�(R�R�(R�R�(R
RPRRdRMtintRRORtRCt chunksizeRRhRR
RsRRjR{tImportErrorR�R>RJR}Rt G711_ULAWt G711_ALAWtOpenDecompressorR=Rz(RTRUtkludgeRR{RJtscheme((s/sys/lib/python2.7/aifc.pyRF�sd
cCs�t|�}ygx`t|�D]R}t|�}t|�}t|�}|sR|r|jj|||f�qqWWnKtk
r�dGt|j�Gt|j�dkr�dGndGdG|GHnXdS(Ns!Warning: MARK chunk contains onlyiRotmarkerss
instead of(R
trangeRRR?tappendR R%(RTRUtnmarkerstiRnRqtname((s/sys/lib/python2.7/aifc.pyRH�s$
(RRRYR]R^R_RaRbRcReRfRgRiRkRlRmRpRrRvRzR}R�RFRH(((s/sys/lib/python2.7/aifc.pyR3�s,$ 4 <t
Aifc_writecBs:eZd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z
d �Zd
�Zd�Z
d�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z!d �Z"d!�Z#RS("cCsot|�td�kr3|}tj|d�}nd}|j|�|ddkrbd|_n d|_dS(NRtwbs???i�s.aiffii(R[R\RRYRC(RTRtfilename((s/sys/lib/python2.7/aifc.pyR]2s
cCs�||_t|_d|_d|_d|_d|_d|_d|_ d|_
d|_d|_d|_
d|_g|_d|_d|_dS(NR�snot compressedii(RAt
_AIFC_versionR;RhRjR<t_compR>RPRMRORdt_nframeswrittent_datawrittent_datalengthR?t_marklengthRC(RTR
((s/sys/lib/python2.7/aifc.pyRY?s cCs|jr|j�ndS(N(RARa(RT((s/sys/lib/python2.7/aifc.pyt__del__Qs cCs"|jrtd�nd|_dS(Ns0cannot change parameters after starting to writei(R�RRC(RT((s/sys/lib/python2.7/aifc.pytaiffXs cCs"|jrtd�nd|_dS(Ns0cannot change parameters after starting to writei(R�RRC(RT((s/sys/lib/python2.7/aifc.pytaifc]s cCs:|jrtd�n|dkr-td�n||_dS(Ns0cannot change parameters after starting to writeisbad # of channels(R�RRP(RTt nchannels((s/sys/lib/python2.7/aifc.pytsetnchannelsbs
cCs|jstd�n|jS(Nsnumber of channels not set(RPR(RT((s/sys/lib/python2.7/aifc.pyRcis cCsF|jrtd�n|dks-|dkr9td�n||_dS(Ns0cannot change parameters after starting to writeiisbad sample width(R�RRM(RTt sampwidth((s/sys/lib/python2.7/aifc.pytsetsampwidthns
cCs|jstd�n|jS(Nssample width not set(RMR(RT((s/sys/lib/python2.7/aifc.pyRfus cCs:|jrtd�n|dkr-td�n||_dS(Ns0cannot change parameters after starting to writeisbad frame rate(R�RRO(RTt framerate((s/sys/lib/python2.7/aifc.pytsetframeratezs
cCs|jstd�n|jS(Nsframe rate not set(ROR(RT((s/sys/lib/python2.7/aifc.pyRg�s cCs"|jrtd�n||_dS(Ns0cannot change parameters after starting to write(R�RRd(RTRu((s/sys/lib/python2.7/aifc.pyt
setnframes�s cCs|jS(N(R�(RT((s/sys/lib/python2.7/aifc.pyRe�scCsC|jrtd�n|d kr-td�n||_||_dS(
Ns0cannot change parameters after starting to writeR�R�R�R�R�R�sunsupported compression type(sNONEsULAWsulawsALAWsalawsG722(R�RRhRj(RTtcomptypetcompname((s/sys/lib/python2.7/aifc.pytsetcomptype�s cCs|jS(N(Rh(RT((s/sys/lib/python2.7/aifc.pyRi�scCs|jS(N(Rj(RT((s/sys/lib/python2.7/aifc.pyRk�scCs�|\}}}}}}|jr-td�n|d krEtd�n|j|�|j|�|j|�|j|�|j||�dS(
Ns0cannot change parameters after starting to writeR�R�R�R�R�R�sunsupported compression type(sNONEsULAWsulawsALAWsalawsG722(R�RR�R�R�R�R�(RTtinfoR�R�R�RuR�R�((s/sys/lib/python2.7/aifc.pyt setparams�s
cCsR|js|js|jr*td�n|j|j|j|j|j|jfS(Nsnot all parameters set(RPRMRORRdRhRj(RT((s/sys/lib/python2.7/aifc.pyRl�scCs�|dkrtd�n|dkr0td�nt|�td�krTtd�nxNtt|j��D]7}||j|dkrj|||f|j|<dSqjW|jj|||f�dS(Nismarker ID must be > 0smarker position must be >= 0Rsmarker name must be a string(RR[R�R%R?R�(RTRnRqR�R�((s/sys/lib/python2.7/aifc.pytsetmark�scCs<x%|jD]}||dkr
|Sq
Wtd|f�dS(Nismarker %r does not exist(R?R(RTRnRo((s/sys/lib/python2.7/aifc.pyRp�scCs t|j�dkrdS|jS(Ni(R%R?R<(RT((s/sys/lib/python2.7/aifc.pyRm�scCs|jS(N(R�(RT((s/sys/lib/python2.7/aifc.pyRb�scCs�|jt|��t|�|j|j}|jrH|j|�}n|jj|�|j||_|jt|�|_dS(N( t_ensure_header_writtenR%RMRPR>RARR�R�(RTRRu((s/sys/lib/python2.7/aifc.pytwriteframesraw�s cCsB|j|�|j|jks1|j|jkr>|j�ndS(N(R�R�RdR�R�t_patchheader(RTR((s/sys/lib/python2.7/aifc.pytwriteframes�s
cCs�|jdkrdSz�|jd�|jd@rY|jjtd��|jd|_n|j�|j|jks�|j |jks�|j
r�|j�n|jr�|jj
�d|_nWdd|_|j}d|_|j�XdS(Nii(RAR<R�R�RR't
_writemarkersR�RdR�R�R�R�tCloseCompressorR>Ra(RTR((s/sys/lib/python2.7/aifc.pyRa�s&
cCs^ddl}|jj|jt|��}|jj|jt|��}|jj|j|�S(Ni�(RJR�RwRxR%tCOMPRESSED_BUFFER_SIZEtCompressRd(RTRRJR((s/sys/lib/python2.7/aifc.pyt
_comp_data�scCsddl}|j|d�S(Ni�i(R{tlin2ulaw(RTRR{((s/sys/lib/python2.7/aifc.pyt _lin2ulaw�scCsLddl}t|d�s'd|_n|j|d|j�\}|_|S(Ni�R~i(R{RR<R~t lin2adpcm(RTRR{((s/sys/lib/python2.7/aifc.pyt
_lin2adpcmscCs�|js�|jdkrK|js-d|_n|jdkrKtd�qKn|jdkr�|jsod|_n|jdkr�td�q�n|js�td �n|js�td
�n|js�td�n|j|�ndS(
NR�R�R�R�is9sample width must be 2 when compressing with ULAW or ALAWR�s:sample width must be 2 when compressing with G7.22 (ADPCM)s# channels not specifiedssample width not specifiedssampling rate not specified(sULAWsulawsALAWsalaw(R�RhRMRRPROt
_write_header(RTtdatasize((s/sys/lib/python2.7/aifc.pyR�s$ c
Cs�|jdkr|j|_dSyddl}Wn`tk
r�|jdkr�y ddl}|j|_dSWq�tk
r�q�Xntd�nX|jdkr�|j}n$|jdkr�|j }n td�|j
|�|_|jd |j
|jd
|j|j|jd|jdg
}|jdkr?|j|d<n(|jd
kr^|j|d<n td�|jj|�|jjd d�}|j|_dS(NR�i�R�R�s$cannot write compressed AIFF-C filesR�R�sunsupported compression typeiiidiis$cannot compress more than 2 channelsR(sULAWsulaw(sULAWsulaw(sALAWsalaw(RhR�R>RJR�R{R�RR�R�tOpenCompressorR�RKRLRMRNRORxR�RPRQRRRSR�R�(RTRJR{R�RXR((s/sys/lib/python2.7/aifc.pyt_init_compressionsB
cCs'|jr%|jdkr%|j�n|jjd�|jsX||j|j|_n|j|j|j|_|jd@r�|jd|_n|jr&|jdkr�|jd|_|jd@r#|jd|_q#q&|jd kr&|jd
d|_|jd@r#|jd|_q#q&ny|jj �|_
Wn ttfk
r^d|_
nX|j|j�}|jr�|jjd�|jjd
�t|jd�t|j|j�n|jjd�|jjd�t|j|�t|j|j�|j
dk r'|jj �|_nt|j|j�|jdkr\t|jd�nt|j|jd�t|j|j�|jr�|jj|j�t|j|j�n|jjd�|j
dk r�jj �|_nt|j|jd�t|jd�t|jd�dS(NR�R4iR�R�R�R�iR�iiR6R9R5R7iR8i(sULAWsulawsALAWsalaw(sULAWsulawsALAWsalawsG722(RCRhR�RARRdRPRMR�Rbt_form_length_postAttributeErrortIOErrorR<t_write_form_lengthR#R;R t_nframes_posR1ROR)Rjt_ssnd_length_pos(RTt
initlengtht
commlength((s/sys/lib/python2.7/aifc.pyR�Es^
cCsw|jr<d t|j�}|d@r3|d}nd}nd}d}t|jd||jd|d|�|S(
Niiiiiiiii(RCR%RjR#RAR�(RTt
datalengthR�t
verslength((s/sys/lib/python2.7/aifc.pyR�xs
"cCs6|jj�}|jd@rB|jd}|jjtd��n |j}||jkr�|j|jkr�|jdkr�|jj |d�dS|jj |j
d�|j|�}|jj |jd�t
|j|j�|jj |jd�t
|j|d�|jj |d�|j|_||_dS(Niii(RARbR�RR'R�RdR�R�RsR�R�R�R#R�(RTtcurposR�R((s/sys/lib/python2.7/aifc.pyR��s&
cCst|j�dkrdS|jjd�d}x[|jD]P}|\}}}|t|�dd}t|�d@dkr9|d}q9q9Wt|j|�|d|_t|jt|j��xP|jD]E}|\}}}t|j|�t|j|�t|j|�q�WdS(NiR:iiii(R%R?RARR#R�R R)(RTRRoRnRqR�((s/sys/lib/python2.7/aifc.pyR��s"
($RRR]RYR�R�R�R�RcR�RfR�RgR�ReR�RiRkR�RlR�RpRmRbR�R�RaR�R�R�R�R�R�R�R�R�(((s/sys/lib/python2.7/aifc.pyR�sD
& 3
cCsi|dkr0t|d�r'|j}q0d}n|dkrFt|�S|dkr\t|�Std�dS( NtmodeRZtrtwR�s$mode must be 'r', 'rb', 'w', or 'wb'(R�srb(R�swb(R<RR�R3R�R(RR�((s/sys/lib/python2.7/aifc.pyR�s
t__main__is/usr/demos/data/audio/bach.aiffR�tReadingsnchannels =snframes =ssampwidth =sframerate =scomptype =scompname =itWritingR�isDone.(/t__doc__RR\t__all__t ExceptionRR�RRR
RRRRR R!R"R#R)R1RUR2R3R�R<RRRtsystargvR�tfnRRcReRfRgRiRktgntgR�RlRvRR�Ra(((s/sys/lib/python2.7/aifc.pyt<module>�sj
!��
|