Thursday, 7 November 2013

Pure drumming

All too often I find ways to distract myself when I've intended to do some guitar practice. Recently I thought it would be nice to create some drum tracks to play with. "No problem!" You say, "Use Hydrogen." This is a start, but what if you don't know much about drumming and find clicking beats on a track comes out a bit flat? The first thing to come to mind was to try some MIDI pads, but that seemed like overkill, especially when something with buttons on it was much closer to hand—my Xbox 360 controller.

There are drawbacks to doing this. For one thing proper MIDI pads or keyboards can sense velocity (how hard you hit the keys) which can be used to control volume for example. And the ideal layout for battling alien invasions isn't necessarily the one you'd want to write the next Four Sticks. But if you've got some kind of gamepad lying about that you want to transform into a miniature drum kit then it's certainly possible.
The trick is that most synths, including drumkits like Hydrogen, talk MIDI. Since it stands for "Musical Instrument Digital Interface" you can probably guess that most gamepads do not. Gamepads usually use a protocol called USB-HID "Human Interface Device". But since these are all just 0s and 1s shuffling around your computer we should be able to find something that can redirect them from one to the other. The something I used is called Pure Data.
Pure Data is a "data flow programming language", in simple terms you draw lines between things you want to connect. Once you've got a "patch" (graph) you like that's the program and you just run it.
Pure Data isn't part of normal Fedora, but you can get it easily by following the instructions at Planet CCRMA (pronounced 'karma'). Just follow the "Adding the planet CCRMA repositories." instructions, then do
yum install pd-extended
to get Pure Data, this is actually one of their examples. (CCRMA is the Stanford University Centre for Computer Research in Music and Acoustics.)
Then it's just "pd" to run it, or start it from the menu.

Getting graphic

It's possible to just load the patch and run it, but read on if you want to know what it's doing or how to adjust it.
Things aren't quite as simple as joining a 'X-Box' box to a 'Hydrogen' box, mainly because we need to say what we want the buttons to do. This is the whole patch:
And this is the first bit that connects to the 360 controller (or any USB HID controller). The "HID" part is the magic box that receives gamepad instructions, the rest of the controls are to set it up: with the patch running you can click "print" to get info on the available controllers, then use the radio control to select the controller you want (mine is number 4) and the checkbox on the left to start reading. (The radio control here isn't a plain control, it's a copy of the one from the HID help information.)
...and this is the end:
"makenote" generates midi notes and "noteout" sends them into the world. The two numbers are "velocity" (100 out of 127) and duration (10ms, which Hydrogen will ignore).
What 'note' is played goes in at the left hand side. The 'print' writes text to the PD console whenever a note plays. Hydrogen will interpret this as which sample to play, which is where the nest in the middle comes in:
'route key' sorts out the key presses that arrive from the controller from other events (like joysticks). Which 'btn_' is which can be found by clicking the top left print with the controller running: each 'route btn_X' is picking up a particular button and the 'sel 1' means it only triggers on pressing, not when releasing. The numbers on the right are notes. Each one maps to a slot in the Hydrogen dumkit starting at 36 for the first slot (the kick drum in the normal kit). With the default kit this means:
• A - Tom low
• B - Snare Jazz
• X - Closed HH
• Y - Snare rock
• Left shoulder - Crash
• Top right - Stick
• Back - Tom Mid
• Start - Pedal HH
• Big-X - Kick
If you want to know more about Pure Data you can check out the website for more information and mailing lists, but one of the most useful help sources is hidden in plain sight. If you right click any object (box) you get a menu,  click help and a new patch window will open demonstrating all the things you can do with that object. The HID help window is where I found the augmented radio control needed to make an input selector.

Bash it out

Load PD and Hydrogen (and Jack if you're using it). Since Hydrogen uses the ALSA MIDI system set the PD output to ALSA too (the "Media" menu). If you're using QJackCtl to manage Jack you can also look at the ALSA MIDI tab to check PD and Hydrogen have connected.
Then just load the patch in PD and set it running by toggling edit mode (Ctrl-e, or menu, Edit|Edit mode). Click the top left print to check which input number you need, use the radio box to select it and then click the checkbox if it's empty.
Things not working?
• Check note numbers are coming up in the PD console when you press buttons (that's the text window that launches when you run PD). If not you might have a problem with the HID input, look carefully at the 'print' information again.
• If note numbers are coming up, check Hydrogen is working by clicking on a drum name in the main window, that should play a sample. If it doesn't then Hydrogen isn't able to play sound properly. If it does there might be a MIDI connection problem, check the MIDI-in light at the top of the Hydrogen window flashes when you press a button. If it doesn't then MIDI signals aren't being received.

Beat a drum?

This certainly works and is quite fun to play with. If nothing else it was a good way to find out whether it's worth going ahead and getting some proper MIDI controllers for this.
For those on Windows or Mac, PD-extended and Hydrogen are both available for these, so you should be able to get this working there too.