io.wavfile

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.

Module Contents

Classes

WavFileWarning()

Functions

_read_fmt_chunk(fid,is_big_endian) Returns
_read_data_chunk(fid,format_tag,channels,bit_depth,is_big_endian,mmap=False)
_skip_unknown_chunk(fid,is_big_endian)
_read_riff_chunk(fid)
read(filename,mmap=False) Open a WAV file
write(filename,rate,data) Write a numpy array as a WAV file.
_array_tofile(fid,data)
_array_tofile(fid,data)
class WavFileWarning
_read_fmt_chunk(fid, is_big_endian)
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
_read_data_chunk(fid, format_tag, channels, bit_depth, is_big_endian, mmap=False)
_skip_unknown_chunk(fid, is_big_endian)
_read_riff_chunk(fid)
read(filename, mmap=False)

Open a WAV file

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

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).

New in version 0.12.0.

rate : int
Sample rate of wav file.
data : numpy array
Data read from wav file. Data-type is determined from the file; see 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.

[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
write(filename, rate, data)

Write a numpy array as a WAV file.

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.
  • 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.

[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
_array_tofile(fid, data)
_array_tofile(fid, data)