3 \-E@sddlZdddddgZGdddeZdZdZddlZddlZddlZdd l m Z dd l m Z e ddZ GdddZGdddZdddZeZdS)NopenopenfpError Wave_read Wave_writec@s eZdZdS)rN)__name__ __module__ __qualname__r r /usr/lib64/python3.6/wave.pyrNsbhi)Chunk) namedtuple _wave_paramsz7nchannels sampwidth framerate nframes comptype compnamec@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+S),rc Csd|_d|_t|dd|_|jjdkr0td|jjddkrHtdd|_d|_xd|_ yt|jdd}Wnt k rPYnX|j}|d kr|j |d|_n2|d kr|jstd ||_|j |j |_d|_ P|jqVW|j s|j rtd dS) Nr)Z bigendiansRIFFz file does not start with RIFF idsWAVEznot a WAVE filer sfmt sdatazdata chunk before fmt chunkz#fmt chunk and/or data chunk missing)_convert _soundposr_fileZgetnamerreadZ_fmt_chunk_read _data_chunk_data_seek_neededEOFError_read_fmt_chunkZ chunksize _framesize_nframesskip)selffilechunkZ chunknamer r r initfp}s:  zWave_read.initfpc CsRd|_t|tr"tj|d}||_y|j|Wn|jrF|jYnXdS)Nrb)_i_opened_the_file isinstancestrbuiltinsrr"close)rfr r r __init__s  zWave_read.__init__cCs |jdS)N)r()rr r r __del__szWave_read.__del__cCs|S)Nr )rr r r __enter__szWave_read.__enter__cGs |jdS)N)r()rargsr r r __exit__szWave_read.__exit__cCs|jS)N)r)rr r r getfpszWave_read.getfpcCsd|_d|_dS)Nr r)rr)rr r r rewindszWave_read.rewindcCs"d|_|j}|rd|_|jdS)N)rr$r()rr r r r r(s zWave_read.closecCs|jS)N)r)rr r r tellszWave_read.tellcCs|jS)N) _nchannels)rr r r getnchannelsszWave_read.getnchannelscCs|jS)N)r)rr r r getnframesszWave_read.getnframescCs|jS)N) _sampwidth)rr r r getsampwidthszWave_read.getsampwidthcCs|jS)N) _framerate)rr r r getframerateszWave_read.getframeratecCs|jS)N) _comptype)rr r r getcomptypeszWave_read.getcomptypecCs|jS)N) _compname)rr r r getcompnameszWave_read.getcompnamecCs*t|j|j|j|j|j|jS)N)rr3r6r8r4r:r<)rr r r getparamss zWave_read.getparamscCsdS)Nr )rr r r getmarkersszWave_read.getmarkerscCs tddS)Nzno marks)r)ridr r r getmarkszWave_read.getmarkcCs*|dks||jkrtd||_d|_dS)Nrzposition not in ranger )rrrr)rposr r r setposszWave_read.setposcCs|jr8|jjdd|j|j}|r2|jj|dd|_|dkrDdS|jj||j}|jdkrxtjdkrxt j ||j}|j r|r|j |}|jt ||j |j|_|S)Nrr big)rrseekrrrr5sys byteorderaudioopbyteswaprlenr2)rnframesrAdatar r r readframess   zWave_read.readframescCsztjd|jd\}|_|_}}|tkrNtjd|jdd}|dd|_ntd|f|j|j|_d |_ d |_ dS) Nzr@rBrMrr r r r r^s* c@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9S):rc CsRd|_t|tr"tj|d}||_y|j|Wn|jrF|jYnXdS)Nwb)r$r%r&r'rr"r()rr)r r r r*#s  zWave_write.__init__cCs@||_d|_d|_d|_d|_d|_d|_d|_d|_d|_ dS)NrF) rrr2r5r7r_nframeswritten _datawritten _datalength_headerwritten)rr r r r r"/szWave_write.initfpcCs |jdS)N)r()rr r r r+;szWave_write.__del__cCs|S)Nr )rr r r r,>szWave_write.__enter__cGs |jdS)N)r()rr-r r r r.AszWave_write.__exit__cCs(|jrtd|dkrtd||_dS)Nz0cannot change parameters after starting to writer zbad # of channels)rXrr2)r nchannelsr r r setnchannelsGs zWave_write.setnchannelscCs|jstd|jS)Nznumber of channels not set)r2r)rr r r r3NszWave_write.getnchannelscCs0|jrtd|dks|dkr&td||_dS)Nz0cannot change parameters after starting to writer rzbad sample width)rXrr5)rrUr r r setsampwidthSs zWave_write.setsampwidthcCs|jstd|jS)Nzsample width not set)r5r)rr r r r6ZszWave_write.getsampwidthcCs0|jrtd|dkrtdtt||_dS)Nz0cannot change parameters after starting to writerzbad frame rate)rXrintroundr7)r framerater r r setframerate_s zWave_write.setframeratecCs|jstd|jS)Nzframe rate not set)r7r)rr r r r8fszWave_write.getframeratecCs|jrtd||_dS)Nz0cannot change parameters after starting to write)rXrr)rrKr r r setnframeskszWave_write.setnframescCs|jS)N)rW)rr r r r4pszWave_write.getnframescCs.|jrtd|dkrtd||_||_dS)Nz0cannot change parameters after starting to writerRzunsupported compression type)rR)rXrr9r;)rcomptypecompnamer r r setcomptypess zWave_write.setcomptypecCs|jS)N)r9)rr r r r:{szWave_write.getcomptypecCs|jS)N)r;)rr r r r<~szWave_write.getcompnamecCsV|\}}}}}}|jrtd|j||j||j||j||j||dS)Nz0cannot change parameters after starting to write)rXrr\r]rarbre)rZparamsr[rUr`rKrcrdr r r setparamss    zWave_write.setparamscCs>|j s|j s|j r tdt|j|j|j|j|j|jS)Nznot all parameters set)r2r5r7rrrr9r;)rr r r r=szWave_write.getparamscCs tddS)Nzsetmark() not supported)r)rr?rAnamer r r setmarkszWave_write.setmarkcCs tddS)Nzno marks)r)rr?r r r r@szWave_write.getmarkcCsdS)Nr )rr r r r>szWave_write.getmarkerscCs|jS)N)rW)rr r r r1szWave_write.tellcCst|ttfst|jd}|jt|t||j|j}|j rN|j |}|jdkrpt j dkrpt j ||j}|jj||jt|7_|j||_dS)NBr rD)r%bytes bytearray memoryviewcast_ensure_header_writtenrJr5r2rrFrGrHrIrwriterXrW)rrLrKr r r writeframesraws  zWave_write.writeframesrawcCs"|j||j|jkr|jdS)N)rprYrX _patchheader)rrLr r r writeframess  zWave_write.writeframesc CsXz2|jr0|jd|j|jkr&|j|jjWdd|_|j}|rRd|_|jXdS)Nr)rrnrYrXrqflushr$r()rr r r r r(s  zWave_write.closecCs>|js:|jstd|js"td|js0td|j|dS)Nz# channels not specifiedzsample width not specifiedzsampling rate not specified)rZr2rr5r7 _write_header)rZdatasizer r r rnsz!Wave_write._ensure_header_writtencCs|jjd|js$||j|j|_|j|j|j|_y|jj|_Wntt fk rfd|_YnX|jjt j dd|jdddt |j|j |j|j |j|j|j|jdd |jdk r|jj|_|jjt j d |jd |_dS) NsRIFFzr1rprrr(rnrtrqr r r r r s8     cCsJ|dkrt|dr|j}nd}|dkr.t|S|dkr>t|StddS) Nmoder#rwrVz$mode must be 'r', 'rb', 'w', or 'wb')r}r#)r~rV)hasattrr|rrr)r)r|r r r rs )Nr rNr)N)r'__all__ ExceptionrrTZ _array_fmtsrHrSrFr!r collectionsrrrrrrr r r r Js"  ,d