grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026B ñ°¤”:¡ã@s¾dZddlmZddlZeddƒZdZdZdZd Zd Z d Z d Z d Z dZ dZdZdZdZdZeeee e egZGdd„deƒZdd„Zdd„ZGdd„dƒZGdd„dƒZd"dd„Zd#d d!„ZdS)$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. é)Ú namedtupleNÚ _sunau_paramsz7nchannels sampwidth framerate nframes comptype compnameidns.éééééééééééélÿÿc@s eZdZdS)ÚErrorN)Ú__name__Ú __module__Ú __qualname__©rrú*/opt/alt/python37/lib64/python3.7/sunau.pyr‰srcCs<d}x2tdƒD]&}| d¡}|s$t‚|dt|ƒ}qW|S)Nrrré)ÚrangeÚreadÚEOFErrorÚord)ÚfileÚxÚiZbyterrrÚ _read_u32Œs rcCsJg}x2tdƒD]&}t|dƒ\}}| dt|ƒ¡|}qW| t|ƒ¡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}| |d¡}d|_nd|_| |¡dS)NÚrÚrbTF)ÚtypeÚbuiltinsÚopenÚ_openedÚinitfp)ÚselfÚfr,rrrÚ__init__Ÿs  zAu_read.__init__cCs|jr| ¡dS)N)Ú_fileÚclose)r0rrrÚ__del__¨szAu_read.__del__cCs|S)Nr)r0rrrÚ __enter__¬szAu_read.__enter__cGs | ¡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 tk�rd |_|_ ntd ƒ‚tt|ƒƒ|_tt|ƒƒ|_|j�sLtd ƒ‚|j|j|_|jdk�rŽ| |jd¡|_|j d¡\|_}}nd|_y| ¡|_Wn ttfk �rÂd|_YnXdS)Nrzbad magic numberr zheader size too smallédzheader size ridiculously largezencoding not (yet) supportedrrrrzunknown encodingzbad # of channelsóó)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/²sT            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| ¡| ¡| ¡| ¡| ¡| ¡ƒS)N)rrUrVrWrXr\r])r0rrrÚ getparamss zAu_read.getparamscCsdS)Nr)r0rrrÚ getmarkersszAu_read.getmarkerscCs tdƒ‚dS)Nzno marks)r)r0ÚidrrrÚgetmark szAu_read.getmarkcCsp|jtkrl|tkr|j ¡}n|j ||j¡}|jt|ƒ|j7_|jtkrhddl }|  ||j ¡}|SdS)Nr) r@rAr?r3rrEr<ÚlenrBÚaudioopZulaw2linrD)r0Únframesr$rcrrrÚ readframess   zAu_read.readframescCs*|jdkrtdƒ‚|j |j¡d|_dS)Nz cannot seekr)rOrQr3Úseekr<)r0rrrÚrewinds zAu_read.rewindcCs|jS)N)r<)r0rrrrN!sz Au_read.tellcCsP|dks|| ¡krtdƒ‚|jdkr.tdƒ‚|j |j||j¡||_dS)Nrzposition not in rangez cannot seek)rXrrOrQr3rfrEr<)r0ÚposrrrÚsetpos$s  zAu_read.setposcCs"|j}|rd|_|jr| ¡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}| |d¡}d|_nd|_| |¡dS)Nr)rÚwbTF)r+r,r-r.r/)r0r1r,rrrr25s  zAu_write.__init__cCs|jr| ¡d|_dS)N)r3r4)r0rrrr5>szAu_write.__del__cCs|S)Nr)r0rrrr6CszAu_write.__enter__cGs | ¡dS)N)r4)r0r7rrrr8FszAu_write.__exit__cCsF||_d|_d|_d|_d|_t|_d|_d|_d|_ d|_ d|_ dS)Nrr;rY) r3rJrKrDrEr?Ú_nframesÚ_nframeswrittenÚ _datawrittenÚ _datalengthrLÚ _comptype)r0rrrrr/IszAu_write.initfpcCs(|jrtdƒ‚|dkrtdƒ‚||_dS)Nz0cannot change parameters after starting to write)rrrz"only 1, 2, or 4 channels supported)rmrrK)r0Ú nchannelsrrrÚ setnchannelsVs zAu_write.setnchannelscCs|jstdƒ‚|jS)Nznumber of channels not set)rKr)r0rrrrU]szAu_write.getnchannelscCs(|jrtdƒ‚|dkrtdƒ‚||_dS)Nz0cannot change parameters after starting to write)rrrrzbad sample width)rmrrD)r0Ú sampwidthrrrÚ setsampwidthbs zAu_write.setsampwidthcCs|jstdƒ‚|jS)Nzsample width not specified)rJrrD)r0rrrrViszAu_write.getsampwidthcCs|jrtdƒ‚||_dS)Nz0cannot change parameters after starting to write)rmrrJ)r0Ú frameraterrrÚ setframeratenszAu_write.setframeratecCs|jstdƒ‚|jS)Nzframe rate not set)rJr)r0rrrrWsszAu_write.getframeratecCs(|jrtdƒ‚|dkrtdƒ‚||_dS)Nz0cannot change parameters after starting to writerz# of frames cannot be negative)rmrrl)r0rdrrrÚ setnframesxs zAu_write.setnframescCs|jS)N)rm)r0rrrrXszAu_write.getnframescCs|dkr||_ntdƒ‚dS)N)r[rYzunknown compression type)rpr)r0r+ÚnamerrrÚ setcomptype‚szAu_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|\}}}}}}| |¡| |¡| |¡| |¡| ||¡dS)N)rrrtrvrwry)r0ZparamsrqrsrurdZcomptypeZcompnamerrrÚ setparams“s     zAu_write.setparamscCs*t| ¡| ¡| ¡| ¡| ¡| ¡ƒS)N)rrUrVrWrXr\r])r0rrrr^›s zAu_write.getparamscCs|jS)N)rm)r0rrrrN sz Au_write.tellcCs~t|ttfƒst|ƒ d¡}| ¡|jdkrDddl}| ||j ¡}t |ƒ|j }|j   |¡|j||_|jt |ƒ|_dS)NÚBrYr)Ú isinstancer#Ú bytearrayÚ memoryviewÚcastÚ_ensure_header_writtenrprcZlin2ulawrDrbrEr3r"rmrn)r0r$rcrdrrrÚwriteframesraw£s   zAu_write.writeframesrawcCs.| |¡|j|jks"|j|jkr*| ¡dS)N)r�rmrlrornÚ _patchheader)r0r$rrrÚ writeframes¯s   zAu_write.writeframescCs^|jrZz6| ¡|j|jks(|j|jkr0| ¡|j ¡Wd|j}d|_|jrX|  ¡XdS)N) r3r€rmrlrornr‚Úflushr.r4)r0rrrrr4µs  zAu_write.closecCs<|js8|jstdƒ‚|js"tdƒ‚|js0tdƒ‚| ¡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  ¡|_Wn ttfk �rd|_YnXt |j |ƒ||_t |j |ƒt |j |jƒt |j |j ƒ|j  |j¡|j  d |t |jƒd ¡dS) Nr[rrrrzinternal errorrYr r iøÿÿÿr:r )rprDrFrErGrHrIrrBrKr'r3r=rbrLrlr?rNÚ_form_length_posrPrQrorJr")r0ÚencodingZ header_sizeÚlengthrrrr…ÑsJ               zAu_write._write_headercCsH|jdkrtdƒ‚|j |j¡t|j|jƒ|j|_|j dd¡dS)Nz cannot seekrr)r†rQr3rfr'rnro)r0rrrr‚ûs  zAu_write._patchheaderN)rrrr2r5r6r8r/rrrUrtrVrvrWrwrXryr\r]rzr^rNr�rƒr4r€r…r‚rrrrrj3s2    *rjcCsJ|dkrt|dƒr|j}nd}|dkr.t|ƒS|dkr>t|ƒStdƒ‚dS)NÚmoder*)Úrr*)Úwrkz$mode must be 'r', 'rb', 'w', or 'wb')Úhasattrr‰r(rjr)r1r‰rrrr-s r-cCstjdtdd�t||d�S)NzDsunau.openfp is deprecated since Python 3.7. Use sunau.open instead.r)Ú stacklevel)r‰)ÚwarningsÚwarnÚDeprecationWarningr-)r1r‰rrrÚopenfps r‘)N)N)Ú__doc__Ú collectionsrrŽrr=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-r‘rrrrÚhsB  Q