fsDc@sdZdZdZdZdZdZdZdZdZd Z d Z d Z d Z d Z dZeeeeee gZGdddeZddZddZGdddZGdddZdddZeZdS(uStuff 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.iiiiiiiiiiiilcBs|EeZdZdS(uErrorN(u__name__u __module__u __qualname__(u __locals__((u*/opt/alt/python33/lib64/python3.3/sunau.pyuErrorsuErrorcCsSd}xFtdD]8}|jd}|s7tn|dt|}qW|S(Niiii(urangeureaduEOFErroruord(ufileuxuiubyte((u*/opt/alt/python33/lib64/python3.3/sunau.pyu _read_u32s u _read_u32cCseg}xEtdD]7}t|d\}}|jdt||}qW|jt|dS(Niii(urangeudivmoduinsertuintuwriteubytes(ufileuxudatauiudum((u*/opt/alt/python33/lib64/python3.3/sunau.pyu _write_u32s  u _write_u32cBs|EeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%S(&uAu_readcCs\t|tdkrBddl}|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|jndS(N(u_fileuclose(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu__del__s uAu_read.__del__c Cs||_d|_tt|}|tkr?tdntt||_|jdkrrtdn|jdkrtdnt||_|jtkrt|j|_ntt||_ |j t krtdn|j t t fkr d|_ d |_n|j tkrBd |_|_ nr|j tkrdd|_|_ nP|j tkrd |_|_ n.|j tkrd |_|_ n td tt||_tt||_|j|j|_|jdkri|j|jd|_xVtt|jD]3}|j|d kr/|jd||_Pq/q/Wn d|_y|j|_Wn!ttfk rd|_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.pyuinitfpsV          uAu_read.initfpcCs|jS(N(u_file(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyugetfpsu Au_read.getfpcCs|jS(N(u _nchannels(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getnchannelssuAu_read.getnchannelscCs|jS(N(u _sampwidth(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getsampwidthsuAu_read.getsampwidthcCs|jS(N(u _framerate(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getframeratesuAu_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 getnframess 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 getcomptypes 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 getcompnames uAu_read.getcompnamecCs:|j|j|j|j|j|jfS(N(u getnchannelsu getsampwidthu getframerateu getnframesu getcomptypeu getcompname(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getparamssuAu_read.getparamscCsdS(N(uNone(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getmarkerssuAu_read.getmarkerscCstddS(Nuno marks(uError(uselfuid((u*/opt/alt/python33/lib64/python3.3/sunau.pyugetmarksuAu_read.getmarkcCs|jtkr|tkr-|jj}n|jj||j}|jt||j7_|jtkrddl }|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>|jdkrtdn|jj|jd|_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||jkr-tdn|jdkrKtdn|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"|jjnd|_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_reads$  /             uAu_readcBs(|EeZdZddZddZddZddZd 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/S(0uAu_writecCs\t|tdkrBddl}|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|jnd|_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@|jrtdn|dkr3tdn||_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|jstdn|jS(Nunumber of channels not set(u _nchannelsuError(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getnchannelsJs uAu_write.getnchannelscCs@|jrtdn|dkr3tdn||_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|jstdn|jS(Nusample width not specified(u _framerateuErroru _sampwidth(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getsampwidthVs uAu_write.getsampwidthcCs%|jrtdn||_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|jstdn|jS(Nuframe rate not set(u _framerateuError(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getframerate`s uAu_write.getframeratecCs@|jrtdn|dkr3tdn||_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 tddS(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 setparamss     uAu_write.setparamscCs:|j|j|j|j|j|jfS(N(u getnchannelsu getsampwidthu getframerateu getnframesu getcomptypeu getcompname(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu getparamssuAu_write.getparamscCs|jS(N(u_nframeswritten(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyutellsu 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.pyuwriteframesraws  uAu_write.writeframesrawcCsB|j||j|jks1|j|jkr>|jndS(N(uwriteframesrawu_nframeswrittenu_nframesu _datalengthu _datawrittenu _patchheader(uselfudata((u*/opt/alt/python33/lib64/python3.3/sunau.pyu writeframess uAu_write.writeframesc Cs|jrzn|j|j|jks:|j|jkrG|jn|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.pyucloses    uAu_write.closecCsb|js^|js!tdn|js9tdn|jsQtdn|jndS(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_writtens    uAu_write._ensure_header_writtenc Cs|jdkr|jdkr0t}d|_q|jdkrQt}d|_q|jdkrrt}d|_qtdn-|jdkrt}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 |jt |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_headersD       uAu_write._write_headercCsg|jdkrtdn|jj|jt|j|j|j|_|jjdddS(Nu cannot seekii(u_form_length_posuNoneuOSErroru_fileuseeku _write_u32u _datawrittenu _datalength(uself((u*/opt/alt/python33/lib64/python3.3/sunau.pyu _patchheaders  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|dr'|j}q0d}n|dkrFt|S|dkr\t|StddS( Numodeurburuwuwbu$mode must be 'r', 'rb', 'w', or 'wb'(ururb(uwuwb(uNoneuhasattrumodeuAu_readuAu_writeuError(ufumode((u*/opt/alt/python33/lib64/python3.3/sunau.pyuopens       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