Cacophony for the whole family
Allen Downey
This is an example that demonstrates some of the features in the Think DSP library.
It is inspired by the performance of a grade school band I witnessed recently. My goal is to simulate the sound of a beginner band.
First, a function that translates from a MIDI number to a frequency:
Now here's a randomized version that simulates three kinds of errors: poor tuning, playing the wrong note, and popping an overtone. Notice that it is possible to make all three errors.
This function takes a MIDI number and duration and makes a Wave:
Let's test make_note. MIDI number 60 is middle C.
Sounds good.
Now we can make 10 notes and play them at the same time. Since Wave provides __add__
, we can use sum
:
And we can test it with a middle C:
Good, sounds like angry bees.
And now, a rousing chorus of that old crowd favorite, Hot Cross Buns.
Wave provides __or__
, which concatenates notes, so we can use reduce
:
And that brings a tear of pride to any parent's eye.
On a more serious note, this example tells us something about how the ear interprets complex sounds with many tones and harmonics.
Let's take a look at the spectrum of that middle C:
And we can zoom in on the first few harmonics:
A few observations:
The kids playing out of tune have a bigger effect on the harmonics, and less effect on the fundamental, so the ear can still pick out a clear pitch, and
Some of the unintentional overtones overlap with the harmonics, so they change the timbre, but don't stick out as much as you might expect,
The high harmonics are so spread out that they basically contribute white noise and don't affect the perceived pitch.