ó zfc@sadZddlZddlZdddgZdefd„ƒYZdZd„Zd „Zd „Z d „Z d „Z d Z d„Z d„Zd„Zd„Zd„Zd„Zd„ZddlmZdfd„ƒYZdfd„ƒYZed„ZeZedkr]ddlZejd r<ejjdƒnejdZeedƒZ zôdGeGHd Ge j!ƒGHd!Ge j"ƒGHd"Ge j#ƒGHd#Ge j$ƒGHd$Ge j%ƒGHd%Ge j&ƒGHejd&rKejd&Z'd'Ge'GHee'd(ƒZ(zEe(j)e j*ƒƒx+e j+d)ƒZ,e, r#Pne(j-e,ƒq WWde(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 | +-----------------+ | | +----+------------+ | | 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, 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/usr/lib64/python2.7/aifc.pyRŽsl@QEcCsBy!tjd|jdƒƒdSWntjk r=t‚nXdS(Ns>lii(tstructtunpacktreadterrortEOFError(tfile((s/usr/lib64/python2.7/aifc.pyt _read_long“s!cCsBy!tjd|jdƒƒdSWntjk r=t‚nXdS(Ns>Lii(RRRRR (R ((s/usr/lib64/python2.7/aifc.pyt _read_ulong™s!cCsBy!tjd|jdƒƒdSWntjk r=t‚nXdS(Ns>hii(RRRRR (R ((s/usr/lib64/python2.7/aifc.pyt _read_shortŸs!cCsBy!tjd|jdƒƒdSWntjk r=t‚nXdS(Ns>Hii(RRRRR (R ((s/usr/lib64/python2.7/aifc.pyt _read_ushort¥s!cCs_t|jdƒƒ}|dkr*d}n|j|ƒ}|d@dkr[|jdƒ}n|S(Niit(tordR(R tlengthtdatatdummy((s/usr/lib64/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 R t _HUGE_VALtpow(tftexpontsignthimanttlomant((s/usr/lib64/python2.7/aifc.pyt _read_float·s     '    cCs|jtjd|ƒƒdS(Ns>h(twriteRtpack(Rtx((s/usr/lib64/python2.7/aifc.pyt _write_shortÈscCs|jtjd|ƒƒdS(Ns>H(RRR(RR((s/usr/lib64/python2.7/aifc.pyt _write_ushortËscCs|jtjd|ƒƒdS(Ns>l(RRR(RR((s/usr/lib64/python2.7/aifc.pyt _write_longÎscCs|jtjd|ƒƒdS(Ns>L(RRR(RR((s/usr/lib64/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/usr/lib64/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/usr/lib64/python2.7/aifc.pyt _write_floatÜs8     $          (tChunkt Aifc_readcBsÔeZdZd„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z d„Z d „Z d „Zd „Zd „Zd „Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„ZRS(cCs^d|_d|_d|_g|_d|_||_t|ƒ}|jƒdkr`t d‚n|j dƒ}|dkr‡d|_ n!|dkrŸd|_ n t d‚d|_ d|_ xÚd|_yt|jƒ}Wntk ríPnX|jƒ}|d kr|j|ƒd|_ nj|d krO||_ |j d ƒ}d|_n:|d krmt|ƒ|_n|d kr‰|j|ƒn|jƒq½W|j s«|j r·t d‚n|j rZ|jrZddl}|jd|j|jd |j|jg}|jdkr|j|d|j|d|j}z |r(d|_|jƒnWd|jjƒXdS(N(R=R<tCloseDecompressorRAR^(RTtdecomp((s/usr/lib64/python2.7/aifc.pyR^ps   cCs|jS(N(R@(RT((s/usr/lib64/python2.7/aifc.pyttellyscCs|jS(N(RP(RT((s/usr/lib64/python2.7/aifc.pyt getnchannels|scCs|jS(N(t_nframes(RT((s/usr/lib64/python2.7/aifc.pyt getnframesscCs|jS(N(RM(RT((s/usr/lib64/python2.7/aifc.pyt getsampwidth‚scCs|jS(N(RO(RT((s/usr/lib64/python2.7/aifc.pyt getframerate…scCs|jS(N(t _comptype(RT((s/usr/lib64/python2.7/aifc.pyt getcomptypeˆscCs|jS(N(t _compname(RT((s/usr/lib64/python2.7/aifc.pyt getcompname‹scCs:|jƒ|jƒ|jƒ|jƒ|jƒ|jƒfS(N(ReRhRiRgRkRm(RT((s/usr/lib64/python2.7/aifc.pyt getparams‘scCs t|jƒdkrdS|jS(Ni(R%R?R<(RT((s/usr/lib64/python2.7/aifc.pyt getmarkers–scCs<x%|jD]}||dkr |Sq Wtd|f‚dS(Nismarker %r does not exist(R?R(RTtidtmarker((s/usr/lib64/python2.7/aifc.pytgetmark›scCs=|dks||jkr'td‚n||_d|_dS(Nisposition not in rangei(RfRR@RF(RTtpos((s/usr/lib64/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( RFREtseekRR@t _framesizeR>R%RPRM(RTtnframesRRsR((s/usr/lib64/python2.7/aifc.pyt readframes§s   $cCsNddl}|jj|jt|ƒdƒ}|jjt|ƒ|j|ƒS(Niÿÿÿÿi(RJR=tSetParamtFRAME_BUFFER_SIZER%t DecompressRP(RTRRJR((s/usr/lib64/python2.7/aifc.pyt _decomp_data»s  cCsddl}|j|dƒS(Niÿÿÿÿi(taudiooptulaw2lin(RTRR}((s/usr/lib64/python2.7/aifc.pyt _ulaw2linÂs cCsLddl}t|dƒs'd|_n|j|d|jƒ\}|_|S(Niÿÿÿÿt _adpcmstatei(R}thasattrR<R€t adpcm2lin(RTRR}((s/usr/lib64/python2.7/aifc.pyt _adpcm2linÆs    cCspt|ƒ|_t|ƒ|_t|ƒdd|_tt|ƒƒ|_|j|j|_|j rZd}|j dkr’d}dGHd|_ n|j dƒ|_ |rt |jj dƒƒ}|d@dkrß|d}n|j ||_ |jjd dƒnt|ƒ|_|j d krl|j d krryd dl}Wntk rUqrX|j|_d |_dSnyd dl}Wnitk rí|j dkráy)d dl}|j|_d |_dSWqátk rÝqáXntd‚nX|j dkr |j}n$|j dkr$|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 RPR RfRMtintRRORvRCt chunksizeRRjRR RuRRlR}t ImportErrorRƒR>RJRRt G711_ULAWt G711_ALAWtOpenDecompressorR=R|(RTRUtkludgeRR}RJtscheme((s/usr/lib64/python2.7/aifc.pyRGÏ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 onlyiRqtmarkerss instead of(R trangeR RR?tappendR R%(RTRUtnmarkerstiRpRstname((s/usr/lib64/python2.7/aifc.pyRH s     $  N(RRR<RARYR_R`RaR^RdReRgRhRiRkRmRnRoRrRtRxR|RRƒRGRH(((s/usr/lib64/python2.7/aifc.pyR3ÿs.$ 5                  RPRMRORft_nframeswrittent _datawrittent _datalengthR?t _marklengthRC(RTR ((s/usr/lib64/python2.7/aifc.pyRYMs                cCs|jr|jƒndS(N(RAR^(RT((s/usr/lib64/python2.7/aifc.pyt__del___s cCs"|jrtd‚nd|_dS(Ns0cannot change parameters after starting to writei(R�RRC(RT((s/usr/lib64/python2.7/aifc.pytaifffs  cCs"|jrtd‚nd|_dS(Ns0cannot change parameters after starting to writei(R�RRC(RT((s/usr/lib64/python2.7/aifc.pytaifcks  cCs:|jrtd‚n|dkr-td‚n||_dS(Ns0cannot change parameters after starting to writeisbad # of channels(R�RRP(RTt nchannels((s/usr/lib64/python2.7/aifc.pyt setnchannelsps     cCs|jstd‚n|jS(Nsnumber of channels not set(RPR(RT((s/usr/lib64/python2.7/aifc.pyRews  cCsF|jrtd‚n|dks-|dkr9td‚n||_dS(Ns0cannot change parameters after starting to writeiisbad sample width(R�RRM(RTt sampwidth((s/usr/lib64/python2.7/aifc.pyt setsampwidth|s    cCs|jstd‚n|jS(Nssample width not set(RMR(RT((s/usr/lib64/python2.7/aifc.pyRhƒs  cCs:|jrtd‚n|dkr-td‚n||_dS(Ns0cannot change parameters after starting to writeisbad frame rate(R�RRO(RTt framerate((s/usr/lib64/python2.7/aifc.pyt setframerateˆs     cCs|jstd‚n|jS(Nsframe rate not set(ROR(RT((s/usr/lib64/python2.7/aifc.pyRi�s  cCs"|jrtd‚n||_dS(Ns0cannot change parameters after starting to write(R�RRf(RTRw((s/usr/lib64/python2.7/aifc.pyt setnframes”s  cCs|jS(N(R�(RT((s/usr/lib64/python2.7/aifc.pyRg™scCsC|jrtd‚n|d kr-td‚n||_||_dS( Ns0cannot change parameters after starting to writeR„R†R‡RˆR‰R…sunsupported compression type(R„R†R‡RˆR‰R…(R�RRjRl(RTtcomptypetcompname((s/usr/lib64/python2.7/aifc.pyt setcomptypeœs      cCs|jS(N(Rj(RT((s/usr/lib64/python2.7/aifc.pyRk¤scCs|jS(N(Rl(RT((s/usr/lib64/python2.7/aifc.pyRm§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(R„R†R‡RˆR‰R…(R�RR¥R§R©RªR­(RTtinfoR¤R¦R¨RwR«R¬((s/usr/lib64/python2.7/aifc.pyt setparams¯s        cCsR|j s|j s|j r*td‚n|j|j|j|j|j|jfS(Nsnot all parameters set(RPRMRORRfRjRl(RT((s/usr/lib64/python2.7/aifc.pyRn»s cCsÂ|dkrtd‚n|dkr0td‚nt|ƒtdƒkrTtd‚nxNtt|jƒƒD]7}||j|dkrj|||f|j| 0smarker position must be >= 0Rsmarker name must be a string(RttypeR“R%R?R”(RTRpRsR—R–((s/usr/lib64/python2.7/aifc.pytsetmarkÁs     cCs<x%|jD]}||dkr |Sq Wtd|f‚dS(Nismarker %r does not exist(R?R(RTRpRq((s/usr/lib64/python2.7/aifc.pyRrÎscCs t|jƒdkrdS|jS(Ni(R%R?R<(RT((s/usr/lib64/python2.7/aifc.pyRoÔscCs|jS(N(R�(RT((s/usr/lib64/python2.7/aifc.pyRdÙscCs‚|jt|ƒƒt|ƒ|j|j}|jrH|j|ƒ}n|jj|ƒ|j||_|jt|ƒ|_dS(N( t_ensure_header_writtenR%RMRPR>RARR�Rž(RTRRw((s/usr/lib64/python2.7/aifc.pytwriteframesrawÜs cCsB|j|ƒ|j|jks1|j|jkr>|jƒndS(N(R³R�RfRŸRžt _patchheader(RTR((s/usr/lib64/python2.7/aifc.pyt writeframesås cCsí|jdkrdSz­|jdƒ|jd@rY|jjtdƒƒ|jd|_n|jƒ|j|jks�|j |jks�|j r�|j ƒn|j r¿|j j ƒd|_ nWdd|_|j}d|_|jƒXdS(Nii(RAR<R²RžRR't _writemarkersR�RfRŸR R´RœtCloseCompressorR>R^(RTR((s/usr/lib64/python2.7/aifc.pyR^ës&          cCs^ddl}|jj|jt|ƒƒ}|jj|jt|ƒƒ}|jj|j|ƒS(Niÿÿÿÿ(RJRœRyRzR%tCOMPRESSED_BUFFER_SIZEtCompressRf(RTRRJR((s/usr/lib64/python2.7/aifc.pyt _comp_datas cCsddl}|j|dƒS(Niÿÿÿÿi(R}tlin2ulaw(RTRR}((s/usr/lib64/python2.7/aifc.pyt _lin2ulaw s cCsLddl}t|dƒs'd|_n|j|d|jƒ\}|_|S(NiÿÿÿÿR€i(R}R�R<R€t lin2adpcm(RTRR}((s/usr/lib64/python2.7/aifc.pyt _lin2adpcms    cCsà|jsÜ|jd krK|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(R†R‡RˆR‰(R�RjRMRRPROt _write_header(RTtdatasize((s/usr/lib64/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 |ƒ|_ |j d |j |jd |j|j|jd |jd g }|jd kr?|j|d RJRŒR}R¼RR�RŽtOpenCompressorRœRKRLRMRNRORzR¸RPRQRRRSR¹Rº(RTRJR}R‘RXR((s/usr/lib64/python2.7/aifc.pyt_init_compression-sB             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 t t fk 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(R†R‡RˆR‰(R†R‡RˆR‰R…(RCRjRÂRARRfRPRMRŸRdt_form_length_postAttributeErrortIOErrorR<t_write_form_lengthR#R;R t _nframes_posR1ROR)Rlt_ssnd_length_pos(RTt initlengtht commlength((s/usr/lib64/python2.7/aifc.pyR¿Ss^         cCsw|jr<d t|jƒ}|d@r3|d}nd}n d}d}t|jd||jd|d|ƒ|S( Niiii iiiii(RCR%RlR#RAR (RTt datalengthRÊt verslength((s/usr/lib64/python2.7/aifc.pyRƆs     "cCs6|jjƒ}|jd@rB|jd}|jjtdƒƒn |j}||jkr’|j|jkr’|jdkr’|jj |dƒdS|jj |j dƒ|j |ƒ}|jj |j dƒt |j|jƒ|jj |jdƒt |j|dƒ|jj |dƒ|j|_||_dS(Niii(RARdRžRR'RŸRfR�R RuRÃRÆRÇR#RÈ(RTtcurposRËR((s/usr/lib64/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)(RTRRqRpRsR—((s/usr/lib64/python2.7/aifc.pyR¶©s" N(&RRR<RAR_RYR¡R¢R£R¥ReR§RhR©RiRªRgR­RkRmR¯RnR±RrRoRdR³RµR^RºR¼R¾R²RÂR¿RÆR´R¶(((s/usr/lib64/python2.7/aifc.pyR˜ sF                          & 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ÏRZ(RÐR™(R<R�RÎR3R˜R(RRÎ((s/usr/lib64/python2.7/aifc.pyR¼s       t__main__is/usr/demos/data/audio/bach.aiffRÏtReadings nchannels =s nframes =s sampwidth =s framerate =s comptype =s compname =itWritingRÐisDone.(/t__doc__RR]t__all__t ExceptionRR›R R R RRRRR R!R"R#R)R1RUR2R3R˜R<RRRtsystargvR”tfnRReRgRhRiRkRmtgntgR¯RnRxRRµR^(((s/usr/lib64/python2.7/aifc.pyt‡sj              !ÿ"ÿ