0
0
SciPydata~5 mins

WAV audio file handling in SciPy

Choose your learning style9 modes available
Introduction

WAV files store sound data. Handling them lets you read, analyze, and save audio in your programs.

You want to read a sound recording to analyze its volume or frequency.
You need to save processed audio after editing or filtering.
You want to convert audio data into numbers for machine learning.
You want to play or visualize sound waves in a project.
You need to extract audio features like duration or sample rate.
Syntax
SciPy
from scipy.io import wavfile

# Read a WAV file
sample_rate, data = wavfile.read('filename.wav')

# Write data to a WAV file
wavfile.write('output.wav', sample_rate, data)

sample_rate is how many samples per second the audio has.

data is a NumPy array with the sound values.

Examples
This reads 'sound.wav' and stores the sample rate and audio data.
SciPy
from scipy.io import wavfile
sample_rate, data = wavfile.read('sound.wav')
This saves the audio data back to a new WAV file.
SciPy
wavfile.write('new_sound.wav', sample_rate, data)
Shows the sample rate and shape of the audio data array.
SciPy
print(f'Sample rate: {sample_rate} Hz')
print(f'Data shape: {data.shape}')
Sample Program

This program reads a WAV file, prints its sample rate, data type, shape, and duration. It also normalizes the audio data if it is stored as integers.

SciPy
from scipy.io import wavfile
import numpy as np

# Read WAV file
sample_rate, data = wavfile.read('example.wav')

# Print basic info
print(f'Sample rate: {sample_rate} Hz')
print(f'Data type: {data.dtype}')
print(f'Data shape: {data.shape}')

# Calculate duration in seconds
duration = data.shape[0] / sample_rate
print(f'Duration: {duration:.2f} seconds')

# Normalize audio data to range -1 to 1 if integer type
if np.issubdtype(data.dtype, np.integer):
    max_val = np.iinfo(data.dtype).max
    min_val = np.iinfo(data.dtype).min
    data_norm = data.astype(np.float32)
    data_norm[data_norm < 0] /= -min_val
    data_norm[data_norm >= 0] /= max_val
    print(f'First 5 normalized samples: {data_norm[:5]}')
else:
    print('Data is not integer type, skipping normalization.')
OutputSuccess
Important Notes

WAV files can have one (mono) or two (stereo) channels. The data shape changes accordingly.

Normalization helps when you want to process audio values between -1 and 1.

Make sure the WAV file exists in your working folder or provide the full path.

Summary

Use scipy.io.wavfile.read to load WAV audio into arrays.

Use scipy.io.wavfile.write to save arrays back to WAV files.

Check sample rate and data shape to understand your audio file.