3 \¯Fã@s°dZddlmZeddƒZdZdZdZdZd Zd Z d Z d Z d Z dZ dZdZdZdZeeeee egZGdd„deƒZdd„Zdd„ZGdd„dƒZGdd„dƒZd dd„ZeZdS)!aÚStuff to parse Sun and NeXT audio files. An audio file consists of a header followed by the data. The structure of the header is as follows. +---------------+ | magic word | +---------------+ | header size | +---------------+ | data size | +---------------+ | encoding | +---------------+ | sample rate | +---------------+ | # of channels | +---------------+ | info | | | +---------------+ The magic word consists of the 4 characters '.snd'. Apart from the info field, all header fields are 4 bytes in size. They are all 32-bit unsigned integers encoded in big-endian byte order. The header size really gives the start of the data. The data size is the physical size of the data. From the other parameters the number of frames can be calculated. The encoding gives the way in which audio samples are encoded. Possible values are listed below. The info field currently consists of an ASCII string giving a human-readable description of the audio file. The info field is padded with NUL bytes to the header size. Usage. Reading audio files: f = sunau.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' or 'ULAW') getcompname() -- returns human-readable version of compression type ('not compressed' matches 'NONE') 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 audio files: f = sunau.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 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. The close() method is called automatically when the class instance is destroyed. é)Ú namedtupleÚ _sunau_paramsz7nchannels sampwidth framerate nframes comptype compnameidns.éééééééééééélÿÿc@s eZdZdS)ÚErrorN)Ú__name__Ú __module__Ú __qualname__©rrú/usr/lib64/python3.6/sunau.pyrˆsrcCs<d}x2tdƒD]&}|jdƒ}|s$t‚|dt|ƒ}qW|S)Nrrré)ÚrangeÚreadÚEOFErrorÚord)ÚfileÚxÚiZbyterrrÚ _read_u32‹s rcCsJg}x2tdƒD]&}t|dƒ\}}|jdt|ƒƒ|}qW|jt|ƒƒdS)Nrrr)rÚdivmodÚinsertÚintÚwriteÚbytes)rrÚdatarÚdÚmrrrÚ _write_u32”s r'c@s¬eZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)S)*ÚAu_readcCs@t|ƒtdƒkr,ddl}|j|dƒ}d|_nd|_|j|ƒdS)NÚrÚrbTF)ÚtypeÚbuiltinsÚopenÚ_openedÚinitfp)ÚselfÚfr,rrrÚ__init__žs  zAu_read.__init__cCs|jr|jƒdS)N)Ú_fileÚclose)r0rrrÚ__del__§szAu_read.__del__cCs|S)Nr)r0rrrÚ __enter__«szAu_read.__enter__cGs |jƒdS)N)r4)r0ÚargsrrrÚ__exit__®szAu_read.__exit__c Cs¸||_d|_tt|ƒƒ}|tkr(tdƒ‚tt|ƒƒ|_|jdkrHtdƒ‚|jdkrZtdƒ‚t|ƒ|_|jtkrzt|jƒ|_tt|ƒƒ|_ |j t krštdƒ‚|j t t fkr¶d|_ d |_nj|j tkrÎd |_|_ nR|j tkræd|_|_ n:|j tkrþd |_|_ n"|j tkrd |_|_ ntd ƒ‚tt|ƒƒ|_tt|ƒƒ|_|j|j|_|jdkr~|j|jdƒ|_|jjd ƒ\|_}}nd|_y|jƒ|_Wn ttfk r²d|_YnXdS)Nrzbad magic numberr zheader size too smallédzheader size ridiculously largezencoding not (yet) supportedrrrrzunknown encodingóó)r3Ú _soundposr!rÚAUDIO_FILE_MAGICrZ _hdr_sizeÚ _data_sizeÚAUDIO_UNKNOWN_SIZEÚ _encodingÚ_simple_encodingsÚAUDIO_FILE_ENCODING_MULAW_8ÚAUDIO_FILE_ENCODING_ALAW_8Ú _sampwidthÚ _framesizeÚAUDIO_FILE_ENCODING_LINEAR_8ÚAUDIO_FILE_ENCODING_LINEAR_16ÚAUDIO_FILE_ENCODING_LINEAR_24ÚAUDIO_FILE_ENCODING_LINEAR_32Ú _framerateÚ _nchannelsrÚ_infoÚ partitionÚtellÚ _data_posÚAttributeErrorÚOSError)r0rÚmagicÚ_rrrr/±sP            zAu_read.initfpcCs|jS)N)r3)r0rrrÚgetfpÝsz Au_read.getfpcCs|jS)N)rK)r0rrrÚ getnchannelsàszAu_read.getnchannelscCs|jS)N)rD)r0rrrÚ getsampwidthãszAu_read.getsampwidthcCs|jS)N)rJ)r0rrrÚ getframerateæszAu_read.getframeratecCs(|jtkrtS|jtkr$|j|jSdS)Nr)r>r?r@rArE)r0rrrÚ getnframesés    zAu_read.getnframescCs$|jtkrdS|jtkrdSdSdS)NÚULAWÚALAWÚNONE)r@rBrC)r0rrrÚ getcomptypeðs   zAu_read.getcomptypecCs$|jtkrdS|jtkrdSdSdS)NzCCITT G.711 u-lawzCCITT G.711 A-lawznot compressed)r@rBrC)r0rrrÚ getcompnameøs   zAu_read.getcompnamecCs*t|jƒ|jƒ|jƒ|jƒ|jƒ|jƒƒS)N)rrUrVrWrXr\r])r0rrrÚ getparamss zAu_read.getparamscCsdS)Nr)r0rrrÚ getmarkersszAu_read.getmarkerscCs tdƒ‚dS)Nzno marks)r)r0ÚidrrrÚgetmarkszAu_read.getmarkcCsp|jtkrl|tkr|jjƒ}n|jj||jƒ}|jt|ƒ|j7_|jtkrhddl }|j ||j ƒ}|SdS)Nr) r@rAr?r3rrEr<ÚlenrBÚaudioopZulaw2linrD)r0Únframesr$rcrrrÚ readframes s   zAu_read.readframescCs*|jdkrtdƒ‚|jj|jƒd|_dS)Nz cannot seekr)rOrQr3Úseekr<)r0rrrÚrewinds zAu_read.rewindcCs|jS)N)r<)r0rrrrNsz Au_read.tellcCsP|dks||jƒkrtdƒ‚|jdkr.tdƒ‚|jj|j||jƒ||_dS)Nrzposition not in rangez cannot seek)rXrrOrQr3rfrEr<)r0ÚposrrrÚsetpos!s  zAu_read.setposcCs"|j}|rd|_|jr|jƒdS)N)r3r.r4)r0rrrrr4)s z Au_read.closeN)rrrr2r5r6r8r/rTrUrVrWrXr\r]r^r_rarergrNrir4rrrrr(œs( , r(c@sÔeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Zd3S)4ÚAu_writecCs@t|ƒtdƒkr,ddl}|j|dƒ}d|_nd|_|j|ƒdS)Nr)rÚwbTF)r+r,r-r.r/)r0r1r,rrrr22s  zAu_write.__init__cCs|jr|jƒd|_dS)N)r3r4)r0rrrr5;szAu_write.__del__cCs|S)Nr)r0rrrr6@szAu_write.__enter__cGs |jƒdS)N)r4)r0r7rrrr8CszAu_write.__exit__cCsF||_d|_d|_d|_d|_t|_d|_d|_d|_ d|_ d|_ dS)Nrr;rY) r3rJrKrDrEr?Ú_nframesÚ_nframeswrittenÚ _datawrittenÚ _datalengthrLÚ _comptype)r0rrrrr/FszAu_write.initfpcCs(|jrtdƒ‚|dkrtdƒ‚||_dS)Nz0cannot change parameters after starting to writerrrz"only 1, 2, or 4 channels supported)rrr)rmrrK)r0Ú nchannelsrrrÚ setnchannelsSs zAu_write.setnchannelscCs|jstdƒ‚|jS)Nznumber of channels not set)rKr)r0rrrrUZszAu_write.getnchannelscCs(|jrtdƒ‚|dkrtdƒ‚||_dS)Nz0cannot change parameters after starting to writerrrrzbad sample width)rrrr)rmrrD)r0Ú sampwidthrrrÚ setsampwidth_s zAu_write.setsampwidthcCs|jstdƒ‚|jS)Nzsample width not specified)rJrrD)r0rrrrVfszAu_write.getsampwidthcCs|jrtdƒ‚||_dS)Nz0cannot change parameters after starting to write)rmrrJ)r0Ú frameraterrrÚ setframeratekszAu_write.setframeratecCs|jstdƒ‚|jS)Nzframe rate not set)rJr)r0rrrrWpszAu_write.getframeratecCs(|jrtdƒ‚|dkrtdƒ‚||_dS)Nz0cannot change parameters after starting to writerz# of frames cannot be negative)rmrrl)r0rdrrrÚ setnframesus zAu_write.setnframescCs|jS)N)rm)r0rrrrX|szAu_write.getnframescCs|dkr||_ntdƒ‚dS)Nr[rYzunknown compression type)r[rY)rpr)r0r+ÚnamerrrÚ setcomptypeszAu_write.setcomptypecCs|jS)N)rp)r0rrrr\…szAu_write.getcomptypecCs$|jdkrdS|jdkrdSdSdS)NrYzCCITT G.711 u-lawrZzCCITT G.711 A-lawznot compressed)rp)r0rrrr]ˆs   zAu_write.getcompnamecCsH|\}}}}}}|j|ƒ|j|ƒ|j|ƒ|j|ƒ|j||ƒdS)N)rrrtrvrwry)r0ZparamsrqrsrurdZcomptypeZcompnamerrrÚ setparamss     zAu_write.setparamscCs*t|jƒ|jƒ|jƒ|jƒ|jƒ|jƒƒS)N)rrUrVrWrXr\r])r0rrrr^˜s zAu_write.getparamscCs|jS)N)rm)r0rrrrNsz Au_write.tellcCs~t|ttfƒst|ƒjdƒ}|jƒ|jdkrDddl}|j||j ƒ}t |ƒ|j }|j j |ƒ|j||_|jt |ƒ|_dS)NÚBrYr)Ú isinstancer#Ú bytearrayÚ memoryviewÚcastÚ_ensure_header_writtenrprcZlin2ulawrDrbrEr3r"rmrn)r0r$rcrdrrrÚwriteframesraw s   zAu_write.writeframesrawcCs.|j|ƒ|j|jks"|j|jkr*|jƒdS)N)rrmrlrornÚ _patchheader)r0r$rrrÚ writeframes¬s   zAu_write.writeframesc Cs^|jrZz6|jƒ|j|jks(|j|jkr0|jƒ|jjƒWd|j}d|_|jrX|j ƒXdS)N) r3r€rmrlrornr‚Úflushr.r4)r0rrrrr4²s  zAu_write.closecCs<|js8|jstdƒ‚|js"tdƒ‚|js0tdƒ‚|jƒdS)Nz# of channels not specifiedzsample width not specifiedzframe rate not specified)rmrKrrDrJÚ _write_header)r0rrrr€ÄszAu_write._ensure_header_writtenc Cs‚|jdkrl|jdkr t}d|_qŠ|jdkr6t}d|_qŠ|jdkrLt}d|_qŠ|jdkrbt}d|_qŠtdƒ‚n|jdkr‚t}d|_ntdƒ‚|j|j |_t |j t ƒdt |jƒ}|d d @}t |j |ƒ|jtkrÚt}n |j|j}y|j jƒ|_Wn ttfk rd|_YnXt |j |ƒ||_t |j |ƒt |j |jƒt |j |j ƒ|j j|jƒ|j jd |t |jƒd ƒdS) Nr[rrrrzinternal errorrYr r r:r iøÿÿÿ)rprDrFrErGrHrIrrBrKr'r3r=rbrLrlr?rNÚ_form_length_posrPrQrorJr")r0ÚencodingZ header_sizeZlengthrrrr…ÎsJ               zAu_write._write_headercCsH|jdkrtdƒ‚|jj|jƒt|j|jƒ|j|_|jjddƒdS)Nz cannot seekrr)r†rQr3rfr'rnro)r0rrrr‚øs  zAu_write._patchheaderN)rrrr2r5r6r8r/rrrUrtrVrvrWrwrXryr\r]rzr^rNrrƒr4r€r…r‚rrrrrj0s2    *rjNcCsJ|dkrt|dƒr|j}nd}|dkr.t|ƒS|dkr>t|ƒStdƒ‚dS) NÚmoder*ÚrÚwrkz$mode must be 'r', 'rb', 'w', or 'wb')r‰r*)rŠrk)Úhasattrrˆr(rjr)r1rˆrrrr-s r-)N)Ú__doc__Ú collectionsrrr=rBrFrGrHrIZAUDIO_FILE_ENCODING_FLOATZAUDIO_FILE_ENCODING_DOUBLEZAUDIO_FILE_ENCODING_ADPCM_G721ZAUDIO_FILE_ENCODING_ADPCM_G722Z AUDIO_FILE_ENCODING_ADPCM_G723_3Z AUDIO_FILE_ENCODING_ADPCM_G723_5rCr?rAÚ Exceptionrrr'r(rjr-ZopenfprrrrÚhs@  Q