a �DOg]� @s^ddlZddlZddlZddgZGdd�de�ZdZdd�Zdd �Zd d �Z d d �Z dd�Z dZ dd�Z dd�Zdd�Zdd�Zdd�Zdd�Zdd�ZddlmZdd 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#d.k�rZddl$Z$e$j%d/d��s>e$j%�&d0�e$j%d/Z'e"e'd1���Z(e)d2e'�e)d3e(�*��e)d4e(�+��e)d5e(�,��e)d6e(�-��e)d7e(�.��e)d8e(�/��e$j%d9d��r:e$j%d9Z0e)d:e0�e"e0d;��@Z1e1�2e(�3��e(�4d<�Z5e5�s�qe1�6e5��q�Wd�n1�s(0Ye)d=�Wd�n1�sP0YdS)?�N�Error�openc@s eZdZdS)rN)�__name__� __module__� __qualname__�rr�/usr/lib64/python3.9/aifc.pyr�sl@QEcCs8zt�d|�d��dWStjy2td�Yn0dS)N�>l�r��struct�unpack�read�error�EOFError��filerrr� _read_long�srcCs8zt�d|�d��dWStjy2td�Yn0dS)N�>Lr rr rrrr� _read_ulong�srcCs8zt�d|�d��dWStjy2td�Yn0dS)N�>h�rr rrrr� _read_short�srcCs8zt�d|�d��dWStjy2td�Yn0dS)N�>Hrrr rrrr� _read_ushort�srcCs@t|�d��}|dkrd}n |�|�}|d@dkr<|�d�}|S)N�r�)�ordr)r�length�data�dummyrrr� _read_string�s   r!g�������cCs�t|�}d}|dkr d}|d}t|�}t|�}||krN|krNdkrXnnd}n0|dkrft}n"|d}|d|td |d �}||S) Nrr�������g��i�?lg@�?)rr� _HUGE_VAL�pow)�f�expon�sign�himant�lomantrrr� _read_float�s"r-cCs|�t�d|��dS)Nr��writer �pack�r(�xrrr� _write_short�sr3cCs|�t�d|��dS)Nrr.r1rrr� _write_ushort�sr4cCs|�t�d|��dS)Nr r.r1rrr� _write_long�sr5cCs|�t�d|��dS)Nrr.r1rrr� _write_ulong�sr6cCsRt|�dkrtd��|�t�dt|���|�|�t|�d@dkrN|�d�dS)N��z%string exceeds maximum pstring length�Brr�)�len� ValueErrorr/r r0)r(�srrr� _write_string�s   r=c Cs�ddl}|dkrd}|d}nd}|dkr8d}d}d}n�|�|�\}}|dks^|dks^||krp|dB}d}d}nh|d}|dkr�|�||�}d}||B}|�|d�}|�|�}t|�}|�||d�}|�|�}t|�}t||�t||�t||�dS) Nrr#r"i@rr$i�?� )�math�frexp�ldexp�floor�intr4r6) r(r2r?r*r)r+r,ZfmantZfsmantrrr� _write_float�s8       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@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_readNcCs6d|_d|_g|_d|_||_t|�}|��dkr:td��|�d�}|dkrTd|_ n|dkrdd|_ ntd��d|_ d|_ d|_ zt|j�}Wnt y�Y�qYn0|��}|d kr�|�|�d|_ nH|d kr�||_ |�d �}d|_ n(|d kr�t|�|_n|d k�r|�|�|��qx|j �r*|j �s2td��dS)Nr�FORMz file does not start with FORM idr �AIFF�AIFCrznot an AIFF or AIFF-C file�COMM�SSND��FVER�MARKz$COMM chunk and/or SSND chunk missing)�_version�_convert�_markers� _soundpos�_filerEZgetnamerr�_aifcZ_comm_chunk_read� _ssnd_chunk�_ssnd_seek_neededr�_read_comm_chunkr� _readmark�skip)�selfr�chunkZformdataZ chunknamer rrr�initfp4sH          zAifc_read.initfpcCsLt|t�r>t�|d�}z|�|�WqH|���YqH0n |�|�dS)N�rb)� isinstance�str�builtinsrr^�close�r\r(Z file_objectrrr�__init__\s   zAifc_read.__init__cCs|S�Nr�r\rrr� __enter__hszAifc_read.__enter__cGs |��dSrf�rc�r\�argsrrr�__exit__kszAifc_read.__exit__cCs|jSrf)rUrgrrr�getfpqszAifc_read.getfpcCsd|_d|_dS)Nrr)rXrTrgrrr�rewindtszAifc_read.rewindcCs |j}|durd|_|��dSrf)rUrc�r\rrrrrcxszAifc_read.closecCs|jSrf)rTrgrrr�tell~szAifc_read.tellcCs|jSrf)� _nchannelsrgrrr� getnchannels�szAifc_read.getnchannelscCs|jSrf)�_nframesrgrrr� getnframes�szAifc_read.getnframescCs|jSrf)� _sampwidthrgrrr� getsampwidth�szAifc_read.getsampwidthcCs|jSrf)� _frameratergrrr� getframerate�szAifc_read.getframeratecCs|jSrf�� _comptypergrrr� getcomptype�szAifc_read.getcomptypecCs|jSrf�� _compnamergrrr� getcompname�szAifc_read.getcompnamecCs*t|��|��|��|��|��|���Srf)rGrrrvrxrtr{r~rgrrr� getparams�s  �zAifc_read.getparamscCst|j�dkrdS|jS�Nr�r:rSrgrrr� getmarkers�szAifc_read.getmarkerscCs2|jD]}||dkr|Sqtd�|���dS�Nrzmarker {0!r} does not exist�rSr�format�r\�id�markerrrr�getmark�s   zAifc_read.getmarkcCs*|dks||jkrtd��||_d|_dS)Nrzposition not in ranger)rsrrTrX)r\�posrrr�setpos�szAifc_read.setposcCs�|jrD|j�d�|j�d�}|j|j}|r>|j�|d�d|_|dkrPdS|j�||j�}|jrv|rv|�|�}|jt|�|j|j |_|S)NrrNr) rXrW�seekrrT� _framesizerRr:rqru)r\�nframesr r�rrrr� readframes�s      � zAifc_read.readframescCsddl}|�|d�S�Nrr)�audioopZalaw2lin�r\rr�rrr� _alaw2lin�szAifc_read._alaw2lincCsddl}|�|d�Sr�)r�Zulaw2linr�rrr� _ulaw2lin�szAifc_read._ulaw2lincCs2ddl}t|d�sd|_|�|d|j�\}|_|S�Nr� _adpcmstater)r��hasattrr�Z adpcm2linr�rrr� _adpcm2lin�s  zAifc_read._adpcm2lincCsVt|�|_t|�|_t|�dd|_tt|��|_|jdkrFtd��|jdkrXtd��|j|j|_ |j �rFd}|j dkr�d}t � d�d |_ |�d �|_|r�t|j�d��}|d@dkr�|d}|j ||_ |j�d d�t|�|_|jd k�rR|jd k�r |j|_n4|jdv�r |j|_n|jdv�r6|j|_ntd��d|_n d |_d|_dS)N�rNr�bad sample width�bad # of channels�rzWarning: bad COMM chunk size�r r"�NONE�G722��ulaw�ULAW��alaw�ALAW�unsupported compression typer�not compressed)rrqrrsrurCr-rwrr�rV� chunksize�warnings�warnrrzrrr�r!r}r�rRr�r�)r\r]ZkludgerrrrrY�sD                 zAifc_read._read_comm_chunkcCs�t|�}zDt|�D]6}t|�}t|�}t|�}|s6|r|j�|||f�qWnBty�dt|j�t|j�dkrvdnd|f}t� |�Yn0dS)Nz;Warning: MARK chunk contains only %s marker%s instead of %sr�r<) r�rangerr!rS�appendrr:r�r�)r\r]Znmarkers�ir�r��name�wrrrrZ�s  ��zAifc_read._readmark)rrrrUr^rerhrlrmrnrcrprrrtrvrxr{r~rr�r�r�r�r�r�r�rYrZrrrrrHs2$( *rHc@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_writeNcCs\t|t�rNt�|d�}z|�|�Wn|���Yn0|�d�rXd|_n |�|�dS)N�wbz.aiffr)r`rarbrr^rc�endswithrVrdrrrre/s   zAifc_write.__init__cCs^||_t|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ g|_ d|_d|_dS)Nr�r�rr)rU� _AIFC_versionrQrzr}rRrqrurwrs�_nframeswritten� _datawritten� _datalengthrS� _marklengthrVrorrrr^?szAifc_write.initfpcCs |��dSrfrirgrrr�__del__PszAifc_write.__del__cCs|SrfrrgrrrrhSszAifc_write.__enter__cGs |��dSrfrirjrrrrlVszAifc_write.__exit__cCs|jrtd��d|_dS)N�0cannot change parameters after starting to writer�r�rrVrgrrr�aiff\szAifc_write.aiffcCs|jrtd��d|_dS)Nr�rr�rgrrr�aifcaszAifc_write.aifccCs(|jrtd��|dkrtd��||_dS)Nr�rr�)r�rrq)r\� nchannelsrrr� setnchannelsfs zAifc_write.setnchannelscCs|jstd��|jS)Nznumber of channels not set)rqrrgrrrrrmszAifc_write.getnchannelscCs0|jrtd��|dks|dkr&td��||_dS)Nr�rr r�)r�rru)r\� sampwidthrrr� setsampwidthrs zAifc_write.setsampwidthcCs|jstd��|jS)Nzsample width not set)rurrgrrrrvyszAifc_write.getsampwidthcCs(|jrtd��|dkrtd��||_dS)Nr�rzbad frame rate)r�rrw)r\� frameraterrr� setframerate~s zAifc_write.setframeratecCs|jstd��|jS)Nzframe rate not set)rwrrgrrrrx�szAifc_write.getframeratecCs|jrtd��||_dS)Nr�)r�rrs)r\r�rrr� setnframes�szAifc_write.setnframescCs|jSrf�r�rgrrrrt�szAifc_write.getnframescCs.|jrtd��|dvrtd��||_||_dS�Nr�)r�r�r�r�r�r�r�)r�rrzr})r\�comptype�compnamerrr� setcomptype�s zAifc_write.setcomptypecCs|jSrfryrgrrrr{�szAifc_write.getcomptypecCs|jSrfr|rgrrrr~�szAifc_write.getcompnamecCsf|\}}}}}}|jrtd��|dvr.td��|�|�|�|�|�|�|�|�|�||�dSr�)r�rr�r�r�r�r�)r\�paramsr�r�r�r�r�r�rrr� setparams�s    zAifc_write.setparamscCs8|jr|jr|jstd��t|j|j|j|j|j|j�S)Nznot all parameters set)rqrurwrrGrsrzr}rgrrrr�s  �zAifc_write.getparamscCs�|dkrtd��|dkr td��t|t�s2td��tt|j��D],}||j|dkr@|||f|j|<dSq@|j�|||f�dS)Nrzmarker ID must be > 0zmarker position must be >= 0zmarker name must be bytes)rr`�bytesr�r:rSr�)r\r�r�r�r�rrr�setmark�s zAifc_write.setmarkcCs2|jD]}||dkr|Sqtd�|���dSr�r�r�rrrr��s   zAifc_write.getmarkcCst|j�dkrdS|jSr�r�rgrrrr��szAifc_write.getmarkerscCs|jSrfr�rgrrrrp�szAifc_write.tellcCszt|ttf�st|��d�}|�t|��t|�|j|j}|j rN|� |�}|j � |�|j ||_ |j t|�|_ dS)Nr8)r`r�� bytearray� memoryview�cast�_ensure_header_writtenr:rurqrRrUr/r�r�)r\rr�rrr�writeframesraw�s   zAifc_write.writeframesrawcCs.|�|�|j|jks"|j|jkr*|��dSrf)r�r�rsr�r�� _patchheader)r\rrrr� writeframes�s    �zAifc_write.writeframescCs�|jdurdSzx|�d�|jd@r<|j�d�|jd|_|��|j|jksb|j|jksb|jrj|� �Wd|_ |j}d|_|� �nd|_ |j}d|_|� �0dS)Nrrr9) rUr�r�r/� _writemarkersr�rsr�r�r�rRrc)r\r(rrrrc�s,       ��  �zAifc_write.closecCsddl}|�|d�Sr�)r�Zlin2alawr�rrr� _lin2alaw�szAifc_write._lin2alawcCsddl}|�|d�Sr�)r�Zlin2ulawr�rrr� _lin2ulawszAifc_write._lin2ulawcCs2ddl}t|d�sd|_|�|d|j�\}|_|Sr�)r�r�r�Z lin2adpcmr�rrr� _lin2adpcms  zAifc_write._lin2adpcmcCsf|jsb|jdvr.|jsd|_|jdkr.td��|js|jdkr|j|_n&|jdvr(|j|_n|jdvr:|j|_dS)Nr�r�r�)rzr�rRr�r�rgrrr�_init_compressions      zAifc_write._init_compressionc CsH|jr|jdkr|��|j�d�|js<||j|j|_|j|j|j|_|jd@rf|jd|_|jr�|jdvr�|jd|_|jd@r�|jd|_n0|jdkr�|jdd|_|jd@r�|jd|_z|j� �|_ Wnt t fy�d|_ Yn0|� |j�}|j�r@|j�d �|j�d �t|jd�t|j|j�n |j�d �|j�d �t|j|�t|j|j�|j du�r�|j� �|_t|j|j�|jd v�r�t|jd�nt|j|jd�t|j|j�|j�r�|j�|j�t|j|j�|j�d�|j du�r|j� �|_t|j|jd�t|jd�t|jd�dS)Nr�rIr)r�r�r�r�rr��r rKrOrJrLr�rNrMr)rVrzr�rUr/rsrqrur�rp�_form_length_pos�AttributeError�OSError�_write_form_lengthr6rQr3� _nframes_posrDrwr=r}�_ssnd_length_pos)r\Z initlength� commlengthrrrr�%s^                        zAifc_write._write_headercCs\|jr*dt|j�}|d@r$|d}d}nd}d}t|jd||jd|d|�|S) Nr�r� r�rr rN�)rVr:r}r6rUr�)r\� datalengthr�Z verslengthrrrr�Xs"����zAifc_write._write_form_lengthcCs�|j��}|jd@r,|jd}|j�d�n|j}||jkrd|j|jkrd|jdkrd|j�|d�dS|j�|j d�|� |�}|j�|j d�t |j|j�|j�|j d�t |j|d�|j�|d�|j|_||_dS)Nrr9rrN)rUrpr�r/r�rsr�r�r�r�r�r�r6r�)r\Zcurposr�r rrrr�es*     �� zAifc_write._patchheadercCs�t|j�dkrdS|j�d�d}|jD]:}|\}}}|t|�dd}t|�d@dkr(|d}q(t|j|�|d|_t|jt|j��|jD]2}|\}}}t|j|�t|j|�t|j|�q�dS)NrrPrr�rN)r:rSrUr/r6r�r3r=)r\rr�r�r�r�rrrr�{s"          zAifc_write._writemarkers)(rrrrUrer^r�rhrlr�r�r�rrr�rvr�rxr�rtr�r{r~r�rr�r�r�rpr�r�rcr�r�r�r�r�r�r�r�r�rrrrr�sJ    3 r�cCsJ|durt|d�r|j}nd}|dvr.t|�S|dvr>t|�Std��dS)N�moder_)�rr_)r�r�z$mode must be 'r', 'rb', 'w', or 'wb')r�r�rHr�r)r(r�rrrr�s �__main__rz/usr/demos/data/audio/bach.aiffr�ZReadingz nchannels =z nframes =z sampwidth =z framerate =z comptype =z compname =rZWritingr�izDone.)N)7r rbr��__all__� Exceptionrr�rrrrr!r&r-r3r4r5r6r=rDr]rE� collectionsrFrGr��__doc__r�r�r�r�r�rHr�rr�sys�argvr��fnr(�printrrrtrvrxr{r~Zgn�gr�rr�rr�rrrr��sx !  �          .