13 — Audio and Interfaces
Unity Tutorial Series: Audio
Introduction
In this week’s tutorial series, I’ll introduce the basics of Unit’s audio systems, with a particular focus on 3D audio and what sets it apart from simple (boring) 2 channel stereo.
Specifically, we will cover:
- An introduction to audio in Unity
- Creating New Interfaces for Audio Expression
The Project
The inspiration for this week, comes from a surprisingly neat performance I found when searching for instruments that would lend themselves well to our physical (arduino) and digital (Unity) platform. I naturally, thought of the theremin.
The theremin is was invented in 1928 by Leon Theremin and consists of two antennas that sense the relative position of the hands and control frequency (pitch) with one hand, and amplitude volume with the other.
We’ll be making something similar, if not a little bit simplified, in this week’s tutorials. Here’s a photo of the interface and a diagram of the LDR (photoresistor) portion of the circuit (don’t judge my terrible wiring!).
In the interface above, I’ve chosen to have a potentiometer (which will control my amplitude) and an LDR (which will control my frequency). I could have just as easily went with two LDRs, which would have allowed me to control both pitch and volume, but I want to try both, so that I’m able to better isolate each input type and figure out what I like better.
Approach
Our approach, will first be to ensure that we have our physical interface working well. By well, I mean that we have the very basics working. In my case, which for us, is making sure our Arduino and potentiometer are set up and sending good values to the serial port.
Step 1: Wire it up!
The first step, is to make sure you have a potentiometer and some wires, then wire up your circuit. Here’s mine:
- (Red wire) Connect a wire from the Arduino’s 3v pin to the + pin on your potentiometer (this is the clockwise side)
- (Blue wire) Connect a wire from the Arduino’s A0 pin (analog in/out 0) to the middle pin on your potetiometer.
- (Black wire) COnnect a wire from the Arduino’s ground pin (GND) to the final pin on the potentiometer.
Step 2: Code it!
Next, open up your Arduino IDE and either code the program to output the serial values from the A0 pin, or, choose File > Examples > Basics > Analog Read Serial, or you can copy-paste what I have below.
Test it! Upload your code to the Arduino board and open the serial monitor, make sure you’re getting some values:
You should be getting values between 0 and 1023, with a bit of jittering in between. If so, congrats! We’ve finished setting up the first half of our interface!