def write(filename, rate, data):
if hasattr(filename,'<STR_LIT>'):<EOL><INDENT>fid = filename<EOL><DEDENT>else:<EOL><INDENT>fid = open(filename, '<STR_LIT:wb>')<EOL><DEDENT>try:<EOL><INDENT>dkind = data.dtype.kind<EOL>if not (dkind == '<STR_LIT:i>' or dkind == '<STR_LIT:f>' or (dkind == '<STR_LIT:u>' and data.dtype.itemsize == <NUM_LIT:1>)):<EOL><INDENT>raise ValueError("<STR_LIT>" % data.dtype)<EOL><DEDENT>fid.write(b'<STR_LIT>')<EOL>fid.write(b'<STR_LIT>')<EOL>fid.write(b'<STR_LIT>')<EOL>fid.write(b'<STR_LIT>')<EOL>if dkind == '<STR_LIT:f>':<EOL><INDENT>comp = <NUM_LIT:3><EOL><DEDENT>else:<EOL><INDENT>comp = <NUM_LIT:1><EOL><DEDENT>if data.ndim == <NUM_LIT:1>:<EOL><INDENT>noc = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>noc = data.shape[<NUM_LIT:1>]<EOL><DEDENT>bits = data.dtype.itemsize * <NUM_LIT:8><EOL>sbytes = rate*(bits // <NUM_LIT:8>)*noc<EOL>ba = noc * (bits // <NUM_LIT:8>)<EOL>fid.write(struct.pack('<STR_LIT>', <NUM_LIT:16>, comp, noc, rate, sbytes, ba, bits))<EOL>fid.write(b'<STR_LIT:data>')<EOL>fid.write(struct.pack('<STR_LIT>', data.nbytes))<EOL>if data.dtype.byteorder == '<STR_LIT:>>' or (data.dtype.byteorder == '<STR_LIT:=>' and sys.byteorder == '<STR_LIT>'):<EOL><INDENT>data = data.byteswap()<EOL><DEDENT>_array_tofile(fid, data)<EOL>size = fid.tell()<EOL>fid.seek(<NUM_LIT:4>)<EOL>fid.write(struct.pack('<STR_LIT>', size-<NUM_LIT:8>))<EOL><DEDENT>finally:<EOL><INDENT>if not hasattr(filename,'<STR_LIT>'):<EOL><INDENT>fid.close()<EOL><DEDENT>else:<EOL><INDENT>fid.seek(<NUM_LIT:0>)<EOL><DEDENT><DEDENT>
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
-----
* The file can be an open file or a filename.

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