# Python, Pitch Shifting, and the Pianoputer

Record a sound, change its pitch 50 times and assign each new sound to a key of your computer keyboard. You get a Pianoputer !

A sound can be encoded as an array (or list) of values, like this:

To make this sound play twice faster, we remove every second value in the array:

By doing so we didn’t only halved the sound’s duration, we also doubled its frequency, making it higher-pitched than the original.

If on the contrary we repeat each value of the array twice, we produce a sound that is slower, with a longer period, and therefore lower-pitched:

Here is a simple Python function that can change the speed of a sound by any factor:

What is more difficult to do is to change the duration of a sound while preserving its pitch (sound stretching), or change the pitch of a sound while preserving its duration (pitch shifting).

## Sound stretching

Sound stretching can be done using the classical phase vocoder method. You first break the sound into overlapping bits, and you rearrange these bits so that they will overlap even more (if you want to shorten the sound) or less (if you want to stretch the sound), like in this figure:

The difficulty is that the reorganized bits can interfer badly with one another, and some phase-transformation is necessary so that this won’t happen. Here is the Python code, freely rewritten from there:

## Pitch shifting

Pitch-shifting is easy once you have sound stretching. If you want a higer pitch, you first stretch the sound while conserving the pitch, then you speed up the result, such that the final sound has the same duration as the initial one, but a higher pitch due to the speed change.

Doubling the frequency of a sound increases the pitch of one octave, which is 12 musical semitones. Therefore to increase the pitch by $n$ semitones we must multiply the frequency by a factor $2^{\frac{n}{12}}$:

## Application: the Pianoputer

Let us play around with our new pitch-shifter. We first strike a bowl:

Then we create 50 pitch-shifted derivatives of that sound, ranging from the very low to the very high:

We will assign each sound to a key of the computer keyboard, following the order in this file, which organizes the keyboard like this:

We simply tell the computer to play the corresponding sound when a key is pressed, and stop the sound when the key is released:

And we have turned our computer into a piano ! Now, to thank you for reading until there, let me play a little turkish song for you:

Here are all the files you need if you want to try this at home. Since not everyone uses Python, I also coded a pianoputer in Javascript/HTML5 (here) but it is very far from good. It would be really great if an experienced HTML5/JS/elm developer improved it, or rewrote it from scratch.

## What next ?

On a more general note, I find that computers have been under-used to produce performance music. I get it that it is easier to use a piano keyboard or record from an instrument directly, but look at what you can do with just a bowl and 60 lines of Python !

Even a cheap computer has so many controls that would make it a proper music station: you can sing to the microphone, make gestures to the webcam, modulate stuff using the mouse, and control the rest from your keyboard. So many ways to express yourself, and there is a Python package for each of them… Any artistic hacker wanting to make steps in that direction ?