grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026a XC?h]ã @sbdZddlZddlZddlZddgZGdd„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�r^ddl$Z$e$j%d0d…�sBe$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�s�qe1 6e5¡�qôWdƒn1�s,0Ye)d>ƒWdƒn1�sT0YdS)@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/python39/lib64/python3.9/aifc.pyr�sl@QEcCs8zt d| d¡¡dWStjy2td‚Yn0dS)Nú>lér©ÚstructZunpackÚreadÚerrorÚEOFError©ÚfilerrrÚ _read_long”srcCs8zt d| d¡¡dWStjy2td‚Yn0dS)Nú>Lr rr rrrrÚ _read_ulongšsrcCs8zt d| d¡¡dWStjy2td‚Yn0dS)Nú>hérr rrrrÚ _read_short srcCs8zt d| d¡¡dWStjy2td‚Yn0dS)Nú>Hrrr rrrrÚ _read_ushort¦srcCs@t| d¡ƒ}|dkrd}n | |¡}|d@dkr<| d¡}|S)Néró)Úordr )rÚlengthÚdataÚdummyrrrÚ _read_string¬s   r gâÿÿÿÿÿïcCs�t|ƒ}d}|dkr d}|d}t|ƒ}t|ƒ}||krN|krNdkrXnnd}n0|dkrft}n"|d}|d|td |d ƒ}||S) Nrréÿÿÿÿé€géÿiÿ?lg@é?)rrÚ _HUGE_VALÚpow)ÚfÚexponÚsignÚhimantÚlomantrrrÚ _read_float¸s"r,cCs| t d|¡¡dS)Nr©Úwriter Úpack©r'ÚxrrrÚ _write_shortÉsr2cCs| t d|¡¡dS)Nrr-r0rrrÚ _write_ushortÌsr3cCs| t d|¡¡dS)Nr r-r0rrrÚ _write_longÏsr4cCs| t d|¡¡dS)Nrr-r0rrrÚ _write_ulongÒsr5cCsRt|ƒ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   r<c 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) Nrr"r!i@rr#iþ?é )ÚmathZfrexpZldexpZfloorÚintr3r5) r'r1r>r)r(r*r+ZfmantZfsmantrrrÚ _write_floatÝs8       r@)Ú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_readNcCs6d|_d|_g|_d|_||_t|ƒ}| ¡dkr:tdƒ‚| d¡}|dkrTd|_ n|dkrdd|_ ntdƒ‚d|_ d|_ d|_ zt|jƒ}Wnt y¤Y�qYn0| ¡}|d krÈ| |¡d|_ nH|d krè||_ | d ¡}d|_ n(|d krüt|ƒ|_n|d k�r| |¡| ¡qx|j �r*|j �s2tdƒ‚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Ú_filerAZgetnamerr Ú_aifcZ_comm_chunk_readÚ _ssnd_chunkÚ_ssnd_seek_neededrÚ_read_comm_chunkrÚ _readmarkÚskip)ÚselfrÚchunkZformdataZ chunknamerrrrÚinitfp4sH          zAifc_read.initfpcCsLt|tƒr>t |d¡}z| |¡WqH| ¡‚YqH0n | |¡dS)NÚrb)Ú isinstanceÚstrÚbuiltinsrrZÚclose©rXr'Z file_objectrrrÚ__init__\s   zAifc_read.__init__cCs|S©Nr©rXrrrÚ __enter__hszAifc_read.__enter__cGs | ¡dSrb©r_©rXÚargsrrrÚ__exit__kszAifc_read.__exit__cCs|jSrb)rQrcrrrÚgetfpqszAifc_read.getfpcCsd|_d|_dS)Nrr)rTrPrcrrrÚrewindtszAifc_read.rewindcCs |j}|durd|_| ¡dSrb)rQr_©rXrrrrr_xszAifc_read.closecCs|jSrb)rPrcrrrÚtell~szAifc_read.tellcCs|jSrb)Ú _nchannelsrcrrrÚ getnchannels�szAifc_read.getnchannelscCs|jSrb)Ú_nframesrcrrrÚ getnframes„szAifc_read.getnframescCs|jSrb)Ú _sampwidthrcrrrÚ getsampwidth‡szAifc_read.getsampwidthcCs|jSrb)Ú _frameratercrrrÚ getframerateŠszAifc_read.getframeratecCs|jSrb©Ú _comptypercrrrÚ getcomptype�szAifc_read.getcomptypecCs|jSrb©Ú _compnamercrrrÚ getcompname�szAifc_read.getcompnamecCs*t| ¡| ¡| ¡| ¡| ¡| ¡ƒSrb)rCrnrrrtrprwrzrcrrrÚ getparams–s  þzAifc_read.getparamscCst|jƒdkrdS|jS©Nr©r9rOrcrrrÚ getmarkers›szAifc_read.getmarkerscCs2|jD]}||dkr|Sqtd |¡ƒ‚dS©Nrzmarker {0!r} does not exist©rOrÚformat©rXÚidÚmarkerrrrÚgetmark s   zAifc_read.getmarkcCs*|dks||jkrtdƒ‚||_d|_dS)Nrzposition not in ranger)rorrPrT)rXÚ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)NrrJr) rTrSÚseekr rPÚ _framesizerNr9rmrq)rXÚnframesrr†rrrrÚ readframes¬s      ÿ zAifc_read.readframescCsddl}| |d¡S©Nrr)ÚaudioopZalaw2lin©rXrr�rrrÚ _alaw2linÁszAifc_read._alaw2lincCsddl}| |d¡SrŒ)r�Zulaw2linrŽrrrÚ _ulaw2linÅszAifc_read._ulaw2lincCs2ddl}t|dƒsd|_| |d|j¡\}|_|S©NrÚ _adpcmstater)r�Úhasattrr’Z adpcm2linrŽrrrÚ _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|jdv�r |j|_n|jdv�r6|j|_ntdƒ‚d|_n d |_d|_dS)NérJrúbad sample widthúbad # of channelsérzWarning: bad COMM chunk sizeér r!óNONEóG722©óulawóULAW©óalawóALAWúunsupported compression typerónot compressed)rrmrrorqr?r,rsrr‰rRZ chunksizeÚwarningsÚwarnr rvrrrˆr ryr”rNr�r�)rXrYZkludgerrrrrUÑsD                 zAifc_read._read_comm_chunkcCs”t|ƒ}zDt|ƒD]6}t|ƒ}t|ƒ}t|ƒ}|s6|r|j |||f¡qWnBtyŽdt|jƒt|jƒdkrvdnd|f}t  |¡Yn0dS)Nz;Warning: MARK chunk contains only %s marker%s instead of %srÚr;) rÚrangerr rOÚappendrr9r¤r¥)rXrYZnmarkersÚirƒr†ÚnameÚwrrrrVûs  ÿÿzAifc_read._readmark)rrrrQrZrardrhrirjr_rlrnrprrrtrwrzr{r~r…r‡r‹r�r�r”rUrVrrrrrDs2$( *rDc@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¡}z| |¡Wn| ¡‚Yn0| d¡rXd|_n | |¡dS)NÚwbz.aiffr)r\r]r^rrZr_ÚendswithrRr`rrrra/s   zAifc_write.__init__cCs^||_t|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ g|_ d|_d|_dS)Nršr£rr)rQÚ _AIFC_versionrMrvryrNrmrqrsroÚ_nframeswrittenÚ _datawrittenÚ _datalengthrOÚ _marklengthrRrkrrrrZ?szAifc_write.initfpcCs | ¡dSrbrercrrrÚ__del__PszAifc_write.__del__cCs|SrbrrcrrrrdSszAifc_write.__enter__cGs | ¡dSrbrerfrrrrhVszAifc_write.__exit__cCs|jrtdƒ‚d|_dS)Nú0cannot change parameters after starting to writer©r°rrRrcrrrÚaiff\szAifc_write.aiffcCs|jrtdƒ‚d|_dS)Nrµrr¶rcrrrÚaifcaszAifc_write.aifccCs(|jrtdƒ‚|dkrtdƒ‚||_dS)Nrµrr—)r°rrm)rXÚ nchannelsrrrÚ setnchannelsfs zAifc_write.setnchannelscCs|jstdƒ‚|jS)Nznumber of channels not set)rmrrcrrrrnmszAifc_write.getnchannelscCs0|jrtdƒ‚|dks|dkr&tdƒ‚||_dS)Nrµrr r–)r°rrq)rXÚ sampwidthrrrÚ setsampwidthrs zAifc_write.setsampwidthcCs|jstdƒ‚|jS)Nzsample width not set)rqrrcrrrrryszAifc_write.getsampwidthcCs(|jrtdƒ‚|dkrtdƒ‚||_dS)Nrµrzbad frame rate)r°rrs)rXÚ frameraterrrÚ setframerate~s zAifc_write.setframeratecCs|jstdƒ‚|jS)Nzframe rate not set)rsrrcrrrrt…szAifc_write.getframeratecCs|jrtdƒ‚||_dS)Nrµ)r°rro)rXrŠrrrÚ setnframesŠszAifc_write.setnframescCs|jSrb©r°rcrrrrp�szAifc_write.getnframescCs.|jrtdƒ‚|dvrtdƒ‚||_||_dS©Nrµ)ršr�ržr r¡r›r¢)r°rrvry)rXÚcomptypeÚcompnamerrrÚ setcomptype’s zAifc_write.setcomptypecCs|jSrbrurcrrrrw›szAifc_write.getcomptypecCs|jSrbrxrcrrrrzžszAifc_write.getcompnamecCsf|\}}}}}}|jrtdƒ‚|dvr.tdƒ‚| |¡| |¡| |¡| |¡| ||¡dSrÁ)r°rrºr¼r¾r¿rÄ)rXÚparamsr¹r»r½rŠrÂrÃrrrÚ setparams¦s    zAifc_write.setparamscCs8|jr|jr|jstdƒ‚t|j|j|j|j|j|jƒS)Nznot all parameters set)rmrqrsrrCrorvryrcrrrr{³s  ÿzAifc_write.getparamscCs„|dkrtdƒ‚|dkr tdƒ‚t|tƒs2tdƒ‚tt|jƒƒD],}||j|dkr@|||f|j|<dSq@|j |||f¡dS)Nrzmarker ID must be > 0zmarker position must be >= 0zmarker name must be bytes)rr\Úbytesr§r9rOr¨)rXrƒr†rªr©rrrÚsetmark¹s zAifc_write.setmarkcCs2|jD]}||dkr|Sqtd |¡ƒ‚dSrr€r‚rrrr…Æs   zAifc_write.getmarkcCst|jƒdkrdS|jSr|r}rcrrrr~ÌszAifc_write.getmarkerscCs|jSrbrÀrcrrrrlÑszAifc_write.tellcCszt|ttfƒst|ƒ d¡}| t|ƒ¡t|ƒ|j|j}|j rN|  |¡}|j   |¡|j ||_ |j t|ƒ|_ dS)Nr7)r\rÇÚ bytearrayÚ memoryviewÚcastÚ_ensure_header_writtenr9rqrmrNrQr.r°r±)rXrrŠrrrÚwriteframesrawÔs   zAifc_write.writeframesrawcCs.| |¡|j|jks"|j|jkr*| ¡dSrb)rÍr°ror²r±Ú _patchheader)rXrrrrÚ writeframesßs    ÿzAifc_write.writeframescCs¨|jdurdSzx| d¡|jd@r<|j d¡|jd|_| ¡|j|jksb|j|jksb|jrj|  ¡Wd|_ |j}d|_|  ¡nd|_ |j}d|_|  ¡0dS)Nrrr8) rQrÌr±r.Ú _writemarkersr°ror²r³rÎrNr_)rXr'rrrr_ås,       ÿþ  ýzAifc_write.closecCsddl}| |d¡SrŒ)r�Zlin2alawrŽrrrÚ _lin2alawþszAifc_write._lin2alawcCsddl}| |d¡SrŒ)r�Zlin2ulawrŽrrrÚ _lin2ulawszAifc_write._lin2ulawcCs2ddl}t|dƒsd|_| |d|j¡\}|_|Sr‘)r�r“r’Z lin2adpcmrŽrrrÚ _lin2adpcms  zAifc_write._lin2adpcmcCsf|jsb|jdvr.|jsd|_|jdkr.tdƒ‚|js|jdkr|j|_n&|jdvr(|j|_n|jdvr:|j|_dS)Nr›rœrŸ)rvrÓrNrÒrÑrcrrrÚ_init_compressions      zAifc_write._init_compressionc CsH|jr|jdkr| ¡|j d¡|js<||j|j|_|j|j|j|_|jd@rf|jd|_|jrÊ|jdvrš|jd|_|jd@rÊ|jd|_n0|jdkrÊ|jdd|_|jd@rÊ|jd|_z|j  ¡|_ Wnt t fyöd|_ Yn0|  |j¡}|j�r@|j d ¡|j d ¡t|jdƒt|j|jƒn |j d ¡|j d ¡t|j|ƒt|j|jƒ|j du�rŠ|j  ¡|_t|j|jƒ|jd v�r²t|jdƒnt|j|jdƒt|j|jƒ|j�rö|j |j¡t|j|jƒ|j d¡|j du�r|j  ¡|_t|j|jdƒt|jdƒt|jdƒdS)NršrEr)r�ržr r¡rr›ér rGrKrFrHrÔrJrIr)rRrvrÖrQr.rormrqr²rlÚ_form_length_posÚAttributeErrorÚOSErrorÚ_write_form_lengthr5rMr2Ú _nframes_posr@rsr<ryÚ_ssnd_length_pos)rXZ initlengthÚ commlengthrrrrÕ%s^                        zAifc_write._write_headercCs\|jr*dt|jƒ}|d@r$|d}d}nd}d}t|jd||jd|d|ƒ|S) Nr™ré r˜rr rJé)rRr9ryr5rQr³)rXÚ datalengthrÞZ verslengthrrrrÛ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)Nrr8rrJ)rQrlr±r.r²ror°r³rˆrØrÛrÜr5rÝ)rXZcurposrárrrrrÎes*     ÿþ zAifc_write._patchheadercCsÊt|jƒdkrdS|j d¡d}|jD]:}|\}}}|t|ƒdd}t|ƒd@dkr(|d}q(t|j|ƒ|d|_t|jt|jƒƒ|jD]2}|\}}}t|j|ƒt|j|ƒt|j|ƒq’dS)NrrLrrérJ)r9rOrQr.r5r³r2r<)rXrr„rƒr†rªrrrrÐ{s"          zAifc_write._writemarkers)(rrrrQrarZr´rdrhr·r¸rºrnr¼rrr¾rtr¿rprÄrwrzrÆr{rÈr…r~rlrÍrÏr_rÑrÒrÓrÌrÖrÕrÛrÎrÐrrrrr¬sJ    3 r¬cCsJ|durt|dƒr|j}nd}|dvr.t|ƒS|dvr>t|ƒStdƒ‚dS)NÚmoder[)Úrr[)r«r­z$mode must be 'r', 'rb', 'w', or 'wb')r“rãrDr¬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.)N)7Ú__doc__r r^r¤Ú__all__Ú Exceptionrr¯rrrrr r%r,r2r3r4r5r<r@rYrAÚ collectionsrBrCr¹r»r½rŠrÂrÃrDr¬rrÚsysÚargvr¨Úfnr'ÚprintrnrprrrtrwrzZgnÚgrÆr{r‹rrÏrrrrÚs|  !  ÿ          .