B
    î&]\1  ã               @   s¾   d Z ddlmZmZmZ ddlZddlZddlZddlZdddgZ	G dd„ de
ƒZdZd	Zd
ZeefZdd„ Zddd„Zdd„ Zdd„ Zddd„Zdd„ Zejd d	kr²dd„ Zndd„ ZdS )zÂ
Module to read / write wav files using numpy arrays

Functions
---------
`read`: Return the sample rate (in samples/sec) and data from a WAV file.

`write`: Write a numpy array as a WAV file.

é    )ÚdivisionÚprint_functionÚabsolute_importNÚWavFileWarningÚreadÚwritec               @   s   e Zd ZdS )r   N)Ú__name__Ú
__module__Ú__qualname__© r   r   ú/lib/python3.7/site-packages/scipy/io/wavfile.pyr      s   é   é   iþÿ  c             C   sH  |r
d}nd}t  |d |  d¡¡d  }}d}|dk r@tdƒ‚t  |d |  d¡¡}|d7 }|\}}}}	}
}|tkr|d	krt  |d
 |  d¡¡d }|d7 }|dkr|  d¡}|d7 }|dd… }|rÖd}nd}| |¡rt  |d |dd… ¡d }ntdƒ‚|tkrtdƒ‚||kr6|  || ¡ |||||	|
|fS )aÔ  
    Returns
    -------
    size : int
        size of format subchunk in bytes (minus 8 for "fmt " and itself)
    format_tag : int
        PCM, float, or compressed format
    channels : int
        number of channels
    fs : int
        sampling frequency in samples per second
    bytes_per_second : int
        overall byte rate for the file
    block_align : int
        bytes per sample, including all channels
    bit_depth : int
        bits per sample
    ú>ú<ÚIé   r   é   z.Binary structure of wave file is not compliantZHHIIHHé   ÚHé   é   é   s      €  ª 8›qs      €  ª 8›qNzUnknown wave file format)ÚstructÚunpackr   Ú
ValueErrorÚWAVE_FORMAT_EXTENSIBLEÚendswithÚKNOWN_WAVE_FORMATS)ÚfidÚis_big_endianÚfmtÚsizeZresZ
bytes_readÚ
format_tagÚchannelsÚfsÚbytes_per_secondÚblock_alignÚ	bit_depthZext_chunk_sizeZextensible_chunk_dataZraw_guidÚtailr   r   r   Ú_read_fmt_chunk'   s:    



r*   Fc             C   sÌ   |r
d}nd}t  ||  d¡¡d }|d }|dkr:d}	n0|rDd}	nd}	|tkr^|	d	| 7 }	n|	d
| 7 }	|s„tj|  |¡|	d}
n0|  ¡ }tj| |	d||| fd}
|  || ¡ |dkrÈ|
 	d|¡}
|
S )Nz>Iz<Ir   r   é   Zu1r   r   zi%dzf%d)ÚdtypeÚc)r,   ÚmodeÚoffsetÚshaper   éÿÿÿÿ)
r   r   r   ÚWAVE_FORMAT_PCMÚnumpyZ
frombufferÚtellZmemmapÚseekZreshape)r   r#   r$   r(   r    Úmmapr!   r"   Zbytes_per_sampler,   ÚdataÚstartr   r   r   Ú_read_data_chunki   s,    r9   c             C   s<   |r
d}nd}|   d¡}|r8t ||¡d }|  |d¡ d S )Nz>Iz<Ir   r   r   )r   r   r   r5   )r   r    r!   r7   r"   r   r   r   Ú_skip_unknown_chunk   s    
r:   c             C   s|   |   d¡}|dkrd}d}n$|dkr.d}d}ntd t|ƒ¡ƒ‚t ||   d¡¡d	 d
 }|   d¡}|dkrttdƒ‚||fS )Nr   s   RIFFFz<Is   RIFXTz>Iz!File format {}... not understood.r   r+   s   WAVEzNot a WAV file.)r   r   ÚformatÚreprr   r   )r   Zstr1r    r!   Ú	file_sizeZstr2r   r   r   Ú_read_riff_chunk   s    

