o w[eTF@sdZddlmZddlmZddlZddlZddlZddlZgdZ Gddde Z dZ d Z ed d ZGd d d ZGdddZdddZdS)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. )Chunk) namedtupleN)openError Wave_read Wave_writec@s eZdZdS)rN)__name__ __module__ __qualname__r r /usr/lib/python3.10/wave.pyrTsr)NbhNi _wave_paramsz7nchannels sampwidth framerate nframes comptype compnamec@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,S)-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 cCsd|_d|_t|dd|_|jdkrtd|jddkr$tdd|_d|_ d|_ z t|jdd}Wn t y@Yn0w|}|d krR| |d|_n|d krk|js]td ||_|j |j |_d|_ n|q+|jrv|jsztd dS) Nr) bigendianRIFFz file does not start with RIFF idWAVEznot a WAVE filer fmt datazdata chunk before fmt chunkz#fmt chunk and/or data chunk missing)_convert _soundposr_filegetnamerread_fmt_chunk_read _data_chunk_data_seek_neededEOFError_read_fmt_chunk chunksize _framesize_nframesskip)selffilechunk chunknamer r r initfp}s@   zWave_read.initfpcCJd|_t|trt|d}||_z||WdS|jr$|)Nrb_i_opened_the_file isinstancestrbuiltinsrr*closer&fr r r __init__s  zWave_read.__init__cC |dSNr2r&r r r __del__ zWave_read.__del__cC|Sr7r r9r r r __enter__zWave_read.__enter__cGr6r7r8r&argsr r r __exit__r;zWave_read.__exit__cC|jSr7)rr9r r r getfpzWave_read.getfpcCsd|_d|_dS)Nr r)rrr9r r r rewinds zWave_read.rewindcCs&d|_|j}|rd|_|dSdSr7)rr.r2r&r'r r r r2s  zWave_read.closecCrBr7)rr9r r r tellrDzWave_read.tellcCrBr7) _nchannelsr9r r r getnchannelsrDzWave_read.getnchannelscCrBr7)r$r9r r r getnframesrDzWave_read.getnframescCrBr7) _sampwidthr9r r r getsampwidthrDzWave_read.getsampwidthcCrBr7) _framerater9r r r getframeraterDzWave_read.getframeratecCrBr7 _comptyper9r r r getcomptyperDzWave_read.getcomptypecCrBr7 _compnamer9r r r getcompnamerDzWave_read.getcompnamecCs*t||||||Sr7)rrIrLrNrJrQrTr9r r r getparamss  zWave_read.getparamscCdSr7r r9r r r getmarkersr>zWave_read.getmarkerscCtdNzno marksrr&idr r r getmarkzWave_read.getmarkcCs*|dks ||jkr td||_d|_dS)Nrzposition not in ranger )r$rrr)r&posr r r setposs zWave_read.setposcCs|jr|jdd|j|j}|r|j|dd|_|dkr"dS|j||j}|jdkrzWave_write.__enter__cGr6r7r8r?r r r rAKr;zWave_write.__exit__cCs(|jrtd|dkrtd||_dS)N0cannot change parameters after starting to writer rq)r~rrH)r& nchannelsr r r setnchannelsQs  zWave_write.setnchannelscC|jstd|jS)Nznumber of channels not set)rHrr9r r r rIXzWave_write.getnchannelscCs0|jrtd|dks|dkrtd||_dS)Nrr rrp)r~rrK)r&rzr r r setsampwidth]s  zWave_write.setsampwidthcCr)Nzsample width not set)rKrr9r r r rLdrzWave_write.getsampwidthcCs0|jrtd|dkrtdtt||_dS)Nrrzbad frame rate)r~rintroundrM)r& framerater r r setframerateis zWave_write.setframeratecCr)Nzframe rate not set)rMrr9r r r rNprzWave_write.getframeratecCs|jrtd||_dSNr)r~rr$)r&rir r r setnframesus zWave_write.setnframescCrBr7r}r9r r r rJzrDzWave_write.getnframescCs.|jrtd|dvrtd||_||_dS)Nr)rrzunsupported compression type)r~rrPrS)r&comptypecompnamer r r setcomptype}s  zWave_write.setcomptypecCrBr7rOr9r r r rQrDzWave_write.getcomptypecCrBr7rRr9r r r rTrDzWave_write.getcompnamecCsV|\}}}}}}|jrtd|||||||||||dSr)r~rrrrrr)r&paramsrrzrrirrr r r setparamss    zWave_write.setparamscCs8|jr |jr |js tdt|j|j|j|j|j|jS)Nznot all parameters set)rHrKrMrrr$rPrSr9r r r rUs  zWave_write.getparamscCrX)Nzsetmark() not supportedrZ)r&r\r_namer r r setmarkr^zWave_write.setmarkcCrXrYrZr[r r r r]r^zWave_write.getmarkcCrVr7r r9r r r rWr>zWave_write.getmarkerscCrBr7rr9r r r rGrDzWave_write.tellcCst|ttfst|d}|t|t||j|j}|j r'| |}|jdkr8t j dkr8t ||j}|j||jt|7_|j||_dS)NBr rb)r/bytes bytearray memoryviewcast_ensure_header_writtenrhrKrHrrdrerfrgrwriter~r})r&rjrir r r writeframesraws  zWave_write.writeframesrawcCs&|||j|jkr|dSdSr7)rrr~ _patchheader)r&rjr r r writeframess   zWave_write.writeframescCszz+|jr|d|j|jkr||jWd|_|j}|r*d|_|dSdSd|_|j}|rs& I 6d