Skip to main content

wavio is a Python module that defines two functions:

  • wavio.read reads a WAV file and returns an object that holds the sampling rate, sample width (in bytes), and a numpy array containing the data.

  • wavio.write writes a numpy array to a WAV file, optionally using a specified sample width.

The functions can read and write 8-, 16-, 24- and 32-bit integer WAV files.

The module uses the wave module in Python’s standard library, so it has the same limitations as that module. In particular, the wave module does not support compressed WAV files, and it does not handle floating point WAV files. When floating point data is passed to wavio.write it is converted to integers before being written to the WAV file.

wavio requires Python 3.7 or later.

wavio depends on numpy (http://www.numpy.org). NumPy version 1.19.0 or later is required. The unit tests in wavio require pytest.

The API of the functions in wavio should not be considered stable. There may be backwards-incompatible API changes between releases.

Important notice

In version 0.0.5, the data handling in wavio.write has been changed in a backwards-incompatible way. The API for scaling the input in 0.0.4 was a flexible interface that only its creator could love. The new API is simpler, and it is hoped that it does the right thing by default in most cases. In particular:

  • When the input data is an integer type, the values are not scaled or shifted. The only change that might happen is the data will be clipped if the values do not fit in the output integer type.

  • If the input data is a floating point type, sampwidth must be given. The default behavior is to scale input values in the range [-1.0, 1.0] to the output range [min_int+1, max_int], where min_int and max_int are the minimum and maximum values of the output data type determined by sampwidth. See the description of scale in the docstring of wavio.write for more options. Regardless of the value of scale, the float input 0.0 is always mapped to the midpoint of the output type; wavio.write will not translate the values up or down.

  • A warning is now generated if any data values are clipped. A parameter allows the generation of the warning to be disabled or converted to an exception.

Examples

The following examples are also found in the docstring of wavio.write.

Create a 3 second 440 Hz sine wave, and save it in a 24-bit WAV file.

>>> import numpy as np
>>> import wavio
>>> rate = 22050           # samples per second
>>> T = 3                  # sample duration (seconds)
>>> n = int(rate*T)        # number of samples
>>> t = np.arange(n)/rate  # grid of time values
>>> f = 440.0              # sound frequency (Hz)
>>> x = np.sin(2*np.pi * f * t)

x is a single sine wave with amplitude 1, so we can use the default scale.

>>> wavio.write("sine24.wav", x, rate, sampwidth=3)

Create a file that contains the 16 bit integer values -10000 and 10000 repeated 100 times. Use a sample rate of 8000.

>>> x = np.empty(200, dtype=np.int16)
>>> x[::2] = -10000
>>> x[1::2] = 10000
>>> wavio.write("foo.wav", x, 8000)

Check that the file contains what we expect. The values are checked for exact equality. The input was an integer array, so the values are not scaled.

>>> w = wavio.read("foo.wav")
>>> np.all(w.data[:, 0] == x)
True

Write floating point data to a 16 bit WAV file. The floating point values are assumed to be within the range [-2, 2], and we want the values 2 and -2 to correspond to the full output range, even if the actual values in the data do not fill this range. We do that by specifying scale=2.

T, rate and t are from above. The data is the sum of two sinusoids, with frequencies 440 and 880 Hz, modulated by a parabolic curve that is zero at the start and end of the data.

>>> envelope = (4/T**2)*(t * (T - t))
>>> omega1 = 2*np.pi*440
>>> omega2 = 2*np.pi*880
>>> y = envelope*(np.sin(omega1*t) + 0.3*np.sin(omega2*t + 0.2))
>>> y.min(), y.max()
(-1.1745469775555515, 1.093833464065767)

Write the WAV file, with scale=2.

>>> wavio.write('harmonic.wav', y, rate, sampwidth=2, scale=2)

Check the minimum and maximum integers that were actually written to the file:

>>> w = wavio.read("harmonic.wav")
>>> w.data.min(), w.data.max()
(-19243, 17921)

If we want the WAV file to use as much of the range of the output integer type as possible (while still mapping 0.0 in the input to 0 in the output), we set scale=”auto”.

>>> wavio.write('harmonic_full.wav', y, rate, sampwidth=2, scale="auto")
>>> w = wavio.read('harmonic_full.wav')
>>> w.data.min(), w.data.max()
(-32768, 30517)

Author: Warren Weckesser

Repository: https://github.com/WarrenWeckesser/wavio

License: BSD 2-clause (http://opensource.org/licenses/BSD-2-Clause)

Metadata

Release files for wavio 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wavio 0.0.9
File Size Uploaded
wavio-0.0.9.tar.gz 10.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wavio 0.0.9
File Interpreter ABI Platform
wavio-0.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 20.0 kB

Release files / wavio-0.0.9.tar.gz

Download URL wavio-0.0.9.tar.gz
Size 10.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5aa683a6b41b67d738b3d2d5883bca36ab2398c84ab7fa11940e0d9554cd1e85
BLAKE2b-256 checksum
How to use checksums
d2fae016582788aead27a0add5fe8b121bf0a370947c6779cf4e0fae35c3d3bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / wavio-0.0.9-py3-none-any.whl

Download URL wavio-0.0.9-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c5966fd0f9ba4b6fddeb3d6622f9a6b62ca9e4c3daf5e6c3633a29a58dfbfed5
BLAKE2b-256 checksum
How to use checksums
879a50c7b7107b98330d295f82a38a8d33210fb33ca472cbdd18dce4ca2fdaec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.0.9 This release

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page