## Play notes, chords and arbitrary waveforms from Python

Reading Stephen Wolfram's latest discussion of teaching computational thinking (which, though I mostly agree with it, is more an extended ad for Wolfram Programming Lab than a discussion of what computational thinking is and why we should teach it) I found myself musing over ideas for future computer classes for Los Alamos Makers. Students, and especially kids, like to see something other than words on a screen. Graphics and games good, or robotics when possible ... but another fun project a novice programmer can appreciate is music.

I found myself curious what you could do with Python, since I hadn't played much with Python sound generation libraries. I did discover a while ago that Python is rather bad at playing audio files, though I did eventually manage to write a music player script that works quite well. What about generating tones and chords?

A web search revealed that this is another thing Python is bad at. I found lots of people asking about chord generation, and a handful of half-baked ideas that relied on long obsolete packages or external program. But none of it actually worked, at least without requiring Windows or relying on larger packages like fluidsynth (which looked worth exploring some day when I have more time).

### Play an arbitrary waveform with Pygame and NumPy

But I did find one example based on a long-obsolete Python package called Numeric which, when rewritten to use NumPy, actually played a sound. You can take a NumPy array and play it using a pygame.sndarray object this way:

import pygame, pygame.sndarray def play_for(sample_wave, ms): """Play the given NumPy array, as a sound, for ms milliseconds.""" sound = pygame.sndarray.make_sound(sample_wave) sound.play(-1) pygame.time.delay(ms) sound.stop()

Then you just need to calculate the waveform you want to play. NumPy can generate sine waves on its own, while scipy.signal can generate square and sawtooth waves. Like this:

import numpy import scipy.signal sample_rate = 44100 def sine_wave(hz, peak, n_samples=sample_rate): """Compute N samples of a sine wave with given frequency and peak amplitude. Defaults to one second. """ length = sample_rate / float(hz) omega = numpy.pi * 2 / length xvalues = numpy.arange(int(length)) * omega onecycle = peak * numpy.sin(xvalues) return numpy.resize(onecycle, (n_samples,)).astype(numpy.int16) def square_wave(hz, peak, duty_cycle=.5, n_samples=sample_rate): """Compute N samples of a sine wave with given frequency and peak amplitude. Defaults to one second. """ t = numpy.linspace(0, 1, 500 * 440/hz, endpoint=False) wave = scipy.signal.square(2 * numpy.pi * 5 * t, duty=duty_cycle) wave = numpy.resize(wave, (n_samples,)) return (peak / 2 * wave.astype(numpy.int16)) # Play A (440Hz) for 1 second as a sine wave: play_for(sine_wave(440, 4096), 1000) # Play A-440 for 1 second as a square wave: play_for(square_wave(440, 4096), 1000)

### Playing chords

That's all very well, but it's still a single tone, not a chord.

To generate a chord of two notes, you can add the waveforms for the two notes. For instance, 440Hz is concert A, and the A one octave above it is double the frequence, or 880 Hz. If you wanted to play a chord consisting of those two As, you could do it like this:

play_for(sum([sine_wave(440, 4096), sine_wave(880, 4096)]), 1000)

Simple octaves aren't very interesting to listen to.
What you want is chords like major and minor triads and so forth.
If you google for `chord ratios`

Google helpfully gives
you a few of them right off, then links to a page with
a table of
ratios for some common chords.

For instance, the major triad ratios are listed as `4:5:6`

.
What does that mean? It means that for a C-E-G triad (the first C
chord you learn in piano), the E's frequency is 5/4 of the C's
frequency, and the G is 6/4 of the C.

You can pass that list, [4, 5, 5] to a function that will calculate the right ratios to produce the set of waveforms you need to add to get your chord:

def make_chord(hz, ratios): """Make a chord based on a list of frequency ratios.""" sampling = 4096 chord = waveform(hz, sampling) for r in ratios[1:]: chord = sum([chord, sine_wave(hz * r / ratios[0], sampling)]) return chord def major_triad(hz): return make_chord(hz, [4, 5, 6]) play_for(major_triad(440), length)

Even better, you can pass in the waveform you want to use when you're adding instruments together:

def make_chord(hz, ratios, waveform=None): """Make a chord based on a list of frequency ratios using a given waveform (defaults to a sine wave). """ sampling = 4096 if not waveform: waveform = sine_wave chord = waveform(hz, sampling) for r in ratios[1:]: chord = sum([chord, waveform(hz * r / ratios[0], sampling)]) return chord def major_triad(hz, waveform=None): return make_chord(hz, [4, 5, 6], waveform) play_for(major_triad(440, square_wave), length)

There are still some problems. For instance, sawtooth_wave() works fine individually or for pairs of notes, but triads of sawtooths don't play correctly. I'm guessing something about the sampling rate is making their overtones cancel out part of the sawtooth wave. Triangle waves (in scipy.signal, that's a sawtooth wave with rising ramp width of 0.5) don't seem to work right even for single tones. I'm sure these are solvable, perhaps by fiddling with the sampling rate. I'll probably need to add graphics so I can look at the waveform for debugging purposes.

In any case, it was a fun morning hack. Most chords work pretty well, and it's nice to know how to to play any waveform I can generate.

The full script is here: play_chord.py on GitHub.

[

*11:29 Oct 05, 2016 More programming | permalink to this entry |*]