QfbdZddlZddlZddlZddgZej edGddeZdZ d Z d Z d Z d Z d ZdZdZdZdZdZdZdZdZej.5ej0deddlmZdddddlmZeddZdej>_dej@_dejB_dejD_dejF_d ejH_Gd!d"Z%Gd#d$Z&d6d%Z'ed&k(rkddl(Z(e(jRd'dse(jRjUd(e(jRd'Z+e'e+d)5Z,e-d*e+e-d+e,j]e-d,e,j_e-d-e,jae-d.e,jce-d/e,jee-d0e,jge(jRd1drye(jRd1Z4e-d2e4e'e4d35Z5e5jme,jo e,jqd4Z9e9sne5jue9& ddde-d5dddyy#1swY xYw#1swY(xYw#1swYyxYw)7aJStuff 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. NErroropen) )removec eZdZy)rN)__name__ __module__ __qualname__+/opt/alt/python312/lib64/python3.12/aifc.pyrrsr l@QEc tjd|jddS#tj$rtdwxYw)N>lrstructunpackreaderrorEOFErrorfiles r _read_longr@!}}T499Q<033 <<!D ! '*Ac tjd|jddS#tj$rtdwxYw)N>Lrrrrs r _read_ulongrrrc tjd|jddS#tj$rtdwxYw)N>hrrrs r _read_shortr#rrc tjd|jddS#tj$rtdwxYw)N>Hr"rrrs r _read_ushortr&rrct|jd}|dk(rd}n|j|}|dzdk(r|jd}|S)Nrr )ordr)rlengthdatadummys r _read_stringr-sK 1 F {yy  zQ !  Kr gct|}d}|dkrd}|dz}t|}t|}||cxk(r |cxk(rdk(r nnd}||zS|dk(r t}||zS|dz }|dz|ztd |d z z}||zS) Nr(rgi?lg@?)r#r _HUGE_VALpow)fexponsignhimantlomants r _read_floatr:s NE D qy ^F ^F %&%A%  !8O &  !8O  k !F *c#urz.B B !8Or cN|jtjd|y)Nr!writerpackr5xs r _write_shortrAGGFKKa !r cN|jtjd|y)Nr%r<r?s r _write_ushortrDrBr cN|jtjd|y)Nrr<r?s r _write_longrFrBr cN|jtjd|y)Nrr<r?s r _write_ulongrHrBr ct|dkDr td|jtjdt||j|t|dzdk(r|jdyy)Nz%string exceeds maximum pstring lengthBr(r)len ValueErrorr=rr>)r5ss r _write_stringrPs_ 1v|@AAGGFKKSV $%GGAJ 1vzQ r cddl}|dkrd}|dz}nd}|dk(rd}d}d}n|j|\}}|dkDs |dk\s||k7r |dz}d}d}n|dz}|dkr|j||}d}||z}|j|d}|j|}t |}|j||z d}|j|}t |}t ||t ||t ||y) Nrr0r/i@r(r1i? )mathfrexpldexpfloorintrDrH) r5r@rSr7r6r8r9fmantfsmants r _write_floatrZs 1u FAvzz!} u 5=EQJ%5.KEFFEMEqy 5%0DLEJJub)EZZ&F[FJJuv~r2EZZ&F[F!UFFr ignore)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)ceZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZy) Aifc_readNcd|_d|_g|_d|_||_t |}|j dk7r td|jd}|dk(rd|_ n|dk(rd|_ n tdd|_ d|_ d|_ t |j}|j }|d k(r|j|d|_ nQ|d k(r ||_ |jd }d|_ n,|d k(rt||_n|d k(r|j!||j##t$rYnwxYw|jr |js tdy)NrFORMz file does not start with FORM idrAIFFAIFCr(znot an AIFF or AIFF-C fileCOMMSSNDFVERMARKz$COMM chunk and/or SSND chunk missing)_version_convert_markers _soundpos_filer\getnamerr_aifc_comm_chunk_read _ssnd_chunk_ssnd_seek_neededr_read_comm_chunkr _readmarkskip)selfrchunkformdata chunknamer,s rinitfpzAifc_read.initfp:sk    d  ==?g %:; ;::a= w DJ  DJ45 5 !%&D " djj) IG#%%e,()%g%#(  1 )*&g% +E 2 g%u% JJL%  $$D,<,<>? ?-=sD?? E  E ct|tr)tj|d} |j |y|j |y#|j xYw)Nrb) isinstancestrbuiltinsrr{closerwr5 file_objects r__init__zAifc_read.__init__bsP a "--40K  K( KKN  !!#s A Ac|SNr rws r __enter__zAifc_read.__enter__n r c$|jyrrrwargss r__exit__zAifc_read.__exit__q  r c|jSr)rnrs rgetfpzAifc_read.getfpws zzr c d|_d|_y)Nr(r)rsrmrs rrewindzAifc_read.rewindzs!"r cP|j}|d|_|jyyr)rnrrwrs rrzAifc_read.close~s&zz  DJ JJL r c|jSr)rmrs rtellzAifc_read.tell ~~r c|jSr) _nchannelsrs r getnchannelszAifc_read.getnchannels r c|jSr)_nframesrs r getnframeszAifc_read.getnframess }}r c|jSr) _sampwidthrs r getsampwidthzAifc_read.getsampwidthrr c|jSr) _frameraters r getframeratezAifc_read.getframeraterr c|jSr _comptypers r getcomptypezAifc_read.getcomptyperr c|jSr _compnamers r getcompnamezAifc_read.getcompnamerr c t|j|j|j|j |j |j Sr)r^rrrrrrrs r getparamszAifc_read.getparamssSD--/1B1B1D --/1B ,,.0@0@0BD Dr cLt|jdk(ry|jSNrrMrlrs r getmarkerszAifc_read.getmarkers t}}  "}}r cp|jD]}||dk(s |cStdj|Nrzmarker {0!r} does not existrlrformatrwidmarkers rgetmarkzAifc_read.getmark9mmFVAY $188<==r c^|dks||jkDr td||_d|_y)Nrzposition not in ranger()rrrmrs)rwposs rsetposzAifc_read.setposs/ 7cDMM)/0 0!"r c|jrv|jjd|jjd}|j|j z}|r|jj|dzd|_|dk(ry|jj||j z}|j r|r|j |}|jt||j|jzzz|_|S)Nrrgr ) rsrrseekrrm _framesizerkrMrr)rwnframesr,rr+s r readframeszAifc_read.readframess  ! !    ! !! $$$))!,E..4??2C  %%cAg.%&D " a<$$Wt%>? ==T==&D#d):>//9J+KK r ctj5tjdtddl}dddj |dS#1swYxYwNr[categoryrr")warningscatch_warnings simplefilterDeprecationWarningaudioopalaw2linrwr+rs r _alaw2linzAifc_read._alaw2linE  $ $ &  ! !(5G H 'a((' & AActj5tjdtddl}dddj |dS#1swYxYwr)rrrrrulaw2linrs r _ulaw2linzAifc_read._ulaw2linrrctj5tjdtddl}dddt |dsd|_j|d|j \}|_|S#1swYCxYwNr[rr _adpcmstater")rrrrrhasattrr adpcm2linrs r _adpcm2linzAifc_read._adpcm2linsk  $ $ &  ! !(5G H 't]+#D !(!2!24D>W,$(OODM^^'99$(NNDM^^'99$(NNDM^^'99$(NNDM >??"#)%DN.DNr c|t|} t|D]F}t|}t|}t|}|s|s)|jj |||fHy#t $rPdt|jdt|jdk(rdndd|}tj|YywxYw)Nz"Warning: MARK chunk contains only z markerr(rOz instead of ) r#rangerr-rlappendrrMrr)rwrxnmarkersirrnamews rruzAifc_read._readmarksu% 8_ ' '#E*$MM(("c49% dmm$C ,>!,Cb&LA MM!   s3A" A""AB;:B;)r r r rnr{rrrrrrrrrrrrrrrrrrrrrrrtrur r rr`r`sH E&@P    D  > # *) ) ) */Xr r`ceZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#d"Z$d#Z%d$Z&d%Z'd&Z(y)' Aifc_writeNct|trBtj|d} |j ||j drd|_yy|j |y#|j xYw)Nwbz.aiffr)r~rrrr{rendswithrprs rrzAifc_write.__init__Csj a "--40K  K( zz'" # KKN !!#s A%%A8c||_t|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ g|_ d|_d|_y)Nrrrr()rn _AIFC_versionrjrrrkrrrr_nframeswritten _datawritten _datalengthrl _marklengthrprs rr{zAifc_write.initfpSso %  *     r c$|jyrrrs r__del__zAifc_write.__del__drr c|Srr rs rrzAifc_write.__enter__grr c$|jyrrrs rrzAifc_write.__exit__jrr c@|jr tdd|_y)N0cannot change parameters after starting to writerrrrprs raiffzAifc_write.aiffp   JK K r c@|jr tdd|_y)Nrr(rrs raifczAifc_write.aifcurr c`|jr td|dkr td||_y)Nrr(r)rrr)rw nchannelss r setnchannelszAifc_write.setnchannelszs1   JK K q=+, ,#r cH|js td|jS)Nznumber of channels not set)rrrs rrzAifc_write.getnchannelss45 5r cj|jr td|dks|dkDr td||_y)Nrr(rr)rrr)rw sampwidths r setsampwidthzAifc_write.setsampwidths7   JK K q=IM*+ +#r cH|js td|jS)Nzsample width not set)rrrs rrzAifc_write.getsampwidths./ /r c`|jr td|dkr td||_y)Nrrzbad frame rate)rrr)rw framerates r setframeratezAifc_write.setframerates1   JK K >() )#r cH|js td|jS)Nzframe rate not set)rrrs rrzAifc_write.getframerates,- -r c@|jr td||_y)Nr)rrr)rwrs r setnframeszAifc_write.setnframess   JK K r c|jSrrrs rrzAifc_write.getnframes###r cl|jr td|dvr td||_||_yNr)rrrrrrrrr)rrrr)rwcomptypecompnames r setcomptypezAifc_write.setcomptypes@   JK K I I67 7!!r c|jSrrrs rrzAifc_write.getcomptyperr c|jSrrrs rrzAifc_write.getcompnamerr c|\}}}}}}|jr td|dvr td|j||j||j ||j ||j ||yr()rrrrr r#r+)rwparamsrrrrr)r*s r setparamszAifc_write.setparamssGMD 9i(H   JK K I I67 7 )$ )$ )$   8,r c|jr|jr |js tdt |j|j|j|j |j |jS)Nznot all parameters set)rrrrr^rrrrs rrzAifc_write.getparamssUdooT__01 1DOOT__doo MM4>>4>>K Kr cN|dkr td|dkr tdt|ts tdtt |j D]+}||j |dk(s|||f|j |<y|j j |||fy)Nrzmarker ID must be > 0zmarker position must be >= 0zmarker name must be bytes)rr~bytesrrMrlr)rwrrrrs rsetmarkzAifc_write.setmarks 7/0 0 767 7$&34 4s4==)*AT]]1%a((#%sD= a + b#t_-r cp|jD]}||dk(s |cStdj|rrrs rrzAifc_write.getmarkrr cLt|jdk(ry|jSrrrs rrzAifc_write.getmarkersrr c|jSrr%rs rrzAifc_write.tellr&r ct|ttfst|j d}|j t |t ||j|jzz}|jr|j|}|jj||j|z|_ |jt |z|_ y)NrK)r~r3 bytearray memoryviewcast_ensure_header_writtenrMrrrkrnr=rr)rwr+rs rwriteframesrawzAifc_write.writeframesraws$ 23d#((-D ##CI.d)$// AB ====&D #33g= --D 9r c|j||j|jk7s|j|jk7r|j yyr)r=rrr r _patchheader)rwr+s r writeframeszAifc_write.writeframessG D!   4== 0$"3"33    4r c,|jy |jd|jdzr/|jjd|jdz|_|j |j |j k7s%|j|jk7s |jr|jd|_ |j}d|_|jy#d|_ |j}d|_|jwxYw)Nrr(rL) rnr<rr= _writemarkersrrr r r?rkr)rwr5s rrzAifc_write.closes ::     ' ' *  1$   )$($5$5$9!    ##t}}4""d&7&77""!!#!DM ADJ GGI!DM ADJ GGIs B-C'',Dctj5tjdtddl}dddj |dS#1swYxYwr)rrrrrlin2alawrs r _lin2alawzAifc_write._lin2alawrrctj5tjdtddl}dddj |dS#1swYxYwr)rrrrrlin2ulawrs r _lin2ulawzAifc_write._lin2ulawrrctj5tjdtddl}dddt |dsd|_j|d|j \}|_|S#1swYCxYwr)rrrrrrr lin2adpcmrs r _lin2adpcmzAifc_write._lin2adpcmsk  $ $ &  ! !(5G H 't]+#D !(!2!24D>W $ OODM ^^1 1 NNDM ^^1 1 NNDM ^^1 1 NNDM2r ch|jr|jdk7r|j|jj d|j s!||j |jzz|_|j |j z|jz|_|jdzr|jdz|_|jr|jdvr8|jdz|_|jdzr^|jdz|_nI|jdk(r:|jdzdz|_|jdzr|jdz|_ |jj|_ |j|j}|jrm|jj d |jj d t|jdt|j|jn|jj d |jj d t|j|t!|j|j |j|jj|_t|j|j |jd vrt!|jdn#t!|j|jdzt%|j|j&|jrE|jj |jt)|j|j*|jj d|j|jj|_t|j|jdzt|jdt|jdy#ttf$r d|_ YwxYw)Nrrbr()rrrrr"rrrrdrhrcre)rrrrrrgrfr)rprrRrnr=rrrr r_form_length_posAttributeErrorOSError_write_form_lengthrHrjrA _nframes_posrZrrPr_ssnd_length_pos)rw initlength commlengths rrOzAifc_write._write_headerJs ::$..G3  " " $ !}}&4??T__+LMDM==4??:T__L   a #//!3D  ::~~!EE#'#3#3q#8 ##a''+'7'7!';D$7*$($4$4q$8Q#> ##a''+'7'7!';D$ )$(JJOO$5D !,,T-=-=> :: JJ  W % JJ  W % Q ' T]] 3 JJ  W % !TZZ,TZZ1  , $  1D TZZ/ >>J J Q ' T__q%8 9TZZ1 :: JJ  T^^ , $**dnn 5 !  ,$(JJOO$5D !TZZ!1!1A!56TZZ#TZZ#;( )$(D ! )sPP10P1c|jr%dt|jz}|dzr|dz}d}nd}d}t|jd|z|j zdz|zdz|z|S) Nrr( rrrrg)rprMrrHrnr )rw datalengthr[ verslengths rrWzAifc_write._write_form_length}s ::#dnn"55JA~'!^ JJJTZZZ$2B2B!B"#"$&(")+5"6 7r cL|jj}|jdzr+|jdz}|jjdn |j}||jk(rE|j |j k(r,|jdk(r|jj|dy|jj|jd|j|}|jj|jdt|j|j |jj|jdt|j|dz|jj|d|j |_||_y)Nr(rLrrg)rnrrr=r rrr rrTrWrXrHrY)rwcurposr_r,s rr?zAifc_write._patchheaders5"   q **Q.J JJ  W %**J )) )mmt333!# JJOOFA &  --q1'' 3 ))1-TZZ!5!56 --q1TZZa0 ",, %r c8t|jdk(ry|jjdd}|jD]3}|\}}}|t|zdzdz}t|dzdk(s/|dz}5t |j||dz|_t |jt|j|jD]J}|\}}}t |j|t |j|t|j|Ly)Nrrir"r(rg)rMrlrnr=rHr rArP)rwr*rrrrs rrBzAifc_write._writemarkerss t}}  "  !mmF"MBTc$i'!+a/F4y1}!! $ TZZ(!A:TZZT]]!34mmF"MBT R ( S ) $**d + $r ))r r r rnrr{r rrrrrrrrr rr#rr+rrr0rr4rrrr=r@rrErHrKrMr<rRrOrWr?rBr r rrr#s< E "   $ $ $  $" -K .>  $ : 2) ) ) )&+1$f &,,r rc|t|dr |j}nd}|dvr t|S|dvr t|St d)Nmoder})rr})rrz$mode must be 'r', 'rb', 'w', or 'wb')rrfr`rr)r5rfs rrrsO | 1f 66DD {|  !}:;;r __main__r(z/usr/demos/data/audio/bach.aiffrgReadingz nchannels =z nframes =z sampwidth =z framerate =z comptype =z compname =r"WritingrizDone.r);__doc__rrr__all__ _deprecatedr Exceptionrrrrr#r&r-r3r:rArDrFrHrPrZrrrrxr\ collectionsr]r^rrrrr)r*r`rrsysargvrfnr5printrrrrrrgngr0rrr+r@r r rrvsFP F Xg. I  ! ! ! !  " """""BXH($67#.SU "W !8 !5 7  J "%  MM^N,N,` < z 88AB< 9: !B b#! i mQ^^-. mQ\\^, mQ^^-. mQ^^-. mQ]]_- mQ]]_- 88AB<!B )R b#! AKKM*<<-DMM$'   'N%  ^ s2+J &CJ%*AJ2J% JJ" J%%J.