grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026î ¤ fZ{ã@sÒdZddlZddlZddlZdddgZGdd„deƒZdZdd „Zd d „Z d d „Z dd„Z dd„Z dZ dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zdd!lmZdd"lmZed#d$ƒZGd%d&„d&ƒZGd'd(„d(ƒZdd)d„ZeZed*krÎddlZej d+d… r”ej j!d,ƒnej d+Z"ee"d-ƒ�Z#e$d.e"ƒe$d/e#j%ƒƒe$d0e#j&ƒƒe$d1e#j'ƒƒe$d2e#j(ƒƒe$d3e#j)ƒƒe$d4e#j*ƒƒej d5d…rÅej d5Z+e$d6e+ƒee+d7ƒ�HZ,e,j-e#j.ƒƒx+e#j/d8ƒZ0e0 r¡Pne,j1e0ƒq‡WWdQXe$d9ƒnWdQXndS):aJStuff 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 namedtuple 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(b'') 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. éNÚErrorÚopenÚopenfpc@seZdZdS)rN)Ú__name__Ú __module__Ú __qualname__©rrú)/opt/alt/python34/lib64/python3.4/aifc.pyr�s l@QEc CsCy!tjd|jdƒƒdSWntjk r>t‚YnXdS)Nz>lér)ÚstructÚunpackÚreadÚerrorÚEOFError)Úfilerrr Ú _read_long”s!rc CsCy!tjd|jdƒƒdSWntjk r>t‚YnXdS)Nz>Lr r)r r r rr)rrrr Ú _read_ulongšs!rc CsCy!tjd|jdƒƒdSWntjk r>t‚YnXdS)Nz>hér)r r r rr)rrrr Ú _read_short s!rc CsCy!tjd|jdƒƒdSWntjk r>t‚YnXdS)Nz>Hrr)r r r rr)rrrr Ú _read_ushort¦s!rcCs_t|jdƒƒ}|dkr*d}n|j|ƒ}|d@dkr[|jdƒ}n|S)Néró)Úordr )rÚlengthÚdataÚdummyrrr Ú _read_string¬s  rgâÿÿÿÿÿïcCs¿t|ƒ}d}|dkr1d }|d}nt|ƒ}t|ƒ}||kok|kokdknryd}n>|dkrŽt}n)|d}|d|td|d ƒ}||S) Nrri€giÿiÿ?lg@é?éÿÿÿÿ)rrÚ _HUGE_VALÚpow)ÚfÚexponÚsignÚhimantÚlomantrrr Ú _read_float¸s     '    r&cCs|jtjd|ƒƒdS)Nz>h)Úwriter Úpack)r!Úxrrr Ú _write_shortÉsr*cCs|jtjd|ƒƒdS)Nz>H)r'r r()r!r)rrr Ú _write_ushortÌsr+cCs|jtjd|ƒƒdS)Nz>l)r'r r()r!r)rrr Ú _write_longÏsr,cCs|jtjd|ƒƒdS)Nz>L)r'r r()r!r)rrr Ú _write_ulongÒsr-cCswt|ƒdkr!tdƒ‚n|jtjdt|ƒƒƒ|j|ƒt|ƒd@dkrs|jdƒndS)Néÿz%string exceeds maximum pstring lengthÚBrrs)ÚlenÚ ValueErrorr'r r()r!Úsrrr Ú _write_stringÕs  r3c 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) Nri€ri@iÿiþ?é r)ÚmathZfrexpZldexpZfloorÚintr+r-) r!r)r5r#r"r$r%ZfmantZfsmantrrr Ú _write_floatÝs8     $          r7)ÚChunk)Ú namedtupleÚ _aifc_paramsz7nchannels sampwidth framerate nframes comptype compnamec@s0eZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1S)2Ú Aifc_readc Cs³d|_d|_g|_d|_||_t|ƒ}|jƒdkrZtdƒ‚n|jdƒ}|dkr�d|_ n$|dkr™d|_ n tdƒ‚d|_ xÛd|_ yt|jƒ}Wnt k râPYnX|jƒ}|d kr|j |ƒd|_ nj|d krD||_|jd ƒ}d|_ n:|d krbt|ƒ|_n|d kr~|j|ƒn|jƒq±W|j s |j r¯tdƒ‚ndS)NrsFORMz file does not start with FORM idr sAIFFsAIFCrznot an AIFF or AIFF-C filesCOMMsSSNDésFVERsMARKz$COMM chunk and/or SSND chunk missing)Ú_versionÚ_convertÚ_markersÚ _soundposÚ_filer8Zgetnamerr Ú_aifcZ_comm_chunk_readÚ_ssnd_seek_neededrÚ_read_comm_chunkÚ _ssnd_chunkrÚ _readmarkÚskip)ÚselfrÚchunkZformdataZ chunknamerrrr Úinitfp)sH                       zAifc_read.initfpcCs5t|tƒr$tj|dƒ}n|j|ƒdS)NÚrb)Ú isinstanceÚstrÚbuiltinsrrJ)rHr!rrr Ú__init__PszAifc_read.__init__cCs|S)Nr)rHrrr Ú __enter__VszAifc_read.__enter__cGs|jƒdS)N)Úclose)rHÚargsrrr Ú__exit__YszAifc_read.__exit__cCs|jS)N)rA)rHrrr Úgetfp_szAifc_read.getfpcCsd|_d|_dS)Nrr)rCr@)rHrrr Úrewindbs zAifc_read.rewindcCs/|j}|dk r+d|_|jƒndS)N)rArQ)rHrrrr rQfs   zAifc_read.closecCs|jS)N)r@)rHrrr ÚtelllszAifc_read.tellcCs|jS)N)Ú _nchannels)rHrrr Ú getnchannelsoszAifc_read.getnchannelscCs|jS)N)Ú_nframes)rHrrr Ú getnframesrszAifc_read.getnframescCs|jS)N)Ú _sampwidth)rHrrr Ú getsampwidthuszAifc_read.getsampwidthcCs|jS)N)Ú _framerate)rHrrr Ú getframeratexszAifc_read.getframeratecCs|jS)N)Ú _comptype)rHrrr Ú getcomptype{szAifc_read.getcomptypecCs|jS)N)Ú _compname)rHrrr Ú getcompname~szAifc_read.getcompnamecCs=t|jƒ|jƒ|jƒ|jƒ|jƒ|jƒƒS)N)r:rXr\r^rZr`rb)rHrrr Ú getparams„szAifc_read.getparamscCs t|jƒdkrdS|jS)Nr)r0r?)rHrrr Ú getmarkers‰szAifc_read.getmarkerscCsAx%|jD]}||dkr |Sq Wtdj|ƒƒ‚dS)Nrzmarker {0!r} does not exist)r?rÚformat)rHÚidÚmarkerrrr ÚgetmarkŽszAifc_read.getmarkcCs@|dks||jkr*tdƒ‚n||_d|_dS)Nrzposition not in ranger)rYrr@rC)rHÚposrrr Úsetpos”s zAifc_read.setposcCsÖ|jrd|jjdƒ|jjdƒ}|j|j}|rX|jj|dƒnd|_n|dkrtdS|jj||jƒ}|jr®|r®|j|ƒ}n|jt|ƒ|j|j |_|S)Nrr<r) rCrEÚseekr r@Ú _framesizer>r0rWr[)rHÚnframesrrirrrr Ú readframesšs   zAifc_read.readframescCsddl}|j|dƒS)Nrr)ÚaudioopZalaw2lin)rHrrorrr Ú _alaw2lin¯s zAifc_read._alaw2lincCsddl}|j|dƒS)Nrr)roZulaw2lin)rHrrorrr Ú _ulaw2lin³s zAifc_read._ulaw2lincCsLddl}t|dƒs'd|_n|j|d|jƒ\}|_|S)NrÚ _adpcmstater)roÚhasattrrrZ adpcm2lin)rHrrorrr Ú _adpcm2lin·s   !zAifc_read._adpcm2lincCs¶t|ƒ|_t|ƒ|_t|ƒdd|_tt|ƒƒ|_|j|j|_|j r d}|j dkršd}t j dƒd|_ n|j dƒ|_|r t|jj dƒƒ}|d@dkrç|d}n|j ||_ |jjddƒnt|ƒ|_|jd kr²|jd krI|j|_nH|jdkrg|j|_n*|jdkr…|j|_n tdƒ‚d|_q²nd |_d|_dS)Nér<rérzWarning: bad COMM chunk sizeér sNONEsG722óulawóULAWóalawóALAWzunsupported compression typersnot compressedr)rxry)rzr{)rrWrrYr[r6r&r]rlrBZ chunksizeÚwarningsÚwarnr r_rrrkrrartr>rqrpr)rHrIZkludgerrrr rD¿s<      zAifc_read._read_comm_chunkc CsÐt|ƒ}ygx`t|ƒD]R}t|ƒ}t|ƒ}t|ƒ}|sR|r|jj|||fƒqqWWnVtk rËdt|jƒt|jƒdkr­dnd|f}tj |ƒYnXdS)Nz;Warning: MARK chunk contains only %s marker%s instead of %srÚr2) rÚrangerrr?Úappendrr0r|r})rHrIZnmarkersÚirfriÚnameÚwrrr rFås     $ * zAifc_read._readmarkN)rrrrJrOrPrSrTrUrQrVrXrZr\r^r`rbrcrdrhrjrnrprqrtrDrFrrrr r;s0 $ '                      &r;c@sÀeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Zd3d4„Zd5d6„Zd7d8„Zd9d:„Zd;d<„Z d=d>„Z!d?d@„Z"dAdB„Z#dCdD„Z$dEdF„Z%dGdH„Z&dIS)JÚ Aifc_writecCslt|tƒr*|}tj|dƒ}nd}|j|ƒ|dd…dkr_d|_n d|_dS)NÚwbz???éz.aiffrréûÿÿÿ)rLrMrNrrJrB)rHr!Úfilenamerrr rOs  zAifc_write.__init__cCs‹||_t|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ g|_ d|_d|_dS)NsNONEsnot compressedrr)rAÚ _AIFC_versionr=r_rar>rWr[r]rYÚ_nframeswrittenÚ _datawrittenÚ _datalengthr?Ú _marklengthrB)rHrrrr rJ$s              zAifc_write.initfpcCs|jƒdS)N)rQ)rHrrr Ú__del__5szAifc_write.__del__cCs|S)Nr)rHrrr rP8szAifc_write.__enter__cGs|jƒdS)N)rQ)rHrRrrr rS;szAifc_write.__exit__cCs%|jrtdƒ‚nd|_dS)Nz0cannot change parameters after starting to writer)rŠrrB)rHrrr ÚaiffAs zAifc_write.aiffcCs%|jrtdƒ‚nd|_dS)Nz0cannot change parameters after starting to writer)rŠrrB)rHrrr ÚaifcFs zAifc_write.aifccCs@|jrtdƒ‚n|dkr3tdƒ‚n||_dS)Nz0cannot change parameters after starting to writerzbad # of channels)rŠrrW)rHÚ nchannelsrrr Ú setnchannelsKs   zAifc_write.setnchannelscCs|jstdƒ‚n|jS)Nznumber of channels not set)rWr)rHrrr rXRs zAifc_write.getnchannelscCsL|jrtdƒ‚n|dks0|dkr?tdƒ‚n||_dS)Nz0cannot change parameters after starting to writerr zbad sample width)rŠrr[)rHÚ sampwidthrrr Ú setsampwidthWs  zAifc_write.setsampwidthcCs|jstdƒ‚n|jS)Nzsample width not set)r[r)rHrrr r\^s zAifc_write.getsampwidthcCs@|jrtdƒ‚n|dkr3tdƒ‚n||_dS)Nz0cannot change parameters after starting to writerzbad frame rate)rŠrr])rHÚ frameraterrr Ú setframeratecs   zAifc_write.setframeratecCs|jstdƒ‚n|jS)Nzframe rate not set)r]r)rHrrr r^js zAifc_write.getframeratecCs%|jrtdƒ‚n||_dS)Nz0cannot change parameters after starting to write)rŠrrY)rHrmrrr Ú setnframesos zAifc_write.setnframescCs|jS)N)rŠ)rHrrr rZtszAifc_write.getnframescCsI|jrtdƒ‚n|d kr3tdƒ‚n||_||_dS) Nz0cannot change parameters after starting to writeóNONEóulawóULAWóalawóALAWóG722zunsupported compression type)r˜r™ršr›rœr�)rŠrr_ra)rHÚcomptypeÚcompnamerrr Ú setcomptypews   zAifc_write.setcomptypecCs|jS)N)r_)rHrrr r`€szAifc_write.getcomptypecCs|jS)N)ra)rHrrr rbƒszAifc_write.getcompnamecCs“|\}}}}}}|jr0tdƒ‚n|d krKtdƒ‚n|j|ƒ|j|ƒ|j|ƒ|j|ƒ|j||ƒdS) Nz0cannot change parameters after starting to writeóNONEóulawóULAWóalawóALAWóG722zunsupported compression type)r¡r¢r£r¤r¥r¦)rŠrr’r”r–r—r )rHZparamsr‘r“r•rmržrŸrrr Ú setparams‹s      zAifc_write.setparamscCsX|j s|j s|j r-tdƒ‚nt|j|j|j|j|j|jƒS)Nznot all parameters set)rWr[r]rr:rYr_ra)rHrrr rc˜szAifc_write.getparamscCsÂ|dkrtdƒ‚n|dkr6tdƒ‚nt|tƒsTtdƒ‚nxNtt|jƒƒD]7}||j|dkrj|||f|j| 0zmarker position must be >= 0zmarker name must be bytes)rrLÚbytesrr0r?r€)rHrfrir‚r�rrr Úsetmarkžs  zAifc_write.setmarkcCsAx%|jD]}||dkr |Sq Wtdj|ƒƒ‚dS)Nrzmarker {0!r} does not exist)r?rre)rHrfrgrrr rh«szAifc_write.getmarkcCs t|jƒdkrdS|jS)Nr)r0r?)rHrrr rd±szAifc_write.getmarkerscCs|jS)N)rŠ)rHrrr rV¶szAifc_write.tellcCs¯t|ttfƒs-t|ƒjdƒ}n|jt|ƒƒt|ƒ|j|j}|j ru|j |ƒ}n|j j |ƒ|j ||_ |j t|ƒ|_ dS)Nr/)rLr¨Ú bytearrayÚ memoryviewÚcastÚ_ensure_header_writtenr0r[rWr>rAr'rŠr‹)rHrrmrrr Úwriteframesraw¹s zAifc_write.writeframesrawcCsB|j|ƒ|j|jks1|j|jkr>|jƒndS)N)r®rŠrYrŒr‹Ú _patchheader)rHrrrr Ú writeframesÄs zAifc_write.writeframesc CsÅ|jdkrdSz…|jdƒ|jd@rS|jjdƒ|jd|_n|jƒ|j|jksŠ|j|jksŠ|jr—|j ƒnWdd|_ |j}d|_|j ƒXdS)Nrrs) rAr­r‹r'Ú _writemarkersrŠrYrŒr�r¯r>rQ)rHr!rrr rQÊs        zAifc_write.closecCsddl}|j|dƒS)Nrr)roZlin2alaw)rHrrorrr Ú _lin2alawãs zAifc_write._lin2alawcCsddl}|j|dƒS)Nrr)roZlin2ulaw)rHrrorrr Ú _lin2ulawçs zAifc_write._lin2ulawcCsLddl}t|dƒs'd|_n|j|d|jƒ\}|_|S)Nrrrr)rorsrrZ lin2adpcm)rHrrorrr Ú _lin2adpcmës   !zAifc_write._lin2adpcmcCsª|js¦|jd krN|js-d|_n|jdkrNtdƒ‚qNn|jsftdƒ‚n|js~td ƒ‚n|js–td ƒ‚n|j|ƒndS) NóULAWóulawóALAWóalawóG722rzRsample width must be 2 when compressing with ulaw/ULAW, alaw/ALAW or G7.22 (ADPCM)z# channels not specifiedzsample width not specifiedzsampling rate not specified)rµr¶r·r¸r¹)rŠr_r[rrWr]Ú _write_header)rHZdatasizerrr r­òs      z!Aifc_write._ensure_header_writtencCs^|jdkr|j|_n<|jdkr<|j|_n|jdkrZ|j|_ndS)NsG722óulawóULAWóalawóALAW)r»r¼)r½r¾)r_r´r>r³r²)rHrrr Ú_init_compressions zAifc_write._init_compressioncCs(|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|_ YnX|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)NsNONEsFORMróulawóULAWóalawóALAWróG722ér sAIFCsFVERsAIFFsCOMMr<sSSNDr)rÀrÁrÂrÃ)rÁrÀrÃrÂrÄ)rBr_r¿rAr'rYrWr[rŒrVÚ_form_length_posÚAttributeErrorÚOSErrorÚ_write_form_lengthr-r=r*Ú _nframes_posr7r]r3raÚ_ssnd_length_pos)rHZ initlengthÚ commlengthrrr rº s^        zAifc_write._write_headercCsw|jr<d t|jƒ}|d@r3|d}nd}n d}d}t|jd||jd|d|ƒ|S) Nrvr†ré rr r<érw)rBr0rar-rAr�)rHÚ datalengthrÌZ verslengthrrr rÉ=s     "zAifc_write._write_form_lengthcCs0|jjƒ}|jd@r<|jd}|jjdƒn |j}||jkrŒ|j|jkrŒ|jdkrŒ|jj|dƒdS|jj|j dƒ|j |ƒ}|jj|j dƒt |j|jƒ|jj|j dƒt |j|dƒ|jj|dƒ|j|_||_dS)Nrsrr<)rArVr‹r'rŒrYrŠr�rkrÆrÉrÊr-rË)rHZcurposrÏrrrr r¯Js&    zAifc_write._patchheadercCst|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)NrsMARKrrér<)r0r?rAr'r-r�r*r3)rHrrgrfrir‚rrr r±`s" zAifc_write._writemarkersN)'rrrrOrJrŽrPrSr�r�r’rXr”r\r–r^r—rZr r`rbr§rcr©rhrdrVr®r°rQr²r³r´r­r¿rºrÉr¯r±rrrr r„ùsH                             3 r„cCsl|dkr0t|dƒr'|j}q0d}n|dkrFt|ƒS|dkr\t|ƒStdƒ‚dS) NÚmoderKÚrrƒr…z$mode must be 'r', 'rb', 'w', or 'wb')rÒzrb)rƒzwb)rsrÑr;r„r)r!rÑrrr rss       Ú__main__rz/usr/demos/data/audio/bach.aiffrÒZReadingz nchannels =z nframes =z sampwidth =z framerate =z comptype =z compname =rZWritingrƒizDone.)2Ú__doc__r rNr|Ú__all__Ú Exceptionrr‰rrrrrrr&r*r+r,r-r3r7rIr8Ú collectionsr9r:r;r„rrrÚsysÚargvr€Úfnr!ÚprintrXrZr\r^r`rbZgnÚgr§rcrnrr°rrrr Ú‡sh               ! ôÿ{