Welcome to Freewebmaster.fr
Home > Hardware > RaspiDrums
RaspiDrums
Abstract
In a previous article, I showed that accelerometers can be used to turn a Raspberry Pi into a Rhythm coach, or a Drumometer. But let's be serious now! I'm going to show you how to turn your Raspberry Pi into a drum kit. This is still a work in progress, but I got some really promising results with my "RaspiDrums". Note that the software works on any device that has linux, and an sensor input -- even the mic input of your laptop! I'm just showing how to develop the drum module, as I don't need any drums because my drum kit is a Pearl Rhythm Traveler with muffle heads.

Introduction

As it was mentioned in my previous article, using accelerometers as drum triggers is deadly easy. So why not pushing it further, and try to develop a full drum kit? A few months have passed since I first had the idea of turning my Raspberry Pi into a drum kit, and because I wanted to do something great, I had to learn how to use C++11. That was really helpful, and I think that the project wouldn't be that good if I had used C. In that project, the software part is clearly the heart of the drum module. It's ensuring the communication between an ADC, connected to an accelerometer, and the Raspberry Pi. Then, the data is processed and sent to the soundcard. It sounds simple, and that's the whole point! It is simple. And it's also entirely reconfigurable, which means that you can change the drums sounds, and all sort of parameters to personalise your drum kit. But before I go into more details, I will describe you the hardware, and show you what you need to make your own RaspiDrums.

Presentation of the hardware

General overview

The figure 2.1 shows how the thing works.
diagram
How the RaspiDrums work!
As you hit the drums, an analogue accelerometer transforms the vibrations of the drum head into an electric signal. This signal is then converted by the ADC in a sequence of bits that depends of the acceleration measured by the accelerometer. The Raspberry Pi reads those bits, and converts them into a value that is proportional to the drum head's acceleration. Thus, the stronger you hit the drum, the higher this number is. This value is used to set the volume of the drum sound, which is sent to the soundcard, and ultimately to your headphones.

Hardware required

So, according to what we just said, here is what you need: Attention: the choice of the accelerometer is important, please read next section to understand why! The external soundcard isn't really necessary, but the audio output of the raspi is really bad, so that's up to you. Anyway, if you choose an external soundcard, don't forget to make sure that it is compatible with your Raspberry Pi.

Accelerometer + ADC

As you should know if you read my previous project, the Rhythm coach, accelerometers have a lot of advantages to be used as drum triggers. The most important one is that they don't need any signal conditioning, the output is always very well controlled. Another advantage is that they often come soldered to a breakout board, which makes things a lot easier. Since the Raspberry Pi has only one Spi bus, I decided to use an analogue accelerometer connected to an analogue to digital converter (ADC). The MCP3204 that I used has 4 inputs, so we can connect up to four drums to the Pi. It is very important to make sure that the accelerometer can take a really high acceleration, as the drummer can reach ±200g very easily.

Usb soundcard

Whether you want to use an external soundcard or not is up to you. It will definitely add some latency, but the integrated soundcard of the raspberry pi is really, really bad... That's unfortunate, but at least, it has a soundcard. I used an Aureon Dual Usb from Terratec, which is compatible with the Pi and has a good audio quality. Further information will be given later, especially about the alsa configuration.

Wiring the whole thing

Here we are, if you've got all the components, you can now start to wire your RaspiDrums. The only tricky part is to wire the ADC to the Pi, so if you need some help, feel free to use the following schematic:
RaspiDrums Schematic
This figure shows how to connect the Raspberry Pi to the ADC.

Description of the software

As you might guess, once all the components are correctly wired together, the most important thing is to get a good software. I spent some time to make the software flexible, which is why it had to be C++11. You can download the full Eclipse project here: RaspiDrums.zip. Please read the following sections to understand how to use the software properly.

How it works

The RaspiDrums software uses the Alsa native driver to play the drums' sounds. All the configuration files are located in the Data folder. You can configure how the software behaves with regards to Alsa by changing the contents of the alsaConfig.xml file. Be careful though, wrong parameters can cause an unexpected behaviour of the software, i.e. no sound, crash... Here is a example of what that file may contain:
<root> <device>default</device> <capture>0</capture> <format>SND_PCM_FORMAT_S16_LE</format> <sampleRate>48000</sampleRate> <nChannels>1</nChannels> <bufferTime>10000</bufferTime> <periodTime>5000</periodTime> <access>SND_PCM_ACCESS_RW_INTERLEAVED</access> </root>

device
The device is used by Alsa to determine which soundcard to use. I recommend you keep default, but you can also try plughw:0,0, or hw:0,0.
capture
When capture is 0, Alsa is in playback mode. When it's set to 1, Alsa uses your microphone to record sounds.
format
The sound format should be SND_PCM_FORMAT_S16_LE. If you change that, you have to change the format of all your sound samples.
sampleRate
You can choose any sample rate, as long as it is supported by you soundcard. The default is 48kHz, but you can choose 44.1kHz if you want. Note that you have to record your sample sounds at the same rate, as there will be a pitch change if you don't.
nChannels
Right now the software only works with one channel (mono), but it will be upgraded to handle stereo tracks.
bufferTime
The buffer time determine the size of the buffer used by Alsa to transfer the data to your soundcard. You can try different values, but increasing it will lead to more latency, and decreasing it could lead a bad sound quality.
periodTime
The period time has to be at least half the buffer time. That's actually the best value, but you can choose any fraction of the buffer time.
access
Finally, the access determines how Alsa accesses you soundcard. The default is SND_PCM_ACCESS_RW_INTERLEAVED, and that's the only supported access type at the moment (maybe I'll add mmap as well).

