a
    ‡ù»jIk  ã                   @   s¼   d Z ddlZddlZddlZddlZddlZddlmZ g d¢ZG dd„ de	ƒZ
G dd„ deƒZejejhZd	d
„ Zdd„ Zddd„Zdd„ Zdd„ Zdd„ Zddd„Zdd„ Z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.

é    N)ÚIntEnum)ÚWavFileWarningÚreadÚwritec                   @   s   e Zd ZdS )r   N)Ú__name__Ú
__module__Ú__qualname__© r	   r	   ú6E:\soccer\ml_env\Lib\site-packages\scipy/io/wavfile.pyr      s   r   c                   @   s|  e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"d Z#d!Z$d"Z%d#Z&d$Z'd%Z(d&Z)d'Z*d(Z+d)Z,d*Z-d+Z.d,Z/d-Z0d.Z1d/Z2d0Z3d1Z4d2Z5d3Z6d4Z7d5Z8d6Z9d7Z:d8Z;d9Z<d:Z=d;Z>d<Z?d=Z@d>ZAd?ZBd@ZCdAZDdBZEdCZFdDZGdEZHdFZIdGZJdHZKdIZLdJZMdKZNdLZOdMZPdNZQdOZRdPZSdQZTdRZUdSZVdTZWdUZXdVZYdWZZdXZ[dYZ\dZZ]d[Z^d\Z_d]Z`d^Zad_Zbd`ZcdaZddbZedcZfddZgdeZhdfZidgZjdhZkdiZldjZmdkZndlZodmZpdnZqdoZrdpZsdqZtdrZudsZvdtZwduZxdvZydwZzdxZ{dyZ|dzZ}d{Z~d|Zd}Z€d~Z�dZ‚d€Zƒd�Z„d‚Z…dƒZ†d„Z‡d…Zˆd†Z‰d‡ZŠdˆZ‹d‰ZŒdŠZ�d‹ZŽdŒZ�d�Z�dŽZ‘d�Z’d�Z“d‘Z”d’Z•d“Z–d”Z—d•Z˜d–Z™d—Zšd˜Z›d™ZœdšZ�d›ZždœZŸd�Z džZ¡dŸZ¢d Z£d¡Z¤d¢Z¥d£Z¦d¤Z§d¥Z¨d¦Z©d§Zªd¨Z«d©Z¬dªZ­d«Z®d¬Z¯d­Z°d®Z±d¯Z²d°Z³d±Z´d²Zµd³Z¶d´Z·dµZ¸d¶Z¹d·Zºd¸Z»d¹Z¼dºZ½d»Z¾d¼Z¿d½ZÀd¾ZÁd¿ZÂdÀZÃdÁZÄdÂZÅdÃZÆdÄZÇdÅZÈdÆZÉdÇZÊdÈZËdÉZÌdÊZÍdËZÎdÌZÏdÍZÐdÎZÑdÏZÒdÐZÓdÑZÔdÒZÕdÓZÖdÔZ×dÕZØdÖZÙd×ZÚdØZÛdÙZÜdÚZÝdÛZÞdÜZßdÝZàdÞZádßZâdàZãdáZädâZådãZædäZçdåZèdæZédçZêdèZëdéZìdêZídëZîdìZïdíZðdîZñdïZòdðZódñZôdòZõdóZödôZ÷dõZødöZùd÷ZúdøZûdùZüdúZýdûZþdüZÿdý�Z dþ�Zdÿ�Z�d �Z�d�Z�d�Z�d�Z�d�Z�d�Z�d�Z	�d�Z
�d�Z�d	�Z�d
�Z�d�Z�d�Z�dS (  ÚWAVE_FORMATzš
    WAVE form wFormatTag IDs

    Complete list is in mmreg.h in Windows 10 SDK.  ALAC and OPUS are the
    newest additions, in v10.0.14393 2016-07
    r   é   é   é   é   é   é   é   é   é	   é
   é   é   é   é   é   é   é   é   é   é   é   é   é    é!   é"   é#   é$   é%   é&   é'   é(   é0   é1   é2   é3   é4   é5   é6   é7   é8   é9   é:   é;   é<   é=   é@   éA   éB   éC   éD   éE   éP   éR   éS   éU   éY   é`   éa   éb   éc   éd   ée   éf   ég   éi   ép   éq   ér   és   ét   éu   év   éw   éx   éy   éz   é{   é€   é�   é‚   éƒ   é„   é…   é†   éˆ   é‰   éŠ   é‹   éŒ   é�   é‘   é’   é“   é”   é—   é˜   é™   é    é¡   é¢   é£   é¤   é°   éÿ   é   i  i  i  i   i!  i#  i%  i0  i1  i2  i3  i4  i5  i6  i@  iA  iB  iP  iQ  iU  i`  ia  ib  ic  id  ip  iq  ir  is  it  iu  iv  iw  ix  i€  i�  i   i  i  i  i  i  i   i0  i@  iA  iI  iP  iQ  i`  ip  iq  ir  is  i€  i�  i…  i   iP  iQ  i   i  i  iP  i   i  i€  i�  i®  i   i  i  i  i  i   i  i  i  i  i   i  i   i   i  i  i  i	  i
  i  i  i  iq  iy  i  i  i  iÄ  i    i   i3  iCA  iB  iCB  iLC  iLV  iVW  iOg  iPg  iQg  iog  ipg  iqg  ial  i p  iOp  imp  ias  ibs  ics  i!z  i"z  i ¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i	¡  i
¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i¡  i ¡  i!¡  i"¡  i#¡  i$¡  i¬ñ  iþÿ  iÿÿ  N(  r   r   r   Ú__doc__ÚUNKNOWNÚPCMZADPCMÚ
IEEE_FLOATZVSELPZIBM_CVSDZALAWZMULAWZDTSZDRMZ	WMAVOICE9Z
WMAVOICE10Z	OKI_ADPCMZ	DVI_ADPCMZ	IMA_ADPCMZMEDIASPACE_ADPCMZSIERRA_ADPCMZ
G723_ADPCMZDIGISTDZDIGIFIXZDIALOGIC_OKI_ADPCMZMEDIAVISION_ADPCMZCU_CODECZHP_DYN_VOICEZYAMAHA_ADPCMZSONARCZDSPGROUP_TRUESPEECHZECHOSC1ZAUDIOFILE_AF36ZAPTXZAUDIOFILE_AF10ZPROSODY_1612ZLRCZ	DOLBY_AC2ZGSM610ZMSNAUDIOZANTEX_ADPCMEZCONTROL_RES_VQLPCZDIGIREALZ	DIGIADPCMZCONTROL_RES_CR10ZNMS_VBXADPCMZCS_IMAADPCMZECHOSC3ZROCKWELL_ADPCMZROCKWELL_DIGITALKZXEBECZ
G721_ADPCMZ	G728_CELPZMSG723ZINTEL_G723_1Z
INTEL_G729Z
SHARP_G726ZMPEGZRT24ZPACZ
MPEGLAYER3ZLUCENT_G723ZCIRRUSZESPCMZVOXWAREZCANOPUS_ATRACZ
G726_ADPCMZ
G722_ADPCMZDSATZDSAT_DISPLAYZVOXWARE_BYTE_ALIGNEDZVOXWARE_AC8ZVOXWARE_AC10ZVOXWARE_AC16ZVOXWARE_AC20ZVOXWARE_RT24ZVOXWARE_RT29ZVOXWARE_RT29HWZVOXWARE_VR12ZVOXWARE_VR18ZVOXWARE_TQ40ZVOXWARE_SC3ZVOXWARE_SC3_1Z	SOFTSOUNDZVOXWARE_TQ60ZMSRT24ZG729AZMVI_MVI2ZDF_G726Z	DF_GSM610ZISIAUDIOZONLIVEZMULTITUDE_FT_SX20ZINFOCOM_ITS_G721_ADPCMZCONVEDIA_G729Z
CONGRUENCYZSBC24ZDOLBY_AC3_SPDIFZMEDIASONIC_G723ZPROSODY_8KBPSZZYXEL_ADPCMZPHILIPS_LPCBBZPACKEDZMALDEN_PHONYTALKZRACAL_RECORDER_GSMZRACAL_RECORDER_G720_AZRACAL_RECORDER_G723_1ZRACAL_RECORDER_TETRA_ACELPZNEC_AACZRAW_AAC1ZRHETOREX_ADPCMZIRATZ	VIVO_G723Z
VIVO_SIRENZPHILIPS_CELPZPHILIPS_GRUNDIGZDIGITAL_G723ZSANYO_LD_ADPCMZSIPROLAB_ACEPLNETZSIPROLAB_ACELP4800ZSIPROLAB_ACELP8V3ZSIPROLAB_G729ZSIPROLAB_G729AZSIPROLAB_KELVINZVOICEAGE_AMRZ	G726ADPCMZDICTAPHONE_CELP68ZDICTAPHONE_CELP54ZQUALCOMM_PUREVOICEZQUALCOMM_HALFRATEZTUBGSMZMSAUDIO1ZWMAUDIO2ZWMAUDIO3ZWMAUDIO_LOSSLESSZWMASPDIFZUNISYS_NAP_ADPCMZUNISYS_NAP_ULAWZUNISYS_NAP_ALAWZUNISYS_NAP_16KZSYCOM_ACM_SYC008ZSYCOM_ACM_SYC701_G726LZSYCOM_ACM_SYC701_CELP54ZSYCOM_ACM_SYC701_CELP68ZKNOWLEDGE_ADVENTURE_ADPCMZFRAUNHOFER_IIS_MPEG2_AACZDTS_DSZCREATIVE_ADPCMZCREATIVE_FASTSPEECH8ZCREATIVE_FASTSPEECH10Z
UHER_ADPCMZULEAD_DV_AUDIOZULEAD_DV_AUDIO_1ZQUARTERDECKZILINK_VCZ	RAW_SPORTZESST_AC3ZGENERIC_PASSTHRUZIPI_HSXZ	IPI_RPELPZCS2ZSONY_SCXZSONY_SCYZSONY_ATRAC3ZSONY_SPCZTELUM_AUDIOZTELUM_IA_AUDIOZNORCOM_VOICE_SYSTEMS_ADPCMZFM_TOWNS_SNDZMICRONASZMICRONAS_CELP833ZBTV_DIGITALZINTEL_MUSIC_CODERZINDEO_AUDIOZQDESIGN_MUSICZON2_VP7_AUDIOZON2_VP6_AUDIOZ	VME_VMPCMZTPCZLIGHTWAVE_LOSSLESSZOLIGSMZOLIADPCMZOLICELPZOLISBCZOLIOPRZLH_CODECZLH_CODEC_CELPZLH_CODEC_SBC8ZLH_CODEC_SBC12ZLH_CODEC_SBC16ZNORRISZ
ISIAUDIO_2ZSOUNDSPACE_MUSICOMPRESSZMPEG_ADTS_AACZMPEG_RAW_AACZ	MPEG_LOASZNOKIA_MPEG_ADTS_AACZNOKIA_MPEG_RAW_AACZVODAFONE_MPEG_ADTS_AACZVODAFONE_MPEG_RAW_AACZ
MPEG_HEAACZVOXWARE_RT24_SPEECHZSONICFOUNDRY_LOSSLESSZINNINGS_TELECOM_ADPCMZLUCENT_SX8300PZLUCENT_SX5363SZCUSEEMEZNTCSOFT_ALF2CM_ACMZDVMZDTS2ZMAKEAVISZDIVIO_MPEG4_AACZNOKIA_ADAPTIVE_MULTIRATEZ
DIVIO_G726ZLEAD_SPEECHZLEAD_VORBISZWAVPACK_AUDIOZOGG_VORBIS_MODE_1ZOGG_VORBIS_MODE_2ZOGG_VORBIS_MODE_3ZOGG_VORBIS_MODE_1_PLUSZOGG_VORBIS_MODE_2_PLUSZOGG_VORBIS_MODE_3_PLUSZALACZ	_3COM_NBXZOPUSZFAAD_AACZAMR_NBZAMR_WBZAMR_WPZGSM_AMR_CBRZGSM_AMR_VBR_SIDZCOMVERSE_INFOSYS_G723_1ZCOMVERSE_INFOSYS_AVQSBCZCOMVERSE_INFOSYS_SBCZSYMBOL_G729_AZVOICEAGE_AMR_WBZINGENIENT_G726Z	MPEG4_AACZENCORE_G726Z	ZOLL_ASAOZSPEEX_VOICEZVIANIX_MASCZWM9_SPECTRUM_ANALYZERZWMF_SPECTRUM_ANAYZERZGSM_610ZGSM_620ZGSM_660ZGSM_690ZGSM_ADAPTIVE_MULTIRATE_WBZPOLYCOM_G722ZPOLYCOM_G728ZPOLYCOM_G729_AZPOLYCOM_SIRENZGLOBAL_IP_ILBCZRADIOTIME_TIME_SHIFT_RADIOZNICE_ACAZ
NICE_ADPCMZVOCORD_G721ZVOCORD_G726ZVOCORD_G722_1ZVOCORD_G728ZVOCORD_G729ZVOCORD_G729_AZVOCORD_G723_1Z
VOCORD_LBCZ	NICE_G728ZFRACE_TELECOM_G729ZCODIANZFLACÚ
EXTENSIBLEZDEVELOPMENTr	   r	   r	   r
   r      s  r   c                 C   sR   zt | ƒj}W n ty(   | d›}Y n0 td|› d�d dd„ tD ƒ¡ ƒ‚d S )Nz#06xzUnknown wave file format: z. Supported formats: z, c                 s   s   | ]}|j V  qd S )N)Úname)Ú.0Úxr	   r	   r
   Ú	<genexpr>=  ó    z$_raise_bad_format.<locals>.<genexpr>)r   rz   Ú
