Your IP : 13.59.58.68


Current Path : /lib64/python3.8/__pycache__/
Upload File :
Current File : //lib64/python3.8/__pycache__/aifc.cpython-38.opt-1.pyc

U

e5d.��
@sBdZddlZddlZddlZdddgZGdd�de�ZdZdd	�Zd
d�Z	dd
�Z
dd�Zdd�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!dAd/d�Z"dBd0d�Z#e$d1k�r>ddl%Z%e%j&d2d��sNe%j&�'d3�e%j&d2Z(e"e(d4���Z)e*d5e(�e*d6e)�+��e*d7e)�,��e*d8e)�-��e*d9e)�.��e*d:e)�/��e*d;e)�0��e%j&d<d��r4e%j&d<Z1e*d=e1�e"e1d>��6Z2e2�3e)�4��e)�5d?�Z6e6�s�q"e2�7e6��qW5QRXe*d@�W5QRXdS)CaJStuff 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            |
  +-----------------+
  | <size>          |
  +----+------------+
  |    | AIFC       |
  |    +------------+
  |    | <chunks>   |
  |    |    .       |
  |    |    .       |
  |    |    .       |
  +----+------------+

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
      <version number of AIFF-C defining document> (AIFF-C only).
  MARK
      <# of markers> (2 bytes)
      list of markers:
          <marker ID> (2 bytes, must be > 0)
          <position> (4 bytes)
          <marker name> ("pstring")
  COMM
      <# of channels> (2 bytes)
      <# of sound frames> (4 bytes)
      <size of the samples> (2 bytes)
      <sampling frequency> (10 bytes, IEEE 80-bit extended
          floating point)
      in AIFF-C files only:
      <compression type> (4 bytes)
      <human-readable version of compression type> ("pstring")
  SSND
      <offset> (4 bytes, not used by this program)
      <blocksize> (4 bytes, not used by this program)
      <sound data>

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.8/aifc.pyr�sl@QEcCs:zt�d|�d��dWStjk
r4td�YnXdS)N�>l�r��structZunpack�read�error�EOFError��filerrr	�
_read_long�srcCs:zt�d|�d��dWStjk
r4td�YnXdS)N�>Lrrrrrrr	�_read_ulong�srcCs:zt�d|�d��dWStjk
r4td�YnXdS)N�>h�rrrrrr	�_read_short�srcCs:zt�d|�d��dWStjk
r4td�YnXdS)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�?� )�mathZfrexpZldexpZfloor�intr4r6)	r(r2r?r*r)r+r,ZfmantZfsmantrrr	�_write_float�s8




