m %UIc @s[dZdkZdkZdddgZdefdYZdZddd d d d d ddf ZdZdZ dZ dZ dZ dZ dZdZdZdZdklZdfdYZdfdYZedZeZed joJdkZeid! oeiid"neid!Zeed#Zd$GeGHd%GeiGHd&Gei GHd'Gei!GHd(Gei"GHd)Gei#GHd*Gei$GHeid+oeid+Z%d,Ge%GHee%d-Z&e&i'ei(x5d!o-ei)d.Z*e* oPne&i+e*qWe&i,ei,d/GHnndS(0sDStuff 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. NtErrortopentopenfpcBstZRS(N(t__name__t __module__(((t!/data/zmath/lib/python2.4/aifc.pyRsl@QEtCOMTtINSTtMIDItAESDtAPPLtNAMEtAUTHs(c) tANNOcCsDy!tid|iddSWntij o tnXdS(Ns>lii(tstructtunpacktfiletreadterrortEOFError(R((Rt _read_longs!cCsDy!tid|iddSWntij o tnXdS(Ns>Lii(RRRRRR(R((Rt _read_ulongs!cCsDy!tid|iddSWntij o tnXdS(Ns>hii(RRRRRR(R((Rt _read_shorts!cCsct|id}|djo d}n|i|}|d@djo|id}n|S(Niit(tordRRtlengthtdatatdummy(RRRR((Rt _read_strings  f1.7976931348623099e+308cCst|}d}|djod}|d}nt|}t|}||jo|jo djno d}nA|djo t}n*|d}|d|td |d }||S( Niiiif0.0ii?lf2.0i?( RtftexpontsignRthimanttlomantt _HUGE_VALtpow(RRR R!R((Rt _read_floats    *    cCs|itid|dS(Ns>h(RtwriteRtpacktx(RR'((Rt _write_shortscCs|itid|dS(Ns>L(RR%RR&R'(RR'((Rt _write_longscCsX|itt||i|t|d@djo|itdndS(Nii(RR%tchrtlents(RR,((Rt _write_strings c Csbdk}|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 (tmathR'RRR R!tfrexptfmanttldexptfloortfsmanttlongR(RR)( RR'RR3R R!RR.R0((Rt _write_floats8              (sChunkt Aifc_readcBstZdZdZdZdZdZdZdZdZ d Z d Z d Z d Z d ZdZdZdZdZdZdZdZdZdZRS(NcCsd|_d|_d|_g|_d|_t||_ |i i djo t dn|i i d}|djo d|_n$|djo d|_n t dd|_xd|_yt|i }Wntj oPnX|i }|d jo|i|d|_n|d jo%||_|i d }d|_n`|d jot||_n@|d jo|i|n"|tjont d|i|iqW|i p |i o t dn|io|iodk}|id|i|id |i|i g}|i"djo|i#|dRRRLRSRHRMRY((Rtinitfpsb                         + cCs@t|tdjoti|d}n|i|dS(NRtrb(ttypeRt __builtin__RR>R^(R>R((Rt__init__OscCs|iS(N(R>RF(R>((RtgetfpXscCsd|_d|_dS(Nii(R>RKRD(R>((Rtrewind[s cCs1|io|iid|_nd|_dS(N(R>RAtCloseDecompressorR@RF(R>((Rtclose_s   cCs|iS(N(R>RD(R>((RttellescCs|iS(N(R>RZ(R>((Rt getnchannelshscCs|iS(N(R>t_nframes(R>((Rt getnframeskscCs|iS(N(R>RV(R>((Rt getsampwidthnscCs|iS(N(R>RX(R>((Rt getframerateqscCs|iS(N(R>t _comptype(R>((Rt getcomptypetscCs|iS(N(R>t _compname(R>((Rt getcompnamewscCs:|i|i|i|i|i|ifS(N(R>RhRkRlRjRnRp(R>((Rt getparams}scCs%t|idjodSn|iS(Ni(R+R>RCR@(R>((Rt getmarkersscCsAx*|iD]}||djo|Sq q Wtd|fdS(Nismarker %r does not exist(R>RCtmarkertidR(R>RtRs((Rtgetmarks   cCs@|djp||ijo tdn||_d|_dS(Nisposition not in rangei(tposR>RiRRDRK(R>Rv((Rtsetposs  cCs|io^|iid|iid}|i|i}|o|ii|dnd|_n|djodSn|ii||i}|i o|o|i |}n|it ||i |i|_|S(NiiR(R>RKROtseekRRRDt _framesizeRvtnframesRRBR+RZRV(R>RzRRvR((Rt readframess   $cCsKdk}|ii|it|d}|iit||i |S(Ni( RSR>RAtSetParamtFRAME_BUFFER_SIZER+RRt DecompressRZ(R>RRRS((Rt _decomp_datas  cCsdk}|i|dS(Ni(taudiooptulaw2linR(R>RR((Rt _ulaw2lins cCsKdk}t|dp d|_n|i|d|i\}|_|S(Nt _adpcmstatei(RthasattrR>R@Rt adpcm2linR(R>RR((Rt _adpcm2lins    cCst||_t||_t|dd|_tt||_ |i|i|_ |i o'd}|i djod}dGHd|_ n|id|_|o^t|iid}|d@djo|d}n|i ||_ |iid dnt||_|id jo_|id joGy dk}Wntj oqX|i|_|i d|_ dSny dk}Wnstj og|id joGy-dk}|i|_|i d |_ dSWqtj oqXntdnX|id jo|i}|i d |_ n7|idjo|i}|i d |_ n td|i ||_!|i"|_qnd |_d|_dS(NiiiiisWarning: bad COMM chunk sizeiiitNONEtG722tULAWis#cannot read compressed AIFF-C filestALAWsunsupported compression typesnot compressed(#RRLR>RZRRiRVtintR$RXRyRItkludget chunksizeRRmRRRRxRRoRt ImportErrorRRBRSRRt G711_ULAWtschemet G711_ALAWtOpenDecompressorRAR(R>RLRSRRRR((RRNsf            cCst|}yjxct|D]U}t|}t|}t|}|p|o|i i |||fqqWWnOt j oCdGt|i Gt|i djodGndGdG|GHnXdS(Ns!Warning: MARK chunk contains onlyiRstmarkerss instead of(RRLtnmarkerstrangetiRtRRvRtnameR>RCtappendRR+(R>RLRRRvRtR((RRPs      % (RRR^RbRcRdRfRgRhRjRkRlRnRpRqRrRuRwR{RRRRNRP(((RR6s,$ 7                  =t Aifc_writecBs:tZdZdZdZdZdZdZdZdZ d Z d Z d Z d Z d ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ d Z!d!Z"d"Z#RS(#NcCsst|tdjo|}ti|d}nd}|i||ddjo d|_n d|_dS(NRtwbs???is.aiffii(R`RtfilenameRaRR>R^RI(R>RR((RRb+s  cCs||_t|_d|_d|_d|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_g|_d|_d|_dS(NRsnot compressedii(RR>RFt _AIFC_versionR?RmRoR@t_compRBRZRVRXRit_nframeswrittent _datawrittent _datalengthRCt _marklengthRI(R>R((RR^8s                cCs|io|indS(N(R>RFRf(R>((Rt__del__Js cCs$|io tdnd|_dS(Ns0cannot change parameters after starting to writei(R>RRRI(R>((RtaiffQs  cCs$|io tdnd|_dS(Ns0cannot change parameters after starting to writei(R>RRRI(R>((RtaifcVs  cCs>|io tdn|djo tdn||_dS(Ns0cannot change parameters after starting to writeisbad # of channels(R>RRt nchannelsRZ(R>R((Rt setnchannels[s     cCs|ip tdn|iS(Nsnumber of channels not set(R>RZR(R>((RRhbs  cCsK|io tdn|djp |djo tdn||_dS(Ns0cannot change parameters after starting to writeiisbad sample width(R>RRt sampwidthRV(R>R((Rt setsampwidthgs    cCs|ip tdn|iS(Nssample width not set(R>RVR(R>((RRkns  cCs>|io tdn|djo tdn||_dS(Ns0cannot change parameters after starting to writeisbad frame rate(R>RRt framerateRX(R>R((Rt setframeratess     cCs|ip tdn|iS(Nsframe rate not set(R>RXR(R>((RRlzs  cCs$|io tdn||_dS(Ns0cannot change parameters after starting to write(R>RRRzRi(R>Rz((Rt setnframess  cCs|iS(N(R>R(R>((RRjscCsG|io tdn|djo tdn||_||_dS(Ns0cannot change parameters after starting to writeRRRRsunsupported compression type(sNONEsULAWsALAWsG722(R>RRtcomptypeRmtcompnameRo(R>RR((Rt setcomptypes      cCs|iS(N(R>Rm(R>((RRnscCs|iS(N(R>Ro(R>((RRpscCs|\}}}}}}|io tdn|djo tdn|i ||i ||i ||i ||i ||dS(Ns0cannot change parameters after starting to writeRRRRsunsupported compression type(sNONEsULAWsALAWsG722(RRRRzRRR>RRRRRRR(R>t.2RRRRzRR((Rt setparamss        cCsV|i p|i p |i o tdn|i|i|i|i|i|ifS(Nsnot all parameters set(R>RZRVRXRRiRmRo(R>((RRqs! cCs|djo tdn|djo tdnt|tdjo tdnxStt|iD]<}||i|djo|||f|i| 0smarker position must be >= 0Rsmarker name must be a string( RtRRvR`RRR+R>RCRR(R>RtRvRR((Rtsetmarks      cCsAx*|iD]}||djo|Sq q Wtd|fdS(Nismarker %r does not exist(R>RCRsRtR(R>RtRs((RRus   cCs%t|idjodSn|iS(Ni(R+R>RCR@(R>((RRrscCs|iS(N(R>R(R>((RRgscCs|it|t||i|i}|io|i|}n|ii ||i ||_ |i t||_ dS(N( R>t_ensure_header_writtenR+RRVRZRzRBRFR%RR(R>RRz((Rtwriteframesraws cCsE|i||i|ijp|i|ijo|indS(N(R>RRRRiRRt _patchheader(R>R((Rt writeframess &cCs|id|id@o*|iitd|id|_n|i|i|ijp|i |ijp |i o|i n|i o|i i d|_ n|iid|_dS(Nii(R>RRRFR%R*t _writemarkersRRiRRRRtCloseCompressorR@tflush(R>((RRfs  0    cCs[dk}|ii|it|}|ii|it|}|ii |i |S(N( RSR>RR|R}R+RRtCOMPRESSED_BUFFER_SIZEtCompressRi(R>RRRS((Rt _comp_datas cCsdk}|i|dS(Ni(Rtlin2ulawR(R>RR((Rt _lin2ulaws cCsKdk}t|dp d|_n|i|d|i\}|_|S(NRi(RRR>R@Rt lin2adpcmR(R>RR((Rt _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( R>RRmRVRRZRXt _write_headertdatasize(R>R((RRs$           c Cs|idjo|i|_dSny dk}Wnctj oW|idjo7ydk}|i|_dSWqtj oqXntdnX|idjo |i }n'|idjo |i }n td|i ||_ |id|i|id|i|i|id|idg }|id jo|i|d RmRRBRSRRRRRRRtOpenCompressorRRTRURVRWRXR}RRYRZR[R\R]RRR(R>RRSRRYR((Rt_init_compressions:        = cCs|io|idjo|in|iid|ip||i|i |_n|i|i|i |_ |i d@o|i d|_ n|io|idjo6|i d|_ |i d@o|i d|_ q3q7|idjo:|i dd |_ |i d@o|i d|_ q3q7n|ii |_ |i |i }|ioG|iid |iid t|id t|i|in|iid |iid t|i|t|i|i|ii |_t|i|it|i|i dt|i|i|io*|ii|it|i|in|iid|ii |_t|i|i dt|idt|iddS(NRR7iRRiRiiR9R<R8R:iR;i(sULAWsALAW(R>RIRmRRFR%Rit initlengthRZRVRRgt_form_length_post_write_form_lengtht commlengthR)R?R(t _nframes_posR5RXR-Rot_ssnd_length_pos(R>RR((RR6sP    cCs|io:ddt|i}|d@o|d}nd}n d}d}t|id||id|d||S( Niiii iiii( R>RIR+RoRt verslengthR)RFRt datalength(R>RRR((RRas   +cCs?|ii}|id@o'|id}|iitdn |i}||ijo>|i |i jo+|i djo|ii |ddSn|ii |i d|i|}|ii |idt|i|i |ii |idt|i|d|ii |d|i |_ ||_dS(Niii(R>RFRgtcurposRRR%R*RRiRRRxRRRRR)R(R>RRR((RRns"  3 cCs!t|idjodSn|iidd}x]|iD]R}|\}}}|t|dd}t|d@djo|d}q>q>Wt |i||d|_ t |it|ixP|iD]E}|\}}}t |i|t |i|t |i|qWdS(NiR=iiii(R+R>RCRFR%RRsRtRvRR)RR(R-(R>RRvRRsRt((RRs&   ($RRRbR^RRRRRhRRkRRlRRjRRnRpRRqRRuRrRgRRRfRRRRRRRRR(((RR sD                          & + cCsw|djo't|do |i}q4d}n|djot|Sn%|djot|Sn tddS( NtmodeR_trtwRs$mode must be 'r', 'rb', 'w', or 'wb'(Rsrb(Rswb(RR@RRR6RR(RR((RRs     t__main__is/usr/demos/data/audio/bach.aiffRtReadings nchannels =s nframes =s sampwidth =s framerate =s comptype =s compname =itWritingRisDone.(-t__doc__RRat__all__t ExceptionRRRQRRRRR"R$R(R)R-R5RLRER6RR@RRRtsystargvRtfnRRhRjRkRlRnRptgntgRRqR{RRRf(RRaR$RRRRRR-R5RRR)R6RRRRQRRRRRRER"R(R((Rt?sd  !         !