ValueErrorÚjoinÚKNOWN_WAVE_FORMATS)Ú
format_tagÚformat_namer	   r	   r
   Ú_raise_bad_format6  s    þr„   c                 C   st  |r
d}nd}t  |d |  d¡¡d }|dk r8tdƒ‚t  |d |  d¡¡}d}|\}}}}	}
}|tj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v�rt|ƒ ||k�r$|  || ¡ t	| |ƒ |tj
k�rb|	||
 k�rbtd|› d|
› d|	› �ƒ‚|||||	|
|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

    Notes
    -----
    Assumes file pointer is immediately after the 'fmt ' id
    ú>ú<ÚIr   r   r   z.Binary structure of wave file is not compliantZHHIIHHr   ÚHr   r   r   s      €  ª 8›qs      €  ª 8›qNz{WAV header is invalid: nAvgBytesPerSec must equal product of nSamplesPerSec and nBlockAlign, but file has nSamplesPerSec = z, nBlockAlign = z, and nAvgBytesPerSec = )ÚstructÚunpackr   r   r   ry   Úendswithr�   r„   Ú_handle_pad_byterw   )ÚfidÚis_big_endianÚfmtÚsizeÚresZ
bytes_readr‚   ÚchannelsÚfsÚbytes_per_secondÚblock_alignÚ	bit_depthZext_chunk_sizeZextensible_chunk_dataZraw_guidÚtailr	   r	   r
   Ú_read_fmt_chunk@  sN    




