o w[e] @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/usr/lib/python3.10/aifc.pyrsl@QEcC0z td|ddWStjytdw)N>lrstructunpackreaderrorEOFErrorfilerrr _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_shortr6cCr0)Nrr1r4rrr _write_ushortr7r8cCr0)Nr r1r4rrr _write_longr7r9cCr0)Nrr1r4rrr _write_ulongr7r:cCsVt|dkr td|tdt|||t|d@dkr)|ddSdS)Nz%string exceeds maximum pstring lengthBrr)len ValueErrorr2r r3)r*srrr _write_strings  rAc 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? )mathfrexpldexpfloorintr8r:) r*r5rCr,r+r-r.fmantfsmantrrr _write_floats8       rJ)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_filerKgetnamerr_aifc_comm_chunk_read _ssnd_chunk_ssnd_seek_neededr_read_comm_chunkr _readmarkskip)selfrchunkformdata chunknamer"rrrinitfp4sP        zAifc_read.initfpcCsFt|trt|d}z||WdS|||dS)Nrb) isinstancestrbuiltinsrrhcloserdr* file_objectrrr__init__\s  zAifc_read.__init__cC|SNrrdrrr __enter__hzAifc_read.__enter__cG |dSrrrmrdargsrrr__exit__k zAifc_read.__exit__cC|jSrr)r[rsrrrgetfpqzAifc_read.getfpcCsd|_d|_dS)Nrr)r`rZrsrrrrewindts zAifc_read.rewindcCs$|j}|durd|_|dSdSrr)r[rmrdrrrrrmxs  zAifc_read.closecCr|rr)rZrsrrrtell~r~zAifc_read.tellcCr|rr) _nchannelsrsrrr getnchannelsr~zAifc_read.getnchannelscCr|rr)_nframesrsrrr getnframesr~zAifc_read.getnframescCr|rr) _sampwidthrsrrr getsampwidthr~zAifc_read.getsampwidthcCr|rr) _frameratersrrr getframerater~zAifc_read.getframeratecCr|rr _comptypersrrr getcomptyper~zAifc_read.getcomptypecCr|rr _compnamersrrr getcompnamer~zAifc_read.getcompnamecCs*t||||||Srr)rMrrrrrrrsrrr getparamss  zAifc_read.getparamscCt|jdkr dS|jSNrr>rYrsrrr getmarkerszAifc_read.getmarkerscC.|jD] }||dkr|Sqtd|Nrzmarker {0!r} does not existrYrformatrdidmarkerrrrgetmark  zAifc_read.getmarkcCs*|dks ||jkr td||_d|_dS)Nrzposition not in ranger)rrrZr`)rdposrrrsetposs zAifc_read.setposcCs|jr"|jd|jd}|j|j}|r|j|dd|_|dkr(dS|j||j}|jr;|r;||}|jt||j|j |_|S)NrrTr) r`r_seekrrZ _framesizerXr>rr)rdnframesr"rr!rrr readframess       zAifc_read.readframescCddl}||dSNrr)audioopalaw2linrdr!rrrr _alaw2lin zAifc_read._alaw2lincCrr)rulaw2linrrrr _ulaw2linrzAifc_read._ulaw2lincC2ddl}t|ds d|_||d|j\}|_|SNr _adpcmstater)rhasattrr 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)NrTrbad sample widthbad # of channelsrzWarning: bad COMM chunk sizer r$NONEG722ulawULAWalawALAWunsupported compression typernot compressed)rrrrrrGr/rrrr] chunksizewarningswarnrrrrrr#rrrXrr)rdrekludger rrrrasF                   zAifc_read._read_comm_chunkcCst|}z#t|D]}t|}t|}t|}|s|r$|j|||fq WdStyIdt|jt|jdkrrr)rdrenmarkersirrnamewrrrrbs$  zAifc_read._readmark)rrrr[rhrprtrzr}rrmrrrrrrrrrrrrrrrrarbrrrrrNs4$(  *rNc@s0eZdZdZddZddZddZdd 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/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9Zd:d;Z dd?Z"d@dAZ#dBdCZ$dDdEZ%dFdGZ&dHdIZ'dS)J Aifc_writeNcCs\t|tr't|d}z||Wn||dr%d|_dSdS||dS)Nwbz.aiffr)rjrkrlrrhrmendswithr]rnrrrrp/s    zAifc_write.__init__cCs^||_t|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ g|_ d|_d|_dS)Nrrrr)r[ _AIFC_versionrWrrrXrrrr_nframeswritten _datawritten _datalengthrY _marklengthr]rrrrrh?s zAifc_write.initfpcCrvrrrwrsrrr__del__Pr{zAifc_write.__del__cCrqrrrrsrrrrtSruzAifc_write.__enter__cGrvrrrwrxrrrrzVr{zAifc_write.__exit__cC|jrtdd|_dS)N0cannot change parameters after starting to writerrrr]rsrrraiff\ zAifc_write.aiffcCr)NrrrrsrrraifcarzAifc_write.aifccCs(|jrtd|dkrtd||_dS)Nrrr)rrr)rd nchannelsrrr setnchannelsf  zAifc_write.setnchannelscC|jstd|jS)Nznumber of channels not set)rrrsrrrrmzAifc_write.getnchannelscCs0|jrtd|dks|dkrtd||_dS)Nrrr r)rrr)rd sampwidthrrr setsampwidthrs  zAifc_write.setsampwidthcCr)Nzsample width not set)rrrsrrrryrzAifc_write.getsampwidthcCs(|jrtd|dkrtd||_dS)Nrrzbad frame rate)rrr)rd frameraterrr setframerate~rzAifc_write.setframeratecCr)Nzframe rate not set)rrrsrrrrrzAifc_write.getframeratecCs|jrtd||_dS)Nr)rrr)rdrrrr setnframesrzAifc_write.setnframescCr|rrrrsrrrrr~zAifc_write.getnframescCs.|jrtd|dvrtd||_||_dSNr)rrrrrrr)rrrr)rdcomptypecompnamerrr setcomptypes  zAifc_write.setcomptypecCr|rrrrsrrrrr~zAifc_write.getcomptypecCr|rrrrsrrrrr~zAifc_write.getcompnamecCsf|\}}}}}}|jrtd|dvrtd|||||||||||dSr)rrrrrrr)rdparamsrrrrrrrrr setparamss    zAifc_write.setparamscCs8|jr |jr |js tdt|j|j|j|j|j|jS)Nznot all parameters set)rrrrrMrrrrsrrrrs  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)rrjbytesrr>rYr)rdrrrrrrrsetmarks zAifc_write.setmarkcCrrrrrrrrrzAifc_write.getmarkcCrrrrsrrrrrzAifc_write.getmarkerscCr|rrrrsrrrrr~zAifc_write.tellcCszt|ttfst|d}|t|t||j|j}|j r'| |}|j ||j ||_ |j t||_ dS)Nr<)rjr bytearray memoryviewcast_ensure_header_writtenr>rrrXr[r2rr)rdr!rrrrwriteframesraws   zAifc_write.writeframesrawcCs2|||j|jks|j|jkr|dSdSrr)rrrrr _patchheader)rdr!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=) r[rrr2 _writemarkersrrrrrrXrm)rdr*rrrrms4           zAifc_write.closecCrr)rlin2alawrrrr _lin2alawrzAifc_write._lin2alawcCrr)rlin2ulawrrrr _lin2ulawrzAifc_write._lin2ulawcCrr)rrr 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)rrrrrr _write_header)rddatasizerrrr s  z!Aifc_write._ensure_header_writtencCsF|jdkr |j|_dS|jdvr|j|_dS|jdvr!|j|_dSdS)Nrrr)rrrXrrrsrrr_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)NrrOr)rrrrrrr rQrUrPrRrrTrSr)r]rrr[r2rrrrr_form_length_posAttributeErrorOSError_write_form_lengthr:rWr6 _nframes_posrJrrAr_ssnd_length_pos)rd initlength commlengthrrrr%sb                         zAifc_write._write_headercCs\|jrdt|j}|d@r|d}d}nd}d}t|jd||jd|d||S) Nrr rrr rT)r]r>rr:r[r)rd datalengthr  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=rrT)r[rrr2rrrrrrrrr:r)rdcurposr r"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)NrrVrrrT)r>rYr[r2r:rr6rA)rdr rrrrrrrr{s&         zAifc_write._writemarkers)(rrrr[rprhrrtrzrrrrrrrrrrrrrrrrrrrrrrmrrrrrrrrrrrrrrsL    3 rcCsF|durt|dr |j}nd}|dvrt|S|dvrt|Std)Nmoderi)rri)rrz$mode must be 'r', 'rb', 'w', or 'wb')rrrNrr)r*rrrrrs __main__rz/usr/demos/data/audio/bach.aiffrReadingz nchannels =z nframes =z sampwidth =z framerate =z comptype =z compname =rWritingrizDone.rr)7__doc__r rlr__all__ Exceptionrrrrrrr#r(r/r6r8r9r:rArJrerK collectionsrLrMrrrrrrrNrrrsysargvrfnr*printrrrrrrgngrrrr!rrrrrs   !             $