def write(filename, rate, data):
    '\n    Write a NumPy array as a WAV file.\n\n    Parameters\n    ----------\n    filename : string or open file handle\n        Output wav file.\n    rate : int\n        The sample rate (in samples/sec).\n    data : ndarray\n        A 1-D or 2-D NumPy array of either integer or float data-type.\n\n    Notes\n    -----\n    * Writes a simple uncompressed WAV file.\n    * To write multiple-channels, use a 2-D array of shape\n      (Nsamples, Nchannels).\n    * The bits-per-sample and PCM/float will be determined by the data-type.\n\n    Common data types: [1]_\n\n    =====================  ===========  ===========  =============\n         WAV format            Min          Max       NumPy dtype\n    =====================  ===========  ===========  =============\n    32-bit floating-point  -1.0         +1.0         float32\n    32-bit PCM             -2147483648  +2147483647  int32\n    16-bit PCM             -32768       +32767       int16\n    8-bit PCM              0            255          uint8\n    =====================  ===========  ===========  =============\n\n    Note that 8-bit PCM is unsigned.\n\n    References\n    ----------\n    .. [1] IBM Corporation and Microsoft Corporation, "Multimedia Programming\n       Interface and Data Specifications 1.0", section "Data Format of the\n       Samples", August 1991\n       http://www.tactilemedia.com/info/MCI_Control_Info.html\n\n    Examples\n    --------\n    Create a 100Hz sine wave, sampled at 44100Hz.\n    Write to 16-bit PCM, Mono.\n\n    >>> from scipy.io.wavfile import write\n    >>> samplerate = 44100; fs = 100\n    >>> t = np.linspace(0., 1., samplerate)\n    >>> amplitude = np.iinfo(np.int16).max\n    >>> data = amplitude * np.sin(2. * np.pi * fs * t)\n    >>> write("example.wav", samplerate, data)\n\n    '
    if hasattr(filename, 'write'):
        fid = filename
    else:
        fid = open(filename, 'wb')
    fs = rate
    try:
        dkind = data.dtype.kind
        if (not ((dkind == 'i') or (dkind == 'f') or ((dkind == 'u') and (data.dtype.itemsize == 1)))):
            raise ValueError(("Unsupported data type '%s'" % data.dtype))
        header_data = b''
        header_data += b'RIFF'
        header_data += b'\x00\x00\x00\x00'
        header_data += b'WAVE'
        header_data += b'fmt '
        if (dkind == 'f'):
            format_tag = WAVE_FORMAT_IEEE_FLOAT
        else:
            format_tag = WAVE_FORMAT_PCM
        if (data.ndim == 1):
            channels = 1
        else:
            channels = data.shape[1]
        bit_depth = (data.dtype.itemsize * 8)
        bytes_per_second = ((fs * (bit_depth // 8)) * channels)
        block_align = (channels * (bit_depth // 8))
        fmt_chunk_data = struct.pack('<HHIIHH', format_tag, channels, fs, bytes_per_second, block_align, bit_depth)
        if (not ((dkind == 'i') or (dkind == 'u'))):
            fmt_chunk_data += b'\x00\x00'
        header_data += struct.pack('<I', len(fmt_chunk_data))
        header_data += fmt_chunk_data
        if (not ((dkind == 'i') or (dkind == 'u'))):
            header_data += b'fact'
            header_data += struct.pack('<II', 4, data.shape[0])
        if ((((len(header_data) - 4) - 4) + ((4 + 4) + data.nbytes)) > 4294967295):
            raise ValueError('Data exceeds wave file size limit')
        fid.write(header_data)
        fid.write(b'data')
        fid.write(struct.pack('<I', data.nbytes))
        if ((data.dtype.byteorder == '>') or ((data.dtype.byteorder == '=') and (sys.byteorder == 'big'))):
            data = data.byteswap()
        _array_tofile(fid, data)
        size = fid.tell()
        fid.seek(4)
        fid.write(struct.pack('<I', (size - 8)))
    finally:
        if (not hasattr(filename, 'write')):
            fid.close()
        else:
            fid.seek(0)
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
>>> 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)
def write(filename, rate, data):
    '\n    Write a NumPy array as a WAV file.\n\n    Parameters\n    ----------\n    filename : string or open file handle\n        Output wav file.\n    rate : int\n        The sample rate (in samples/sec).\n    data : ndarray\n        A 1-D or 2-D NumPy array of either integer or float data-type.\n\n    Notes\n    -----\n    * Writes a simple uncompressed WAV file.\n    * To write multiple-channels, use a 2-D array of shape\n      (Nsamples, Nchannels).\n    * The bits-per-sample and PCM/float will be determined by the data-type.\n\n    Common data types: [1]_\n\n    =====================  ===========  ===========  =============\n         WAV format            Min          Max       NumPy dtype\n    =====================  ===========  ===========  =============\n    32-bit floating-point  -1.0         +1.0         float32\n    32-bit PCM             -2147483648  +2147483647  int32\n    16-bit PCM             -32768       +32767       int16\n    8-bit PCM              0            255          uint8\n    =====================  ===========  ===========  =============\n\n    Note that 8-bit PCM is unsigned.\n\n    References\n    ----------\n    .. [1] IBM Corporation and Microsoft Corporation, "Multimedia Programming\n       Interface and Data Specifications 1.0", section "Data Format of the\n       Samples", August 1991\n       http://www.tactilemedia.com/info/MCI_Control_Info.html\n\n    Examples\n    --------\n    Create a 100Hz sine wave, sampled at 44100Hz.\n    Write to 16-bit PCM, Mono.\n\n    >>> from scipy.io.wavfile import write\n    >>> samplerate = 44100; fs = 100\n    >>> t = np.linspace(0., 1., samplerate)\n    >>> amplitude = np.iinfo(np.int16).max\n    >>> data = amplitude * np.sin(2. * np.pi * fs * t)\n    >>> write("example.wav", samplerate, data)\n\n    '
    if hasattr(filename, 'write'):
        fid = filename
    else:
        fid = open(filename, 'wb')
    fs = rate
    try:
        dkind = data.dtype.kind
        if (not ((dkind == 'i') or (dkind == 'f') or ((dkind == 'u') and (data.dtype.itemsize == 1)))):
            raise ValueError(("Unsupported data type '%s'" % data.dtype))
        header_data = b
        header_data += b'RIFF'
        header_data += b'\x00\x00\x00\x00'
        header_data += b'WAVE'
        header_data += b'fmt '
        if (dkind == 'f'):
            format_tag = WAVE_FORMAT_IEEE_FLOAT
        else:
            format_tag = WAVE_FORMAT_PCM
        if (data.ndim == 1):
            channels = 1
        else:
            channels = data.shape[1]
        bit_depth = (data.dtype.itemsize * 8)
        bytes_per_second = ((fs * (bit_depth // 8)) * channels)
        block_align = (channels * (bit_depth // 8))
        fmt_chunk_data = struct.pack('<HHIIHH', format_tag, channels, fs, bytes_per_second, block_align, bit_depth)
        if (not ((dkind == 'i') or (dkind == 'u'))):
            fmt_chunk_data += b'\x00\x00'
        header_data += struct.pack('<I', len(fmt_chunk_data))
        header_data += fmt_chunk_data
        if (not ((dkind == 'i') or (dkind == 'u'))):
            header_data += b'fact'
            header_data += struct.pack('<II', 4, data.shape[0])
        if ((((len(header_data) - 4) - 4) + ((4 + 4) + data.nbytes)) > 4294967295):
            raise ValueError('Data exceeds wave file size limit')
        fid.write(header_data)
        fid.write(b'data')
        fid.write(struct.pack('<I', data.nbytes))
        if ((data.dtype.byteorder == '>') or ((data.dtype.byteorder == '=') and (sys.byteorder == 'big'))):
            data = data.byteswap()
        _array_tofile(fid, data)
        size = fid.tell()
        fid.seek(4)
        fid.write(struct.pack('<I', (size - 8)))
    finally:
        if (not hasattr(filename, 'write')):
            fid.close()
        else:
            fid.seek(0)