detly detly - 1 year ago 162
Python Question

How do I write a 24-bit WAV file in Python?

I want to generate a 24-bit WAV-format audio file using Python 2.7 from an array of floating point values between -1 and 1. I can't use because it only supports 16 or 32 bits. The documentation for Python's own wave module doesn't specify what format of data it takes.

So is it possible to do this in Python?

Answer Source

Another option is available in wavio (also on PyPI:, a small module I created as a work-around to the problem of scipy not yet supporting 24 bit WAV files. The file contains the function write, which writes a numpy array to a WAV file. To write a 24-bit file, use the argument sampwidth=3. The only dependency of wavio is numpy; wavio uses the standard library wave to deal with the WAV file format.

For example,

In [21]: import numpy as np

In [22]: import wavio

In [23]: rate = 22050             # samples per second

In [24]: T = 3                    # sample duration (seconds)

In [25]: f = 440.0                # sound frequency (Hz)

In [26]: t = np.linspace(0, T, T*rate, endpoint=False)

In [27]: sig = np.sin(2 * np.pi * f * t)

In [28]: wavio.write("sine24.wav", sig, rate, sampwidth=3)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download