r>   c          	   C   sv  t | dƒr| }d}n
t| dƒ}z*t|ƒ\}}d}d}d}t}x| ¡ |k rF| d¡}	|	shtdƒ‚nt|	ƒdk r|tdƒ‚|	d	krÄd
}t||ƒ}
|
dd… \}}}|
d }|dkrÂtd 	|¡ƒ‚qB|	dkrØt
||ƒ qB|	dkr|sîtdƒ‚t||||||ƒ}qB|	dkrt
||ƒ qB|	dkr.t
||ƒ qBt dt¡ t
||ƒ qBW W dt | dƒsb| ¡  n
| d¡ X ||fS )aÀ  
    Open a WAV file

    Return the sample rate (in samples/sec) and data from a WAV file.

    Parameters
    ----------
    filename : string or open file handle
        Input wav file.
    mmap : bool, optional
        Whether to read data as memory-mapped.
        Only to be used on real files (Default: False).

        .. versionadded:: 0.12.0

    Returns
    -------
    rate : int
        Sample rate of wav file.
    data : numpy array
        Data read from wav file.  Data-type is determined from the file;
        see Notes.

    Notes
    -----
    This function cannot read wav files with 24-bit data.

    Common data types: [1]_

    =====================  ===========  ===========  =============
         WAV format            Min          Max       NumPy dtype
    =====================  ===========  ===========  =============
    32-bit floating-point  -1.0         +1.0         float32
    32-bit PCM             -2147483648  +2147483647  int32
    16-bit PCM             -32768       +32767       int16
    8-bit PCM              0            255          uint8
    =====================  ===========  ===========  =============

    Note that 8-bit PCM is unsigned.

    References
    ----------
    .. [1] IBM Corporation and Microsoft Corporation, "Multimedia Programming
       Interface and Data Specifications 1.0", section "Data Format of the
       Samples", August 1991
       http://www.tactilemedia.com/info/MCI_Control_Info.html

    r   FÚrbr   r+   r   zUnexpected end of file.zIncomplete wav chunk.s   fmt Tr   )r+   r   é    é@   é`   é€   z4Unsupported bit depth: the wav file has {}-bit data.s   facts   datazNo fmt chunk before datas   LIST)s   JUNKs   Fakez-Chunk (non-data) not understood, skipping it.Nr   )ÚhasattrÚopenr>   r2   r4   r   r   Úlenr*   r;   r:   r9   ÚwarningsÚwarnr   Úcloser5   )Úfilenamer6   r   r=   r    Zfmt_chunk_receivedr$   r(   r#   Zchunk_idZ	fmt_chunkr%   r7   r   r   r   r   ´   sT    1











c          	   C   s2  t | dƒr| }n
t| dƒ}|}zê|jj}|dks\|dks\|dkrN|jjdks\td|j ƒ‚d}|d	7 }|d
7 }|d7 }|d7 }|dkrŽt}nt}|jdkr¢d}n
|j	d }|jjd }	||	d  | }
||	d  }t
 d||||
||	¡}|dks|dks|d7 }|t
 dt|ƒ¡7 }||7 }|dksV|dksV|d7 }|t
 dd|j	d ¡7 }t|ƒd d d|j  dkr~tdƒ‚| |¡ | d¡ | t
 d|j¡¡ |jjdksÎ|jjdkrÖtjdkrÖ| ¡ }t||ƒ | ¡ }| d¡ | t
 d|d ¡¡ W dt | dƒs"| ¡  n
| d¡ X dS )aY  
    Write a numpy array as a WAV file.

    Parameters
    ----------
    filename : string or open file handle
        Output wav file.
    rate : int
        The sample rate (in samples/sec).
    data : ndarray
        A 1-D or 2-D numpy array of either integer or float data-type.

    Notes
    -----
    * Writes a simple uncompressed WAV file.
    * To write multiple-channels, use a 2-D array of shape
      (Nsamples, Nchannels).
    * The bits-per-sample and PCM/float will be determined by the data-type.

    Common data types: [1]_

    =====================  ===========  ===========  =============
         WAV format            Min          Max       NumPy dtype
    =====================  ===========  ===========  =============
    32-bit floating-point  -1.0         +1.0         float32
    32-bit PCM             -2147483648  +2147483647  int32
    16-bit PCM             -32768       +32767       int16
    8-bit PCM              0            255          uint8
    =====================  ===========  ===========  =============

    Note that 8-bit PCM is unsigned.

    References
    ----------
    .. [1] IBM Corporation and Microsoft Corporation, "Multimedia Programming
       Interface and Data Specifications 1.0", section "Data Format of the
       Samples", August 1991
       http://www.tactilemedia.com/info/MCI_Control_Info.html

    r   ÚwbÚiÚfÚur   zUnsupported data type '%s'ó    s   RIFFs       s   WAVEs   fmt r+   z<HHIIHHs     z<Is   factz<IIr   r   l   ÿÿ z!Data exceeds wave file size limits   datar   ú=ZbigN)rD   rE   r,   ZkindÚitemsizer   ÚWAVE_FORMAT_IEEE_FLOATr2   Úndimr0   r   ZpackrF   Únbytesr   Ú	byteorderÚsysZbyteswapÚ_array_tofiler4   r5   rI   )rJ   Zrater7   r   r%   ZdkindZheader_datar#   r$   r(   r&   r'   Zfmt_chunk_datar"   r   r   r   r     s^    )




 




c             C   s   |   | ¡  d¡j¡ d S )NÚb)r   ZravelZviewr7   )r   r7   r   r   r   rW     s    rW   c             C   s   |   | ¡ ¡ d S )N)r   Ztostring)r   r7   r   r   r   rW   ”  s    )F)F)Ú__doc__Z
__future__r   r   r   rV   r3   r   rG   Ú__all__ÚUserWarningr   r2   rR   r   r   r*   r9   r:   r>   r   r   Úversion_inforW   r   r   r   r   Ú<module>
   s,   C
#
hs
