Linking Arduino to another Application with OSC Communication Part 2 – Making a Rhythm Machine with Sonic Pi

Linking Arduino to another Application with OSC Communication Part 2 – Making a Rhythm Machine with Sonic Pi

Arduino OSC Communication

DSC_0175-1024x576

In our last article, we learned about the OSC communication basics with Arduino. Since OSC (Open Sound Control) was developed as a protocol for musical instruments, today we will try to create an instrument device.

While I was kind of lost about what kind of device to create, I thought that it would be a good idea to create an OSC keyboard for Arduino. After doing some research, I found about the sound programming app Sonic Pi, which was featured in “Audio programming with Sonic Pi!.” It runs on Raspberry Pi ,and it’s compatible with OSC. So, today let’s create a rhythm machine that works with Sonic Pi.

Today’s Electronics Recipe

Expected time to complete: 150 minutes

Parts needed:

System Overall Structure

We designed the basic flow as follow. The data will be sent from the Arduino controller circuit to the Processing server using OSC. Based on the controller input value, it will be converted into sound programs for Sonic Pi. Then, they will be sent to Sonic Pi, which will create and play sounds. It’s fine if you don’t understand all the details. Sonic Pi is written with a programming language called Ruby, and today, we will use Ruby for the OSC signals.

fig20151007_01

Figure 1 System flow

sonicpi

Figure 2 Sonic Pi, the sound programming environment

Check this article for more information about Sonic Pi. Sonic Pi not only runs on Raspberry Pi, it also runs on Windows and Mac OS X. We already have the Processing environment set up from the part 1, so let’s download the Mac OS X version. Once we figure out the overall system structure, we will start building the actual device.

Creating the Rhythm Machine Controller Circuit

First of all, we need to build a controller. Let’s make a cool one! The cases I’m usually using are all flat and big, and I just couldn’t find a good one. So, I kept looking.

DSC_0166-1024x576

Picture 1 Chopstick box

I found the perfect size box, which actually is a chopstick box. The depth seems good too.

DSC_0167-1024x576

Picture 2 Inside the box

There’s something about making a rhythm machine out of a box that has Kanji (Chinese characters) on it. Well, now that we found a good case, let’s start creating the circuit.

Controller Interface

Before we build the circuit, let’s align the parts and get an image of the interface we want. When we build the circuit there will be many limitations, so it may not always go as planned. It’s still better to decide on the specs beforehand like part arrangement or where adaptors/LAN cables will come out from (if they come out from numerous directions, it doesn’t fit well). Thinking about all kinds of ideas is the fun part.

DSC_0169-1024x576

Picture 3 Mounting the parts and thinking about the interface

Since we’re building a rhythm machine, we arranged the parts so that the sounds can be toggled on and off using a toggle switch and the 8-beat rhythm could be controlled. We are using Arduino Ethernet (Arduino and Ethernet shield combined) due to the depth of the case. We want to add the 7 segment LED as shown in Photo 3, but we don’t have enough Arduino pins, so let’s think about that later. Also, we used a transparent acrylic board for the lid. We will drill holes in the board for each part.

Circuit Framing

Once we decided on the arrangement, we will process the case and build the circuit.

DSC_0170-1024x576

Picture 4 Making the case based on the parts

DSC_0171-1024x576

Picture 5 Drilling holes in the acrylic board for the each part

DSC_0172-1024x576

Photo 6 Arranging the parts

Once we have most of the parts arranged in the case, let’s start building the circuit. There are many parts for this one, so we need to save space on the breadboard and save pins on Arduino. If we simply use 8 pins from Arduino for the 8 toggle switches, we won’t be able to add other parts in the future if we we want to make improvements, so let’s try to get creative with the circuits.

OSC_RYTHM_MACHINE-1024x519

Figure 3 Rhythm machine interface circuit

The circuit we are building today has 8 toggle switches spread out in groups of 4s to determine the power input status. When we created the speaker, we arranged switches with different resistor values to remember which switch was pushed. We will use the same method here. (The actual device has LEDs too, but for the sake of the circuits explanation, we will skip that part)

Previously, we didn’t evaluate when multiple switches were being pushed, but we need to for this one. So, let’s go a step further. We will prepare 1KΩ, 2KΩ, 4KΩ, 8KΩ for the resistor values, so that even when multiple buttons get pushed, Arduino can determine each one by the input value. In simpler terms, if the resistor value is 1KΩ, 2KΩ, 4KΩ, 8KΩ from the top, the total will be 1 when #1 is being pushed, or 3 when #1 and #2 are being pushed, or 5 when #1 and #3 are being pushed, and so on. This way, if the total value is different for all patterns, Arduino can determine which buttons are being pushed. As long as it follows this rule, you can use other resistors as well.

With this method, the amount of pins used by Arduino will decrease significantly. But it still takes up a lot of space on the breadboard. So if you want more, try using an IC called shift register, which limits input and output.

DSC_0175-1024x576

Picture 7 Mounting the circuit once you have arranged the parts

DSC_0174-1024x576

Picture 8 The interface is complete!

Once the interface is done, let’s move on to the program.

Implementing the OSC Program

We will be using the same program as before to connect Arduino to processing using OSC. In this case, the controller status is being monitored by analog A0~2, so we will send that value to processing.

OSC program from Arduino to Processing server

Next, let’s look at the program on the Processing side. The flow on the Processing side will look like this.

  1. [Receive OSC] Receive input values from Arduino
  2. Create Sonic Pi code based on input values
  3. [Send OSC] Send code (/run-code) from Processing to Sonic Pi
  4. Play the code received by Sonic Pi

With this flow, it is possible to communicate between Arduino – Processing – Sonic Pi. Some may ask, “Can’t I just connect Arduino and Sonic Pi directly?” Actually, we tried that first, but it seems that Sonic Pi can only communicate with OSC in a  local host (network within the PC), so we had to throw in the Processing in between.

Program for the Processing server

The Rhythm Machine is Done

Once we’ve written the programs, run them, and we’re done! In this program, each toggle switch can turn on/off bass drum, and we assigned the rhythm speed for the volume resistor. By changing this program, you can use it as another instrument aside from a rhythm machine.

※ For more information about Sonic Pi programs or how to use them, check out Audio programming with Sonic Pi!

  • Turn the switch ON to trigger the bass drum sound
  • Turn all of it ON to play the sound continuously
  • The tempo changes with the volume knob

Try it out, it should work like this.

Summary

In the second part, we applied our knowledge about the OSC and created a musical instrument device. When creating your own device, you can choose where to place the knobs and buttons, and what each of them transmits. You can create controllers that control more than just sounds. You can program it to control videos simultaneously, or use ultrasonic wave sensors or a photo reflector to create a contactless Theremin-like controller. If you’re a musician, it may be interesting to create your own instrument and perform on stage.