Wavio

0.0.9 · active · verified Wed Apr 15

Wavio is a Python module for reading and writing WAV files using NumPy arrays. It provides two main functions, `wavio.read` and `wavio.write`, for handling 8-, 16-, 24-, and 32-bit integer WAV files. It leverages Python's standard `wave` module and currently does not support compressed WAV files or direct floating-point WAV file output, converting floating-point input to integers. The current version is 0.0.9, with recent updates focusing on NumPy 2.0.0 compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate a 3-second 440 Hz sine wave using NumPy and save it as a 24-bit WAV file, then read it back. It illustrates the basic usage of `wavio.write` and `wavio.read`.

import numpy as np
import wavio

# Parameters for the sine wave
rate = 22050  # samples per second
T = 3         # sample duration (seconds)
f = 440.0     # sound frequency (Hz)

# Create time array and sine wave data
t = np.linspace(0, T, int(T * rate), endpoint=False)
x = np.sin(2 * np.pi * f * t)

# Write the sine wave to a 24-bit WAV file
wavio.write("sine24.wav", x, rate, sampwidth=3)

# Read the WAV file back
wav_read = wavio.read("sine24.wav")
print(f"Read WAV file: {wav_read.filename}")
print(f"Sample rate: {wav_read.rate} Hz")
print(f"Sample width: {wav_read.sampwidth} bytes")
print(f"Data shape: {wav_read.data.shape}")

view raw JSON →