NfdZddlZddlZddlZddgZejedGddeZdZ d Z d Z d Z d Z d ZdZdZdZdZdZdZdZdZej5ejdeddlmZdddn #1swxYwYddlmZeddZdej_dej _dej!_dej"_dej#_d ej$_Gd!d"Z%Gd#d$Z&d6d%Z'ed&krddl(Z(e(j)d'dse(j)*d(e(j)d'Z+e'e+d)5Z,e-d*e+e-d+e,.e-d,e,/e-d-e,0e-d.e,1e-d/e,2e-d0e,3e(j)d1dre(j)d1Z4e-d2e4e'e4d35Z5e56e,7 e,8d4Z9e9sne5:e9. dddn #1swxYwYe-d5ddddS#1swxYwYdSdS)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) )removeceZdZdS)rN)__name__ __module__ __qualname__/usr/lib/python3.11/aifc.pyrrsDr l@QEc tjd|ddS#tj$r tdwxYw)N>lrstructunpackreaderrorEOFErrorfiles r _read_longrM!}T499Q<<0033 <!!!D ! -0Ac tjd|ddS#tj$r tdwxYw)N>Lrrrrs r _read_ulongrrrc tjd|ddS#tj$r tdwxYw)N>hrrrs r _read_shortr#rrc tjd|ddS#tj$r tdwxYw)N>Hr"rrrs r _read_ushortr&rrct|d}|dkrd}n||}|dzdkr|d}|S)Nrr )ordr)rlengthdatadummys r _read_stringr-s[ 1  F {{yy   zQ !  Kr gct|}d}|dkrd}|dz}t|}t|}||cxkr |cxkrdkrnnd}n/|dkrt}n!|dz }|dz|ztd |d z z}||zS) Nr(rgi?lg@?)r#r _HUGE_VALpow)fexponsignhimantlomants r _read_floatr:s NNE D qyy ^^F ^^F %%%%&%%%%A%%%%%  &   k !F *c#urz.B.B B !8Or cV|tjd|dS)Nr!writerpackr5xs r _write_shortrA&GGFKa !!!!!r cV|tjd|dS)Nr%r<r?s r _write_ushortrDrBr cV|tjd|dS)Nrr<r?s r _write_longrFrBr cV|tjd|dS)Nrr<r?s r _write_ulongrHrBr c8t|dkrtd|tjdt|||t|dzdkr|ddSdS)Nz%string exceeds maximum pstring lengthBr(r)len ValueErrorr=rr>)r5ss r _write_stringrPs 1vv||@AAAGGFKSVV $ $%%%GGAJJJ 1vvzQ r cNddl}|dkrd}|dz}nd}|dkrd}d}d}n||\}}|dks |dks||kr |dz}d}d}n|dz}|dkr|||}d}||z}||d}||}t |}|||z d}||}t |}t ||t ||t ||dS) Nrr0r/i@r(r1i? )mathfrexpldexpfloorintrDrH) r5r@rSr7r6r8r9fmantfsmants r _write_floatrZsOKKK1uu FAvvzz!}} u 5==EQJJ%5..KEFFFEMEqyy 5%00DLEJJub))EZZ&&F[[FJJuv~r22EZZ&&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 ZdZdZdZdZdZdZdZdZdZdZdZdZdZdS) Aifc_readNcBd|_d|_g|_d|_||_t |}|dkrtd|d}|dkrd|_ n|dkrd|_ ntdd|_ d|_ d|_ t |j}n#t$rYnwxYw|}|d kr||d|_ n`|d kr$||_ |d }d|_ n6|d krt||_n|d kr||||j r|j stddS)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:s    d  ==??g % %:;; ;::a== w  DJJ  DJJ455 5 ! %&D " dj))     IG##%%e,,,()%%g%%#(  1 )*&&g%% +E 2 2 g%%u%%% JJLLL% &$ @D,< @>?? ? @ @s1C CCct|trGtj|d} ||dS#|xYw||dS)Nrb) isinstancestrbuiltinsrr{closerwr5 file_objects r__init__zAifc_read.__init__bsu a   "-400K  K((((( !!### KKNNNNNs AAc|SNr rws r __enter__zAifc_read.__enter__n r c.|dSrrrwargss r__exit__zAifc_read.__exit__q r c|jSr)rnrs rgetfpzAifc_read.getfpws zr c"d|_d|_dS)Nr(r)rsrmrs rrewindzAifc_read.rewindzs!"r cR|j}|d|_|dSdSr)rnrrwrs rrzAifc_read.close~s/z  DJ JJLLLLL  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||||||Sr)r^rrrrrrrs r getparamszAifc_read.getparamsskD--//1B1B1D1D --//1B1B ,,..0@0@0B0BDD Dr cDt|jdkrdS|jSNrrMrlrs r getmarkerszAifc_read.getmarkers$ t}   " "4}r cz|jD]}||dkr|cStd|Nrzmarker {0!r} does not existrlrformatrwidmarkers rgetmarkzAifc_read.getmarkKm  FVAY 188<<===r cb|dks ||jkrtd||_d|_dS)Nrzposition not in ranger()rrrmrs)rwposs rsetposzAifc_read.setposs; 77cDM))/00 0!"r c|jri|jd|jd}|j|jz}|r|j|dzd|_|dkrdS|j||jz}|jr|r||}|jt||j|j zzz|_|S)Nrrgr ) rsrrseekrrm _framesizerkrMrr)rwnframesr,rr+s r readframeszAifc_read.readframess  ! '   ! !! $ $ $$))!,,E.4?2C / %%cAg...%&D " a<<3$$Wt%>?? = 'T '==&&D#d)):>/9J+KK r ctj5tjdtddl}dddn #1swxYwY||dSNr[categoryrr")warningscatch_warnings simplefilterDeprecationWarningaudioopalaw2linrwr+rs r _alaw2linzAifc_read._alaw2lin  $ & &    !(5G H H H H NNN               a((( AAActj5tjdtddl}dddn #1swxYwY||dSr)rrrrrulaw2linrs r _ulaw2linzAifc_read._ulaw2linrrctj5tjdtddl}dddn #1swxYwYt |dsd|_||d|j\}|_|SNr[rr _adpcmstater")rrrrrhasattrr adpcm2linrs r _adpcm2linzAifc_read._adpcm2lins  $ & &    !(5G H H H H NNN               t]++ $#D !(!2!24DW,,$(ODMM^'999$(NDMM^'999$(NDMM^'999$(NDMM >???"#)(%DN.DNNNr ct|} t|D]P}t|}t|}t|}|s|r|j|||fQdS#t $rOdt|jdt|jdkrdndd|}tj |YdSwxYw)Nz"Warning: MARK chunk contains only z markerr(rOz instead of ) r#rangerr-rlappendrrMrr)rwrxnmarkersirrnamews rruzAifc_read._readmarksu%% 8__ : : '' ''#E**:$:M(("c4999 : :    dm$$$$C ,>,>!,C,Cbb&L&L(A M!        sA A33AC  C )r r r rnr{rrrrrrrrrrrrrrrrrrrrrrrtrur r rr`r`sH E&@&@&@P      DDD  >>> ### *))) ))) ))) */*/*/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(dS)' Aifc_writeNc&t|trftj|d} ||n#|xYw|dr d|_dSdS||dS)Nwbz.aiffr)r~rrrr{rendswithrprs rrzAifc_write.__init__Cs a   "-400K  K(((( !!###zz'""     KKNNNNNs AAc||_t|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ g|_ d|_d|_dS)Nrrrr()rn _AIFC_versionrjrrrkrrrr_nframeswritten _datawritten _datalengthrl _marklengthrprs rr{zAifc_write.initfpSss %  *     r c.|dSrrrs r__del__zAifc_write.__del__drr c|Srr rs rrzAifc_write.__enter__grr c.|dSrrrs rrzAifc_write.__exit__jrr c@|jrtdd|_dS)N0cannot change parameters after starting to writerrrrprs raiffzAifc_write.aiffp(   LJKK K r c@|jrtdd|_dS)Nrr(rrs raifczAifc_write.aifcurr cj|jrtd|dkrtd||_dS)Nrr(r)rrr)rw nchannelss r setnchannelszAifc_write.setnchannelszs@   LJKK K q==+,, ,#r c<|jstd|jS)Nznumber of channels not set)rrrs rrzAifc_write.getnchannelss# 6455 5r cv|jrtd|dks|dkrtd||_dS)Nrr(rr)rrr)rw sampwidths r setsampwidthzAifc_write.setsampwidthsH   LJKK K q==IMM*++ +#r c<|jstd|jS)Nzsample width not set)rrrs rrzAifc_write.getsampwidths# 0.// /r cj|jrtd|dkrtd||_dS)Nrrzbad frame rate)rrr)rw framerates r setframeratezAifc_write.setframerates@   LJKK K >>()) )#r c<|jstd|jS)Nzframe rate not set)rrrs rrzAifc_write.getframerates# .,-- -r c@|jrtd||_dS)Nr)rrr)rwrs r setnframeszAifc_write.setnframess(   LJKK K r c|jSrrrs rrzAifc_write.getnframes ##r ct|jrtd|dvrtd||_||_dSNr)rrrrrrrrr)rrrr)rwcomptypecompnames r setcomptypezAifc_write.setcomptypesS   LJKK K I I I677 7!!r c|jSrrrs rrzAifc_write.getcomptyperr c|jSrrrs rrzAifc_write.getcompnamerr c>|\}}}}}}|jrtd|dvrtd|||||||||||dSr')rrrrrr"r*)rwparamsrrrrr(r)s r setparamszAifc_write.setparamssGMD 9i(H   LJKK K I I I677 7 )$$$ )$$$ )$$$     8,,,,,r c|jr|jr|jstdt |j|j|j|j|j|jS)Nznot all parameters set)rrrrr^rrrrs rrzAifc_write.getparamss[ 2do 2T_ 2011 1DOT_do M4>4>KK Kr cr|dkrtd|dkrtdt|tstdtt |jD])}||j|dkr|||f|j|<dS*|j|||fdS)Nrzmarker ID must be > 0zmarker position must be >= 0zmarker name must be bytes)rr~bytesrrMrlr)rwrrrrs rsetmarkzAifc_write.setmarks 77/00 0 77677 7$&& 5344 4s4=))**  AT]1%a(((#%sD= a ) b#t_-----r cz|jD]}||dkr|cStd|rrrs rrzAifc_write.getmarkrr cDt|jdkrdS|jSrrrs rrzAifc_write.getmarkersrr c|jSrr$rs rrzAifc_write.tellr%r ct|ttfs"t|d}|t |t ||j|jzz}|j r| |}|j ||j |z|_ |j t |z|_ dS)NrK)r~r2 bytearray memoryviewcast_ensure_header_writtenrMrrrkrnr=rr)rwr+rs rwriteframesrawzAifc_write.writeframesraws$ 233 .d##((--D ##CII...d))$/ AB = '==&&D #3g= -D 9r c|||j|jks|j|jkr|dSdSr)r<rrrr _patchheader)rwr+s r writeframeszAifc_write.writeframessW D!!!  4= 0 0$"333        43r c|jdS |d|jdzr)|jd|jdz|_||j|jks|j|jks|jr| d|_ |j}d|_| dS#d|_ |j}d|_| wxYw)Nrr(rL) rnr;rr= _writemarkersrrrr r>rkr)rwr5s rrzAifc_write.closes :  F   ' ' * * * 1$ :   )))$($5$9!    #t}44"d&777"8!!###!DM ADJ GGIIIII!DM ADJ GGIIIIs BC +C8ctj5tjdtddl}dddn #1swxYwY||dSr)rrrrrlin2alawrs r _lin2alawzAifc_write._lin2alawrrctj5tjdtddl}dddn #1swxYwY||dSr)rrrrrlin2ulawrs r _lin2ulawzAifc_write._lin2ulawrrctj5tjdtddl}dddn #1swxYwYt |dsd|_||d|j\}|_|Sr)rrrrrrr lin2adpcmrs r _lin2adpcmzAifc_write._lin2adpcms  $ & &    !(5G H H H H NNN               t]++ $#D !(!2!24DW $ $ ODMMM ^1 1 1 NDMMM ^1 1 1 NDMMM ^1 1 1 NDMMM2 1r c |jr|jdkr||jd|js||j|jzz|_|j|jz|jz|_|jdzr|jdz|_|jrh|jdvr)|jdz|_|jdzr|jdz|_n6|jdkr+|jdzdz|_|jdzr|jdz|_ |j |_ n#ttf$r d|_ YnwxYw| |j}|jrd|jd |jd t|jdt|j|jn|jd |jd t|j|t!|j|j|j |j |_t|j|j|jd vrt!|jdnt!|j|jdzt%|j|j|jr9|j|jt)|j|j|jd|j |j |_t|j|jdzt|jdt|jddS)Nrrbr()rrrrr"rrrrdrhrcre)rrrrrrgrfr)rprrQrnr=rrrrr_form_length_posAttributeErrorOSError_write_form_lengthrHrjrA _nframes_posrZrrPr_ssnd_length_pos)rw initlength commlengths rrNzAifc_write._write_headerJsb : %$.G33  " " $ $ $ !!!} N&4?T_+LMDM=4?:T_L  a  4#/!3D  : <~!EEE#'#3q#8 #a'<'+'7!';D$7**$($4q$8Q#> #a'<'+'7!';D$ )$(JOO$5$5D ! !( ) ) )$(D ! ! ! ),,T-=>> : & J  W % % % J  W % % % Q ' ' ' T] 3 3 3 3 J  W % % % !!!TZ,,,TZ111  , $  1 1D TZ/// >J J J Q ' ' ' ' T_q%8 9 9 9TZ111 : 6 J  T^ , , , $*dn 5 5 5 !!!  ,$(JOO$5$5D !TZ!1A!5666TZ###TZ#####sD##D>=D>c|jr$dt|jz}|dzr|dz}d}nd}d}t|jd|z|jzdz|zdz|z|S) Nrr( rrrrg)rprMrrHrnr )rw datalengthrZ verslengths rrVzAifc_write._write_form_length}s : #dn"5"55JA~ ,'!^ JJJJTZZ$2B!B"#"$&(")+5"6 7 7 7r c|j}|jdzr%|jdz}|jdn|j}||jkr8|j|jkr(|jdkr|j|ddS|j|j d| |}|j|j dt|j|j|j|j dt|j|dz|j|d|j|_||_dS)Nr(rLrrg)rnrrr=rrrr rrSrVrWrHrX)rwcurposr^r,s rr>zAifc_write._patchheadersO""  q  +*Q.J J  W % % % %*J ) ) )mt333!## JOOFA & & & F -q111'' 33 )1---TZ!5666 -q111TZa000 """, %r c"t|jdkrdS|jdd}|jD];}|\}}}|t|zdzdz}t|dzdkr|dz}rAr r rrr#sJ< E "   $$$ $$$ $$$    $$$""" - - -KKK . . .>>>  $$$ : : :   2))) ))) ))) )))&+++1$1$1$f   &&&,,,,,,r rc|t|dr|j}nd}|dvrt|S|dvrt|St d)Nmoder})rr})rrz$mode must be 'r', 'rb', 'w', or 'wb')rrer`rr)r5res rrrsd | 1f   6DDD {||   !}}:;;;r __main__r(z/usr/demos/data/audio/bach.aiffrfReadingz 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`rrsysargvrfnr5printrrrrrrgngr/rrr+r?r r rrus%FFP  F Xg....     I    !!! !!! !!! !!!  " """""""""""""BXH($6777#"""""z.SUU "W !8 !5 7  J "%  MMMMMMMM^N,N,N,N,N,N,N,N,` < < < < zJJJ 8ABB<; 9::: !B b#! i mQ^^--... mQ\\^^,,, mQ^^--... mQ^^--... mQ]]__--- mQ]]__--- 8ABB< !B E)R b# (! AKKMM***(<<--DMM$''' (  ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( E'NNN% sJ*BBBC5KAJ7+ K7J; ;K>J; ?KK!K