grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026o ®û|j]ã @stdZddlZddlZddlZddgZGdd„deƒZdZdd„Zd d „Z d d „Z d d„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!d?d.d„Z"e#d/k�r8ddl$Z$e$j%d0d…s e$j% &d1¡e$j%d0Z'e"e'd2ƒ�ƒZ(e)d3e'ƒe)d4e( *¡ƒe)d5e( +¡ƒe)d6e( ,¡ƒe)d7e( -¡ƒe)d8e( .¡ƒe)d9e( /¡ƒe$j%d:d…�r%e$j%d:Z0e)d;e0ƒe"e0d<ƒ�Z1e1 2e( 3¡¡ e( 4d=¡Z5e5�sne1 6e5¡qúWdƒn 1�swYe)d>ƒWdƒdSWdƒdS1�s1wYdSdS)@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Úopenc@s eZdZdS)rN)Ú__name__Ú __module__Ú __qualname__©rrú+/opt/alt/python310/lib64/python3.10/aifc.pyr�sl@QEcCó0z t d| d¡¡dWStjytd‚w)Nú>lér©ÚstructZunpackÚreadÚerrorÚEOFError©ÚfilerrrÚ _read_long”ó ÿrcCr )Nú>Lr rr rrrrÚ _read_ulongšrrcCr )Nú>hérr rrrrÚ _read_short rrcCr )Nú>Hrrr rrrrÚ _read_ushort¦rrcCs@t| d¡ƒ}|dkrd}n| |¡}|d@dkr| d¡}|S)Néró)Úordr)rÚlengthÚdataÚdummyrrrÚ _read_string¬s   r"gâÿÿÿÿÿïcCsœt|ƒ}d}|dkrd}|d}t|ƒ}t|ƒ}||kr'|kr'dkr/nnd}||S|dkr9t}||S|d}|d|td |d ƒ}||S) Nrréÿÿÿÿé€géÿiÿ?lg@é?)rrÚ _HUGE_VALÚpow)ÚfÚexponÚsignÚhimantÚlomantrrrÚ _read_float¸s "ûþr.cCó| t d|¡¡dS)Nr©Úwriter Úpack©r)ÚxrrrÚ _write_shortÉór5cCr/)Nrr0r3rrrÚ _write_ushortÌr6r7cCr/)Nr r0r3rrrÚ _write_longÏr6r8cCr/)Nrr0r3rrrÚ _write_ulongÒr6r9cCsVt|ƒdkr tdƒ‚| t dt|ƒ¡¡| |¡t|ƒd@dkr)| d¡dSdS)Néÿz%string exceeds maximum pstring lengthÚBrró)ÚlenÚ ValueErrorr1r r2)r)ÚsrrrÚ _write_stringÕs  ÿr@c Csúddl}|dkrd}|d}nd}|dkrd}d}d}nP| |¡\}}|dks/|dks/||kr8|dB}d}d}n4|d}|dkrH| ||¡}d}||B}| |d¡}| |¡}t|ƒ}| ||d¡}| |¡}t|ƒ}t||ƒt||ƒt||ƒdS) Nrr$r#i@rr%iþ?é )ÚmathZfrexpZldexpZfloorÚintr7r9) r)r4rBr+r*r,r-ZfmantZfsmantrrrÚ _write_floatÝs8       rD)Ú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_readNcCs,d|_d|_g|_d|_||_t|ƒ}| ¡dkrtdƒ‚| d¡}|dkr*d|_ n |dkr2d|_ ntdƒ‚d|_ d|_ d|_ zt|jƒ}Wn t yPYn:w| ¡}|d krb| |¡d|_ n#|d krr||_ | d ¡}d|_ n|d kr|t|ƒ|_n |d kr…| |¡| ¡q=|j r�|j s”tdƒ‚dS)NróFORMz file does not start with FORM idr óAIFFóAIFCrznot an AIFF or AIFF-C fileóCOMMóSSNDéóFVERóMARKz$COMM chunk and/or SSND chunk missing)Ú_versionÚ_convertÚ_markersÚ _soundposÚ_filerEZgetnamerrÚ_aifcZ_comm_chunk_readÚ _ssnd_chunkÚ_ssnd_seek_neededrÚ_read_comm_chunkrÚ _readmarkÚskip)ÚselfrÚchunkZformdataZ chunknamer!rrrÚinitfp4sP   ÿ    î ÿzAifc_read.initfpcCsFt|tƒrt |d¡}z| |¡WdS| ¡‚| |¡dS)NÚrb)Ú isinstanceÚstrÚbuiltinsrr^Úclose©r\r)Z file_objectrrrÚ__init__\s  zAifc_read.__init__cCó|S©Nr©r\rrrÚ __enter__hózAifc_read.__enter__cGó | ¡dSrg©rc©r\ÚargsrrrÚ__exit__kó zAifc_read.__exit__cCó|jSrg)rUrhrrrÚgetfpqózAifc_read.getfpcCsd|_d|_dS)Nrr)rXrTrhrrrÚrewindts zAifc_read.rewindcCs$|j}|durd|_| ¡dSdSrg)rUrc©r\rrrrrcxs  þzAifc_read.closecCrqrg)rTrhrrrÚtell~rszAifc_read.tellcCrqrg)Ú _nchannelsrhrrrÚ getnchannels�rszAifc_read.getnchannelscCrqrg)Ú_nframesrhrrrÚ getnframes„rszAifc_read.getnframescCrqrg)Ú _sampwidthrhrrrÚ getsampwidth‡rszAifc_read.getsampwidthcCrqrg)Ú _frameraterhrrrÚ getframerateŠrszAifc_read.getframeratecCrqrg©Ú _comptyperhrrrÚ getcomptype�rszAifc_read.getcomptypecCrqrg©Ú _compnamerhrrrÚ getcompname�rszAifc_read.getcompnamecCs*t| ¡| ¡| ¡| ¡| ¡| ¡ƒSrg)rGrxr|r~rzr�r„rhrrrÚ getparams–s  þzAifc_read.getparamscCót|jƒdkr dS|jS©Nr©r=rSrhrrrÚ getmarkers›ózAifc_read.getmarkerscCó.|jD] }||dkr|Sqtd |¡ƒ‚©Nrzmarker {0!r} does not exist©rSrÚformat©r\ÚidÚmarkerrrrÚgetmark ó  ÿzAifc_read.getmarkcCs*|dks ||jkr tdƒ‚||_d|_dS)Nrzposition not in ranger)ryrrTrX)r\ÚposrrrÚsetpos¦s zAifc_read.setposcCs–|jr"|j d¡|j d¡}|j|j}|r|j |d¡d|_|dkr(dS|j ||j¡}|jr;|r;| |¡}|jt|ƒ|j|j |_|S)NrrNr) rXrWÚseekrrTÚ _framesizerRr=rwr{)r\Únframesr!r”r rrrÚ readframes¬s       ÿzAifc_read.readframescCóddl}| |d¡S©Nrr)ÚaudioopZalaw2lin©r\r rœrrrÚ _alaw2linÁó zAifc_read._alaw2lincCršr›)rœZulaw2linr�rrrÚ _ulaw2linÅrŸzAifc_read._ulaw2lincCó2ddl}t|dƒs d|_| |d|j¡\}|_|S©NrÚ _adpcmstater)rœÚhasattrr£Z adpcm2linr�rrrÚ _adpcm2linÉs  zAifc_read._adpcm2lincCsRt|ƒ|_t|ƒ|_t|ƒdd|_tt|ƒƒ|_|jdkr#tdƒ‚|jdkr,tdƒ‚|j|j|_ |j r¡d}|j dkrGd}t   d¡d |_ | d ¡|_|rnt|j d¡ƒ}|d@dkra|d}|j ||_ |j d d¡t|ƒ|_|jd krŸ|jd kr‚|j|_n|jdvrŒ|j|_n|jdvr–|j|_ntdƒ‚d|_dSdSd |_d|_dS)NérNrúbad sample widthúbad # of channelsérzWarning: bad COMM chunk sizeér r#óNONEóG722©óulawóULAW©óalawóALAWúunsupported compression typerónot compressed)rrwrryr{rCr.r}rr—rVZ chunksizeÚwarningsÚwarnrr€rrr–r"rƒr¥rRr rž)r\r]ZkludgerrrrrYÑsF                  ÷ zAifc_read._read_comm_chunkcCs”t|ƒ}z#t|ƒD]}t|ƒ}t|ƒ}t|ƒ}|s|r$|j |||f¡q WdStyIdt|jƒt|jƒdkrd?„Z"d@dA„Z#dBdC„Z$dDdE„Z%dFdG„Z&dHdI„Z'dS)JÚ Aifc_writeNcCs\t|tƒr't |d¡}z| |¡Wn| ¡‚| d¡r%d|_dSdS| |¡dS)NÚwbz.aiffr)r`rarbrr^rcÚendswithrVrdrrrre/s    ÿzAifc_write.__init__cCs^||_t|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ g|_ d|_d|_dS)Nr«r´rr)rUÚ _AIFC_versionrQr€rƒrRrwr{r}ryÚ_nframeswrittenÚ _datawrittenÚ _datalengthrSÚ _marklengthrVrurrrr^?s zAifc_write.initfpcCrkrgrlrhrrrÚ__del__PrpzAifc_write.__del__cCrfrgrrhrrrriSrjzAifc_write.__enter__cGrkrgrlrmrrrroVrpzAifc_write.__exit__cCó|jrtdƒ‚d|_dS)Nú0cannot change parameters after starting to writer©rÁrrVrhrrrÚaiff\ó zAifc_write.aiffcCrÆ)NrÇrrÈrhrrrÚaifcarÊzAifc_write.aifccCs(|jrtdƒ‚|dkrtdƒ‚||_dS)NrÇrr¨)rÁrrw)r\Ú nchannelsrrrÚ setnchannelsfó  zAifc_write.setnchannelscCó|jstdƒ‚|jS)Nznumber of channels not set)rwrrhrrrrxmózAifc_write.getnchannelscCs0|jrtdƒ‚|dks|dkrtdƒ‚||_dS)NrÇrr r§)rÁrr{)r\Ú sampwidthrrrÚ setsampwidthrs  zAifc_write.setsampwidthcCrÏ)Nzsample width not set)r{rrhrrrr|yrÐzAifc_write.getsampwidthcCs(|jrtdƒ‚|dkrtdƒ‚||_dS)NrÇrzbad frame rate)rÁrr})r\Ú frameraterrrÚ setframerate~rÎzAifc_write.setframeratecCrÏ)Nzframe rate not set)r}rrhrrrr~…rÐzAifc_write.getframeratecCs|jrtdƒ‚||_dS)NrÇ)rÁrry)r\r˜rrrÚ setnframesŠrÊzAifc_write.setnframescCrqrg©rÁrhrrrrz�rszAifc_write.getnframescCs.|jrtdƒ‚|dvrtdƒ‚||_||_dS©NrÇ)r«r®r¯r±r²r¬r³)rÁrr€rƒ)r\ÚcomptypeÚcompnamerrrÚ setcomptype’s  zAifc_write.setcomptypecCrqrgrrhrrrr�›rszAifc_write.getcomptypecCrqrgr‚rhrrrr„žrszAifc_write.getcompnamecCsf|\}}}}}}|jrtdƒ‚|dvrtdƒ‚| |¡| |¡| |¡| |¡| ||¡dSr×)rÁrrÍrÒrÔrÕrÚ)r\ÚparamsrÌrÑrÓr˜rØrÙrrrÚ setparams¦s    zAifc_write.setparamscCs8|jr |jr |js tdƒ‚t|j|j|j|j|j|jƒS)Nznot all parameters set)rwr{r}rrGryr€rƒrhrrrr…³s  ÿzAifc_write.getparamscCs„|dkrtdƒ‚|dkrtdƒ‚t|tƒstdƒ‚tt|jƒƒD]}||j|dkr6|||f|j|<dSq |j |||f¡dS)Nrzmarker ID must be > 0zmarker position must be >= 0zmarker name must be bytes)rr`Úbytesr¸r=rSr¹)r\r�r”r»rºrrrÚsetmark¹s þzAifc_write.setmarkcCr‹rŒr�r�rrrr’Ær“zAifc_write.getmarkcCr†r‡rˆrhrrrr‰ÌrŠzAifc_write.getmarkerscCrqrgrÖrhrrrrvÑrszAifc_write.tellcCszt|ttfƒst|ƒ d¡}| t|ƒ¡t|ƒ|j|j}|j r'|  |¡}|j   |¡|j ||_ |j t|ƒ|_ dS)Nr;)r`rÝÚ bytearrayÚ memoryviewÚcastÚ_ensure_header_writtenr=r{rwrRrUr1rÁrÂ)r\r r˜rrrÚwriteframesrawÔs   zAifc_write.writeframesrawcCs2| |¡|j|jks|j|jkr| ¡dSdSrg)rãrÁryrÃrÂÚ _patchheader)r\r rrrÚ writeframesßs    ÿzAifc_write.writeframescCsÆ|jdurdSzM| d¡|jd@r|j d¡|jd|_| ¡|j|jks1|j|jks1|jrE|  ¡Wd|_ |j}d|_|  ¡dSWd|_ |j}d|_|  ¡dSd|_ |j}d|_|  ¡w)Nrrr<) rUrârÂr1Ú _writemarkersrÁryrÃrÄrärRrc)r\r)rrrrcås4       ÿ  ø ý zAifc_write.closecCršr›)rœZlin2alawr�rrrÚ _lin2alawþrŸzAifc_write._lin2alawcCršr›)rœZlin2ulawr�rrrÚ _lin2ulawrŸzAifc_write._lin2ulawcCr¡r¢)rœr¤r£Z lin2adpcmr�rrrÚ _lin2adpcms  zAifc_write._lin2adpcmcCsj|js3|jdvr|jsd|_|jdkrtdƒ‚|jstdƒ‚|js%tdƒ‚|js,tdƒ‚| |¡dSdS)N©r¯r®r²r±r¬rzRsample 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{rrwr}Ú _write_header)r\Zdatasizerrrrâ s  óz!Aifc_write._ensure_header_writtencCsF|jdkr |j|_dS|jdvr|j|_dS|jdvr!|j|_dSdS)Nr¬r­r°)r€rérRrèrçrhrrrÚ_init_compressions      ÿzAifc_write._init_compressionc Cs@|jr |jdkr | ¡|j d¡|js||j|j|_|j|j|j|_|jd@r3|jd|_|jre|jdvrM|jd|_|jd@rL|jd|_n|jdkre|jdd|_|jd@re|jd|_z|j  ¡|_ Wnt t fy{d|_ Ynw|  |j¡}|jrŸ|j d ¡|j d ¡t|jdƒt|j|jƒn|j d ¡|j d ¡t|j|ƒt|j|jƒ|j durÃ|j  ¡|_t|j|jƒ|jd vrÖt|jdƒn t|j|jdƒt|j|jƒ|jr÷|j |j¡t|j|jƒ|j d¡|j du�r |j  ¡|_t|j|jdƒt|jdƒt|jdƒdS)Nr«rIr)r®r¯r±r²rr¬ér rKrOrJrLrêrNrMr)rVr€rìrUr1ryrwr{rÃrvÚ_form_length_posÚAttributeErrorÚOSErrorÚ_write_form_lengthr9rQr5Ú _nframes_posrDr}r@rƒÚ_ssnd_length_pos)r\Z initlengthÚ commlengthrrrrë%sb       €    ÿ              zAifc_write._write_headercCs\|jrdt|jƒ}|d@r|d}d}nd}d}t|jd||jd|d|ƒ|S) Nrªré r©rr rNé)rVr=rƒr9rUrÄ)r\Ú datalengthrôZ verslengthrrrrñXs"ÿÿÿÿzAifc_write._write_form_lengthcCsÜ|j ¡}|jd@r|jd}|j d¡n|j}||jkr2|j|jkr2|jdkr2|j |d¡dS|j |j d¡|  |¡}|j |j d¡t |j|jƒ|j |j d¡t |j|dƒ|j |d¡|j|_||_dS)Nrr<rrN)rUrvrÂr1rÃryrÁrÄr–rîrñròr9ró)r\Zcurposr÷r!rrrräes&        zAifc_write._patchheadercCsÊt|jƒdkr dS|j d¡d}|jD]}|\}}}|t|ƒdd}t|ƒd@dkr1|d}qt|j|ƒ|d|_t|jt|jƒƒ|jD]}|\}}}t|j|ƒt|j|ƒt|j|ƒqIdS)NrrPrrérN)r=rSrUr1r9rÄr5r@)r\rr‘r�r”r»rrrræ{s&   €      üzAifc_write._writemarkers)(rrrrUrer^rÅrirorÉrËrÍrxrÒr|rÔr~rÕrzrÚr�r„rÜr…rÞr’r‰rvrãrårcrçrèrérârìrërñrärærrrrr½sL    3 r½cCsF|durt|dƒr |j}nd}|dvrt|ƒS|dvrt|ƒStdƒ‚)NÚmoder_)Úrr_)r¼r¾z$mode must be 'r', 'rb', 'w', or 'wb')r¤rùrHr½r)r)rùrrrrŽs Ú__main__rz/usr/demos/data/audio/bach.aiffrúZReadingz nchannels =z nframes =z sampwidth =z framerate =z comptype =z compname =rZWritingr¼izDone.rg)7Ú__doc__r rbrµÚ__all__Ú ExceptionrrÀrrrrr"r'r.r5r7r8r9r@rDr]rEÚ collectionsrFrGrÌrÑrÓr˜rØrÙrHr½rrÚsysÚargvr¹Úfnr)Úprintrxrzr|r~r�r„ZgnÚgrÜr…r™r rårrrrÚsŽ   ! ÿ           üû î$øû