Audio Generation

Theremin Demonstration

Electronics have been used to create music for nearly a century. One of the first electronic instruments is the theremin, an instrument that is played without ever actually touching it.

Because the theremin works by using hand positions to directly determine the frequency and amplitude of a sound wave, it is relatively easy to simulate one in software. The code below uses the mouse pointer in lieu of the thereminist’s hands.

Installing Processing Libraries

NOTE: Before running the Theremin.pde sketchbook, you will need to install the Processing Sound software library, which provides the additional code needed for generating audio effects.
  1. Open the Theremin.pde sketchbook in Processing.
  2. Under the Sketch menu, select Import Library... and choose Add Library...
  3. Scroll down the list to find Sound and select it.
  4. Click on the Install button.
All future projects that require the Sound library will now be able to use this library.

Click to Download: Theremin

As you experiment with the virtual theremin, note the following:

  1. X maps to frequency.
  2. Y maps to amplitude.
  3. You can click at a particular point to fix the harmonic.

After you have thoroughly annoyed everyone in the vicinity made beautiful music, be prepared to discuss the following before moving on to your assignment.

  • What are the parameters required to generate a sound? Note that these are the qualities/quantities that change producing a corresponding change in output.
  • Are these parameters sufficient to generate any type of sound? What other parameters might be needed to generate notes that sound like a guitar or a flute?
  • How do these parameters approximate physical reality? How is this similar to the parameters used to generate visual artifacts, like color, position, or brightness?


Your assignment is to program a virtual piano in Processing—il Processiano! Download the starter code for il Processiano.

Click to Download: Processiano

Extend the code to generate audio with key presses. The starter code generates and displays notes when the corresponding keys are pressed (c, d, e), but your program must meet the following specifications:

  1. Extend the code to work with the entire range c, d, e, f, g, a, and b. The following table will help you map notes to exact frequencies.

  2. Extend the code to work across two octaves. When you press the SHIFT and C keys (AKA capital “C”), the procession should play a C note the next octave higher. HINT: Physics makes this easy. Each note is twice the frequency of itself in the previous octave. In other words, the C one octave above middle C (261.626 Hz) is 2 × 261.626, or roughly 523.252 Hz).

  3. Try playing a song:


Submit the Processiano.pde file of your Processiano sketchbook.


Processiano uses only natural (♮) notes—i.e., no sharps (♯) or flats (♭). Extend the program to allow the user to designate a sharp or flat by using the CTRL or ALT keys along with the designated note key. Note that not all notes have corresponding sharps and flats! This is why some pairs of white keys on a piano are separated by black keys and some are not (e.g., there is no such note as E♯ or F♭).