QfXdZddlmZddlZddlZddlZgdZGddeZdZ dZ d Z d Z ed d Z d ZGddZGddZGddZddZy)a% Stuff to parse WAVE files. Usage. Reading WAVE files: f = wave.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(). When the setpos() and rewind() methods are 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 linear samples) getcompname() -- returns human-readable version of compression type ('not compressed' linear samples) getparams() -- returns a namedtuple consisting of all of the above in the above order getmarkers() -- returns None (for compatibility with the aifc module) getmark(id) -- raises an error since the mark does not exist (for compatibility with the aifc module) 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() and the position given to setpos() are 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 WAVE files: f = wave.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: 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 tell() -- return current position in output file writeframesraw(data) -- write audio frames without patching 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. The close() method is called automatically when the class instance is destroyed. ) namedtupleN)openError Wave_read Wave_writec eZdZy)rN)__name__ __module__ __qualname__+/opt/alt/python312/lib64/python3.12/wave.pyrrRsr ris8q)NbhNi _wave_paramsz7nchannels sampwidth framerate nframes comptype compnamectt|}tdt||D]&}t|D]}|||z|||zdz |z <(t|S)Nrr) bytearraylenrangebytes)datawidth swapped_datarjs r _byteswapr`saSY'L 1c$i 'uA.21q5kLUQ* +(  r c<eZdZd dZdZdZd dZdZd dZdZ y) _Chunkcd|_||_|rd}nd}||_|jd|_t |jdkrt  tj|dz|jdd|_ |r|jdz |_ d|_ |jj|_ d|_y#tj$rt dwxYw#tt f$r d|_YywxYw) NF><LrT)closedalignfileread chunknamerEOFErrorstruct unpack_from chunksizeerror size_readtelloffsetseekableAttributeErrorOSError)selfr(r' bigendian inclheaderstrflags r__init__z_Chunk.__init__ks  GG 1 t~~  "N %#// TYYq\J1MDN !^^a/DN !))..*DK!DM|| % $ %( "!DM "s0C C%C"%C>=C>c|jS)z*Return the name (ID) of the current chunk.)r*r6s rgetnamez_Chunk.getnames ~~r cf|js |jd|_yy#d|_wxYw)NT)r&skipr<s rclosez _Chunk.closes.{{ # " # s' 0cF|jr td|js td|dk(r||jz}n|dk(r||j z}|dks||j kDrt |jj|j|zd||_y)zSeek to specified position into the chunk. Default position is 0 (start of chunk). If the file is not seekable, this will result in an error. I/O operation on closed filez cannot seekrrN) r& ValueErrorr3r5r0r. RuntimeErrorr(seekr2)r6poswhences rrFz _Chunk.seeks ;;;< <}}-( ( Q;&C q[&C 7cDNN*  t{{S(!,r cH|jr td|jS)NrB)r&rDr0r<s rr1z _Chunk.tells ;;;< <~~r cV|jr td|j|jk\ry|dkr|j|jz }||j|jz kDr|j|jz }|jj |}|jt |z|_|j|jk(rS|jrG|jdzr8|jj d}|jt |z|_|S)zRead at most size bytes from the chunk. If size is omitted or negative, read until the end of the chunk. rBr rr)r&rDr0r.r(r)rr')r6sizerdummys rr)z _Chunk.reads ;;;< < >>T^^ + !8>>DNN2D $..4>>1 1>>DNN2Dyy~~d##d)3 >>T^^ + :: NNQ IINN1%E!^^c%j8DN r c|jr td|jrk |j|jz }|j r|jdzr|dz}|j j|d|j|z|_y|j|jkrWtd|j|jz }|j|}|st|j|jkrVyy#t$rY|wxYw)zSkip the rest of the chunk. If you are not interested in the contents of the chunk, this method should be called so that the file points to the start of the next chunk. rBrNi ) r&rDr3r.r0r'r(rFr5minr)r+)r6nrLs rr?z _Chunk.skips ;;;< < == NNT^^3::4>>A#5AA q!$!%!!3nnt~~-D$..4>>9:AIIaLE nnt~~-  sA)D D  D N)TTF)r)) r r r r:r=r@rFr1r)r?r r rrrjs%!2#& .r rceZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZy)raPVariables used in this class: These variables are available to the user though appropriate methods of this class: _file -- the open file with methods read(), close(), and seek() set through the __init__() method _nchannels -- the number of audio channels available through the getnchannels() method _nframes -- the number of audio frames available through the getnframes() method _sampwidth -- the number of bytes per audio sample available through the getsampwidth() method _framerate -- the sampling frequency available through the getframerate() method _comptype -- the AIFF-C compression type ('NONE' if AIFF) available through the getcomptype() method _compname -- the human-readable AIFF-C compression type available through the getcomptype() method _soundpos -- the position in the audio stream available through the tell() method, set through the setpos() method These variables are used internally only: _fmt_chunk_read -- 1 iff the FMT chunk has been read _data_seek_needed -- 1 iff positioned correctly in audio file for readframes() _data_chunk -- instantiation of a chunk class for the DATA chunk _framesize -- size of one frame in the file cd|_d|_t|d|_|jj dk7r t d|jj ddk7r t dd|_d|_ d|_ t|jd}|j }|d k(r|j|d|_nI|d k(rD|js t d ||_|j|jz|_d|_ n|j|jr |js t d y#t$rY/wxYw) Nr)r7RIFFz file does not start with RIFF idr#WAVEznot a WAVE filerfmt datazdata chunk before fmt chunkz#fmt chunk and/or data chunk missing)_convert _soundposr_filer=rr)_fmt_chunk_read _data_chunk_data_seek_neededr+_read_fmt_chunkr. _framesize_nframesr?)r6r(chunkr*s rinitfpzWave_read.initfpsE Da0 ::   7 *:; ; ::??1  ()* * %&D " tzzq9 IG#$$U+'($g%++ =>>#( %4?? B )*& JJL#$##4+;+;=> >,<  s E EEcd|_t|trtj|d}||_ |j |y#|jr|j xYw)Nrb_i_opened_the_file isinstancestrbuiltinsrrar@r6fs rr:zWave_read.__init__sX"& a  a&A&'D #  KKN &&  AA'c$|jyNr@r<s r__del__zWave_read.__del__$  r c|Srmr r<s r __enter__zWave_read.__enter__' r c$|jyrmrnr6argss r__exit__zWave_read.__exit__*rpr c|jSrm)rYr<s rgetfpzWave_read.getfp0s zzr c d|_d|_y)Nrr)r\rXr<s rrewindzWave_read.rewind3s!"r c^d|_|j}|rd|_|jyyrm)rYrer@r6r(s rr@zWave_read.close7s- && &*D # JJL r c|jSrm)rXr<s rr1zWave_read.tell> ~~r c|jSrm) _nchannelsr<s r getnchannelszWave_read.getnchannelsA r c|jSrm)r_r<s r getnframeszWave_read.getnframesDs }}r c|jSrm) _sampwidthr<s r getsampwidthzWave_read.getsampwidthGrr c|jSrm) _framerater<s r getframeratezWave_read.getframerateJrr c|jSrm _comptyper<s r getcomptypezWave_read.getcomptypeMrr c|jSrm _compnamer<s r getcompnamezWave_read.getcompnamePrr c t|j|j|j|j |j |j Srm)rrrrrrrr<s r getparamszWave_read.getparamsSsQD--/1B1B1D((*DOO,='')4+;+;+=? ?r cyrmr r<s r getmarkerszWave_read.getmarkersXr ctdNzno marksrr6ids rgetmarkzWave_read.getmark[ Jr c^|dks||jkDr td||_d|_y)Nrzposition not in ranger)r_rrXr\)r6rGs rsetposzWave_read.setpos^s/ 7cDMM)/0 0!"r cJ|jrZ|jjdd|j|jz}|r|jj|dd|_|dk(ry|jj ||jz}|j dk7r)tjdk(rt||j }|jr|r|j|}|jt||j|j zzz|_|S)Nrr rbig) r\r[rFrXr^r)rsys byteorderrrWrr)r6nframesrGrs r readframeszWave_read.readframesds  ! !    ! !!Q '..4??2C  %%c1-%&D " a<$$Wt%>? ??a CMMU$:T4??3D ==T==&D#d)$//8Y*ZZ r c tjd|jd\}|_|_}}|tk7r|tk7rtd| tjd|jdd}|tk(r~ tjd|jd\}}}|jd } t| d krt  | tk7r% ddl } d | j| } t| |d zdz|_|js td|js td|j|jz|_d|_d|_y#tj $rt dwxYw#tj $rt dwxYw#tj $rt dwxYw#t$rd } Yt| wxYw)NzB&"N'44>&? S\ @]?^$_MM**$q=Q.*+ ++, ,//DOO;)?|| % $ % || % $ %<< )D( ) !>$=MM**>s65E/(F AF+#G /F  F(+G G! G!N)r r r __doc__rar:rorrrwryr{r@r1rrrrrrrrrrrr]r r rrrsp<?>  ?  # *"*r rceZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZy)raVariables used in this class: These variables are user settable through appropriate methods of this class: _file -- the open file with methods write(), close(), tell(), seek() set through the __init__() method _comptype -- the AIFF-C compression type ('NONE' in AIFF) set through the setcomptype() or setparams() method _compname -- the human-readable AIFF-C compression type set through the setcomptype() or setparams() method _nchannels -- the number of audio channels set through the setnchannels() or setparams() method _sampwidth -- the number of bytes per audio sample set through the setsampwidth() or setparams() method _framerate -- the sampling frequency set through the setframerate() or setparams() method _nframes -- the number of audio frames written to the header set through the setnframes() or setparams() method These variables are used internally only: _datalength -- the size of the audio samples written to the header _nframeswritten -- the number of frames actually written _datawritten -- the size of the audio samples actually written cd|_t|trtj|d}||_ |j |y#|jr|j xYw)Nwbrdris rr:zWave_write.__init__sX"& a  a&A&'D #  KKN && rkc||_d|_d|_d|_d|_d|_d|_d|_d|_d|_ y)NrF) rYrWrrrr__nframeswritten _datawritten _datalength_headerwrittenr}s rrazWave_write.initfpsL    #r c$|jyrmrnr<s rrozWave_write.__del__rpr c|Srmr r<s rrrzWave_write.__enter__rsr c$|jyrmrnrus rrwzWave_write.__exit__rpr c`|jr td|dkr td||_y)N0cannot change parameters after starting to writerr)rrr)r6 nchannelss r setnchannelszWave_write.setnchannelss1   JK K q=+, ,#r cH|js td|jS)Nznumber of channels not set)rrr<s rrzWave_write.getnchannelss45 5r cj|jr td|dks|dkDr td||_y)Nrrr#r)rrr)r6rs r setsampwidthzWave_write.setsampwidths7   JK K q=IM*+ +#r cH|js td|jS)Nzsample width not set)rrr<s rrzWave_write.getsampwidths./ /r c|jr td|dkr tdtt||_y)Nrrzbad frame rate)rrintroundr)r6 framerates r setframeratezWave_write.setframerates;   JK K >() )eI./r cH|js td|jS)Nzframe rate not set)rrr<s rrzWave_write.getframerates,- -r c@|jr td||_yNr)rrr_)r6rs r setnframeszWave_write.setnframess   JK K r c|jSrmrr<s rrzWave_write.getnframes###r cl|jr td|dvr td||_||_y)Nr)rzunsupported compression type)rrrr)r6comptypecompnames r setcomptypezWave_write.setcomptypes9   JK K 9 $67 7!!r c|jSrmrr<s rrzWave_write.getcomptyperr c|jSrmrr<s rrzWave_write.getcompnamerr c|\}}}}}}|jr td|j||j||j ||j ||j ||yr)rrrrrrr)r6paramsrrrrrrs r setparamszWave_write.setparamssoGMD 9i(H   JK K )$ )$ )$   8,r c|jr|jr |js tdt |j|j|j|j |j |jS)Nznot all parameters set)rrrrrr_rrr<s rrzWave_write.getparams sSdooT__01 1DOOT__doommT^^T^^= =r ctd)Nzsetmark() not supportedr)r6rrGnames rsetmarkzWave_write.setmark&s-..r ctdrrrs rrzWave_write.getmark)rr cyrmr r<s rrzWave_write.getmarkers,rr c|jSrmrr<s rr1zWave_write.tell/rr c&t|ttfst|j d}|j t |t ||j|jzz}|jr|j|}|jdk7r)tjdk(rt||j}|jj||xjt |z c_|j |z|_y)NBrr)rfrr memoryviewcast_ensure_header_writtenrrrrWrrrrYwriterr)r6rrs rwriteframesrawzWave_write.writeframesraw2s$ 23d#((-D ##CI.d)$// AB ====&D ??a CMMU$:T4??3D  SY&#33g=r cz|j||j|jk7r|jyyrm)rrr _patchheader)r6rs r writeframeszWave_write.writeframes?s5 D!   t00 0     1r c |jrT|jd|j|jk7r|j |jj d|_|j }|rd|_|jyy#d|_|j }|rd|_|jwwxYw)Nr)rYrrrrflushrer@r}s rr@zWave_write.closeDs zz++A.##t'8'88%%'   "DJ**D*.' DJ**D*.' s A B/B?c|jsW|js td|js td|js td|j |yy)Nz# channels not specifiedzsample width not specifiedzsampling rate not specified)rrrrr _write_header)r6datasizes rrz!Wave_write._ensure_header_writtenVsW""??677??899??9::   x (#r c|jjd|js!||j|jzz|_|j|jz|jz|_ |jj |_|jjtjdd|j zdddt|j|j|j|jz|jz|j|jz|jdzd |j|jj |_|jjtjd |j d |_y#ttf$r d|_Y+wxYw) NrSz ",,r N) r r r rr:rarorrrwrrrrrrrrrrrrrrrrr1rrr@rrrr r rrrs2  $ $ $ 0  $"-= / $ > $)#* -r rc|t|dr |j}nd}|dvr t|S|dvr t|St d)Nmoderc)rrc)wrz$mode must be 'r', 'rb', 'w', or 'wb')hasattrrrrr)rjrs rrrsO | 1f 66DD {|  !}:;;r rm)r collectionsrrhr,r__all__rrrrr _array_fmtsrrrrrrr r rrsGR#  7 I X' .NP ll^B*B*Ja-a-H