grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026B a‘¶îû/eã @sDdZddlZddlZddlZdddgZGdd„deƒZdZdd „Zd d „Z d d „Z dd„Z dd„Z dZ dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zdd!lmZdd"lmZed#d$ƒZd%ej_d&ej_d'ej_d(ej_d)ej_d*ej_Gd+d,„d,ƒZ Gd-d.„d.ƒZ!dAd/d„Z"dBd0d„Z#e$d1k�r@ddl%Z%e%j&d2d…�sNe%j& 'd3¡e%j&d2Z(e"e(d4ƒ�ØZ)e*d5e(ƒe*d6e) +¡ƒe*d7e) ,¡ƒe*d8e) -¡ƒe*d9e) .¡ƒe*d:e) /¡ƒe*d;e) 0¡ƒe%j&dƒ�8Z2e2 3e) 4¡¡x"e) 5d?¡Z6e6�sPe2 7e6¡�qWWdQRXe*d@ƒWdQRXdS)CaJStuff 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@s eZdZdS)rN)Ú__name__Ú __module__Ú __qualname__©rrú)/opt/alt/python37/lib64/python3.7/aifc.pyr�sl@QEcCs8yt d| d¡¡dStjk r2td‚YnXdS)Nz>lér)ÚstructÚunpackÚreadÚerrorÚEOFError)Úfilerrr Ú _read_long”srcCs8yt d| d¡¡dStjk r2td‚YnXdS)Nz>Lr r)r r r rr)rrrr Ú _read_ulongšsrcCs8yt d| d¡¡dStjk r2td‚YnXdS)Nz>hér)r r r rr)rrrr Ú _read_short srcCs8yt d| d¡¡dStjk r2td‚YnXdS)Nz>Hrr)r r r rr)rrrr Ú _read_ushort¦srcCs@t| d¡ƒ}|dkrd}n | |¡}|d@dkr<| d¡}|S)Néró)Úordr )rÚlengthÚdataÚdummyrrr Ú _read_string¬s   rgâÿÿÿÿÿïcCs�t|ƒ}d}|dkr d}|d}t|ƒ}t|ƒ}||krN|krNdkrXnnd}n0|dkrft}n"|d}|d|td |d ƒ}||S) Nrréÿÿÿÿi€giÿiÿ?lg@é?)rrÚ _HUGE_VALÚpow)ÚfÚexponÚsignÚhimantÚlomantrrr Ú _read_float¸s"r&cCs| t d|¡¡dS)Nz>h)Úwriter Úpack)r!Úxrrr Ú _write_shortÉsr*cCs| t d|¡¡dS)Nz>H)r'r r()r!r)rrr Ú _write_ushortÌsr+cCs| t d|¡¡dS)Nz>l)r'r r()r!r)rrr Ú _write_longÏsr,cCs| t d|¡¡dS)Nz>L)r'r r()r!r)rrr Ú _write_ulongÒsr-cCsRt|ƒdkrtdƒ‚| t dt|ƒ¡¡| |¡t|ƒd@dkrN| d¡dS)Néÿz%string exceeds maximum pstring lengthÚBrró)ÚlenÚ ValueErrorr'r r()r!Úsrrr Ú _write_stringÕs   r4c Csúddl}|dkrd}|d}nd}|dkr8d}d}d}n | |¡\}}|dks^|dks^||krp|dB}d}d}nh|d}|dkr�| ||¡}d}||B}| |d¡}| |¡}t|ƒ}| ||d¡}| |¡}t|ƒ}t||ƒt||ƒt||ƒdS) Nri€ri@riÿiþ?é )ÚmathZfrexpZldexpZfloorÚintr+r-) r!r)r6r#r"r$r%ZfmantZfsmantrrr Ú _write_floatÝs8       r8)ÚChunk)Ú namedtupleÚ _aifc_paramsz7nchannels sampwidth framerate nframes comptype compnamez3Number of audio channels (1 for mono, 2 for stereo)zSample width in byteszSampling frequencyzNumber of audio framesz(Compression type ("NONE" for AIFF files)zRA human-readable version of the compression type ('not compressed' for AIFF files)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„ZdS)2Ú Aifc_readNcCs8d|_d|_g|_d|_||_t|ƒ}| ¡dkr:tdƒ‚| d¡}|dkrTd|_ n|dkrdd|_ ntdƒ‚d|_ d|_ x¢d|_ yt|jƒ}Wnt k r¤PYnX| ¡}|d krÈ| |¡d|_ nH|d krè||_ | d ¡}d|_ n(|d krüt|ƒ|_n|d k�r| |¡| ¡qzW|j �r,|j �s4tdƒ‚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Ú_filer9Zgetnamerr Ú_aifcZ_comm_chunk_readÚ _ssnd_chunkÚ_ssnd_seek_neededrÚ_read_comm_chunkrÚ _readmarkÚskip)ÚselfrÚchunkZformdataZ chunknamerrrr Úinitfp4sJ        zAifc_read.initfpcCsLt|tƒr>t |d¡}y| |¡WqH| ¡‚YqHXn | |¡dS)NÚrb)Ú isinstanceÚstrÚbuiltinsrrKÚclose)rIr!Ú file_objectrrr Ú__init__\s   zAifc_read.__init__cCs|S)Nr)rIrrr Ú __enter__hszAifc_read.__enter__cGs | ¡dS)N)rP)rIÚargsrrr Ú__exit__kszAifc_read.__exit__cCs|jS)N)rB)rIrrr ÚgetfpqszAifc_read.getfpcCsd|_d|_dS)Nrr)rErA)rIrrr ÚrewindtszAifc_read.rewindcCs |j}|dk rd|_| ¡dS)N)rBrP)rIrrrr rPxszAifc_read.closecCs|jS)N)rA)rIrrr Útell~szAifc_read.tellcCs|jS)N)Ú _nchannels)rIrrr Ú getnchannels�szAifc_read.getnchannelscCs|jS)N)Ú_nframes)rIrrr Ú getnframes„szAifc_read.getnframescCs|jS)N)Ú _sampwidth)rIrrr Ú getsampwidth‡szAifc_read.getsampwidthcCs|jS)N)Ú _framerate)rIrrr Ú getframerateŠszAifc_read.getframeratecCs|jS)N)Ú _comptype)rIrrr Ú getcomptype�szAifc_read.getcomptypecCs|jS)N)Ú _compname)rIrrr Ú getcompname�szAifc_read.getcompnamecCs*t| ¡| ¡| ¡| ¡| ¡| ¡ƒS)N)r;rZr^r`r\rbrd)rIrrr Ú getparams–s zAifc_read.getparamscCst|jƒdkrdS|jS)Nr)r1r@)rIrrr Ú getmarkers›szAifc_read.getmarkerscCs2x|jD]}||dkr|SqWtd |¡ƒ‚dS)Nrzmarker {0!r} does not exist)r@rÚformat)rIÚidÚmarkerrrr Úgetmark s  zAifc_read.getmarkcCs*|dks||jkrtdƒ‚||_d|_dS)Nrzposition not in ranger)r[rrArE)rIÚposrrr Úsetpos¦szAifc_read.setposcCs–|jrD|j d¡|j d¡}|j|j}|r>|j |d¡d|_|dkrPdS|j ||j¡}|jrv|rv| |¡}|jt|ƒ|j|j |_|S)Nrr=r) rErDÚseekr rAÚ _framesizer?r1rYr])rIÚnframesrrkrrrr Ú readframes¬s     zAifc_read.readframescCsddl}| |d¡S)Nrr)ÚaudioopZalaw2lin)rIrrqrrr Ú _alaw2linÁszAifc_read._alaw2lincCsddl}| |d¡S)Nrr)rqZulaw2lin)rIrrqrrr Ú _ulaw2linÅszAifc_read._ulaw2lincCs2ddl}t|dƒsd|_| |d|j¡\}|_|S)NrÚ _adpcmstater)rqÚhasattrrtZ adpcm2lin)rIrrqrrr Ú _adpcm2linÉs  zAifc_read._adpcm2lincCsVt|ƒ|_t|ƒ|_t|ƒdd|_tt|ƒƒ|_|jdkrFtdƒ‚|jdkrXtdƒ‚|j|j|_ |j �rFd}|j dkr�d}t   d¡d |_ | d ¡|_|rÞt|j d¡ƒ}|d@dkrÄ|d}|j ||_ |j d d¡t|ƒ|_|jd k�rR|jd k�r |j|_n4|jdk�r |j|_n|jdk�r6|j|_ntdƒ‚d|_n d |_d|_dS)Nér=rzbad sample widthzbad # of channelsérzWarning: bad COMM chunk sizeér rsNONEsG722)sulawsULAW)salawsALAWzunsupported compression typersnot compressed)rrYrr[r]r7r&r_rrnrCZ chunksizeÚwarningsÚwarnr rarrrmrrcrvr?rsrr)rIrJZkludgerrrr rFÑsD                 zAifc_read._read_comm_chunkcCsšt|ƒ}yHxBt|ƒD]6}t|ƒ}t|ƒ}t|ƒ}|s8|r|j |||f¡qWWnDtk r”dt|jƒt|jƒdkr|dnd|f}t  |¡YnXdS)Nz;Warning: MARK chunk contains only %s marker%s instead of %srÚr3) rÚrangerrr@Úappendrr1rzr{)rIrJZnmarkersÚirhrkÚnameÚwrrr rGûszAifc_read._readmark)rrrrBrKrRrSrUrVrWrPrXrZr\r^r`rbrdrerfrjrlrprrrsrvrFrGrrrr r<s2$( *r<c@s0eZdZdZdd„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 dd?„Z"d@dA„Z#dBdC„Z$dDdE„Z%dFdG„Z&dHdI„Z'dS)JÚ Aifc_writeNcCs\t|tƒrNt |d¡}y| |¡Wn| ¡‚YnX| d¡rXd|_n | |¡dS)NÚwbz.aiffr)rMrNrOrrKrPÚendswithrC)rIr!rQrrr rR/s   zAifc_write.__init__cCs^||_t|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ g|_ d|_d|_dS)NsNONEsnot compressedrr)rBÚ _AIFC_versionr>rarcr?rYr]r_r[Ú_nframeswrittenÚ _datawrittenÚ _datalengthr@Ú _marklengthrC)rIrrrr rK?szAifc_write.initfpcCs | ¡dS)N)rP)rIrrr Ú__del__PszAifc_write.__del__cCs|S)Nr)rIrrr rSSszAifc_write.__enter__cGs | ¡dS)N)rP)rIrTrrr rUVszAifc_write.__exit__cCs|jrtdƒ‚d|_dS)Nz0cannot change parameters after starting to writer)r†rrC)rIrrr Úaiff\szAifc_write.aiffcCs|jrtdƒ‚d|_dS)Nz0cannot change parameters after starting to writer)r†rrC)rIrrr ÚaifcaszAifc_write.aifccCs(|jrtdƒ‚|dkrtdƒ‚||_dS)Nz0cannot change parameters after starting to writerzbad # of channels)r†rrY)rIÚ nchannelsrrr Ú setnchannelsfs zAifc_write.setnchannelscCs|jstdƒ‚|jS)Nznumber of channels not set)rYr)rIrrr rZmszAifc_write.getnchannelscCs0|jrtdƒ‚|dks|dkr&tdƒ‚||_dS)Nz0cannot change parameters after starting to writerr zbad sample width)r†rr])rIÚ sampwidthrrr Ú setsampwidthrs zAifc_write.setsampwidthcCs|jstdƒ‚|jS)Nzsample width not set)r]r)rIrrr r^yszAifc_write.getsampwidthcCs(|jrtdƒ‚|dkrtdƒ‚||_dS)Nz0cannot change parameters after starting to writerzbad frame rate)r†rr_)rIÚ frameraterrr Ú setframerate~s zAifc_write.setframeratecCs|jstdƒ‚|jS)Nzframe rate not set)r_r)rIrrr r`…szAifc_write.getframeratecCs|jrtdƒ‚||_dS)Nz0cannot change parameters after starting to write)r†rr[)rIrorrr Ú setnframesŠszAifc_write.setnframescCs|jS)N)r†)rIrrr r\�szAifc_write.getnframescCs.|jrtdƒ‚|dkrtdƒ‚||_||_dS)Nz0cannot change parameters after starting to write)sNONEsulawsULAWsalawsALAWsG722zunsupported compression type)r†rrarc)rIÚcomptypeÚcompnamerrr Ú setcomptype’s zAifc_write.setcomptypecCs|jS)N)ra)rIrrr rb›szAifc_write.getcomptypecCs|jS)N)rc)rIrrr rdžszAifc_write.getcompnamecCsf|\}}}}}}|jrtdƒ‚|dkr.tdƒ‚| |¡| |¡| |¡| |¡| ||¡dS)Nz0cannot change parameters after starting to write)sNONEsulawsULAWsalawsALAWsG722zunsupported compression type)r†rrŽr�r’r“r–)rIZparamsr�r�r‘ror”r•rrr Ú setparams¦s    zAifc_write.setparamscCs8|jr|jr|jstdƒ‚t|j|j|j|j|j|jƒS)Nznot all parameters set)rYr]r_rr;r[rarc)rIrrr re³szAifc_write.getparamscCs†|dkrtdƒ‚|dkr tdƒ‚t|tƒs2tdƒ‚x 0zmarker position must be >= 0zmarker name must be bytes)rrMÚbytesr}r1r@r~)rIrhrkr€rrrr Úsetmark¹s zAifc_write.setmarkcCs2x|jD]}||dkr|SqWtd |¡ƒ‚dS)Nrzmarker {0!r} does not exist)r@rrg)rIrhrirrr rjÆs  zAifc_write.getmarkcCst|jƒdkrdS|jS)Nr)r1r@)rIrrr rfÌszAifc_write.getmarkerscCs|jS)N)r†)rIrrr rXÑszAifc_write.tellcCszt|ttfƒst|ƒ d¡}| t|ƒ¡t|ƒ|j|j}|j rN|  |¡}|j   |¡|j ||_ |j t|ƒ|_ dS)Nr/)rMr˜Ú bytearrayÚ memoryviewÚcastÚ_ensure_header_writtenr1r]rYr?rBr'r†r‡)rIrrorrr ÚwriteframesrawÔs   zAifc_write.writeframesrawcCs.| |¡|j|jks"|j|jkr*| ¡dS)N)ržr†r[rˆr‡Ú _patchheader)rIrrrr Ú writeframesßs   zAifc_write.writeframescCsŽ|jdkrdSz^| d¡|jd@r<|j d¡|jd|_| ¡|j|jksb|j|jksb|jrj|  ¡Wdd|_ |j}d|_|  ¡XdS)Nrrr0) rBr�r‡r'Ú _writemarkersr†r[rˆr‰rŸr?rP)rIr!rrr rPås         zAifc_write.closecCsddl}| |d¡S)Nrr)rqZlin2alaw)rIrrqrrr Ú _lin2alawþszAifc_write._lin2alawcCsddl}| |d¡S)Nrr)rqZlin2ulaw)rIrrqrrr Ú _lin2ulawszAifc_write._lin2ulawcCs2ddl}t|dƒsd|_| |d|j¡\}|_|S)Nrrtr)rqrurtZ lin2adpcm)rIrrqrrr Ú _lin2adpcms  zAifc_write._lin2adpcmcCsf|jsb|jdkr.|jsd|_|jdkr.tdƒ‚|js|jdkr|j|_n&|jdkr(|j|_n|jdkr:|j|_dS)NsG722)sulawsULAW)salawsALAW)rar¤r?r£r¢)rIrrr Ú_init_compressions      zAifc_write._init_compressionc CsJ|jr|jdkr| ¡|j d¡|js<||j|j|_|j|j|j|_|jd@rf|jd|_|jrÊ|jdkrš|jd|_|jd@rÊ|jd|_n0|jdkrÊ|jdd|_|jd@rÊ|jd|_y|j  ¡|_ Wnt t fk rød|_ YnX|  |j¡}|j�rB|j d ¡|j d ¡t|jdƒt|j|jƒn |j d ¡|j d ¡t|j|ƒt|j|jƒ|j dk �rŒ|j  ¡|_t|j|jƒ|jd k�r´t|jdƒnt|j|jdƒt|j|jƒ|j�rø|j |j¡t|j|jƒ|j d¡|j dk �r|j  ¡|_t|j|jdƒt|jdƒt|jdƒdS)NsNONEsFORMr)sulawsULAWsalawsALAWrsG722ér sAIFCsFVERsAIFFsCOMM)sULAWsulawsALAWsalawsG722r=sSSNDr)rCrar¦rBr'r[rYr]rˆrXÚ_form_length_posÚAttributeErrorÚOSErrorÚ_write_form_lengthr-r>r*Ú _nframes_posr8r_r4rcÚ_ssnd_length_pos)rIZ initlengthÚ commlengthrrr r¥%s^                        zAifc_write._write_headercCs\|jr*dt|jƒ}|d@r$|d}d}nd}d}t|jd||jd|d|ƒ|S) Nryré rxrr r=é)rCr1rcr-rBr‰)rIÚ datalengthr®Z verslengthrrr r«Xs zAifc_write._write_form_lengthcCsÜ|j ¡}|jd@r,|jd}|j d¡n|j}||jkrd|j|jkrd|jdkrd|j |d¡dS|j |j d¡|  |¡}|j |j d¡t |j|jƒ|j |j d¡t |j|dƒ|j |d¡|j|_||_dS)Nrr0rr=)rBrXr‡r'rˆr[r†r‰rmr¨r«r¬r-r­)rIZcurposr±rrrr rŸes&       zAifc_write._patchheadercCsÒt|jƒdkrdS|j d¡d}xD|jD]:}|\}}}|t|ƒdd}t|ƒd@dkr*|d}q*Wt|j|ƒ|d|_t|jt|jƒƒx<|jD]2}|\}}}t|j|ƒt|j|ƒt|j|ƒq˜WdS)NrsMARKrrér=)r1r@rBr'r-r‰r*r4)rIrrirhrkr€rrr r¡{s"          zAifc_write._writemarkers)(rrrrBrRrKrŠrSrUr‹rŒrŽrZr�r^r’r`r“r\r–rbrdr—rer™rjrfrXržr rPr¢r£r¤r�r¦r¥r«rŸr¡rrrr r‚sJ    3 r‚cCsJ|dkrt|dƒr|j}nd}|dkr.t|ƒS|dkr>t|ƒStdƒ‚dS)NÚmoderL)ÚrrL)r�rƒz$mode must be 'r', 'rb', 'w', or 'wb')rur³r<r‚r)r!r³rrr rŽs cCstjdtdd�t||d�S)NzBaifc.openfp is deprecated since Python 3.7. Use aifc.open instead.r)Ú stacklevel)r³)rzr{ÚDeprecationWarningr)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.)N)N)8Ú__doc__r rOrzÚ__all__Ú Exceptionrr…rrrrrrr&r*r+r,r-r4r8rJr9Ú collectionsr:r;r�r�r‘ror”r•r<r‚rrrÚsysÚargvr~Úfnr!ÚprintrZr\r^r`rbrdZgnÚgr—rerprr rrrr Ú‡s|  !