Saturday 19 May 2012

Arduino Drumkit part 3 - a note on velocity control and I/O

While my Arduino is being used elsewhere, I felt this would be a good time to cover one of the more important features of the MIDI protocol - Velocity - and how I intend to implement this into the system.

Velocity is an indication of how hard the instrument (be it piano key, drum pad or guitar string) has been hit. As you'd expect, this is to try and emulate the natural dynamic response of acoustic instruments, which get louder if you play them harder.

So without velocity, what are we left with? A drum kit that has no dynamics. It will just play loud all the time. This might sound like a small price to pay for cost, but it severely limits how convincing an instrument sounds to the listener. As the great John Chowning (inventor of FM synthesis) once said:

          'Many natural sounds seem to have characteristic spectral evolutions which, in addition to providing their 'signature', are largely responsible for what we judge to be their lively quality. In contrast, it is the largely fixed portion spectrum of most synthesized sounds that so readily imparts to the listener the electronic cue and a lifeless quality' *

Although the context is slightly different (he was talking about sythesisers needing more dynamic qualities), the message is the same - dynamic quality is a very good thing. Variety being the spice of life, and whatnot.

The way a lot of samplers deal with velocity is interesting. As anyone who plays an instrument knows, playing and instrument louder or quieter doesnt just change the volume, it generally changes the sound also. Cheaper drum samplers will take one sample, and control the volume of that proportional to the velocity. This doesn't sound particularly good. More expensive samplers, such as Native Instruments' Studio Drummer, Toontrack's Superior Drummer 2.0, have multiple samples per instrument for each velocity level. They record a drum being played at a range of velocities, and map these samples to the MIDI velocity level. This sounds much better.

To this project, velocity control is not essential to the operation of the device, but it is essential to it having a chance of sounding nice and dynamic. The way this will be implemented is by means of peak detection (hopefully written in software to keep costs down!) that will be able to detect what is called 'local maxima'. Local maxima means that say, if a small peak and a big peak were sent one after each other to an input, the device would be able to detect them both. This is opposed to a 'peak value' - a maximum threshold that will trigger once a voltage becomes bigger than the threshold value - which would only be able to detect peaks above a certain level, and would result in all velocity values being the same.

The gradient of the incoming signal can be analysed to detect peaks. Over a peak, this is how the gradients will look:

   ^               g = 0
   |                  /\
   |   g =  +    /    \     g = -
   |              /        \
   |            /            \
  --|-------/               \-------------------->
 (better diagram coming soon!)

To implement this algorithmically, a few samples of the incoming signal can be stored and compared. If the values are increasing, a peak is about to happen. If the values have stopped increasing, a peak is reached. If the values are decreasing, the peak has passed. An algorithm can be written that will analyse the difference between the last 3 or so sampled vales of the input. If it detects an increasing signal, a 'flag' can be set (change a known variable to '1' or something) to denote it is expecting a peak. When the signal starts decreasing, the flag can be set to '0' and the last biggest value of the input can be used as a peak. This value can be mapped to the MIDI standard's 127 levels of velocity. And there you have it! Well, hopefully. This hasn't been implemented yet. If anyone else has got some good ideas about this, I'd love to hear them!


*This paper is freely available - and it's a good read! Check it out here.

No comments:

Post a Comment