Products for USB Sensing and Control
Products for USB Sensing and Control

A Phidgets Theremin With Processing

Creating a non-contact instrument with Phidgets


by Ben

Introduction

What's A Theremin?

Theremin's are unique instruments that are completely contactless. Instead of musicians handling the instrument, they simply move their hands in the air above. The position of the musician's hands determines the sound produced. Check out this video for an example.


In this project, you will recreate this futuristic non-contact effect using a distance sensor.

Prerequisites

This project assumes you are familiar with Processing and have completed the following guides:

Goal

The goal of this project is to translate distance data, collected by the Distance Phidget, into audio output. In the end you should have a functional Theremin.

Parts Inventory


Note: Any distance sensor can be used for this project (including the DST1200 from the Rover Kit), however, results may vary depending on resolution and sample rate.

Hardware

For this project, simply connect two distance sensors to your VINT Hub and connect your VINT Hub to your computer.

Interpreting Data

The Distance Sensors will return values between 0 and 650mm depending on the position of your hands above the sensors. You can map these values directly to pitch and volume. As a hand rises above the pitch sensor, the pitch of the theremin rises. Identically, as a hand rises above the volume sensor, the volume of the theremin rises.For example, if your hand is at 325mm (50% of max distance) your volume should be at 50% max volume.

Software

Step 0: Sound Library

In this project, we will be using Processing's Sound Library, however, the Beads Library, available for Java, could be equally suited to the task. If you wish to complete this project in another language, you will need to find a compatible sound library.

1. Open Processing and navigate to
Sketch > Import Library... > Add Library...
2. Enter "Sound" in the search bar and install it 3. You will see a green checkmark if the installation is successful

The Sound Library will allow you to play a variety of different sounds from within Processing. To start, you will need to create a sine wave oscialltor. Check the SinOsc API on the processing website for more information.

Step 1: Setup

Here we need to define and configure the oscillator and phidgets. Make sure the oscillator is initialised before the Phidgets.



import processing.sound.*;
import com.phidget22.*;

static SinOsc sine;
DistanceSensor pitchDistance;
DistanceSensor volumeDistance;

void setup(){
    size(640, 360);
    background(255);
    
    //Create start oscillator before initializing Phidgets
    sine =  new SinOsc(this);
    sine.play();

    //TODO: Create Phidget Objects, Addess Objects, 
    //      Register for Events, Open Objects
    //      Set Distance Objects Data Interval to Min
}

Step 2: Events

The map() function is very useful for converting a distance value to a pitch value or a distance value to a volume value.




pitchDistance.addDistanceChangeListener(new DistanceSensorDistanceChangeListener() {
    public void onDistanceChange(DistanceSensorDistanceChangeEvent e) {
        print("\nPitch Distance: " + e.getDistance());
        //TODO: use map() to map distance to a frequency range of your choice
        //TODO: Update SinOsc frequency.
    }
});

volumeDistance.addDistanceChangeListener(new DistanceSensorDistanceChangeListener() {
    public void onDistanceChange(DistanceSensorDistanceChangeEvent e) {
        print("\Volume Distance: " + e.getDistance());
        //TODO: use map() to map distance to a volume range
        //TODO: Update SinOsc volume (hint: amplitude).
    }
});


Make sure you close your oscillator before your Phidgets.




void exit() {
    print("Closing...");
    //TODO: close phidgets
    super.exit();
}

Step 3: Draw

The draw function keeps the window open, so include it even if there is no code inside!




void draw(){
    //TODO: Nothing!
}

Conclusion

By the end of this article you will have created your very own instrument! Experiment with different sounds and see if you can recreate your favourite melody!

Have you created a musical application with Phidgets? We would love to hear about it or any other projects at education@phidgets.com

Next Steps

  • More complex oscillators - try varying the types of oscillators or stack overtones to create a richer sound.
  • Visualize the sound output by attaching graphics to the input parameters.
  • Add effects - read the Sound Library API