grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026ž õ fsDc@sßdZdZdZdZdZdZdZdZdZd Z d Z d Z d Z d Z dZeeeeee gZGdd„deƒZdd„Zdd„ZGdd„dƒZGdd„dƒZddd„ZeZdS(uÔ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 tuple 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('') or close() to patch up the sizes in the header. The close() method is called automatically when the class instance is destroyed. idns.iiiiiiiiiiiilÿÿcBs|EeZdZdS(uErrorN(u__name__u __module__u __qualname__(u __locals__((u*/opt/alt/python33/lib64/python3.3/sunau.pyuErrorƒsuErrorcCsSd}xFtdƒD]8}|jdƒ}|s7t‚n|dt|ƒ}qW|S(Niiii(urangeureaduEOFErroruord(ufileuxuiubyte((u*/opt/alt/python33/lib64/python3.3/sunau.pyu _read_u32†s u _read_u32cCseg}xEtdƒD]7}t|dƒ\}}|jdt|ƒƒ|}qW|jt|ƒƒdS(Niii(urangeudivmoduinsertuintuwriteubytes(ufileuxudatauiudum((u*/opt/alt/python33/lib64/python3.3/sunau.pyu _write_u32�s  u _write_u32cBsì|EeZdZdd„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%S(&uAu_readcCs\t|ƒtdƒkrBddl}|j|dƒ}d|_n d|_|j|ƒdS(NuiurbTF(utypeubuiltinsuopenuTrueu_openeduFalseuinitfp(uselfufubuiltins((u*/opt/alt/python33/lib64/python3.3/sunau.pyu__init__™s    uAu_read.__init__cCs|jr|jƒndS(N(u_fileuclose(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu__del__¢s uAu_read.__del__c Cs­||_d|_tt|ƒƒ}|tkr?tdƒ‚ntt|ƒƒ|_|jdkrrtdƒ‚n|jdkr�tdƒ‚nt|ƒ|_|jtkrÃt|jƒ|_ntt|ƒƒ|_ |j t krötdƒ‚n|j t t fkr d|_ d |_n”|j tkrBd |_|_ nr|j tkrdd|_|_ nP|j tkr†d |_|_ n.|j tkr¨d |_|_ n td ƒ‚tt|ƒƒ|_tt|ƒƒ|_|j|j|_|jdkri|j|jdƒ|_xVtt|jƒƒD]3}|j|d kr/|jd|…|_Pq/q/Wn d|_y|jƒ|_Wn!ttfk r¨d|_YnXdS(Niubad magic numberiuheader size too smalliduheader size ridiculously largeuencoding not (yet) supportediiiiuunknown encodingsu(u_fileu _soundposuintu _read_u32uAUDIO_FILE_MAGICuErroru _hdr_sizeu _data_sizeuAUDIO_UNKNOWN_SIZEu _encodingu_simple_encodingsuAUDIO_FILE_ENCODING_MULAW_8uAUDIO_FILE_ENCODING_ALAW_8u _sampwidthu _framesizeuAUDIO_FILE_ENCODING_LINEAR_8uAUDIO_FILE_ENCODING_LINEAR_16uAUDIO_FILE_ENCODING_LINEAR_24uAUDIO_FILE_ENCODING_LINEAR_32u _framerateu _nchannelsureadu_infourangeulenutellu _data_posuAttributeErroruOSErroruNone(uselfufileumagicui((u*/opt/alt/python33/lib64/python3.3/sunau.pyuinitfp¦sV          uAu_read.initfpcCs|jS(N(u_file(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyugetfpÕsu Au_read.getfpcCs|jS(N(u _nchannels(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getnchannelsØsuAu_read.getnchannelscCs|jS(N(u _sampwidth(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getsampwidthÛsuAu_read.getsampwidthcCs|jS(N(u _framerate(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getframerateÞsuAu_read.getframeratecCs4|jtkrtS|jtkr0|j|jSdS(Ni(u _data_sizeuAUDIO_UNKNOWN_SIZEu _encodingu_simple_encodingsu _framesize(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getnframesás uAu_read.getnframescCs.|jtkrdS|jtkr&dSdSdS(NuULAWuALAWuNONE(u _encodinguAUDIO_FILE_ENCODING_MULAW_8uAUDIO_FILE_ENCODING_ALAW_8(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getcomptypeès uAu_read.getcomptypecCs.|jtkrdS|jtkr&dSdSdS(NuCCITT G.711 u-lawuCCITT G.711 A-lawunot compressed(u _encodinguAUDIO_FILE_ENCODING_MULAW_8uAUDIO_FILE_ENCODING_ALAW_8(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getcompnameðs uAu_read.getcompnamecCs:|jƒ|jƒ|jƒ|jƒ|jƒ|jƒfS(N(u getnchannelsu getsampwidthu getframerateu getnframesu getcomptypeu getcompname(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getparamsøsuAu_read.getparamscCsdS(N(uNone(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getmarkersýsuAu_read.getmarkerscCstdƒ‚dS(Nuno marks(uError(uselfuid((u*/opt/alt/python33/lib64/python3.3/sunau.pyugetmarksuAu_read.getmarkcCs�|jtkr™|tkr-|jjƒ}n|jj||jƒ}|jt|ƒ|j7_|jtkr•ddl }|j ||j ƒ}n|SdS(Ni( u _encodingu_simple_encodingsuAUDIO_UNKNOWN_SIZEu_fileureadu _framesizeu _soundposulenuAUDIO_FILE_ENCODING_MULAW_8uaudioopuulaw2linu _sampwidthuNone(uselfunframesudatauaudioop((u*/opt/alt/python33/lib64/python3.3/sunau.pyu readframess  uAu_read.readframescCs>|jdkrtdƒ‚n|jj|jƒd|_dS(Nu cannot seeki(u _data_posuNoneuOSErroru_fileuseeku _soundpos(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyurewindsuAu_read.rewindcCs|jS(N(u _soundpos(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyutellsu Au_read.tellcCsv|dks||jƒkr-tdƒ‚n|jdkrKtdƒ‚n|jj|j||jƒ||_dS(Niuposition not in rangeu cannot seek( u getnframesuErroru _data_posuNoneuOSErroru_fileuseeku _framesizeu _soundpos(uselfupos((u*/opt/alt/python33/lib64/python3.3/sunau.pyusetposs uAu_read.setposcCs/|jr"|jr"|jjƒnd|_dS(N(u_openedu_fileucloseuNone(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyuclose!su Au_read.closeN(u__name__u __module__u __qualname__u__init__u__del__uinitfpugetfpu getnchannelsu getsampwidthu getframerateu getnframesu getcomptypeu getcompnameu getparamsu getmarkersugetmarku readframesurewindutellusetposuclose(u __locals__((u*/opt/alt/python33/lib64/python3.3/sunau.pyuAu_read—s$  /             uAu_readcBs(|EeZdZdd„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/S(0uAu_writecCs\t|ƒtdƒkrBddl}|j|dƒ}d|_n d|_|j|ƒdS(NuiuwbTF(utypeubuiltinsuopenuTrueu_openeduFalseuinitfp(uselfufubuiltins((u*/opt/alt/python33/lib64/python3.3/sunau.pyu__init__(s    uAu_write.__init__cCs#|jr|jƒnd|_dS(N(u_fileucloseuNone(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu__del__1s  uAu_write.__del__cCsg||_d|_d|_d|_d|_t|_d|_d|_d|_ d|_ d|_ dS(NisuULAW( u_fileu _framerateu _nchannelsu _sampwidthu _framesizeuAUDIO_UNKNOWN_SIZEu_nframesu_nframeswrittenu _datawrittenu _datalengthu_infou _comptype(uselfufile((u*/opt/alt/python33/lib64/python3.3/sunau.pyuinitfp6s          uAu_write.initfpcCs@|jrtdƒ‚n|dkr3tdƒ‚n||_dS(Nu0cannot change parameters after starting to writeiiiu"only 1, 2, or 4 channels supported(iii(u_nframeswrittenuErroru _nchannels(uselfu nchannels((u*/opt/alt/python33/lib64/python3.3/sunau.pyu setnchannelsCs   uAu_write.setnchannelscCs|jstdƒ‚n|jS(Nunumber of channels not set(u _nchannelsuError(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getnchannelsJs uAu_write.getnchannelscCs@|jrtdƒ‚n|dkr3tdƒ‚n||_dS(Nu0cannot change parameters after starting to writeiiiubad sample width(iii(u_nframeswrittenuErroru _sampwidth(uselfu sampwidth((u*/opt/alt/python33/lib64/python3.3/sunau.pyu setsampwidthOs   uAu_write.setsampwidthcCs|jstdƒ‚n|jS(Nusample width not specified(u _framerateuErroru _sampwidth(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getsampwidthVs uAu_write.getsampwidthcCs%|jrtdƒ‚n||_dS(Nu0cannot change parameters after starting to write(u_nframeswrittenuErroru _framerate(uselfu framerate((u*/opt/alt/python33/lib64/python3.3/sunau.pyu setframerate[s uAu_write.setframeratecCs|jstdƒ‚n|jS(Nuframe rate not set(u _framerateuError(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getframerate`s uAu_write.getframeratecCs@|jrtdƒ‚n|dkr3tdƒ‚n||_dS(Nu0cannot change parameters after starting to writeiu# of frames cannot be negative(u_nframeswrittenuErroru_nframes(uselfunframes((u*/opt/alt/python33/lib64/python3.3/sunau.pyu setnframeses   uAu_write.setnframescCs|jS(N(u_nframeswritten(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getnframeslsuAu_write.getnframescCs(|dkr||_n tdƒ‚dS(NuNONEuULAWuunknown compression type(uNONEuULAW(u _comptypeuError(uselfutypeuname((u*/opt/alt/python33/lib64/python3.3/sunau.pyu setcomptypeos  uAu_write.setcomptypecCs|jS(N(u _comptype(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getcomptypeusuAu_write.getcomptypecCs.|jdkrdS|jdkr&dSdSdS(NuULAWuCCITT G.711 u-lawuALAWuCCITT G.711 A-lawunot compressed(u _comptype(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getcompnamexs uAu_write.getcompnamecCs`|\}}}}}}|j|ƒ|j|ƒ|j|ƒ|j|ƒ|j||ƒdS(N(u setnchannelsu setsampwidthu setframerateu setnframesu setcomptype(uselfuparamsu nchannelsu sampwidthu framerateunframesucomptypeucompname((u*/opt/alt/python33/lib64/python3.3/sunau.pyu setparams€s     uAu_write.setparamscCs:|jƒ|jƒ|jƒ|jƒ|jƒ|jƒfS(N(u getnchannelsu getsampwidthu getframerateu getnframesu getcomptypeu getcompname(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getparamsˆsuAu_write.getparamscCs|jS(N(u_nframeswritten(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyutell�su Au_write.tellcCsŠ|jƒ|jdkr=ddl}|j||jƒ}nt|ƒ|j}|jj|ƒ|j ||_ |j t|ƒ|_ dS(NuULAWi( u_ensure_header_writtenu _comptypeuaudioopulin2ulawu _sampwidthulenu _framesizeu_fileuwriteu_nframeswrittenu _datawritten(uselfudatauaudioopunframes((u*/opt/alt/python33/lib64/python3.3/sunau.pyuwriteframesraw�s  uAu_write.writeframesrawcCsB|j|ƒ|j|jks1|j|jkr>|jƒndS(N(uwriteframesrawu_nframeswrittenu_nframesu _datalengthu _datawrittenu _patchheader(uselfudata((u*/opt/alt/python33/lib64/python3.3/sunau.pyu writeframesšs uAu_write.writeframesc Cs‹|jr‡zn|jƒ|j|jks:|j|jkrG|jƒn|jjƒ|jrv|jrv|jj ƒnWdd|_XndS(N( u_fileu_ensure_header_writtenu_nframeswrittenu_nframesu _datalengthu _datawrittenu _patchheaderuflushu_openeducloseuNone(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyuclose s    uAu_write.closecCsb|js^|js!tdƒ‚n|js9tdƒ‚n|jsQtdƒ‚n|jƒndS(Nu# of channels not specifiedusample width not specifieduframe rate not specified(u_nframeswrittenu _nchannelsuErroru _sampwidthu _framerateu _write_header(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu_ensure_header_written±s    uAu_write._ensure_header_writtenc Csï|jdkr�|jdkr0t}d|_q®|jdkrQt}d|_q®|jdkrrt}d|_q®tdƒ‚n-|jdkr¢t}d|_n tdƒ‚|j|j|_t |j t ƒdt |j ƒ}|dd @}t |j |ƒ|jtkrt}n|j|j}y|j jƒ|_Wn!ttfk rcd|_YnXt |j |ƒ||_t |j |ƒt |j |jƒt |j |jƒ|j j|j ƒ|j jd |t |j ƒd ƒdS( NuNONEiiiuinternal erroruULAWiisiiøÿÿÿ(u _comptypeu _sampwidthuAUDIO_FILE_ENCODING_LINEAR_8u _framesizeuAUDIO_FILE_ENCODING_LINEAR_16uAUDIO_FILE_ENCODING_LINEAR_32uErroruAUDIO_FILE_ENCODING_MULAW_8u _nchannelsu _write_u32u_fileuAUDIO_FILE_MAGICulenu_infou_nframesuAUDIO_UNKNOWN_SIZEutellu_form_length_posuAttributeErroruOSErroruNoneu _datalengthu _framerateuwrite(uselfuencodingu header_sizeulength((u*/opt/alt/python33/lib64/python3.3/sunau.pyu _write_header»sD       uAu_write._write_headercCsg|jdkrtdƒ‚n|jj|jƒt|j|jƒ|j|_|jjddƒdS(Nu cannot seekii(u_form_length_posuNoneuOSErroru_fileuseeku _write_u32u _datawrittenu _datalength(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu _patchheaderâs  uAu_write._patchheaderN(u__name__u __module__u __qualname__u__init__u__del__uinitfpu setnchannelsu getnchannelsu setsampwidthu getsampwidthu setframerateu getframerateu setnframesu getnframesu setcomptypeu getcomptypeu getcompnameu setparamsu getparamsutelluwriteframesrawu writeframesucloseu_ensure_header_writtenu _write_headeru _patchheader(u __locals__((u*/opt/alt/python33/lib64/python3.3/sunau.pyuAu_write&s.                  'uAu_writecCsl|dkr0t|dƒr'|j}q0d}n|dkrFt|ƒS|dkr\t|ƒStdƒ‚dS( Numodeurburuwuwbu$mode must be 'r', 'rb', 'w', or 'wb'(ururb(uwuwb(uNoneuhasattrumodeuAu_readuAu_writeuError(ufumode((u*/opt/alt/python33/lib64/python3.3/sunau.pyuopenês       uopenN(u__doc__uAUDIO_FILE_MAGICuAUDIO_FILE_ENCODING_MULAW_8uAUDIO_FILE_ENCODING_LINEAR_8uAUDIO_FILE_ENCODING_LINEAR_16uAUDIO_FILE_ENCODING_LINEAR_24uAUDIO_FILE_ENCODING_LINEAR_32uAUDIO_FILE_ENCODING_FLOATuAUDIO_FILE_ENCODING_DOUBLEuAUDIO_FILE_ENCODING_ADPCM_G721uAUDIO_FILE_ENCODING_ADPCM_G722u AUDIO_FILE_ENCODING_ADPCM_G723_3u AUDIO_FILE_ENCODING_ADPCM_G723_5uAUDIO_FILE_ENCODING_ALAW_8uAUDIO_UNKNOWN_SIZEu_simple_encodingsu ExceptionuErroru _read_u32u _write_u32uAu_readuAu_writeuNoneuopenuopenfp(((u*/opt/alt/python33/lib64/python3.3/sunau.pyuhs6  �Ä