ýýüÿr˜   Fc                 C   sV  |r
d}nd}t  |d |  d¡¡d }|| }	||	 }
|tjkr˜d|  krVdkr`n nd}qÔ|	d	v rnd
}qÔ|dkr†|› d|	› �}qÔtd|› d�ƒ‚n<|tjkrÌ|dv rº|› d|	› �}qÔtd|› d�ƒ‚nt|ƒ |  ¡ }|�sêz$|d
krð|n|
}t	j
| ||d�}W n6 tj�y<   |  |d¡ t	j|  |¡|d�}Y n0 |d
k�r2|	dk�r\|› d�n|› d�}t	jt|ƒ|	 t	 |¡jfd
d�}|�r®| d|	f¡|dd…d|	…f< n | d|	f¡|dd…|	 d…f< | |¡ |jdd… ¡}nH|	dv �r"|  ¡ }t	j| |d||
fd�}|  || ¡ ntd|	› d�ƒ‚t| |ƒ |dk�rR| d|¡}|S )a  
    Notes
    -----
    Assumes file pointer is immediately after the 'data' id

    It's possible to not use all available bits in a container, or to store
    samples in a container bigger than necessary, so bytes_per_sample uses
    the actual reported container size (nBlockAlign / nChannels).  Real-world
    examples:

    Adobe Audition's "24-bit packed int (type 1, 20-bit)"

        nChannels = 2, nBlockAlign = 6, wBitsPerSample = 20

    http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Samples/AFsp/M1F1-int12-AFsp.wav
    is:

        nChannels = 2, nBlockAlign = 4, wBitsPerSample = 12

    http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/multichaudP.pdf
    gives an example of:

        nChannels = 2, nBlockAlign = 8, wBitsPerSample = 20
    r…   r†   r‡   r   r   r   r   Úu1>   r   r   r   r   ZV1r9   Úiz(Unsupported bit depth: the WAV file has z-bit integer data.>   r"   r9   Úfz-bit floating-point data.)ÚdtypeÚcount)rœ   r   Úi4Úi8éÿÿÿÿN>   r   r   r   r   Úc)rœ   ÚmodeÚoffsetÚshapezmmap=True not compatible with z-byte container size.)r‰   rŠ   r   r   rw   r   rx   r„   ÚtellÚnumpyÚfromfileÚioÚUnsupportedOperationÚseekZ
frombufferZzerosÚlenrœ   ÚitemsizeZreshapeÚviewr¤   ZmemmaprŒ   )r�   r‚   r’   r–   rŽ   r•   Úmmapr�   r�   Zbytes_per_sampleZ	n_samplesrœ   Ústartr�   ÚdataÚdtÚar	   r	   r
   Ú_read_data_chunk�  sj    
ÿ
ÿ
ÿ  
ÿÿ


r³   c                 C   sF   |r
d}nd}|   d¡}|rBt ||¡d }|  |d¡ t| |ƒ d S )Nú>Iú<Ir   r   r   )r   r‰   rŠ   rª   rŒ   )r�   rŽ   r�   r°   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|ƒ› d	�ƒ‚t ||   d¡¡d
 d }|   d¡}|dkr‚tdt|ƒ› d�ƒ‚||fS )Nr   ó   RIFFFrµ   s   RIFXTr´   zFile format z2 not understood. Only 'RIFF' and 'RIFX' supported.r   r   ó   WAVEz"Not a WAV file. RIFF form type is Ú.)r   r   Úreprr‰   rŠ   )r�   Zstr1rŽ   r�   Ú	file_sizeZstr2r	   r	   r
   Ú_read_riff_chunkÿ  s    

r¼   c                 C   s   |d r|   dd¡ d S )Nr   r   )rª   )r�   r�   r	   r	   r
   rŒ     s    rŒ   c              	   C   sæ  t | dƒr| }d}n
t| dƒ}�zšt|ƒ\}}d}d}| ¡ |k �r˜| d¡}|sŒ|r‚tjd| ¡ d›d|d›d�td	d
� �q˜qÌtdƒ‚n@t	|ƒdk rÌdt
|ƒ› �}|rÄ|rÄtj|d td	d
� nt|ƒ‚|dk�rd}t||ƒ}	|	dd… \}
}}|	d }|	d }q6|dk�rt||ƒ q6|dk�rPd}|�s:tdƒ‚t||
|||||ƒ}q6|dk�rft||ƒ q6|dv �r|t||ƒ q6tjdtd	d
� t||ƒ q6W t | dƒ�s°| ¡  n
| d¡ n"t | dƒ�sÒ| ¡  n
| d¡ 0 ||fS )a  
    Open a WAV file.

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

    Parameters
    ----------
    filename : string or open file handle
        Input WAV file.
    mmap : bool, optional
        Whether to read data as memory-mapped (default: False).  Not compatible
        with some bit depths; see Notes.  Only to be used on real files.

        .. 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.  Data is 1-D for 1-channel WAV, or 2-D of shape
        (Nsamples, Nchannels) otherwise. If a file-like input without a
        C-like file descriptor (e.g., :class:`python:io.BytesIO`) is
        passed, this will not be writeable.

    Notes
    -----
    Common data types: [1]_

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

    WAV files can specify arbitrary bit depth, and this function supports
    reading any integer PCM depth from 1 to 64 bits.  Data is returned in the
    smallest compatible numpy int type, in left-justified format.  8-bit and
    lower is unsigned, while 9-bit and higher is signed.

    For example, 24-bit data will be stored as int32, with the MSB of the
    24-bit data stored at the MSB of the int32, and typically the least
    significant byte is 0x00.  (However, if a file actually contains data past
    its specified bit depth, those bits will be read and output, too. [2]_)

    This bit justification and sign matches WAV's native internal format, which
    allows memory mapping of WAV files that use 1, 2, 4, or 8 bytes per sample
    (so 24-bit files cannot be memory-mapped, but 32-bit can).

    IEEE float PCM in 32- or 64-bit format is supported, with or without mmap.
    Values exceeding [-1, +1] are not clipped.

    Non-linear PCM (mu-law, A-law) is not supported.

    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
    .. [2] Adobe Systems Incorporated, "Adobe Audition 3 User Guide", section
       "Audio file formats: 24-bit Packed Int (type 1, 20-bit)", 2007

    Examples
    --------
    >>> from os.path import dirname, join as pjoin
    >>> from scipy.io import wavfile
    >>> import scipy.io

    Get the filename for an example .wav file from the tests/data directory.

    >>> data_dir = pjoin(dirname(scipy.io.__file__), 'tests', 'data')
    >>> wav_fname = pjoin(data_dir, 'test-44100Hz-2ch-32bit-float-be.wav')

    Load the .wav file contents.

    >>> samplerate, data = wavfile.read(wav_fname)
    >>> print(f"number of channels = {data.shape[1]}")
    number of channels = 2
    >>> length = data.shape[0] / samplerate
    >>> print(f"length = {length}s")
    length = 0.01s

    Plot the waveform.

    >>> import matplotlib.pyplot as plt
    >>> import numpy as np
    >>> time = np.linspace(0., length, data.shape[0])
    >>> plt.plot(time, data[:, 0], label="Left channel")
    >>> plt.plot(time, data[:, 1], label="Right channel")
    >>> plt.legend()
    >>> plt.xlabel("Time [s]")
    >>> plt.ylabel("Amplitude")
    >>> plt.show()

    r   FÚrbr   z%Reached EOF prematurely; finished at Údz bytes, expected z bytes from header.r   )Ú
stacklevelzUnexpected end of file.zIncomplete chunk ID: z, ignoring it.ó   fmt Tr   r   r   ó   factó   datazNo fmt chunk before datas   LIST>   s   JUNKs   Fakez-Chunk (non-data) not understood, skipping it.r   )ÚhasattrÚopenr¼   r¥   r   ÚwarningsÚwarnr   r   r«   rº   r˜   r¶   r³   Úcloserª   )Úfilenamer®   r�   r»   rŽ   Zfmt_chunk_receivedZdata_chunk_receivedZchunk_idÚmsgZ	fmt_chunkr‚   r’   r“   r–   r•   r°   r	   r	   r
   r     sr    f


ÿý
ÿ





ÿ

ÿ
ý
r   c              	   C   sB  t | dƒr| }n
t| dƒ}|}�zú|jj}g d¢}|jj|vrLtd|j ƒ‚d}|d7 }|d7 }|d7 }|d	7 }|d
kr€tj}ntj}|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�sF|dk�sF|d7 }|t dd|j
d ¡7 }t|ƒd d d|j  dk�rnt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 t | dƒ�s| ¡  n
| d¡ n"t | dƒ�s2| ¡  n
| d¡ 0 dS )aÿ  
    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

    Examples
    --------
    Create a 100Hz sine wave, sampled at 44100Hz.
    Write to 16-bit PCM, Mono.

    >>> from scipy.io.wavfile import write
    >>> import numpy as np
    >>> samplerate = 44100; fs = 100
    >>> t = np.linspace(0., 1., samplerate)
    >>> amplitude = np.iinfo(np.int16).max
    >>> data = amplitude * np.sin(2. * np.pi * fs * t)
    >>> write("example.wav", samplerate, data.astype(np.int16))

    r   Úwb)Zfloat32Zfloat64Zuint8Zint16Zint32Zint64zUnsupported data type '%s'r~   r·   s       r¸   rÀ   r›   r   r   z<HHIIHHrš   Úus     rµ   rÁ   z<IIr   r   l   ÿÿ z!Data exceeds wave file size limitrÂ   r…   ú=ÚbigN)rÃ   rÄ   rœ   Úkindrz   r   r   rx   rw   Úndimr¤   r¬   r‰   Úpackr«   Únbytesr   Ú	byteorderÚsysÚbyteswapÚ_array_tofiler¥   rª   rÇ   )rÈ   Zrater°   r�   r“   ZdkindZallowed_dtypesZheader_datar‚   r’   r–   r”   r•   Zfmt_chunk_datar�   r	   r	   r
   r   Å  sh    6



ÿ 

ÿ


ý
r   c                 C   s   |   | ¡  d¡j¡ d S )NÚb)r   Zravelr­   r°   )r�   r°   r	   r	   r
   rÕ   F  s    rÕ   )F)F)ru   r¨   rÓ   r¦   r‰   rÅ   Úenumr   Ú__all__ÚUserWarningr   r   rw   rx   r�   r„   r˜   r³   r¶   r¼   rŒ   r   r   rÕ   r	   r	   r	   r
   Ú<module>   s0   
  
P ÿ
_
 ) 