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)) ...

Get A Concise Introduction to Programming in Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.