[F@sdZddlmZeddZdZdZdZdZd Zd Z d Z d Z d Z dZ dZdZdZdZeeeee egZGdddeZddZddZGdddZGdddZdddZeZdS) aStuff 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('') 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.lc@seZdZdS)ErrorN)__name__ __module__ __qualname__rr/usr/lib/python3.4/sunau.pyrs rcCsSd}xFtdD]8}|jd}|s7tn|dt|}qW|S)Nrrr)rangereadEOFErrorord)filexiZbyterrr _read_u32s rcCseg}xEtdD]7}t|d\}}|jdt||}qW|jt|dS)Nrrr)rdivmodinsertintwritebytes)rrdatardmrrr _write_u32s  r'c@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)S)*Au_readcCs\t|tdkrBddl}|j|d}d|_n d|_|j|dS)NrrbTF)typebuiltinsopen_openedinitfp)selffr,rrr__init__s    zAu_read.__init__cCs|jr|jndS)N)_fileclose)r0rrr__del__s zAu_read.__del__cCs|S)Nr)r0rrr __enter__szAu_read.__enter__cGs|jdS)N)r4)r0argsrrr__exit__szAu_read.__exit__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)Nrzbad magic numberr zheader size too smalldzheader size ridiculously largezencoding not (yet) supportedrrrrzunknown encodingsr))r3 _soundposr!rAUDIO_FILE_MAGICrZ _hdr_size _data_sizeAUDIO_UNKNOWN_SIZE _encoding_simple_encodingsAUDIO_FILE_ENCODING_MULAW_8AUDIO_FILE_ENCODING_ALAW_8 _sampwidth _framesizeAUDIO_FILE_ENCODING_LINEAR_8AUDIO_FILE_ENCODING_LINEAR_16AUDIO_FILE_ENCODING_LINEAR_24AUDIO_FILE_ENCODING_LINEAR_32 _framerate _nchannelsr_inforlentell _data_posAttributeErrorOSError)r0rmagicrrrrr/sV          zAu_read.initfpcCs|jS)N)r3)r0rrrgetfpsz Au_read.getfpcCs|jS)N)rI)r0rrr getnchannelsszAu_read.getnchannelscCs|jS)N)rB)r0rrr getsampwidthszAu_read.getsampwidthcCs|jS)N)rH)r0rrr getframerateszAu_read.getframeratecCs4|jtkrtS|jtkr0|j|jSdS)Nr)r<r=r>r?rC)r0rrr getnframess zAu_read.getnframescCs.|jtkrdS|jtkr&dSdSdS)NULAWALAWNONE)r>r@rA)r0rrr getcomptypes zAu_read.getcomptypecCs.|jtkrdS|jtkr&dSdSdS)NzCCITT G.711 u-lawzCCITT G.711 A-lawznot compressed)r>r@rA)r0rrr getcompnames zAu_read.getcompnamecCs=t|j|j|j|j|j|jS)N)rrRrSrTrUrYrZ)r0rrr getparamsszAu_read.getparamscCsdS)Nr)r0rrr getmarkersszAu_read.getmarkerscCstddS)Nzno marks)r)r0idrrrgetmark szAu_read.getmarkcCs|jtkr|tkr-|jj}n|jj||j}|jt||j7_|jtkrddl }|j ||j }n|SdS)Nr) r>r?r=r3rrCr:rKr@audioopZulaw2linrB)r0nframesr$r_rrr readframess  zAu_read.readframescCs>|jdkrtdn|jj|jd|_dS)Nz cannot seekr)rMrOr3seekr:)r0rrrrewindszAu_read.rewindcCs|jS)N)r:)r0rrrrL!sz Au_read.tellcCsv|dks||jkr-tdn|jdkrKtdn|jj|j||j||_dS)Nrzposition not in rangez cannot seek)rUrrMrOr3rbrCr:)r0posrrrsetpos$s zAu_read.setposcCs/|jr"|jr"|jjnd|_dS)N)r.r3r4)r0rrrr4,sz Au_read.closeN)rrrr2r5r6r8r/rQrRrSrTrUrYrZr[r\r^rarcrLrer4rrrrr(s(     /             r(c@s<eZdZddZddZddZddZd 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/d0Zd1d2Zd3S)4Au_writecCs\t|tdkrBddl}|j|d}d|_n d|_|j|dS)Nr)rwbTF)r+r,r-r.r/)r0r1r,rrrr23s    zAu_write.__init__cCs#|jr|jnd|_dS)N)r3r4)r0rrrr5<s  zAu_write.__del__cCs|S)Nr)r0rrrr6AszAu_write.__enter__cGs|jdS)N)r4)r0r7rrrr8DszAu_write.__exit__cCsg||_d|_d|_d|_d|_t|_d|_d|_d|_ d|_ d|_ dS)NrrV) r3rHrIrBrCr=_nframes_nframeswritten _datawritten _datalengthrJ _comptype)r0rrrrr/Gs          zAu_write.initfpcCs@|jrtdn|dkr3tdn||_dS)Nz0cannot change parameters after starting to writerrrz"only 1, 2, or 4 channels supported)rrr)rjrrI)r0 nchannelsrrr setnchannelsTs   zAu_write.setnchannelscCs|jstdn|jS)Nznumber of channels not set)rIr)r0rrrrR[s zAu_write.getnchannelscCs@|jrtdn|dkr3tdn||_dS)Nz0cannot change parameters after starting to writerrrrzbad sample width)rrrr)rjrrB)r0 sampwidthrrr setsampwidth`s   zAu_write.setsampwidthcCs|jstdn|jS)Nzsample width not specified)rHrrB)r0rrrrSgs zAu_write.getsampwidthcCs%|jrtdn||_dS)Nz0cannot change parameters after starting to write)rjrrH)r0 frameraterrr setframeratels zAu_write.setframeratecCs|jstdn|jS)Nzframe rate not set)rHr)r0rrrrTqs zAu_write.getframeratecCs@|jrtdn|dkr3tdn||_dS)Nz0cannot change parameters after starting to writerz# of frames cannot be negative)rjrri)r0r`rrr setnframesvs   zAu_write.setnframescCs|jS)N)rj)r0rrrrU}szAu_write.getnframescCs(|dkr||_n tddS)NrXrVzunknown compression type)zNONEzULAW)rmr)r0r+namerrr setcomptypes  zAu_write.setcomptypecCs|jS)N)rm)r0rrrrYszAu_write.getcomptypecCs.|jdkrdS|jdkr&dSdSdS)NrVzCCITT G.711 u-lawrWzCCITT G.711 A-lawznot compressed)rm)r0rrrrZs zAu_write.getcompnamecCs`|\}}}}}}|j||j||j||j||j||dS)N)rorqrsrtrv)r0Zparamsrnrprrr`ZcomptypeZcompnamerrr setparamss     zAu_write.setparamscCs=t|j|j|j|j|j|jS)N)rrRrSrTrUrYrZ)r0rrrr[szAu_write.getparamscCs|jS)N)rj)r0rrrrLsz Au_write.tellcCst|ttfs-t|jd}n|j|jdkrjddl}|j||j }nt ||j }|j j ||j||_|jt ||_dS)NBrVr) isinstancer# bytearray memoryviewcast_ensure_header_writtenrmr_Zlin2ulawrBrKrCr3r"rjrk)r0r$r_r`rrrwriteframesraws  zAu_write.writeframesrawcCsB|j||j|jks1|j|jkr>|jndS)N)r~rjrirlrk _patchheader)r0r$rrr writeframess zAu_write.writeframesc Cs|jrzL|j|j|jks:|j|jkrG|jn|jjWd|jrz|jrz|jj nd|_XndS)N) r3r}rjrirlrkrflushr.r4)r0rrrr4s   zAu_write.closecCsb|js^|js!tdn|js9tdn|jsQtdn|jndS)Nz# of channels not specifiedzsample width not specifiedzframe rate not specified)rjrIrrBrH _write_header)r0rrrr}s    zAu_write._ensure_header_writtenc Cs|jdkr|jdkr0t}d|_q|jdkrQt}d|_q|jdkrrt}d|_q|jdkrt}d|_qtdn-|jdkrt}d|_n td|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 |jt |j |j |j j|j|j jd |t |jd dS) NrXrrrrzinternal errorrVr r sr i)rmrBrDrCrErFrGrr@rIr'r3r;rKrJrir=rL_form_length_posrNrOrlrHr")r0encodingZ header_sizeZlengthrrrrsJ        zAu_write._write_headercCsg|jdkrtdn|jj|jt|j|j|j|_|jjdddS)Nz cannot seekrr)rrOr3rbr'rkrl)r0rrrrs  zAu_write._patchheaderN)rrrr2r5r6r8r/rorRrqrSrsrTrtrUrvrYrZrwr[rLr~rr4r}rrrrrrrf1s2                     *rfNcCsl|dkr0t|dr'|j}q0d}n|dkrFt|S|dkr\t|StddS) Nmoder*rwrgz$mode must be 'r', 'rb', 'w', or 'wb')rzrb)rzwb)hasattrrr(rfr)r1rrrrr-s       r-)__doc__ collectionsrrr;r@rDrErFrGZAUDIO_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_5rAr=r? Exceptionrrr'r(rfr-Zopenfprrrrhs<