Pc@s5dZddlZddlZdddgZdefdYZdZdZd Zd Z d Z d Z d Z dZ dZdZdZddlmZdfdYZdfdYZedZeZedkr1ddlZejd r!ejjdnejdZeedZdGeGHdGejGHdGejGHdGej GHd Gej!GHd!Gej"GHd"Gej#GHejd#r.ejd#Z$d$Ge$GHee$d%Z%e%j&ej'x*ej(d&Z)e) rPne%j*e)qe%j+ej+d'GHnndS((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 | +-----------------+ | | +----+------------+ | | AIFC | | +------------+ | | | | | . | | | . | | | . | +----+------------+ 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 (AIFF-C only). MARK <# of markers> (2 bytes) list of markers: (2 bytes, must be > 0) (4 bytes) ("pstring") COMM <# of channels> (2 bytes) <# of sound frames> (4 bytes) (2 bytes) (10 bytes, IEEE 80-bit extended floating point) in AIFF-C files only: (4 bytes) ("pstring") SSND (4 bytes, not used by this program) (4 bytes, not used by this program) 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, ypu 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. iNtErrortopentopenfpcBseZRS((t__name__t __module__(((s..\python\lib\aifc.pyRsl@QEcCsBy!tjd|jddSWntjk r=tnXdS(Ns>lii(tstructtunpacktreadterrortEOFError(tfile((s..\python\lib\aifc.pyt _read_longs!cCsBy!tjd|jddSWntjk r=tnXdS(Ns>Lii(RRRRR (R ((s..\python\lib\aifc.pyt _read_ulongs!cCsBy!tjd|jddSWntjk r=tnXdS(Ns>hii(RRRRR (R ((s..\python\lib\aifc.pyt _read_shorts!cCs_t|jd}|dkr*d}n|j|}|d@dkr[|jd}n|S(Niit(tordR(R tlengthtdatatdummy((s..\python\lib\aifc.pyt _read_strings  gcCst|}d}|dkr1d}|d}nt|}t|}||kok|kokdknryd}n>|dkrt}n)|d}|d|td |d }||S( Niiiigii?lg@i?(R R t _HUGE_VALtpow(tftexpontsignthimanttlomant((s..\python\lib\aifc.pyt _read_floats     '    cCs|jtjd|dS(Ns>h(twriteRtpack(Rtx((s..\python\lib\aifc.pyt _write_shortscCs|jtjd|dS(Ns>L(RRR(RR((s..\python\lib\aifc.pyt _write_longscCswt|dkr!tdn|jtt||j|t|d@dkrs|jtdndS(Nis%string exceeds maximum pstring lengthii(tlent ValueErrorRtchr(Rts((s..\python\lib\aifc.pyt _write_strings  c Cs\ddl}|dkr+d}|d}nd}|dkrRd}d}d}n|j|\}}|dks|dkr|dB}d}d}n|d}|dkr|j||}d}n||B}|j|d}|j|}t|}|j||d}|j|}t|}t||t||t||dS( Niiii@iii?i (tmathtfrexptldexptfloortlongRR ( RRR&RRRRtfmanttfsmant((s..\python\lib\aifc.pyt _write_floats8               (tChunkt Aifc_readcBseZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZRS(cCsTd|_d|_d|_g|_d|_||_t|}|jdkr`t dn|j d}|dkrd|_ n!|dkrd|_ n t dd|_ xd|_ yt|j}Wntk rPnX|j}|d kr|j|d|_ nj|d krF||_|j d }d|_ n:|d krdt||_n|d kr|j|n|jq|j s|j rt dn|j rP|jrPddl}|jd|j|jd |j|jg}|jdkr|j|dt|tdkr-tj|d}n|j|dS(NRtrb(ttypet __builtin__RRU(RPR((s..\python\lib\aifc.pyt__init__KscCs|jS(N(R=(RP((s..\python\lib\aifc.pytgetfpTscCsd|_d|_dS(Nii(RAR<(RP((s..\python\lib\aifc.pytrewindWs cCs3|jr"|jjd|_n|jjdS(N(R9tCloseDecompressorR8R=tclose(RP((s..\python\lib\aifc.pyR][s   cCs|jS(N(R<(RP((s..\python\lib\aifc.pyttellascCs|jS(N(RL(RP((s..\python\lib\aifc.pyt getnchannelsdscCs|jS(N(t_nframes(RP((s..\python\lib\aifc.pyt getnframesgscCs|jS(N(RI(RP((s..\python\lib\aifc.pyt getsampwidthjscCs|jS(N(RK(RP((s..\python\lib\aifc.pyt getframeratemscCs|jS(N(t _comptype(RP((s..\python\lib\aifc.pyt getcomptypepscCs|jS(N(t _compname(RP((s..\python\lib\aifc.pyt getcompnamesscCs:|j|j|j|j|j|jfS(N(R_RbRcRaReRg(RP((s..\python\lib\aifc.pyt getparamsyscCs t|jdkrdS|jS(Ni(R!R;R8(RP((s..\python\lib\aifc.pyt getmarkers~scCs<x%|jD]}||dkr |Sq Wtd|fdS(Nismarker %r does not exist(R;R(RPtidtmarker((s..\python\lib\aifc.pytgetmarkscCs=|dks||jkr'tdn||_d|_dS(Nisposition not in rangei(R`RR<RA(RPtpos((s..\python\lib\aifc.pytsetposs  cCs|jrd|jjd|jjd}|j|j}|rX|jj|dnd|_n|dkrtdS|jj||j}|jr|r|j|}n|jt||j|j |_|S(NiiR( RARCtseekRR<t _framesizeR:R!RLRI(RPtnframesRRmR((s..\python\lib\aifc.pyt readframess   $cCsNddl}|jj|jt|d}|jjt||j|S(Nii(RFR9tSetParamtFRAME_BUFFER_SIZER!t DecompressRL(RPRRFR((s..\python\lib\aifc.pyt _decomp_datas  cCsddl}|j|dS(Nii(taudiooptulaw2lin(RPRRw((s..\python\lib\aifc.pyt _ulaw2lins cCsLddl}t|ds'd|_n|j|d|j\}|_|S(Nit _adpcmstatei(RwthasattrR8Rzt adpcm2lin(RPRRw((s..\python\lib\aifc.pyt _adpcm2lins    cCst||_t||_t|dd|_tt||_|j|j|_|j rd}|j dkrd}dGHd|_ n|j d|_ |rt |jj d}|d@dkr|d}n|j ||_ |jjd dnt||_|j d kr|j d kryyd dl}Wntk rUqyX|j|_|jd|_dSnyd dl}Wnptk r|j d kry0d dl}|j|_|jd |_dSWqtk rqXntdnX|j d kr'|j}|jd |_n4|j dkrR|j}|jd |_n td|j||_|j|_qnd |_ d|_dS(NiiiiisWarning: bad COMM chunk sizeiiitNONEtG722tULAWis#cannot read compressed AIFF-C filestALAWsunsupported compression typesnot compressed(R RLR R`RItintRRKRpR?t chunksizeRRdRR RoRRfRwt ImportErrorR}R:RFRyRt G711_ULAWt G711_ALAWtOpenDecompressorR9Rv(RPRQtkludgeRRwRFtscheme((s..\python\lib\aifc.pyRBsf              cCst|}ygx`t|D]R}t|}t|}t|}|sR|r|jj|||fqqWWnKtk rdGt|jGt|jdkrdGndGdG|GHnXdS(Ns!Warning: MARK chunk contains onlyiRktmarkerss instead of(R trangeR RR;tappendR R!(RPRQtnmarkerstiRjRmtname((s..\python\lib\aifc.pyRDs     $  (RRRURYRZR[R]R^R_RaRbRcReRgRhRiRlRnRrRvRyR}RBRD(((s..\python\lib\aifc.pyR/s,$ 4                  =t Aifc_writecBs:eZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#RS("cCsot|tdkr3|}tj|d}nd}|j||ddkrbd|_n d|_dS(NRtwbs???is.aiffii(RWRXRRUR?(RPRtfilename((s..\python\lib\aifc.pyRY's  cCs||_t|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ d|_ d|_g|_d|_d|_dS(NR~snot compressedii(R=t _AIFC_versionR7RdRfR8t_compR:RLRIRKR`t_nframeswrittent _datawrittent _datalengthR;t _marklengthR?(RPR ((s..\python\lib\aifc.pyRU4s                cCs|jr|jndS(N(R=R](RP((s..\python\lib\aifc.pyt__del__Fs cCs"|jrtdnd|_dS(Ns0cannot change parameters after starting to writei(RRR?(RP((s..\python\lib\aifc.pytaiffMs  cCs"|jrtdnd|_dS(Ns0cannot change parameters after starting to writei(RRR?(RP((s..\python\lib\aifc.pytaifcRs  cCs:|jrtdn|dkr-tdn||_dS(Ns0cannot change parameters after starting to writeisbad # of channels(RRRL(RPt nchannels((s..\python\lib\aifc.pyt setnchannelsWs     cCs|jstdn|jS(Nsnumber of channels not set(RLR(RP((s..\python\lib\aifc.pyR_^s  cCsF|jrtdn|dks-|dkr9tdn||_dS(Ns0cannot change parameters after starting to writeiisbad sample width(RRRI(RPt sampwidth((s..\python\lib\aifc.pyt setsampwidthcs    cCs|jstdn|jS(Nssample width not set(RIR(RP((s..\python\lib\aifc.pyRbjs  cCs:|jrtdn|dkr-tdn||_dS(Ns0cannot change parameters after starting to writeisbad frame rate(RRRK(RPt framerate((s..\python\lib\aifc.pyt setframerateos     cCs|jstdn|jS(Nsframe rate not set(RKR(RP((s..\python\lib\aifc.pyRcvs  cCs"|jrtdn||_dS(Ns0cannot change parameters after starting to write(RRR`(RPRq((s..\python\lib\aifc.pyt setnframes{s  cCs|jS(N(R(RP((s..\python\lib\aifc.pyRascCsC|jrtdn|dkr-tdn||_||_dS(Ns0cannot change parameters after starting to writeR~RRRsunsupported compression type(sNONEsULAWsALAWsG722(RRRdRf(RPtcomptypetcompname((s..\python\lib\aifc.pyt setcomptypes      cCs|jS(N(Rd(RP((s..\python\lib\aifc.pyRescCs|jS(N(Rf(RP((s..\python\lib\aifc.pyRgscCs|\}}}}}}|jr-tdn|dkrEtdn|j||j||j||j||j||dS(Ns0cannot change parameters after starting to writeR~RRRsunsupported compression type(sNONEsULAWsALAWsG722(RRRRRRR(RPtinfoRRRRqRR((s..\python\lib\aifc.pyt setparamss        cCsR|j s|j s|j r*tdn|j|j|j|j|j|jfS(Nsnot all parameters set(RLRIRKRR`RdRf(RP((s..\python\lib\aifc.pyRhs cCs|dkrtdn|dkr0tdnt|tdkrTtdnxNtt|jD]7}||j|dkrj|||f|j| 0smarker position must be >= 0Rsmarker name must be a string(RRWRR!R;R(RPRjRmRR((s..\python\lib\aifc.pytsetmarks     cCs<x%|jD]}||dkr |Sq Wtd|fdS(Nismarker %r does not exist(R;R(RPRjRk((s..\python\lib\aifc.pyRlscCs t|jdkrdS|jS(Ni(R!R;R8(RP((s..\python\lib\aifc.pyRiscCs|jS(N(R(RP((s..\python\lib\aifc.pyR^scCs|jt|t||j|j}|jrH|j|}n|jj||j||_|jt||_dS(N( t_ensure_header_writtenR!RIRLR:R=RRR(RPRRq((s..\python\lib\aifc.pytwriteframesraws cCsB|j||j|jks1|j|jkr>|jndS(N(RRR`RRt _patchheader(RPR((s..\python\lib\aifc.pyt writeframess cCs|jd|jd@rC|jjtd|jd|_n|j|j|jksz|j|jksz|j r|j n|j r|j j d|_ nd|_|jjdS(Nii(RRR=RR#t _writemarkersRR`RRRRtCloseCompressorR8R:R](RP((s..\python\lib\aifc.pyR]s         cCs^ddl}|jj|jt|}|jj|jt|}|jj|j|S(Ni(RFRRsRtR!tCOMPRESSED_BUFFER_SIZEtCompressR`(RPRRFR((s..\python\lib\aifc.pyt _comp_datas cCsddl}|j|dS(Nii(Rwtlin2ulaw(RPRRw((s..\python\lib\aifc.pyt _lin2ulaws cCsLddl}t|ds'd|_n|j|d|j\}|_|S(NiRzi(RwR{R8Rzt lin2adpcm(RPRRw((s..\python\lib\aifc.pyt _lin2adpcms    cCs|js|jd krK|js-d|_n|jdkrKtdqKn|jdkr|jsod|_n|jdkrtdqn|jstdn|jstdn|jstd n|j|ndS( NRRis9sample width must be 2 when compressing with ULAW or ALAWRs:sample width must be 2 when compressing with G7.22 (ADPCM)s# channels not specifiedssample width not specifiedssampling rate not specified(sULAWsALAW(RRdRIRRLRKt _write_header(RPtdatasize((s..\python\lib\aifc.pyRs$           c Cs|jdkr|j|_dSyddl}Wn`tk r|jdkry ddl}|j|_dSWqtk rqXntdnX|jdkr|j}n$|jdkr|j }n td|j ||_ |j d|j |jd|j|j|jd |jd g }|jd kr?|j|d s`           !