rA)�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�Zdd�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_readNcCs8d|_d|_g|_d|_||_t|�}|��dkr:td��|�d�}|dkrTd|_	n|dkrdd|_	ntd��d|_
d|_d|_zt|j�}Wnt
k
r�Y�qYnX|��}|d	kr�|�|�d|_
nH|d
kr�||_|�d�}d|_n(|dkr�t|�|_n|d
k�r|�|�|��qx|j
�r,|j�s4td��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�_filerBZgetnamerr�_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|���YqHXn
|�|�dS)N�rb)�
isinstance�str�builtinsrr[�close�rYr(Zfile_objectrrr	�__init__\s

zAifc_read.__init__cCs|S�Nr�rYrrr	�	__enter__hszAifc_read.__enter__cGs|��dSrc�r`�rY�argsrrr	�__exit__kszAifc_read.__exit__cCs|jSrc)rRrdrrr	�getfpqszAifc_read.getfpcCsd|_d|_dS)Nrr)rUrQrdrrr	�rewindtszAifc_read.rewindcCs |j}|dk	rd|_|��dSrc)rRr`�rYrrrr	r`xszAifc_read.closecCs|jSrc)rQrdrrr	�tell~szAifc_read.tellcCs|jSrc)�
_nchannelsrdrrr	�getnchannels�szAifc_read.getnchannelscCs|jSrc)�_nframesrdrrr	�
getnframes�szAifc_read.getnframescCs|jSrc)�
_sampwidthrdrrr	�getsampwidth�szAifc_read.getsampwidthcCs|jSrc)�
_frameraterdrrr	�getframerate�szAifc_read.getframeratecCs|jSrc��	_comptyperdrrr	�getcomptype�szAifc_read.getcomptypecCs|jSrc��	_compnamerdrrr	�getcompname�szAifc_read.getcompnamecCs*t|��|��|��|��|��|���Src)rDrorsrurqrxr{rdrrr	�	getparams�s�zAifc_read.getparamscCst|j�dkrdS|jS�Nr�r:rPrdrrr	�
getmarkers�szAifc_read.getmarkerscCs2|jD]}||dkr|Sqtd�|���dS�Nrzmarker {0!r} does not exist�rPr�format�rY�id�markerrrr	�getmark�s

zAifc_read.getmarkcCs*|dks||jkrtd��||_d|_dS)Nrzposition not in ranger)rprrQrU)rY�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)NrrKr)
rUrT�seekrrQ�
_framesizerOr:rnrr)rY�nframesr r�rrrr	�
readframes�s 

�
zAifc_read.readframescCsddl}|�|d�S�Nrr)�audioopZalaw2lin�rYrr�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}|jdkr�d}t�
d�d	|_|�d
�|_|r�t|j�d��}|d@dkr�|d}|j||_|j�dd�t|�|_|jdk�rR|jd
k�r
|j|_n4|jdk�r |j|_n|jdk�r6|j|_ntd��d|_nd|_d|_dS)N�rKr�bad sample width�bad # of channels�rzWarning: bad COMM chunk size�rr"�NONE�G722��ulaw�ULAW��alaw�ALAW�unsupported compression typer�not compressed)rrnrrprrr@r-rtrr�rSZ	chunksize�warnings�warnrrwrrr�r!rzr�rOr�r�)rYrZZkludgerrrr	rV�sD









zAifc_read._read_comm_chunkcCs�t|�}zDt|�D]6}t|�}t|�}t|�}|s6|r|j�|||f�qWnDtk
r�dt|j�t|j�dkrxdnd|f}t�	|�YnXdS)Nz;Warning: MARK chunk contains only %s marker%s instead of %sr�r<)
r�rangerr!rP�appendrr:r�r�)rYrZZnmarkers�ir�r��name�wrrr	rW�s��zAifc_read._readmark)rrrrRr[rbrerirjrkr`rmrorqrsrurxr{r|rr�r�r�r�r�r�rVrWrrrr	rEs2$(*rEc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�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 d<d=�Z!d>d?�Z"d@dA�Z#dBdC�Z$dDdE�Z%dFdG�Z&dHdI�Z'dS)J�
Aifc_writeNcCs\t|t�rNt�|d�}z|�|�Wn|���YnX|�d�rXd|_n
|�|�dS)N�wbz.aiffr)r]r^r_rr[r`�endswithrSrarrr	rb/s

zAifc_write.__init__cCs^||_t|_d|_d|_d|_d|_d|_d|_d|_	d|_
d|_d|_g|_
d|_d|_dS)Nr�r�rr)rR�
_AIFC_versionrNrwrzrOrnrrrtrp�_nframeswritten�_datawritten�_datalengthrP�_marklengthrSrlrrr	r[?szAifc_write.initfpcCs|��dSrcrfrdrrr	�__del__PszAifc_write.__del__cCs|Srcrrdrrr	reSszAifc_write.__enter__cGs|��dSrcrfrgrrr	riVszAifc_write.__exit__cCs|jrtd��d|_dS)N�0cannot change parameters after starting to writer�r�rrSrdrrr	�aiff\szAifc_write.aiffcCs|jrtd��d|_dS)Nr�rr�rdrrr	�aifcaszAifc_write.aifccCs(|jrtd��|dkrtd��||_dS)Nr�rr�)r�rrn)rY�	nchannelsrrr	�setnchannelsfs
zAifc_write.setnchannelscCs|jstd��|jS)Nznumber of channels not set)rnrrdrrr	romszAifc_write.getnchannelscCs0|jrtd��|dks|dkr&td��||_dS)Nr�rrr�)r�rrr)rY�	sampwidthrrr	�setsampwidthrs
zAifc_write.setsampwidthcCs|jstd��|jS)Nzsample width not set)rrrrdrrr	rsyszAifc_write.getsampwidthcCs(|jrtd��|dkrtd��||_dS)Nr�rzbad frame rate)r�rrt)rY�	frameraterrr	�setframerate~s
zAifc_write.setframeratecCs|jstd��|jS)Nzframe rate not set)rtrrdrrr	ru�szAifc_write.getframeratecCs|jrtd��||_dS)Nr�)r�rrp)rYr�rrr	�
setnframes�szAifc_write.setnframescCs|jSrc�r�rdrrr	rq�szAifc_write.getnframescCs.|jrtd��|dkrtd��||_||_dS�Nr�)r�r�r�r�r�r�r�)r�rrwrz)rY�comptype�compnamerrr	�setcomptype�szAifc_write.setcomptypecCs|jSrcrvrdrrr	rx�szAifc_write.getcomptypecCs|jSrcryrdrrr	r{�szAifc_write.getcompnamecCsf|\}}}}}}|jrtd��|dkr.td��|�|�|�|�|�|�|�|�|�||�dSr�)r�rr�r�r�r�r�)rYZparamsr�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)rnrrrtrrDrprwrzrdrrr	r|�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:rPr�)rYr�r�r�r�rrr	�setmark�s
zAifc_write.setmarkcCs2|jD]}||dkr|Sqtd�|���dSr�r�r�rrr	r��s

zAifc_write.getmarkcCst|j�dkrdS|jSr}r~rdrrr	r�szAifc_write.getmarkerscCs|jSrcr�rdrrr	rm�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:rrrnrOrRr/r�r�)rYrr�rrr	�writeframesraw�s
zAifc_write.writeframesrawcCs.|�|�|j|jks"|j|jkr*|��dSrc)r�r�rpr�r��_patchheader)rYrrrr	�writeframes�s


�zAifc_write.writeframescCs�|jdkrdSz^|�d�|jd@r<|j�d�|jd|_|��|j|jksb|j	|jksb|j
rj|��W5d|_|j}d|_|��XdS)Nrrr9)rRrOr`r�r�r/�
_writemarkersr�rpr�r�r�)rYr(rrr	r`�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|jdkr.|jsd|_|jdkr.td��|js<td��|jsJtd��|jsXtd��|�|�dS)N�r�r�r�r�r�rzRsample 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)r�rwrrrrnrt�
_write_header)rYZdatasizerrr	r�
s

z!Aifc_write._ensure_header_writtencCs>|jdkr|j|_n&|jdkr(|j|_n|jdkr:|j|_dS)Nr�r�r�)rwr�rOr�r�rdrrr	�_init_compressions




zAifc_write._init_compressionc	CsJ|jr|jdkr|��|j�d�|js<||j|j|_|j|j|j|_|jd@rf|jd|_|jr�|jdkr�|jd|_|jd@r�|jd|_n0|jdkr�|jdd|_|jd@r�|jd|_z|j�	�|_
Wnttfk
r�d|_
YnX|�
|j�}|j�rB|j�d	�|j�d
�t|jd�t|j|j�n|j�d�|j�d�t|j|�t|j|j�|j
dk	�r�|j�	�|_t|j|j�|jd
k�r�t|jd�nt|j|jd�t|j|j�|j�r�|j�|j�t|j|j�|j�d�|j
dk	�r|j�	�|_t|j|jd�t|jd�t|jd�dS)Nr�rFr)r�r�r�r�rr��rrHrLrGrIr�rKrJr)rSrwr�rRr/rprnrrr�rm�_form_length_pos�AttributeError�OSError�_write_form_lengthr6rNr3�_nframes_posrArtr=rz�_ssnd_length_pos)rYZ
initlength�
commlengthrrr	r�%s^




zAifc_write._write_headercCs\|jr*dt|j�}|d@r$|d}d}nd}d}t|jd||jd|d|�|S)	Nr�r�r�rrrK�)rSr:rzr6rRr�)rY�
datalengthr�Z
verslengthrrr	r�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�|jd�t|j|j�|j�|j
d�t|j|d�|j�|d�|j|_||_dS)Nrr9rrK)rRrmr�r/r�rpr�r�r�r�r�r�r6r�)rYZcurposr�r rrr	r�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)NrrMrr�rK)r:rPrRr/r6r�r3r=)rYrr�r�r�r�rrr	r�{s"





zAifc_write._writemarkers)(rrrrRrbr[r�rerir�r�r�ror�rsr�rur�rqr�rxr{r�r|r�r�rrmr�r�r`r�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�moder\)�rr\)r�r�z$mode must be 'r', 'rb', 'w', or 'wb')r�r�rEr�r�r(r�rrr	r�s
cCstjdtdd�t||d�S)NzBaifc.openfp is deprecated since Python 3.7. Use aifc.open instead.r)�
stacklevel)r�)r�r��DeprecationWarningrr�rrr	r�s
��__main__rz/usr/demos/data/audio/bach.aiffr�ZReadingznchannels =znframes   =zsampwidth =zframerate =zcomptype  =zcompname  =rZWritingr�izDone.)N)N)8�__doc__r
r_r��__all__�	Exceptionrr�rrrrr!r&r-r3r4r5r6r=rArZrB�collectionsrCrDr�r�r�r�r�r�rEr�rrr�sys�argvr��fnr(�printrorqrsrurxr{Zgn�gr�r|r�rr�rrrr	�<module>s~	

!�









?>