U e5d.€ã @sBdZddlZddlZddlZdddgZGdd„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ƒ�6Z2e2 3e) 4¡¡e) 5d?¡Z6e6�s�q"e2 7e6¡�qW5QRXe*d@ƒW5QRXdS)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ú/usr/lib64/python3.8/aifc.pyr�sl@QEcCs:zt d| d¡¡dWStjk r4td‚YnXdS)Nú>lér©ÚstructZunpackÚreadÚerrorÚEOFError©Úfilerrr Ú _read_long”srcCs:zt d| d¡¡dWStjk r4td‚YnXdS)Nú>Lr rr rrrr Ú _read_ulongšsrcCs:zt d| d¡¡dWStjk r4td‚YnXdS)Nú>hérr rrrr Ú _read_short srcCs:zt d| d¡¡dWStjk r4td‚YnXdS)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Ésr3cCs| t d|¡¡dS)Nrr.r1rrr Ú _write_ushortÌsr4cCs| t d|¡¡dS)Nr r.r1rrr Ú _write_longÏsr5cCs| t d|¡¡dS)Nrr.r1rrr Ú _write_ulongÒsr6cCsRt|ƒdkrtdƒ‚| t dt|ƒ¡¡| |¡t|ƒd@dkrN| d¡dS)Néÿz%string exceeds maximum pstring lengthÚBrró)ÚlenÚ ValueErrorr/r r0)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Úintr4r6) r(r2r?r*r)r+r,ZfmantZfsmantrrr Ú _write_floatÝs8       rA)Ú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|_ d|_ zt|jƒ}Wnt k r¦Y�qYnX| ¡}|d krÊ| |¡d|_ nH|d krê||_ | d ¡}d|_ n(|d krþt|ƒ|_n|d k�r| |¡| ¡qx|j �r,|j �s4tdƒ‚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Ú_filerBZgetnamerrÚ_aifcZ_comm_chunk_readÚ _ssnd_chunkÚ_ssnd_seek_neededrÚ_read_comm_chunkrÚ _readmarkÚskip)ÚselfrÚchunkZformdataZ chunknamer rrr Úinitfp4sH         zAifc_read.initfpcCsLt|tƒr>t |d¡}z| |¡WqH| ¡‚YqHXn | |¡dS)NÚrb)Ú isinstanceÚstrÚbuiltinsrr[Úclose©rYr(Z file_objectrrr Ú__init__\s   zAifc_read.__init__cCs|S©Nr©rYrrr Ú __enter__hszAifc_read.__enter__cGs | ¡dSrc©r`©rYÚargsrrr Ú__exit__kszAifc_read.__exit__cCs|jSrc)rRrdrrr ÚgetfpqszAifc_read.getfpcCsd|_d|_dS)Nrr)rUrQrdrrr ÚrewindtszAifc_read.rewindcCs |j}|dk rd|_| ¡dSrc)rRr`©rYrrrr r`xszAifc_read.closecCs|jSrc)rQrdrrr Útell~szAifc_read.tellcCs|jSrc)Ú _nchannelsrdrrr Ú getnchannels�szAifc_read.getnchannelscCs|jSrc)Ú_nframesrdrrr Ú getnframes„szAifc_read.getnframescCs|jSrc)Ú _sampwidthrdrrr Ú getsampwidth‡szAifc_read.getsampwidthcCs|jSrc)Ú _frameraterdrrr Ú getframerateŠszAifc_read.getframeratecCs|jSrc©Ú _comptyperdrrr Ú getcomptype�szAifc_read.getcomptypecCs|jSrc©Ú _compnamerdrrr Ú getcompname�szAifc_read.getcompnamecCs*t| ¡| ¡| ¡| ¡| ¡| ¡ƒSrc)rDrorsrurqrxr{rdrrr Ú getparams–s þzAifc_read.getparamscCst|jƒdkrdS|jS©Nr©r:rPrdrrr Ú getmarkers›szAifc_read.getmarkerscCs2|jD]}||dkr|Sqtd |¡ƒ‚dS©Nrzmarker {0!r} does not exist©rPrÚformat©rYÚidÚmarkerrrr Úgetmark s   zAifc_read.getmarkcCs*|dks||jkrtdƒ‚||_d|_dS)Nrzposition not in ranger)rprrQrU)rYÚ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)NrrKr) rUrTÚseekrrQÚ _framesizerOr:rnrr)rYÚnframesr r‡rrrr Ú readframes¬s      ÿ zAifc_read.readframescCsddl}| |d¡S©Nrr)ÚaudioopZalaw2lin©rYrrŽ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|jdk�r |j|_n|jdk�r6|j|_ntdƒ‚d|_n d |_d|_dS)NérKrúbad sample widthúbad # of channelsérzWarning: bad COMM chunk sizeér r"óNONEóG722©óulawóULAW©óalawóALAWúunsupported compression typerónot compressed)rrnrrprrr@r-rtrrŠrSZ chunksizeÚwarningsÚwarnrrwrrr‰r!rzr•rOr‘r�)rYrZZkludgerrrr rVÑsD                 zAifc_read._read_comm_chunkcCs–t|ƒ}zDt|ƒD]6}t|ƒ}t|ƒ}t|ƒ}|s6|r|j |||f¡qWnDtk r�dt|jƒt|jƒdkrxdnd|f}t  |¡YnXdS)Nz;Warning: MARK chunk contains only %s marker%s instead of %srÚr<) rÚrangerr!rPÚappendrr:r¥r¦)rYrZZnmarkersÚir„r‡ÚnameÚwrrr rWûs ÿÿzAifc_read._readmark)rrrrRr[rbrerirjrkr`rmrorqrsrurxr{r|rr†rˆrŒr�r‘r•rVrWrrrr rEs2$( *rEc@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| ¡‚YnX| d¡rXd|_n | |¡dS)NÚwbz.aiffr)r]r^r_rr[r`ÚendswithrSrarrr rb/s   zAifc_write.__init__cCs^||_t|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ g|_ d|_d|_dS)Nr›r¤rr)rRÚ _AIFC_versionrNrwrzrOrnrrrtrpÚ_nframeswrittenÚ _datawrittenÚ _datalengthrPÚ _marklengthrSrlrrr r[?szAifc_write.initfpcCs | ¡dSrcrfrdrrr Ú__del__PszAifc_write.__del__cCs|Srcrrdrrr reSszAifc_write.__enter__cGs | ¡dSrcrfrgrrr riVszAifc_write.__exit__cCs|jrtdƒ‚d|_dS)Nú0cannot change parameters after starting to writer©r±rrSrdrrr Úaiff\szAifc_write.aiffcCs|jrtdƒ‚d|_dS)Nr¶rr·rdrrr ÚaifcaszAifc_write.aifccCs(|jrtdƒ‚|dkrtdƒ‚||_dS)Nr¶rr˜)r±rrn)rYÚ nchannelsrrr Ú setnchannelsfs zAifc_write.setnchannelscCs|jstdƒ‚|jS)Nznumber of channels not set)rnrrdrrr romszAifc_write.getnchannelscCs0|jrtdƒ‚|dks|dkr&tdƒ‚||_dS)Nr¶rr r—)r±rrr)rYÚ sampwidthrrr Ú setsampwidthrs zAifc_write.setsampwidthcCs|jstdƒ‚|jS)Nzsample width not set)rrrrdrrr rsyszAifc_write.getsampwidthcCs(|jrtdƒ‚|dkrtdƒ‚||_dS)Nr¶rzbad frame rate)r±rrt)rYÚ frameraterrr Ú setframerate~s zAifc_write.setframeratecCs|jstdƒ‚|jS)Nzframe rate not set)rtrrdrrr ru…szAifc_write.getframeratecCs|jrtdƒ‚||_dS)Nr¶)r±rrp)rYr‹rrr Ú setnframesŠszAifc_write.setnframescCs|jSrc©r±rdrrr rq�szAifc_write.getnframescCs.|jrtdƒ‚|dkrtdƒ‚||_||_dS©Nr¶)r›ržrŸr¡r¢rœr£)r±rrwrz)rYÚcomptypeÚcompnamerrr Ú setcomptype’s zAifc_write.setcomptypecCs|jSrcrvrdrrr rx›szAifc_write.getcomptypecCs|jSrcryrdrrr r{žszAifc_write.getcompnamecCsf|\}}}}}}|jrtdƒ‚|dkr.tdƒ‚| |¡| |¡| |¡| |¡| ||¡dSrÂ)r±rr»r½r¿rÀrÅ)rYZparamsrº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)rnrrrtrrDrprwrzrdrrr r|³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¨r:rPr©)rYr„r‡r«rªrrr Úsetmark¹s zAifc_write.setmarkcCs2|jD]}||dkr|Sqtd |¡ƒ‚dSr€r�rƒrrr r†Æs   zAifc_write.getmarkcCst|jƒdkrdS|jSr}r~rdrrr rÌszAifc_write.getmarkerscCs|jSrcrÁrdrrr rmÑszAifc_write.tellcCszt|ttfƒst|ƒ d¡}| t|ƒ¡t|ƒ|j|j}|j rN|  |¡}|j   |¡|j ||_ |j t|ƒ|_ dS)Nr8)r]rÇÚ bytearrayÚ memoryviewÚcastÚ_ensure_header_writtenr:rrrnrOrRr/r±r²)rYrr‹rrr ÚwriteframesrawÔs   zAifc_write.writeframesrawcCs.| |¡|j|jks"|j|jkr*| ¡dSrc)rÍr±rpr³r²Ú _patchheader)rYrrrr Ú writeframesßs    ÿzAifc_write.writeframescCsŽ|jdkrdSz^| d¡|jd@r<|j d¡|jd|_| ¡|j|jksb|j |jksb|j rj|  ¡W5d|_|j}d|_| ¡XdS)Nrrr9) rRrOr`rÌr²r/Ú _writemarkersr±rpr³r´rÎ)rYr(rrr r`å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|jdkr.|jsd|_|jdkr.tdƒ‚|js|jdkr|j|_n&|jdkr(|j|_n|jdkr:|j|_dS)Nrœr�r )rwrÓrOrÒrÑrdrrr Ú_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|_z|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)Nr›rFr)ržrŸr¡r¢rrœér rHrLrGrIrÔrKrJr)rSrwrÖrRr/rprnrrr³rmÚ_form_length_posÚAttributeErrorÚOSErrorÚ_write_form_lengthr6rNr3Ú _nframes_posrArtr=rzÚ_ssnd_length_pos)rYZ initlengthÚ commlengthrrr rÕ%s^                        zAifc_write._write_headercCs\|jr*dt|jƒ}|d@r$|d}d}nd}d}t|jd||jd|d|ƒ|S) Nršré r™rr rKé)rSr:rzr6rRr´)rYÚ 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)Nrr9rrK)rRrmr²r/r³rpr±r´r‰rØrÛrÜr6rÝ)rYZcurposrár rrr rÎ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)NrrMrrérK)r:rPrRr/r6r´r3r=)rYrr…r„r‡r«rrr rÐ{s"          zAifc_write._writemarkers)(rrrrRrbr[rµrerir¸r¹r»ror½rsr¿rurÀrqrÅrxr{rÆr|rÈr†rrmrÍrÏr`rÑ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Úmoder\)Úrr\)r¬r®z$mode must be 'r', 'rb', 'w', or 'wb')r”rãrEr­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ã)r¥r¦ÚDeprecationWarningrrå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 r_r¥Ú__all__Ú Exceptionrr°rrrrr!r&r-r3r4r5r6r=rArZrBÚ collectionsrCrDrºr¼r¾r‹rÃrÄrEr­rrrÚsysÚargvr©Úfnr(Úprintrorqrsrurxr{ZgnÚgrÆr|rŒrrÏrrrr Ús~   !  ÿ