3 \�~�@s>dZddlZddlZddlZdddgZGdd�de�ZdZdd �Zd d �Z d d �Z dd�Z dd�Z dZ dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zdd!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/d�Z"e"Z#e$d0k�r:ddl%Z%e%j&d1d��sHe%j&j'd2�e%j&d1Z(e"e(d3���Z)e*d4e(�e*d5e)j+��e*d6e)j,��e*d7e)j-��e*d8e)j.��e*d9e)j/��e*d:e)j0��e%j&d;d��r0e%j&d;Z1e*d�Z6e6�sPe2j7e6��q�WWdQRXe*d?�WdQRXdS)AaJStuff 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. �N�Error�open�openfpc@s eZdZdS)rN)�__name__� __module__� __qualname__�rr�/usr/lib64/python3.6/aifc.pyr�sl@QEc Cs6ytjd|jd��dStjk r0t�YnXdS)Nz>l�r)�struct�unpack�read�error�EOFError)�filerrr � _read_long�src Cs6ytjd|jd��dStjk r0t�YnXdS)Nz>Lr r)r r r rr)rrrr � _read_ulong�src Cs6ytjd|jd��dStjk r0t�YnXdS)Nz>h�r)r r r rr)rrrr � _read_short�src Cs6ytjd|jd��dStjk r0t�YnXdS)Nz>Hrr)r r r rr)rrrr � _read_ushort�srcCs@t|jd��}|dkrd}n |j|�}|d@dkr<|jd�}|S)N�r�)�ordr )r�length�data�dummyrrr � _read_string�s   rg�������cCs�t|�}d}|dkr d }|d}t|�}t|�}||koL|koLdknrXd}n0|dkrft}n"|d}|d|td|d �}||S) Nrri�gi�i�?lg@�?�����)rr� _HUGE_VAL�pow)�f�expon�sign�himant�lomantrrr � _read_float�s"r&cCs|jtjd|��dS)Nz>h)�writer �pack)r!�xrrr � _write_short�sr*cCs|jtjd|��dS)Nz>H)r'r r()r!r)rrr � _write_ushort�sr+cCs|jtjd|��dS)Nz>l)r'r r()r!r)rrr � _write_long�sr,cCs|jtjd|��dS)Nz>L)r'r r()r!r)rrr � _write_ulong�sr-cCsRt|�dkrtd��|jtjdt|���|j|�t|�d@dkrN|jd�dS)N��z%string exceeds maximum pstring length�Brr�)�len� ValueErrorr'r r()r!�srrr � _write_string�s   r4c Cs�ddl}|dkrd}|d}nd}|dkr8d}d}d}n�|j|�\}}|dks^|dks^||krp|dB}d}d}nh|d}|dkr�|j||�}d}||B}|j|d�}|j|�}t|�}|j||d�}|j|�}t|�}t||�t||�t||�dS) Nri�ri@i�i�?� r)�mathZfrexpZldexpZfloor�intr+r-) r!r)r6r#r"r$r%ZfmantZfsmantrrr � _write_float�s8       r8)�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@s�eZdZdZdd�Zdd�Zdd�Zdd �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.d/�Zd0d1�ZdS)2� Aifc_readNc Cs<d|_d|_g|_d|_||_t|�}|j�dkr:td��|jd�}|dkrTd|_ n|dkrdd|_ ntd��d|_ d|_ x�d|_ yt|j�}Wnt k r�PYnX|j�}|d kr�|j|�d|_ nH|d kr�||_ |jd �}d|_ n(|d kr�t|�|_n|d k�r|j|�|j�qzW|j �s0|j �r8td��dS)NrsFORMz file does not start with FORM idr sAIFFsAIFCrznot an AIFF or AIFF-C filesCOMMsSSND�sFVERsMARKz$COMM chunk and/or SSND chunk missing)�_version�_convert�_markers� _soundpos�_filer9Zgetnamerr �_aifcZ_comm_chunk_read� _ssnd_chunk�_ssnd_seek_neededr�_read_comm_chunkr� _readmark�skip)�selfr�chunkZformdataZ chunknamerrrr �initfp4sJ        zAifc_read.initfpc CsLt|t�r>tj|d�}y|j|�WqH|j��YqHXn |j|�dS)N�rb)� isinstance�str�builtinsrrK�close)rIr!� file_objectrrr �__init__\s   zAifc_read.__init__cCs|S)Nr)rIrrr � __enter__hszAifc_read.__enter__cGs |j�dS)N)rP)rI�argsrrr �__exit__kszAifc_read.__exit__cCs|jS)N)rB)rIrrr �getfpqszAifc_read.getfpcCsd|_d|_dS)Nrr)rErA)rIrrr �rewindtszAifc_read.rewindcCs |j}|dk rd|_|j�dS)N)rBrP)rIrrrr rPxszAifc_read.closecCs|jS)N)rA)rIrrr �tell~szAifc_read.tellcCs|jS)N)� _nchannels)rIrrr � getnchannels�szAifc_read.getnchannelscCs|jS)N)�_nframes)rIrrr � getnframes�szAifc_read.getnframescCs|jS)N)� _sampwidth)rIrrr � getsampwidth�szAifc_read.getsampwidthcCs|jS)N)� _framerate)rIrrr � getframerate�szAifc_read.getframeratecCs|jS)N)� _comptype)rIrrr � getcomptype�szAifc_read.getcomptypecCs|jS)N)� _compname)rIrrr � getcompname�szAifc_read.getcompnamecCs*t|j�|j�|j�|j�|j�|j��S)N)r;rZr^r`r\rbrd)rIrrr � getparams�s zAifc_read.getparamscCst|j�dkrdS|jS)Nr)r1r@)rIrrr � getmarkers�szAifc_read.getmarkerscCs2x|jD]}||dkr|SqWtdj|���dS)Nrzmarker {0!r} does not exist)r@r�format)rI�id�markerrrr �getmark�s  zAifc_read.getmarkcCs*|dks||jkrtd��||_d|_dS)Nrzposition not in ranger)r[rrArE)rI�posrrr �setpos�szAifc_read.setposcCs�|jrD|jjd�|jjd�}|j|j}|r>|jj|d�d|_|dkrPdS|jj||j�}|jrv|rv|j|�}|jt|�|j|j |_|S)Nrr=r) rErD�seekr rA� _framesizer?r1rYr])rI�nframesrrkrrrr � readframes�s     zAifc_read.readframescCsddl}|j|d�S)Nrr)�audioopZalaw2lin)rIrrqrrr � _alaw2lin�szAifc_read._alaw2lincCsddl}|j|d�S)Nrr)rqZulaw2lin)rIrrqrrr � _ulaw2lin�szAifc_read._ulaw2lincCs2ddl}t|d�sd|_|j|d|j�\}|_|S)Nr� _adpcmstater)rq�hasattrrtZ adpcm2lin)rIrrqrrr � _adpcm2lin�s  zAifc_read._adpcm2lincCs.t|�|_t|�|_t|�dd|_tt|��|_|j|j|_|j �rd}|j dkrld}t j d�d|_ |j d�|_|r�t|jj d��}|d@dkr�|d}|j ||_ |jjdd�t|�|_|jd k�r*|jd kr�|j|_n2|jdkr�|j|_n|jdk�r|j|_ntd��d|_n d |_d|_dS)N�r=r�rzWarning: bad COMM chunk size�r sNONEsG722�ulaw�ULAW�alaw�ALAWzunsupported compression typersnot compressedr)rzr{)r|r})rrYrr[r]r7r&r_rnrCZ chunksize�warnings�warnr rarrrmrrcrvr?rsrrr)rIrJZkludgerrrr rF�s<               zAifc_read._read_comm_chunkc Cs�t|�}yHxBt|�D]6}t|�}t|�}t|�}|s8|r|jj|||f�qWWnDtk r�dt|j�t|j�dkr|dnd|f}tj |�YnXdS)Nz;Warning: MARK chunk contains only %s marker%s instead of %sr�r3) r�rangerrr@�appendrr1r~r)rIrJZnmarkers�irhrk�name�wrrr rG�szAifc_read._readmark)rrrrBrKrRrSrUrVrWrPrXrZr\r^r`rbrdrerfrjrlrprrrsrvrFrGrrrr r<s2$( &r<c@s0eZdZdZdd�Zdd�Zdd�Zdd �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.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Z dd?�Z"d@dA�Z#dBdC�Z$dDdE�Z%dFdG�Z&dHdI�Z'dS)J� Aifc_writeNc Cs\t|t�rNtj|d�}y|j|�Wn|j��YnX|jd�rXd|_n |j|�dS)N�wbz.aiffr)rMrNrOrrKrP�endswithrC)rIr!rQrrr rR+s   zAifc_write.__init__cCs^||_t|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ g|_ d|_d|_dS)NsNONEsnot compressedrr)rB� _AIFC_versionr>rarcr?rYr]r_r[�_nframeswritten� _datawritten� _datalengthr@� _marklengthrC)rIrrrr rK;szAifc_write.initfpcCs |j�dS)N)rP)rIrrr �__del__LszAifc_write.__del__cCs|S)Nr)rIrrr rSOszAifc_write.__enter__cGs |j�dS)N)rP)rIrTrrr rURszAifc_write.__exit__cCs|jrtd��d|_dS)Nz0cannot change parameters after starting to writer)r�rrC)rIrrr �aiffXszAifc_write.aiffcCs|jrtd��d|_dS)Nz0cannot change parameters after starting to writer)r�rrC)rIrrr �aifc]szAifc_write.aifccCs(|jrtd��|dkrtd��||_dS)Nz0cannot change parameters after starting to writerzbad # of channels)r�rrY)rI� nchannelsrrr � setnchannelsbs zAifc_write.setnchannelscCs|jstd��|jS)Nznumber of channels not set)rYr)rIrrr rZiszAifc_write.getnchannelscCs0|jrtd��|dks|dkr&td��||_dS)Nz0cannot change parameters after starting to writerr zbad sample width)r�rr])rI� sampwidthrrr � setsampwidthns zAifc_write.setsampwidthcCs|jstd��|jS)Nzsample width not set)r]r)rIrrr r^uszAifc_write.getsampwidthcCs(|jrtd��|dkrtd��||_dS)Nz0cannot change parameters after starting to writerzbad frame rate)r�rr_)rI� frameraterrr � setframeratezs zAifc_write.setframeratecCs|jstd��|jS)Nzframe rate not set)r_r)rIrrr r`�szAifc_write.getframeratecCs|jrtd��||_dS)Nz0cannot change parameters after starting to write)r�rr[)rIrorrr � setnframes�szAifc_write.setnframescCs|jS)N)r�)rIrrr r\�szAifc_write.getnframescCs.|jrtd��|d krtd��||_||_dS) Nz0cannot change parameters after starting to write�NONE�ulaw�ULAW�alaw�ALAW�G722zunsupported compression type)r�r�r�r�r�r�)r�rrarc)rI�comptype�compnamerrr � setcomptype�szAifc_write.setcomptypecCs|jS)N)ra)rIrrr rb�szAifc_write.getcomptypecCs|jS)N)rc)rIrrr rd�szAifc_write.getcompnamecCsf|\}}}}}}|jrtd��|d kr.td��|j|�|j|�|j|�|j|�|j||�dS) Nz0cannot change parameters after starting to write�NONE�ulaw�ULAW�alaw�ALAW�G722zunsupported compression type)r�r�r�r�r�r�)r�rr�r�r�r�r�)rIZparamsr�r�r�ror�r�rrr � setparams�s    zAifc_write.setparamscCs>|j s|j s|j r td��t|j|j|j|j|j|j�S)Nznot all parameters set)rYr]r_rr;r[rarc)rIrrr re�szAifc_write.getparamscCs�|dkrtd��|dkr td��t|t�s2td��x 0zmarker position must be >= 0zmarker name must be bytes)rrM�bytesr�r1r@r�)rIrhrkr�r�rrr �setmark�s zAifc_write.setmarkcCs2x|jD]}||dkr|SqWtdj|���dS)Nrzmarker {0!r} does not exist)r@rrg)rIrhrirrr rj�s  zAifc_write.getmarkcCst|j�dkrdS|jS)Nr)r1r@)rIrrr rf�szAifc_write.getmarkerscCs|jS)N)r�)rIrrr rX�szAifc_write.tellcCszt|ttf�st|�jd�}|jt|��t|�|j|j}|j rN|j |�}|j j |�|j ||_ |j t|�|_ dS)Nr/)rMr�� bytearray� memoryview�cast�_ensure_header_writtenr1r]rYr?rBr'r�r�)rIrrorrr �writeframesraw�s   zAifc_write.writeframesrawcCs.|j|�|j|jks"|j|jkr*|j�dS)N)r�r�r[r�r�� _patchheader)rIrrrr � writeframes�s   zAifc_write.writeframesc Cs�|jdkrdSz^|jd�|jd@r<|jjd�|jd|_|j�|j|jksb|j|jksb|jrj|j �Wdd|_ |j}d|_|j �XdS)Nrrr0) rBr�r�r'� _writemarkersr�r[r�r�r�r?rP)rIr!rrr rP�s         zAifc_write.closecCsddl}|j|d�S)Nrr)rqZlin2alaw)rIrrqrrr � _lin2alaw�szAifc_write._lin2alawcCsddl}|j|d�S)Nrr)rqZlin2ulaw)rIrrqrrr � _lin2ulaw�szAifc_write._lin2ulawcCs2ddl}t|d�sd|_|j|d|j�\}|_|S)Nrrtr)rqrurtZ lin2adpcm)rIrrqrrr � _lin2adpcms  zAifc_write._lin2adpcmcCsf|jsb|jd kr.|jsd|_|jdkr.td��|js|jdkr|j|_n&|jdkr(|j|_n|jdkr:|j|_dS)NsG722�ulaw�ULAW�alaw�ALAW)r�r�)r�r�)rar�r?r�r�)rIrrr �_init_compressions      zAifc_write._init_compressioncCsL|jr|jdkr|j�|jjd�|js<||j|j|_|j|j|j|_|jd@rf|jd|_|jr�|jdkr�|jd|_|jd@r�|jd|_n0|jd kr�|jd d |_|jd@r�|jd|_y|jj �|_ Wn t t fk �r�d|_ YnX|j |j�}|j�rD|jjd �|jjd �t|jd �t|j|j�n |jjd�|jjd�t|j|�t|j|j�|j dk �r�|jj �|_t|j|j�|jdk�r�t|jd�nt|j|jd�t|j|j�|j�r�|jj|j�t|j|j�|jjd�|j dk �r|jj �|_t|j|jd�t|jd�t|jd�dS)NsNONEsFORMr�ulaw�ULAW�alaw�ALAWr�G722�r sAIFCsFVERsAIFFsCOMMr=sSSNDr)r�r�r�r�)r�r�r�r�r�)rCrar�rBr'r[rYr]r�rX�_form_length_pos�AttributeError�OSError�_write_form_lengthr-r>r*� _nframes_posr8r_r4rc�_ssnd_length_pos)rIZ initlength� commlengthrrr r�!s^                        zAifc_write._write_headercCs\|jr*d t|j�}|d@r$|d}d}nd}d}t|jd||jd|d|�|S) Nrx�r� rr r=�ry)rCr1rcr-rBr�)rI� datalengthr�Z verslengthrrr r�Ts zAifc_write._write_form_lengthcCs�|jj�}|jd@r,|jd}|jjd�n|j}||jkrd|j|jkrd|jdkrd|jj|d�dS|jj|j d�|j |�}|jj|j d�t |j|j�|jj|j d�t |j|d�|jj|d�|j|_||_dS)Nrr0rr=)rBrXr�r'r�r[r�r�rmr�r�r�r-r�)rIZcurposr�rrrr r�as&       zAifc_write._patchheadercCs�t|j�dkrdS|jjd�d}xD|jD]:}|\}}}|t|�dd}t|�d@dkr*|d}q*Wt|j|�|d|_t|jt|j��x<|jD]2}|\}}}t|j|�t|j|�t|j|�q�WdS)NrsMARKrr�r=)r1r@rBr'r-r�r*r4)rIrrirhrkr�rrr r�ws"          zAifc_write._writemarkers)(rrrrBrRrKr�rSrUr�r�r�rZr�r^r�r`r�r\r�rbrdr�rer�rjrfrXr�r�rPr�r�r�r�r�r�r�r�r�rrrr r� sJ    3 r�cCsJ|dkrt|d�r|j}nd}|dkr.t|�S|dkr>t|�Std��dS) N�moderL�rr�r�z$mode must be 'r', 'rb', 'w', or 'wb')r�rL)r�r�)rur�r<r�r)r!r�rrr r�s �__main__rz/usr/demos/data/audio/bach.aiffr�ZReadingz nchannels =z nframes =z sampwidth =z framerate =z comptype =z compname =rZWritingr�izDone.)N)8�__doc__r rOr~�__all__� Exceptionrr�rrrrrrr&r*r+r,r-r4r8rJr9� collectionsr:r;r�r�r�ror�r�r<r�rrr�sys�argvr��fnr!�printrZr\r^r`rbrdZgn�gr�rerprr�rrrr ��sz  !  ~