In the Data folder, there is a sub-folder named Kits. That folder contains some xml files and sub-folders. Every xml file contains the configuration of a drum kit, and is associated to a folder that contains all the sound samples of that kit. Here is an example of drum kit configuration:

<root> <kitName>Default</kitName> <kitFolder>default</kitFolder> <snareDrum> <drumName>SnareDrum</drumName> <soundFile>snare.raw</soundFile> <threshold>204</threshold> <scanTime>4000</scanTime> <maskTime>40000</maskTime> <curve>exponential</curve> </snareDrum> <snareDrum2> <drumName>SnareDrum2</drumName> <soundFile>snare.raw</soundFile> <threshold>204</threshold> <scanTime>4000</scanTime> <maskTime>40000</maskTime> <curve>exponential</curve> </snareDrum2> </root>

kitName
The first element contains the kit name, of course, it can be any name of your choice.
kifFolder
This element contains the name of the folder in which your store your sound samples. Make sure that name is correct!
After these two elements, you have to add the tags that contain the drums. Each drum contains 6 sub-elements that determine its properties. Below is an explanation of the aim of each of those sub-tags.
drumName
This element contains the name of the drum.
soundFile
Contains the file name (including extension) of the raw sound file. E.g.: snare.raw refers to the file kifFolder/snare.raw.
threshold
This is the minimum value for which you decide that the drum has to be triggered. The ADC I use goes from 0 to 4095 (12 bits). A threshold of 5% is a good value, so I usually use something close to 200. In a future release, this value will be normalised.
scanTime
The scan time is the duration, in microseconds, for which the software looks for the maximum value of the sensor's output. That allows to adjust the volume of the output sound with respects to the drum head's peak acceleration.
maskTime
The mask time is the duration, in microseconds, during which the software ignores the sensor's output. That is used to avoid double triggering.
curve
Curves are used to modify the response of the drums. Instead of having a linear response, curves allow to apply a function to the acceleration in order to modify the drum sound's volume. For example, here is how the exponential curve modifies the volume of the drum:
Non-linear sound response
Non-linear response of the drum sound.
More curves are coming in the future releases.

Things that you need

At the end of this article, you will find the full software, including the binaries, in a zip file. You may want to run it as is, however, you may also want to make some changes. To that end, you will need to compile the software yourself. Also, if you want to use a usb soundcard, you will need to sightly change Alsa's configuration. The following sections explain how to do all those things.

Install the bcm2835 library

Since the software uses the bcm2835 library to read the values from the ADC, you will need to install it. Unfortunately there is no package in the raspbian repository for that library, so you have to download it from its website. Don't worry, it's really easy. You can find the library at this address: airspayce.com. Download the .tar.gz file and, as indicated on the website, do the following:
# download the latest version of the library, say bcm2835-1.xx.tar.gz, then: tar zxvf bcm2835-1.xx.tar.gz cd bcm2835-1.xx ./configure make sudo make check sudo make install

And that's it, the lib is installed!

Install the dependencies

Fortunately, the other dependencies are part of the raspbian distribution. It's really easy to install them, just type this command into a terminal:
sudo apt-get install libasound2-dev libxml2-dev

And here you go, if you're not using an external soundcard, you can use the software now.

Configure Alsa

If you do use a usb soundcard, you need to do one more thing to run the RaspiDrums software. By default, Asa uses the soundcard of the raspberry pi, so we need to change its configuration to use a usb soundcard. To that end, you need to edit Alsa's configuration file. Just type this in a terminal:
sudo nano /etc/modprobe.d/alsa-base.conf

Then, the only thing you need to do is to locate the line that says options snd-usb-audio index=-2 and replace it by # options snd-usb-audio index=-2 (you just need to add an # at the beginning of the line). Next time you boot you rapsberry pi, it will use your usb soundcard (if it's plugged in at the boot time).

Create your own sound samples

Now that you can use the software and even compile it yourself, it would be a good thing for you to know how to add new sounds to your drum kits. So, as mentioned before, you can create new drum kits by adding an xml file and a directory in the Data/Kits folder. The xml file contains the location of your drums sounds, however, those sounds have to be saved in a specific format. The video below shows you how to convert sounds to that format using Audacity:

Get the code!

Of the course, if you want to see the beast in action, the best is to try it yourself. You can download the Eclipse project here: RaspiDrums.zip. It contains the source files, the git folder (you may need Eclipse's git plugin), the binaries, and a default drum kit. The binaries have been built on a Raspberry Pi 2 (it took 1 minute to build the release, so if you try to build on a B or B+, it might take a few minutes). Note that this software also runs a Raspberry Pi B and B+ (not tested on A and A+). Don't forget that you need to start the software in root mode, so go to the "Release" directory, and type: sudo ./RaspiDrums. The default drum kit only has two snare drums, but I haven't tested the software with more than one sensor so far. This is part of the future work, but I believe that using more sensors will be quite easy.

Demonstration!

If you don't have all the required hardware, but you want to see how it works, feel free to watch the following video:


As you can see, it is pretty responsive, and the sound quality is really good. It is actually the sound quality of my Roland drum kit, since recorded the sound directly from the module, and used Audacity to convert it in raw.
Note that this software is still under development, and updates are coming, especially to improve the "curve" stuff.
I hope this project is a source of inspiration for you! I will continue with similar projects in the future. My main goal is to reduce the latency, and keep on using raw sound files to get a great sound.