ó
Ë½÷Xc           @` 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	 d e
 f d „  ƒ  YZ d Z d	 Z d
 Z e e f Z d „  Z e d „ Z d „  Z d „  Z e d „ Z d „  Z e j d d	 k rð d „  Z n	 d „  Z d S(   sÂ   
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.

i    (   t   divisiont   print_functiont   absolute_importNt   WavFileWarningt   readt   writec           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/io/wavfile.pyR      s   i   i   iþÿ  c         C` sÀ  | r d } n d } t  j | d |  j d ƒ ƒ d } } d } | d k  r] t d ƒ ‚ n  t  j | d |  j d ƒ ƒ } | d 7} | \ } } } }	 }
 } | t k rl| d k rlt  j | d
 |  j d	 ƒ ƒ d } | d	 7} | d k r]|  j d ƒ } | d 7} | d d !} | r$d } n d } | j | ƒ rit  j | d | d  ƒ d } qiqlt d ƒ ‚ n  | t k r‡t d ƒ ‚ n  | | k r§|  j | | ƒ n  | | | | |	 |
 | f S(   sÔ  
    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
    t   >t   <t   Ii   i    i   s.   Binary structure of wave file is not compliantt   HHIIHHi   t   Hi   t      €  ª 8›qt      €  ª 8›qs   Unknown wave file formati   i   i   i   (   t   structt   unpackR   t
   ValueErrort   WAVE_FORMAT_EXTENSIBLEt   endswitht   KNOWN_WAVE_FORMATS(   t   fidt   is_big_endiant   fmtt   sizet   rest
   bytes_readt
   format_tagt   channelst   fst   bytes_per_secondt   block_alignt	   bit_deptht   ext_chunk_sizet   extensible_chunk_datat   raw_guidt   tail(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/io/wavfile.pyt   _read_fmt_chunk'   s:    	'
#

	$c         C` s*  | r d } n d } t  j | |  j d ƒ ƒ d } | d } | d k rS d }	 n@ | rb d }	 n d }	 | t k r… |	 d	 | 7}	 n |	 d
 | 7}	 | sº t j |  j | ƒ d |	 ƒ}
 nK |  j ƒ  } t j |  d |	 d d d | d | | f ƒ}
 |  j | | ƒ | d k r&|
 j	 d | ƒ }
 n  |
 S(   Ns   >Is   <Ii   i    i   t   u1R   R	   s   i%ds   f%dt   dtypet   modet   ct   offsett   shapei   iÿÿÿÿ(
   R   R   R   t   WAVE_FORMAT_PCMt   numpyt
   fromstringt   tellt   memmapt   seekt   reshape(   R   R   R   R    R   t   mmapR   R   t   bytes_per_sampleR'   t   datat   start(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/io/wavfile.pyt   _read_data_chunki   s,    	
		!c         C` sW   | r d } n d } |  j  d ƒ } | rS t j | | ƒ d } |  j | d ƒ n  d  S(   Ns   >Is   <Ii   i    i   (   R   R   R   R1   (   R   R   R   R5   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/io/wavfile.pyt   _skip_unknown_chunk   s    	c         C` s·   |  j  d ƒ } | d k r* t } d } n6 | d k rE t } d } n t d j t | ƒ ƒ ƒ ‚ t j | |  j  d ƒ ƒ d d } |  j  d ƒ } | d	 k r­ t d
 ƒ ‚ n  | | f S(   Ni   t   RIFFs   <It   RIFXs   >Is!   File format {}... not understood.i    i   t   WAVEs   Not a WAV file.(   R   t   Falset   TrueR   t   formatt   reprR   R   (   R   t   str1R   R   t	   file_sizet   str2(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/io/wavfile.pyt   _read_riff_chunk   s    			#c         C` s  t  |  d ƒ r |  } t } n t |  d ƒ } z¤t | ƒ \ } } t } d } d } t } xs| j ƒ  | k  rÏ| j d ƒ }	 |	 s“ t d ƒ ‚ n! t |	 ƒ d k  r´ t d ƒ ‚ n  |	 d k rt	 } t
 | | ƒ }
 |
 d d !\ } } } |
 d	 } | d k rÌt d j | ƒ ƒ ‚ qÌq] |	 d k r8t | | ƒ q] |	 d k rw| sYt d ƒ ‚ n  t | | | | | | ƒ } q] |	 d k r“t | | ƒ q] |	 d k r¯t | | ƒ q] t j d t ƒ t | | ƒ q] WWd t  |  d ƒ sð| j ƒ  n | j d ƒ X| | f S(   sÔ  
    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-mmsp.ece.mcgill.ca/documents/audioformats/wave/Docs/riffmci.pdf

    R   t   rbi   i   i   s   Unexpected end of file.s   Incomplete wav chunk.s   fmt i   i   i    i@   i`   i€   s4   Unsupported bit depth: the wav file has {}-bit data.t   factR5   s   No fmt chunk before datat   LISTt   JUNKt   Fakes-   Chunk (non-data) not understood, skipping it.Ni    (   i   i   i    i@   i`   i€   (   RG   RH   (   t   hasattrR<   t   openRC   R,   R/   R   R   t   lenR=   R%   R>   R8   R7   t   warningst   warnR   t   closeR1   (   t   filenameR3   R   RA   R   t   fmt_chunk_receivedR   R    R   t   chunk_idt	   fmt_chunkR   R5   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/io/wavfile.pyR   ´   sT    1	
		c         C` sø  t  |  d ƒ r |  } n t |  d ƒ } | } zš| j j } | d k po | d k po | d k oo | j j d k sˆ t d | j ƒ ‚ n  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
 j d | | | |
 | |	 ƒ } | d k p\| d k sl| d 7} n  | t
 j d t | ƒ ƒ 7} | | 7} | d k p§| d k s×| d 7} | t
 j d d | j	 d ƒ 7} n  t | ƒ d d d | j d k rt d ƒ ‚ n  | j | ƒ | j d ƒ | j t
 j d | j ƒ ƒ | j j d k st| j j d k rƒt j d k rƒ| j ƒ  } n  t | | ƒ | j ƒ  } | j d ƒ | j t
 j d | d ƒ ƒ Wd t  |  d ƒ sæ| j ƒ  n | j d ƒ Xd S(   sm  
    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-mmsp.ece.mcgill.ca/documents/audioformats/wave/Docs/riffmci.pdf

    R   t   wbt   it   ft   ui   s   Unsupported data type '%s't    R9   t       R;   s   fmt i   s   <HHIIHHt     s   <IRE   s   <IIi   i    Iÿÿÿÿ    s!   Data exceeds wave file size limitR5   R   t   =t   bigNi   (   RI   RJ   R'   t   kindt   itemsizeR   t   WAVE_FORMAT_IEEE_FLOATR,   t   ndimR+   R   t   packRK   t   nbytesR   t	   byteordert   syst   byteswapt   _array_tofileR/   R1   RN   (   RO   t   rateR5   R   R   t   dkindt   header_dataR   R   R    R   R   t   fmt_chunk_dataR   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/io/wavfile.pyR     s^    )	$



		

#%$!c         C` s#   |  j  | j ƒ  j d ƒ j ƒ d  S(   Nt   b(   R   t   ravelt   viewR5   (   R   R5   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/io/wavfile.pyRe     s    c         C` s   |  j  | j ƒ  ƒ d  S(   N(   R   t   tostring(   R   R5   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/io/wavfile.pyRe   ”  s    (   t   __doc__t
   __future__R    R   R   Rc   R-   R   RL   t   __all__t   UserWarningR   R,   R^   R   R   R%   R<   R7   R8   RC   R   R   t   version_infoRe   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/io/wavfile.pyt   <module>
   s,   		C#		h	s