Python Audio Tutorial | Audio Module In Python Tutorial

Chapter 181: Audio

Section 181.1: Working with WAV files


Windows environment

import winsound
winsound.PlaySound("path_to_wav_file.wav", winsound.SND_FILENAME)


Support mono/stereo

Doesn’t support compression/decompression

import wave
with"path_to_wav_file.wav", "rb") as wav_file: # Open WAV file in read-only mode.

Get basic information.

n_channels = wav_file.getnchannels() # Number of channels. (1=Mono, 2=Stereo).
sample_width = wav_file.getsampwidth() # Sample width in bytes.
framerate = wav_file.getframerate() # Frame rate.
n_frames = wav_file.getnframes() # Number of frames.
comp_type = wav_file.getcomptype() # Compression type (only supports "NONE").
comp_name = wav_file.getcompname() # Compression name.

Read audio data.

frames = wav_file.readframes(n_frames) # Read n_frames new frames.
assert len(frames) == sample_width * n_frames

Duplicate to a new WAV file.

with"path_to_new_wav_file.wav", "wb") as wav_file: # Open WAV file in write-only

Write audio data.

params = (n_channels, sample_width, framerate, n_frames, comp_type, comp_name)

Section 181.2: Convert any soundfile with python and mpeg

from subprocess import check_call
ok = check_call(['ffmpeg','-i','input.mp3','output.wav'])
if ok:
with open('output.wav', 'rb') as f:
wav_file =
note: What are the differences and similarities between ffmpeg, libav, and avconv?

Section 181.3: Playing Windows’ beeps

Windows provides an explicit interface through which the winsound module allows you to play raw beeps at a given frequency and duration.

import winsound – Python® Notes for Professionals 700

freq = 2500 # Set frequency To 2500 Hertz
dur = 1000 # Set duration To 1000 ms == 1 second
winsound.Beep(freq, dur)

Section 181.4: Audio With Pyglet

import pyglet
audio ="audio.wav")

For further information, see pyglet

Leave a Comment