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

#### sales inquiries

##### quotes, distributor information, purchase orders
sales@phidgets.com

#### technical inquiries

##### support, advice, warranty, returns, misshipment
support@phidgets.com

#### website inquiries

web@phidgets.com

##### Unit 1 - 6115 4 St SE Calgary AB  T2H 2H9 Canada
Projects Dealers Terms and Conditions Discontinued Products Phidget21 Documentation

# A Phidgets Theremin With Processing

Intermediate
by Ben

### Hour of Code

If you are an educator with Hour of Code, you can get the hardware for this project for free here!

## 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.

## 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 0mm 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

### Configure Sound Library

In this project, you 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.

Open Processing and navigate to
Sketch > Import Library... > Add Library...

Enter "Sound" in the search bar and install it

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 initialized 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.




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.
}
});

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).
}
});






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.