o ?Og] @stdZddlZddlZddlZddgZGdddeZdZddZd d Z d d Z d dZ ddZ dZ ddZddZddZddZddZddZddZdd lmZdd!lmZed"d#Zd$ej_d%ej_d&ej_d'ej_d(ej_d)ej_Gd*d+d+Z Gd,d-d-Z!d?d.dZ"e#d/kr8ddl$Z$e$j%d0dse$j%&d1e$j%d0Z'e"e'd2Z(e)d3e'e)d4e(*e)d5e(+e)d6e(,e)d7e(-e)d8e(.e)d9e(/e$j%d:dr%e$j%d:Z0e)d;e0e"e0d<Z1e12e(3 e(4d=Z5e5sne16e5qWdn 1swYe)d>WddSWddS1s1wYdSdS)@aJStuff to parse AIFF-C and AIFF files. Unless explicitly stated otherwise, the description below is true both for AIFF-C files and AIFF files. An AIFF-C file has the following structure. +-----------------+ | FORM | +-----------------+ | | +----+------------+ | | AIFC | | +------------+ | | | | | . | | | . | | | . | +----+------------+ An AIFF file has the string "AIFF" instead of "AIFC". A chunk consists of an identifier (4 bytes) followed by a size (4 bytes, big endian order), followed by the data. The size field does not include the size of the 8 byte header. The following chunk types are recognized. FVER (AIFF-C only). MARK <# of markers> (2 bytes) list of markers: (2 bytes, must be > 0) (4 bytes) ("pstring") COMM <# of channels> (2 bytes) <# of sound frames> (4 bytes) (2 bytes) (10 bytes, IEEE 80-bit extended floating point) in AIFF-C files only: (4 bytes) ("pstring") SSND (4 bytes, not used by this program) (4 bytes, not used by this program) A pstring consists of 1 byte length, a string of characters, and 0 or 1 byte pad to make the total length even. Usage. Reading AIFF files: f = aifc.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(). In some types of audio files, if the setpos() method is 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' for AIFF files) getcompname() -- returns human-readable version of compression type ('not compressed' for AIFF files) getparams() -- returns a namedtuple consisting of all of the above in the above order getmarkers() -- get the list of marks in the audio file or None if there are no marks getmark(id) -- get mark with the specified id (raises an error if the mark does not exist) 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(), the position given to setpos() and the position of marks are all 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 AIFF files: f = aifc.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: aiff() -- create an AIFF file (AIFF-C default) aifc() -- create an AIFF-C file 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 setmark(id, pos, name) -- add specified mark to the list of marks tell() -- return current position in output file (useful in combination with setmark()) 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. Marks can be added anytime. If there are any marks, you must call close() after all frames have been written. The close() method is called automatically when the class instance is destroyed. When a file is opened with the extension '.aiff', an AIFF file is written, otherwise an AIFF-C file is written. This default can be changed by calling aiff() or aifc() before the first writeframes or writeframesraw. NErroropenc@s eZdZdS)rN)__name__ __module__ __qualname__rr+/opt/alt/python310/lib64/python3.10/aifc.pyrsl@QEcC0z td|ddWStjytdw)N>lrstructZunpackreaderrorEOFErrorfilerrr _read_long rcCr )N>Lr rr rrrr _read_ulongrrcCr )N>hrr rrrr _read_shortrrcCr )N>Hrrr rrrr _read_ushortrrcCs@t|d}|dkrd}n||}|d@dkr|d}|S)Nr)ordr)rlengthdatadummyrrr _read_strings   r"gcCst|}d}|dkrd}|d}t|}t|}||kr'|kr'dkr/nnd}||S|dkr9t}||S|d}|d|td |d }||S) Nrrgi?lg@?)rr _HUGE_VALpow)fexponsignhimantlomantrrr _read_floats "r.cC|td|dS)Nrwriter packr)xrrr _write_shortr5cCr/)Nrr0r3rrr _write_ushortr6r7cCr/)Nr r0r3rrr _write_longr6r8cCr/)Nrr0r3rrr _write_ulongr6r9cCsVt|dkr td|tdt|||t|d@dkr)|ddSdS)Nz%string exceeds maximum pstring lengthBrr)len ValueErrorr1r r2)r)srrr _write_strings  r@c Csddl}|dkrd}|d}nd}|dkrd}d}d}nP||\}}|dks/|dks/||kr8|dB}d}d}n4|d}|dkrH|||}d}||B}||d}||}t|}|||d}||}t|}t||t||t||dS) Nrr$r#i@rr%i? )mathZfrexpZldexpZfloorintr7r9) r)r4rBr+r*r,r-ZfmantZfsmantrrr _write_floats8       rD)Chunk) namedtuple _aifc_paramsz7nchannels sampwidth framerate nframes comptype compnamez3Number of audio channels (1 for mono, 2 for stereo)zSample width in byteszSampling frequencyzNumber of audio framesz(Compression type ("NONE" for AIFF files)zRA human-readable version of the compression type ('not compressed' for AIFF files)c@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1ZdS)2 Aifc_readNcCs,d|_d|_g|_d|_||_t|}|dkrtd|d}|dkr*d|_ n |dkr2d|_ ntdd|_ d|_ d|_ zt|j}Wn t yPYn:w|}|d krb||d|_ n#|d krr||_ |d }d|_ n|d kr|t||_n |d kr|||q=|j r|j stddS)NrFORMz file does not start with FORM idr AIFFAIFCrznot an AIFF or AIFF-C fileCOMMSSNDFVERMARKz$COMM chunk and/or SSND chunk missing)_version_convert_markers _soundpos_filerEZgetnamerr_aifcZ_comm_chunk_read _ssnd_chunk_ssnd_seek_neededr_read_comm_chunkr _readmarkskip)selfrchunkZformdataZ chunknamer!rrrinitfp4sP        zAifc_read.initfpcCsFt|trt|d}z||WdS|||dS)Nrb) isinstancestrbuiltinsrr^closer\r)Z file_objectrrr__init__\s  zAifc_read.__init__cC|SNrr\rrr __enter__hzAifc_read.__enter__cG |dSrgrcr\argsrrr__exit__k zAifc_read.__exit__cC|jSrg)rUrhrrrgetfpqzAifc_read.getfpcCsd|_d|_dS)Nrr)rXrTrhrrrrewindts zAifc_read.rewindcCs$|j}|durd|_|dSdSrg)rUrcr\rrrrrcxs  zAifc_read.closecCrqrg)rTrhrrrtell~rszAifc_read.tellcCrqrg) _nchannelsrhrrr getnchannelsrszAifc_read.getnchannelscCrqrg)_nframesrhrrr getnframesrszAifc_read.getnframescCrqrg) _sampwidthrhrrr getsampwidthrszAifc_read.getsampwidthcCrqrg) _frameraterhrrr getframeraterszAifc_read.getframeratecCrqrg _comptyperhrrr getcomptyperszAifc_read.getcomptypecCrqrg _compnamerhrrr getcompnamerszAifc_read.getcompnamecCs*t||||||Srg)rGrxr|r~rzrrrhrrr getparamss  zAifc_read.getparamscCt|jdkr dS|jSNrr=rSrhrrr getmarkerszAifc_read.getmarkerscC.|jD] }||dkr|Sqtd|Nrzmarker {0!r} does not existrSrformatr\idmarkerrrrgetmark  zAifc_read.getmarkcCs*|dks ||jkr td||_d|_dS)Nrzposition not in ranger)ryrrTrX)r\posrrrsetposs zAifc_read.setposcCs|jr"|jd|jd}|j|j}|r|j|dd|_|dkr(dS|j||j}|jr;|r;||}|jt||j|j |_|S)NrrNr) rXrWseekrrT _framesizerRr=rwr{)r\nframesr!rr rrr readframess       zAifc_read.readframescCddl}||dSNrr)audioopZalaw2linr\r rrrr _alaw2lin zAifc_read._alaw2lincCrr)rZulaw2linrrrr _ulaw2linrzAifc_read._ulaw2lincC2ddl}t|ds d|_||d|j\}|_|SNr _adpcmstater)rhasattrrZ adpcm2linrrrr _adpcm2lins  zAifc_read._adpcm2lincCsRt||_t||_t|dd|_tt||_|jdkr#td|jdkr,td|j|j|_ |j rd}|j dkrGd}t dd |_ |d |_|rnt|jd}|d@dkra|d}|j ||_ |jd dt||_|jd kr|jd kr|j|_n|jdvr|j|_n|jdvr|j|_ntdd|_dSdSd |_d|_dS)NrNrbad sample widthbad # of channelsrzWarning: bad COMM chunk sizer r#NONEG722ulawULAWalawALAWunsupported compression typernot compressed)rrwrryr{rCr.r}rrrVZ chunksizewarningswarnrrrrrr"rrrRrr)r\r]ZkludgerrrrrYsF                   zAifc_read._read_comm_chunkcCst|}z#t|D]}t|}t|}t|}|s|r$|j|||fq WdStyIdt|jt|jdkrd?Z"d@dAZ#dBdCZ$dDdEZ%dFdGZ&dHdIZ'dS)J Aifc_writeNcCs\t|tr't|d}z||Wn||dr%d|_dSdS||dS)Nwbz.aiffr)r`rarbrr^rcendswithrVrdrrrre/s    zAifc_write.__init__cCs^||_t|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ g|_ d|_d|_dS)Nrrrr)rU _AIFC_versionrQrrrRrwr{r}ry_nframeswritten _datawritten _datalengthrS _marklengthrVrurrrr^?s zAifc_write.initfpcCrkrgrlrhrrr__del__PrpzAifc_write.__del__cCrfrgrrhrrrriSrjzAifc_write.__enter__cGrkrgrlrmrrrroVrpzAifc_write.__exit__cC|jrtdd|_dS)N0cannot change parameters after starting to writerrrrVrhrrraiff\ zAifc_write.aiffcCr)NrrrrhrrraifcarzAifc_write.aifccCs(|jrtd|dkrtd||_dS)Nrrr)rrrw)r\ nchannelsrrr setnchannelsf  zAifc_write.setnchannelscC|jstd|jS)Nznumber of channels not set)rwrrhrrrrxmzAifc_write.getnchannelscCs0|jrtd|dks|dkrtd||_dS)Nrrr r)rrr{)r\ sampwidthrrr setsampwidthrs  zAifc_write.setsampwidthcCr)Nzsample width not set)r{rrhrrrr|yrzAifc_write.getsampwidthcCs(|jrtd|dkrtd||_dS)Nrrzbad frame rate)rrr})r\ frameraterrr setframerate~rzAifc_write.setframeratecCr)Nzframe rate not set)r}rrhrrrr~rzAifc_write.getframeratecCs|jrtd||_dS)Nr)rrry)r\rrrr setnframesrzAifc_write.setnframescCrqrgrrhrrrrzrszAifc_write.getnframescCs.|jrtd|dvrtd||_||_dSNr)rrrrrrr)rrrr)r\comptypecompnamerrr setcomptypes  zAifc_write.setcomptypecCrqrgrrhrrrrrszAifc_write.getcomptypecCrqrgrrhrrrrrszAifc_write.getcompnamecCsf|\}}}}}}|jrtd|dvrtd|||||||||||dSr)rrrrrrr)r\paramsrrrrrrrrr setparamss    zAifc_write.setparamscCs8|jr |jr |js tdt|j|j|j|j|j|jS)Nznot all parameters set)rwr{r}rrGryrrrhrrrrs  zAifc_write.getparamscCs|dkrtd|dkrtdt|tstdtt|jD]}||j|dkr6|||f|j|<dSq |j|||fdS)Nrzmarker ID must be > 0zmarker position must be >= 0zmarker name must be bytes)rr`bytesrr=rSr)r\rrrrrrrsetmarks zAifc_write.setmarkcCrrrrrrrrrzAifc_write.getmarkcCrrrrhrrrrrzAifc_write.getmarkerscCrqrgrrhrrrrvrszAifc_write.tellcCszt|ttfst|d}|t|t||j|j}|j r'| |}|j ||j ||_ |j t||_ dS)Nr;)r`r bytearray memoryviewcast_ensure_header_writtenr=r{rwrRrUr1rr)r\r rrrrwriteframesraws   zAifc_write.writeframesrawcCs2|||j|jks|j|jkr|dSdSrg)rrryrr _patchheader)r\r rrr writeframess    zAifc_write.writeframescCs|jdurdSzM|d|jd@r|jd|jd|_||j|jks1|j|jks1|jrE| Wd|_ |j}d|_| dSWd|_ |j}d|_| dSd|_ |j}d|_| w)Nrrr<) rUrrr1 _writemarkersrryrrrrRrc)r\r)rrrrcs4           zAifc_write.closecCrr)rZlin2alawrrrr _lin2alawrzAifc_write._lin2alawcCrr)rZlin2ulawrrrr _lin2ulawrzAifc_write._lin2ulawcCrr)rrrZ lin2adpcmrrrr _lin2adpcms  zAifc_write._lin2adpcmcCsj|js3|jdvr|jsd|_|jdkrtd|jstd|js%td|js,td||dSdS)NrrrrrrzRsample width must be 2 when compressing with ulaw/ULAW, alaw/ALAW or G7.22 (ADPCM)z# channels not specifiedzsample width not specifiedzsampling rate not specified)rrr{rrwr} _write_header)r\Zdatasizerrrr s  z!Aifc_write._ensure_header_writtencCsF|jdkr |j|_dS|jdvr|j|_dS|jdvr!|j|_dSdS)Nrrr)rrrRrrrhrrr_init_compressions      zAifc_write._init_compressionc Cs@|jr |jdkr ||jd|js||j|j|_|j|j|j|_|jd@r3|jd|_|jre|jdvrM|jd|_|jd@rL|jd|_n|jdkre|jdd|_|jd@re|jd|_z|j |_ Wnt t fy{d|_ Ynw| |j}|jr|jd |jd t|jdt|j|jn|jd |jd t|j|t|j|j|j dur|j |_t|j|j|jd vrt|jdn t|j|jdt|j|j|jr|j|jt|j|j|jd|j dur |j |_t|j|jdt|jdt|jddS)NrrIr)rrrrrrr rKrOrJrLrrNrMr)rVrrrUr1ryrwr{rrv_form_length_posAttributeErrorOSError_write_form_lengthr9rQr5 _nframes_posrDr}r@r_ssnd_length_pos)r\Z initlength commlengthrrrr%sb                         zAifc_write._write_headercCs\|jrdt|j}|d@r|d}d}nd}d}t|jd||jd|d||S) Nrr rrr rN)rVr=rr9rUr)r\ datalengthrZ verslengthrrrrXs"zAifc_write._write_form_lengthcCs|j}|jd@r|jd}|jdn|j}||jkr2|j|jkr2|jdkr2|j|ddS|j|j d| |}|j|j dt |j|j|j|j dt |j|d|j|d|j|_||_dS)Nrr<rrN)rUrvrr1rryrrrrrrr9r)r\Zcurposrr!rrrres&        zAifc_write._patchheadercCst|jdkr dS|jdd}|jD]}|\}}}|t|dd}t|d@dkr1|d}qt|j||d|_t|jt|j|jD]}|\}}}t|j|t|j|t|j|qIdS)NrrPrrrN)r=rSrUr1r9rr5r@)r\rrrrrrrrr{s&         zAifc_write._writemarkers)(rrrrUrer^rrirorrrrxrr|rr~rrzrrrrrrrrrvrrrcrrrrrrrrrrrrrrsL    3 rcCsF|durt|dr |j}nd}|dvrt|S|dvrt|Std)Nmoder_)rr_)rrz$mode must be 'r', 'rb', 'w', or 'wb')rrrHrr)r)rrrrrs __main__rz/usr/demos/data/audio/bach.aiffrZReadingz nchannels =z nframes =z sampwidth =z framerate =z comptype =z compname =rZWritingrizDone.rg)7__doc__r rbr__all__ Exceptionrrrrrrr"r'r.r5r7r8r9r@rDr]rE collectionsrFrGrrrrrrrHrrrsysargvrfnr)printrxrzr|r~rrZgngrrrr rrrrrs   !             $