Thursday, February 12, 2009

Ubuntu ... cola!

I have come across this cola in a café called Barista on my trip to Uppsala (Sweden) last week. (Check out their web site, it has a live web cam of their coffee machine)



Being the nerd that I (sometimes) am, I bought a couple. It is actually a pretty decent cola, but at 26 SEK per can it is pricy as well. However, since it's a fair trade product, you can actually feel well for paying too much (or maybe the right amount?) for a cola.

Wednesday, February 11, 2009

Sun SPOT Air Guitar source code

So here's the source code to the air guitar, along with some instructions. For those who are already familiar with the Sun SPOTs, there will be no surprises here -- the software is fairly trivial. Ok, here we go ...

What you need:
  • Two Sun SPOTs
  • One basestation Sun SPOT
  • A host computer (I tried on Windows PC, should work on Linux or Mac as well, but I haven't tested.)
  • A working Sun SPOT development environment
  • A working Java development environment
  • A MIDI output. This can be anything from the Windows software synth to Reason

How it works:
  • One spot is going to be your "pick", which you use to strum. Make sure its accelerometer is calibrated correctly!
  • One spot is going to be the "neck". You will use it to change the chord you're playing.
  • The basestation spot is going to be the "pickup". It will dump any radio messages to its serial output.
  • A piece of software called the "amp" runs on the computer. It reads from a serial input and creates the appropriate MIDI events.
The sources are available here: air-guitar-090126.zip. Download the zip file and extract its content to a directory of your choice.


Installing the software on the spots
First, we're installing the pick. Connect the spot you will be using as pick to your computer, change into the Pick/ directory and run ant deploy. The spot will be reflashed.
When you turn on the pick spot, it is very important that its XY-plane is parallel to your body and that the Y-axis is parallel to the floor. After pressing the power button, hold the spot in this position until the middle LED starts fading. (This is required because the rest offset is computed after the spot is turned on. All further calculations of acceleration are performed with respect to this rest offset. If the offset is calculated incorrectly, the spot will not be able to correctly detect up and down strokes.)

The next thing to install is the neck. Connect the spot you will be using as neck to your computer, change into the Neck/ directory and run ant deploy. Make sure you select the right spot! Having two picks or two necks is not especially useful ;)

Ok, now we will need the pickup, which is the basestation spot. It will listen on the radio channel and simply dump everything it gets to the USB port. Connect it to your computer, change into the Pickup/ directory and run ant deploy. Restart all spots.


Making sure things work
Before we start to rock, let's make sure the spots are setup properly. Connect the basestation node (pickup), change into the Pickup/ directory and run ant echo. This will dump any serial output from the pickup.
Take the neck spot and try pressing one of the user buttons. If you press the left button, the message "up" should be printed. If you press the right button, it should say "down".
Now take the pick spot. Move it as if you were strumming a guitar (holding it in parallel to your body, as explained above). You should see a message saying "stroke" whenever you strum.

Ok, if everything worked out, let's hook things up to some MIDI synthesizer.


Creating MIDI output
The amp software runs on the computer. It reads from the USB and plays out MIDI events. As a command line argument, it takes the USB device (i.e. the basestation spot) to read from. Under Windows, this should be something like COM3, under Linux it's something like /dev/ttyACM0.
Compile the amp software and start it. It is very important that you have the rxtx-library on your classpath. If not, you can yank it from the spot SDK. Make sure to provide it with the right USB device to read from. Whenever it get's a "stroke" message from the basestation spot, it will send a MIDI event. Go ahead and try by strumming the Pick spot. If you're on Windows, you should hear the built-in Windows software synth playing a chord on a piano.
You can change the chord to be played using the buttons of the neck spot, as explained above.
If you press the rightbutton on the pick spot, the MIDI channel will be increased by one, thus allowing you to switch instruments.
(Chords and the maximum number of channels can be changed in the amp sources. They're hardcoded for now.)


Routing MIDI to another program (under Windows)
Ok, this is still a bit boring, because the Windows software synth isn't actually that much fun. To redirect MIDI output to another program like Reason for example, you need a piece of software called MIDI Yoke. Download and install it (you might need to restart your computer). Then, go to the Windows control panel and open the "Sounds and Audio devices" dialog. Select the "Audio" tab. There, change the default device for MIDI music playback to "Out to MIDI Yoke: 1".
Now that you've changed the default MIDI output device, you need to setup the receiving application. For Reason 4, do as follows: Start up Reason and go to Edit -> Preferences -> Advanced Control. Make sure "In From MIDI Yoke: 1" is selected as Bus A. Then create a new empty rack. Add some instrument of your choice. For starters, an NN19 with a guitar preset loaded is nice. Now, on the topmost device of the rack (I don't know what it's called), press the button "Adv. MIDI device". Then assign Channel 1 to the device you have just created.
At last, start the amp software as before and start playing ...


That concludes setting up the air guitar. Enjoy!

I am eager to see your modifications of the code! Drop me a note if you implement new features. There is a lot of room for improvement :)
It would be nice to write a hack for Frets On Fire to use the spots to play.