grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026 ”qcß|ã@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,ƒej 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ƒ�EZ,e,j-e#j.ƒƒx(e#j/d8ƒZ0e0 r›Pe,j1e0ƒq„WWdQRXe$d9ƒWdQRXdS):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/python35/lib64/python3.5/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@dkrX|jdƒ}|S)Néró)Úordr )rÚlengthÚdataÚdummyrrr Ú _read_string¬s  rgâÿÿÿÿÿïcCs¼t|ƒ}d}|dkr.d }|d}t|ƒ}t|ƒ}||koh|kohdknrvd}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-cCsqt|ƒdkrtdƒ‚|jtjdt|ƒƒƒ|j|ƒt|ƒd@dkrm|jdƒdS)Néÿz%string exceeds maximum pstring lengthÚBrrs)ÚlenÚ ValueErrorr'r r()r!Úsrrr Ú _write_stringÕs   r3c Cseddl}|dkr+d}|d}nd}|dkrRd}d}d}nè|j|ƒ\}}|dks‹|dks‹||kr¤|dB}d}d}n–|d}|dkrÒ|j||ƒ}d}||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@s6eZdZdZdd„Zdd„Zdd„Zdd „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.d/„Zd0d1„ZdS)2Ú Aifc_readNc Csªd|_d|_g|_d|_||_t|ƒ}|jƒdkrWtdƒ‚|jdƒ}|dkr~d|_ n$|dkr–d|_ n tdƒ‚d|_ xØd|_ yt|jƒ}Wnt k rßPYnX|jƒ}|d kr|j |ƒd|_ ng|d krA||_|jd ƒ}d|_ n7|d kr_t|ƒ|_n|d krx|j|ƒ|jƒq®W|j sš|j r¦tdƒ‚dS)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.initfpc Cs^t|tƒrMtj|dƒ}y|j|ƒWqZ|jƒ‚YqZXn |j|ƒdS)NÚrb)Ú isinstanceÚstrÚbuiltinsrrJÚclose)rHr!Ú file_objectrrr Ú__init__Rs  zAifc_read.__init__cCs|S)Nr)rHrrr Ú __enter__^szAifc_read.__enter__cGs|jƒdS)N)rO)rHÚargsrrr Ú__exit__aszAifc_read.__exit__cCs|jS)N)rA)rHrrr ÚgetfpgszAifc_read.getfpcCsd|_d|_dS)Nrr)rCr@)rHrrr Úrewindjs zAifc_read.rewindcCs,|j}|dk r(d|_|jƒdS)N)rArO)rHrrrr rOns   zAifc_read.closecCs|jS)N)r@)rHrrr ÚtelltszAifc_read.tellcCs|jS)N)Ú _nchannels)rHrrr Ú getnchannelswszAifc_read.getnchannelscCs|jS)N)Ú_nframes)rHrrr Ú getnframeszszAifc_read.getnframescCs|jS)N)Ú _sampwidth)rHrrr Ú getsampwidth}szAifc_read.getsampwidthcCs|jS)N)Ú _framerate)rHrrr Ú getframerate€szAifc_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:rYr]r_r[rarc)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ƒ‚||_d|_dS)Nrzposition not in ranger)rZrr@rC)rHÚposrrr Úsetposœs  zAifc_read.setposcCsÍ|jr^|jjdƒ|jjdƒ}|j|j}|rU|jj|dƒd|_|dkrndS|jj||jƒ}|jr¥|r¥|j|ƒ}|jt|ƒ|j|j |_|S)Nrr<r) rCrEÚseekr r@Ú _framesizer>r0rXr\)rHÚnframesrrjrrrr Ú readframes¢s   zAifc_read.readframescCsddl}|j|dƒS)Nrr)ÚaudioopZalaw2lin)rHrrprrr Ú _alaw2lin·s zAifc_read._alaw2lincCsddl}|j|dƒS)Nrr)rpZulaw2lin)rHrrprrr Ú _ulaw2lin»s zAifc_read._ulaw2lincCsIddl}t|dƒs$d|_|j|d|jƒ\}|_|S)NrÚ _adpcmstater)rpÚhasattrrsZ adpcm2lin)rHrrprrr Ú _adpcm2lin¿s   !zAifc_read._adpcm2lincCsªt|ƒ|_t|ƒ|_t|ƒdd|_tt|ƒƒ|_|j|j|_|j r”d}|j dkr—d}t j dƒd|_ |j dƒ|_|rt|jj dƒƒ}|d@dkrá|d}|j ||_ |jjddƒt|ƒ|_|jd kr¦|jd kr@|j|_nH|jdkr^|j|_n*|jdkr||j|_n tdƒ‚d|_nd |_d|_dS)Nér<rérzWarning: bad COMM chunk sizeér sNONEsG722óulawóULAWóalawóALAWzunsupported compression typersnot compressedr)ryrz)r{r|)rrXrrZr\r6r&r^rmrBZ chunksizeÚwarningsÚwarnr r`rrrlrrbrur>rrrqr)rHrIZkludgerrrr rDÇs<       zAifc_read._read_comm_chunkc CsÍt|ƒ}ydx]t|ƒD]O}t|ƒ}t|ƒ}t|ƒ}|sR|r|jj|||fƒqWWnVtk 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ÚirgrjÚnameÚwrrr rFís     ! * zAifc_read._readmark)rrrrArJrQrRrTrUrVrOrWrYr[r]r_rarcrdrerirkrorqrrrurDrFrrrr r;s2 $ '                     &r;c@sÆeZdZdZdd„Zdd„Zdd„Zdd „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.d/„Zd0d1„Zd2d3„Zd4d5„Zd6d7„Zd8d9„Zd:d;„Z d<d=„Z!d>d?„Z"d@dA„Z#dBdC„Z$dDdE„Z%dFdG„Z&dHdI„Z'dS)JÚ Aifc_writeNc Csvt|tƒretj|dƒ}y|j|ƒWn|jƒ‚YnX|jdƒrrd|_n |j|ƒdS)NÚwbz.aiffr)rLrMrNrrJrOÚendswithrB)rHr!rPrrr rQ!s  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`rbr>rXr\r^rZÚ_nframeswrittenÚ _datawrittenÚ _datalengthr?Ú _marklengthrB)rHrrrr rJ1s              zAifc_write.initfpcCs|jƒdS)N)rO)rHrrr Ú__del__BszAifc_write.__del__cCs|S)Nr)rHrrr rREszAifc_write.__enter__cGs|jƒdS)N)rO)rHrSrrr rTHszAifc_write.__exit__cCs"|jrtdƒ‚d|_dS)Nz0cannot change parameters after starting to writer)r‰rrB)rHrrr ÚaiffNs  zAifc_write.aiffcCs"|jrtdƒ‚d|_dS)Nz0cannot change parameters after starting to writer)r‰rrB)rHrrr ÚaifcSs  zAifc_write.aifccCs:|jrtdƒ‚|dkr-tdƒ‚||_dS)Nz0cannot change parameters after starting to writerzbad # of channels)r‰rrX)rHÚ nchannelsrrr Ú setnchannelsXs     zAifc_write.setnchannelscCs|jstdƒ‚|jS)Nznumber of channels not set)rXr)rHrrr rY_s  zAifc_write.getnchannelscCsF|jrtdƒ‚|dks-|dkr9tdƒ‚||_dS)Nz0cannot change parameters after starting to writerr zbad sample width)r‰rr\)rHÚ sampwidthrrr Ú setsampwidthds    zAifc_write.setsampwidthcCs|jstdƒ‚|jS)Nzsample width not set)r\r)rHrrr r]ks  zAifc_write.getsampwidthcCs:|jrtdƒ‚|dkr-tdƒ‚||_dS)Nz0cannot change parameters after starting to writerzbad frame rate)r‰rr^)rHÚ frameraterrr Ú setframerateps     zAifc_write.setframeratecCs|jstdƒ‚|jS)Nzframe rate not set)r^r)rHrrr r_ws  zAifc_write.getframeratecCs"|jrtdƒ‚||_dS)Nz0cannot change parameters after starting to write)r‰rrZ)rHrnrrr Ú setnframes|s  zAifc_write.setnframescCs|jS)N)r‰)rHrrr r[�szAifc_write.getnframescCsC|jrtdƒ‚|d kr-tdƒ‚||_||_dS) Nz0cannot change parameters after starting to writeóNONEóulawóULAWóalawóALAWóG722zunsupported compression type)r—r˜r™ršr›rœ)r‰rr`rb)rHÚcomptypeÚcompnamerrr Ú setcomptype„s     zAifc_write.setcomptypecCs|jS)N)r`)rHrrr ra�szAifc_write.getcomptypecCs|jS)N)rb)rHrrr rc�szAifc_write.getcompnamecCs�|\}}}}}}|jr-tdƒ‚|d krEtdƒ‚|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”rnr�ržrrr Ú setparams˜s        zAifc_write.setparamscCsU|j s|j s|j r*tdƒ‚t|j|j|j|j|j|jƒS)Nznot all parameters set)rXr\r^rr:rZr`rb)rHrrr rd¥s zAifc_write.getparamscCs¹|dkrtdƒ‚|dkr0tdƒ‚t|tƒsKtdƒ‚xNtt|jƒƒD]7}||j|dkra|||f|j| 0zmarker position must be >= 0zmarker name must be bytes)rrLÚbytesr€r0r?r�)rHrgrjrƒr‚rrr Úsetmark«s     zAifc_write.setmarkcCsAx%|jD]}||dkr |Sq Wtdj|ƒƒ‚dS)Nrzmarker {0!r} does not exist)r?rrf)rHrgrhrrr ri¸szAifc_write.getmarkcCs t|jƒdkrdS|jS)Nr)r0r?)rHrrr re¾szAifc_write.getmarkerscCs|jS)N)r‰)rHrrr rWÃszAifc_write.tellcCs©t|ttfƒs*t|ƒjdƒ}|jt|ƒƒt|ƒ|j|j}|j ro|j |ƒ}|j j |ƒ|j ||_ |j t|ƒ|_ dS)Nr/)rLr§Ú bytearrayÚ memoryviewÚcastÚ_ensure_header_writtenr0r\rXr>rAr'r‰rŠ)rHrrnrrr ÚwriteframesrawÆs zAifc_write.writeframesrawcCs?|j|ƒ|j|jks1|j|jkr;|jƒdS)N)r­r‰rZr‹rŠÚ _patchheader)rHrrrr Ú writeframesÑs zAifc_write.writeframesc Cs¿|jdkrdSz|jdƒ|jd@rP|jjdƒ|jd|_|jƒ|j|jks‡|j|jks‡|jr‘|j ƒWdd|_ |j}d|_|j ƒXdS)Nrrs) rAr¬rŠr'Ú _writemarkersr‰rZr‹rŒr®r>rO)rHr!rrr rO×s        zAifc_write.closecCsddl}|j|dƒS)Nrr)rpZlin2alaw)rHrrprrr Ú _lin2alawðs zAifc_write._lin2alawcCsddl}|j|dƒS)Nrr)rpZlin2ulaw)rHrrprrr Ú _lin2ulawôs zAifc_write._lin2ulawcCsIddl}t|dƒs$d|_|j|d|jƒ\}|_|S)Nrrsr)rprtrsZ lin2adpcm)rHrrprrr Ú _lin2adpcmøs   !zAifc_write._lin2adpcmcCs•|js‘|jd krE|js*d|_|jdkrEtdƒ‚|jsZtdƒ‚|jsotd ƒ‚|js„td ƒ‚|j|ƒdS) 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\rrXr^Ú _write_header)rHZdatasizerrr r¬ÿs          z!Aifc_write._ensure_header_writtencCs[|jdkr|j|_n9|jdkr<|j|_n|jdkrW|j|_dS)NsG722óulawóULAWóalawóALAW)rºr»)r¼r½)r`r³r>r²r±)rHrrr Ú_init_compressions zAifc_write._init_compressioncCs |jr"|jdkr"|jƒ|jjdƒ|jsR||j|j|_|j|j|j|_|jd@r‰|jd|_|jr|jdkrÑ|jd|_|jd@r|jd|_n@|jd kr|jd d |_|jd@r|jd|_y|jj ƒ|_ Wn!t t fk rJd|_ YnX|j |jƒ}|jr¬|jjd ƒ|jjd ƒt|jd ƒt|j|jƒn|jjdƒ|jjdƒt|j|ƒt|j|jƒ|j dk r|jj ƒ|_t|j|jƒ|jdkrEt|jdƒnt|j|jdƒt|j|jƒ|jrž|jj|jƒt|j|jƒ|jjdƒ|j dk rÏ|jj ƒ|_t|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'rZrXr\r‹rWÚ_form_length_posÚAttributeErrorÚOSErrorÚ_write_form_lengthr-r=r*Ú _nframes_posr7r^r3rbÚ_ssnd_length_pos)rHZ initlengthÚ commlengthrrr r¹s^        zAifc_write._write_headercCst|jr9d t|jƒ}|d@r0|d}d}n d}d}t|jd||jd|d|ƒ|S) Nrwéré rr r<érx)rBr0rbr-rArŒ)rHÚ datalengthrËZ verslengthrrr rÈJs     "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<)rArWrŠr'r‹rZr‰rŒrlrÅrÈrÉr-rÊ)rHZcurposrÏrrrr r®Ws&    zAifc_write._patchheadercCst|jƒdkrdS|jjdƒd}xX|jD]M}|\}}}|t|ƒdd}t|ƒd@dkr9|d}q9Wt|j|ƒ|d|_t|jt|jƒƒxP|jD]E}|\}}}t|j|ƒt|j|ƒt|j|ƒqÊWdS)NrsMARKrrér<)r0r?rAr'r-rŒr*r3)rHrrhrgrjrƒrrr r°ms" zAifc_write._writemarkers)(rrrrArQrJr�rRrTrŽr�r‘rYr“r]r•r_r–r[rŸrarcr¦rdr¨rirerWr­r¯rOr±r²r³r¬r¾r¹rÈr®r°rrrr r…sJ                              3 r…cCsi|dkr-t|dƒr'|j}nd}|dkrCt|ƒS|dkrYt|ƒStdƒ‚dS) NÚmoderKÚrr„r†z$mode must be 'r', 'rb', 'w', or 'wb')rÒrK)r„r†)rtrÑr;r…r)r!rÑrrr r€s      Ú__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!ÚprintrYr[r]r_rarcZgnÚgr¦rdrorr¯rrrr Ú‡sh               ! üÿ€