Lc @s=dZddkZddkZdddgZdefdYZdZdZd Zd Z d Z d Z d Z dZ dZdZdZddklZdfdYZdfdYZedZeZedjoDddkZeid oeiidneidZeedZdGeGHdGeiGHdGeiGHdGei GHd Gei!GHd!Gei"GHd"Gei#GHeid#oeid#Z$d$Ge$GHee$d%Z%e%i&ei'x,ei(d&Z)e) oPne%i*e)qe%i+ei+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/usr/lib64/python2.6/aifc.pyRsl@QEcCsDy!tid|iddSWntij o tnXdS(Ns>lii(tstructtunpacktreadterrortEOFError(tfile((s/usr/lib64/python2.6/aifc.pyt _read_longs!cCsDy!tid|iddSWntij o tnXdS(Ns>Lii(RRRRR (R ((s/usr/lib64/python2.6/aifc.pyt _read_ulongs!cCsDy!tid|iddSWntij o tnXdS(Ns>hii(RRRRR (R ((s/usr/lib64/python2.6/aifc.pyt _read_shorts!cCsct|id}|djo d}n|i|}|d@djo|id}n|S(Niit(tordR(R tlengthtdatatdummy((s/usr/lib64/python2.6/aifc.pyt _read_strings  gcCst|}d}|djod}|d}nt|}t|}||jo|jo djno d}nA|djo t}n*|d}|d|td |d }||S( Niiiigii?lg@i?(R R t _HUGE_VALtpow(tftexpontsignthimanttlomant((s/usr/lib64/python2.6/aifc.pyt _read_floats    *    cCs|itid|dS(Ns>h(twriteRtpack(Rtx((s/usr/lib64/python2.6/aifc.pyt _write_shortscCs|itid|dS(Ns>L(RRR(RR((s/usr/lib64/python2.6/aifc.pyt _write_longscCs{t|djotdn|itt||i|t|d@djo|itdndS(Nis%string exceeds maximum pstring lengthii(tlent ValueErrorRtchr(Rts((s/usr/lib64/python2.6/aifc.pyt _write_strings  c Cseddk}|djod}|d}nd}|djod}d}d}n|i|\}}|djp |djo|dB}d}d}n|d}|djo|i||}d}n||B}|i|d}|i|}t|}|i||d}|i|}t|}t||t||t||dS( Niiii@iii?i (tmathtfrexptldexptfloortlongRR ( RRR&RRRRtfmanttfsmant((s/usr/lib64/python2.6/aifc.pyt _write_floats8              (tChunkt Aifc_readcBseZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZRS(cCsnd|_d|_d|_g|_d|_||_t|}|idjo t dn|i d}|djo d|_ n$|djo d|_ n t dd|_ xd|_ yt|i}Wntj oPnX|i}|d jo|i|d|_ nq|d jo%||_|i d }d|_ n?|d jot||_n|d jo|i|n|iq|i p |i o t dn|i o|ioddk}|id|i|id |i|ig}|idjo|i|dx'|iD]}||djo|Sq Wtd|fdS(Nismarker %r does not exist(R;R(RPtidtmarker((s/usr/lib64/python2.6/aifc.pytgetmarks   cCs@|djp||ijo tdn||_d|_dS(Nisposition not in rangei(R`RR<RA(RPtpos((s/usr/lib64/python2.6/aifc.pytsetposs  cCs|io^|iid|iid}|i|i}|o|ii|dnd|_n|djodS|ii||i}|io|o|i|}n|it||i|i |_|S(NiiR( RARCtseekRR<t _framesizeR:R!RLRI(RPtnframesRRmR((s/usr/lib64/python2.6/aifc.pyt readframess   $cCsNddk}|ii|it|d}|iit||i|S(Nii(RFR9tSetParamtFRAME_BUFFER_SIZER!t DecompressRL(RPRRFR((s/usr/lib64/python2.6/aifc.pyt _decomp_datas  cCsddk}|i|dS(Nii(taudiooptulaw2lin(RPRRw((s/usr/lib64/python2.6/aifc.pyt _ulaw2lins cCsNddk}t|dp d|_n|i|d|i\}|_|S(Nit _adpcmstatei(RwthasattrR8Rzt adpcm2lin(RPRRw((s/usr/lib64/python2.6/aifc.pyt _adpcm2lins    c Cst||_t||_t|dd|_tt||_|i|i|_|i o0d}|i djod}dGHd|_ n|i d|_ |o^t |ii d}|d@djo|d}n|i ||_ |iid dnt||_|i d joh|i d joJyd dk}Wntj oqX|i|_|id|_dSnyd dk}Wnvtj oj|i d joJy0d dk}|i|_|id |_dSWqtj oqXntdnX|i d jo|i}|id |_n7|i djo|i}|id |_n td|i||_|i|_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/usr/lib64/python2.6/aifc.pyRBsf          cCst|}yjxct|D]U}t|}t|}t|}|p|o|ii|||fqqWWnOtj oCdGt|iGt|idjodGndGdG|GHnXdS(Ns!Warning: MARK chunk contains onlyiRktmarkerss instead of(R trangeR RR;tappendR R!(RPRQtnmarkerstiRjRmtname((s/usr/lib64/python2.6/aifc.pyRDs      % (RRRURYRZR[R]R^R_RaRbRcReRgRhRiRlRnRrRvRyR}RBRD(((s/usr/lib64/python2.6/aifc.pyR/s,$ 4                  =t Aifc_writecBs:eZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#RS("cCsst|tdjo|}ti|d}nd}|i||ddjo d|_n d|_dS(NRtwbs???is.aiffii(RWRXRRUR?(RPRtfilename((s/usr/lib64/python2.6/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/usr/lib64/python2.6/aifc.pyRU4s                cCs|io|indS(N(R=R](RP((s/usr/lib64/python2.6/aifc.pyt__del__Fs cCs$|io tdnd|_dS(Ns0cannot change parameters after starting to writei(RRR?(RP((s/usr/lib64/python2.6/aifc.pytaiffMs  cCs$|io tdnd|_dS(Ns0cannot change parameters after starting to writei(RRR?(RP((s/usr/lib64/python2.6/aifc.pytaifcRs  cCs>|io tdn|djo tdn||_dS(Ns0cannot change parameters after starting to writeisbad # of channels(RRRL(RPt nchannels((s/usr/lib64/python2.6/aifc.pyt setnchannelsWs     cCs|ip tdn|iS(Nsnumber of channels not set(RLR(RP((s/usr/lib64/python2.6/aifc.pyR_^s  cCsK|io tdn|djp |djo tdn||_dS(Ns0cannot change parameters after starting to writeiisbad sample width(RRRI(RPt sampwidth((s/usr/lib64/python2.6/aifc.pyt setsampwidthcs    cCs|ip tdn|iS(Nssample width not set(RIR(RP((s/usr/lib64/python2.6/aifc.pyRbjs  cCs>|io tdn|djo tdn||_dS(Ns0cannot change parameters after starting to writeisbad frame rate(RRRK(RPt framerate((s/usr/lib64/python2.6/aifc.pyt setframerateos     cCs|ip tdn|iS(Nsframe rate not set(RKR(RP((s/usr/lib64/python2.6/aifc.pyRcvs  cCs$|io tdn||_dS(Ns0cannot change parameters after starting to write(RRR`(RPRq((s/usr/lib64/python2.6/aifc.pyt setnframes{s  cCs|iS(N(R(RP((s/usr/lib64/python2.6/aifc.pyRascCsG|io tdn|djo tdn||_||_dS(Ns0cannot change parameters after starting to writeR~RRRsunsupported compression type(sNONEsULAWsALAWsG722(RRRdRf(RPtcomptypetcompname((s/usr/lib64/python2.6/aifc.pyt setcomptypes      cCs|iS(N(Rd(RP((s/usr/lib64/python2.6/aifc.pyRescCs|iS(N(Rf(RP((s/usr/lib64/python2.6/aifc.pyRgscCs|\}}}}}}|io tdn|djo tdn|i||i||i||i||i||dS(Ns0cannot change parameters after starting to writeR~RRRsunsupported compression type(sNONEsULAWsALAWsG722(RRRRRRR(RPtinfoRRRRqRR((s/usr/lib64/python2.6/aifc.pyt setparamss        cCsV|i p|i p |i o tdn|i|i|i|i|i|ifS(Nsnot all parameters set(RLRIRKRR`RdRf(RP((s/usr/lib64/python2.6/aifc.pyRhs! cCs|djo tdn|djo tdnt|tdjo tdnxPtt|iD]9}||i|djo|||f|i| 0smarker position must be >= 0Rsmarker name must be a string(RRWRR!R;R(RPRjRmRR((s/usr/lib64/python2.6/aifc.pytsetmarks      cCs>x'|iD]}||djo|Sq Wtd|fdS(Nismarker %r does not exist(R;R(RPRjRk((s/usr/lib64/python2.6/aifc.pyRls   cCs"t|idjodS|iS(Ni(R!R;R8(RP((s/usr/lib64/python2.6/aifc.pyRiscCs|iS(N(R(RP((s/usr/lib64/python2.6/aifc.pyR^scCs|it|t||i|i}|io|i|}n|ii||i||_|it||_dS(N( t_ensure_header_writtenR!RIRLR:R=RRR(RPRRq((s/usr/lib64/python2.6/aifc.pytwriteframesraws cCsE|i||i|ijp|i|ijo|indS(N(RRR`RRt _patchheader(RPR((s/usr/lib64/python2.6/aifc.pyt writeframess cCs|id|id@o*|iitd|id|_n|i|i|ijp|i|ijp |i o|i n|i o|i i d|_ n|iidS(Nii(RRR=RR#t _writemarkersRR`RRRRtCloseCompressorR8R](RP((s/usr/lib64/python2.6/aifc.pyR]s      cCs^ddk}|ii|it|}|ii|it|}|ii|i|S(Ni(RFRRsRtR!tCOMPRESSED_BUFFER_SIZEtCompressR`(RPRRFR((s/usr/lib64/python2.6/aifc.pyt _comp_datas cCsddk}|i|dS(Nii(Rwtlin2ulaw(RPRRw((s/usr/lib64/python2.6/aifc.pyt _lin2ulaws cCsNddk}t|dp d|_n|i|d|i\}|_|S(NiRzi(RwR{R8Rzt lin2adpcm(RPRRw((s/usr/lib64/python2.6/aifc.pyt _lin2adpcms    cCs|ip|id jo8|ip d|_n|idjo tdqRn|idjo8|ip d|_n|idjo tdqn|ip tdn|ip tdn|ip td n|i|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/usr/lib64/python2.6/aifc.pyRs$           cCs|idjo|i|_dSyddk}Wnftj oZ|idjo:y ddk}|i|_dSWqtj oqXntdnX|idjo |i}n'|idjo |i }n td|i ||_ |i d|i |id|i|i|id |id g }|id jo|i|d s`           !