O'Reilly logo

A Concise Introduction to Programming in Python by Mark J. Johnson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 19

Sound Manipulation

Sounds are represented in computer memory as a sequence of discrete samples of air pressure readings. CDs are recorded using 44,100 of these samples per second. With that many samples, it is possible to reconstruct the original sound waves in such a way that our ears have a difficult time hearing any difference between the reproduction and the original sound. Given access to those samples in a Python program, we can do some interesting things.

Listing 19.1: Reverse WAV

 1 # reversewav.py
 2 import array
 3 import contextlib
 4 import wave
 5
 6 def datatype(width):
 7 return "B" if width == 1 else "h"
 8
 9 def readwav(fname):
10 with contextlib.closing(wave.open(fname)) as f:
11  params = f.getparams()
12  frames ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required