Punching it Up: Low-latency notes

Sometimes you want a really “punchy” sound. To a musician, this means a sound with a rapid attack and a quick reaction from the keyboard. To a software developer, this means a sound with a very low attack rate and a very low latency through the synthesizer from the keyboard to the audio output. To make a punchy sound, we’re going to use an external USB audio card, and also adjust the sound card settings.

What is Latency?

Latency is the delay from when you hit a note on the keyboard until you hear the sound. Musicians deal with latency all the time, because there are audio delays inherent in all of our equipment. Pipe Organ players have long been accustomed to experiencing a delay between the keyboard action and the sounding of a pipe. However,  organists learn to adapt.
If the value of the latency between the keypress and the sound is constant, a musician has a good chance of being able to compensate. If the latency is unpredictable, even a tiny bit, then a musician will have a harder time keeping their music sounding rhythmic. We are going to try to adjust our organ to reduce latency, and also the variance of the latency.

Why not the internal sound device?

The internal sound chip of the Raspberry Pi 3 is good enough for desktop sounds and casual listening to music, but if you want clearer sounds, and lower latency you will want an external USB sound card. The actual experience you have will vary with the sound card you choose. Here at McLaren Labs we use a Yamaha MG-10XU mixer with USB input as an external sound device and it works great.
Read about how we reduced latency and created a “punchy” sound below the break.

Getting Started

We are going to do some experiments using Pi-Organ-Synth, and we’ll listen carefully to the results.  We are trying to create a “punchy” sound.  So, to begin  with, select a harsh sound and set the Attack time to Zero.

The default sound “Organ-Eight” is based on a Saw (making it harsh) and it sets attack time to Zero.  Also notice the gain setting (100) and the “dry” setting in the Reverb section.  The “dry” setting is the amount of sound that does not have the reverb effect that is sent through to the output.  Reverberation introduces delay, but we want to explore low-latency, so we want to hear the “dry” signal as clear as we can.
Make sure as we do these experiments that you always set the “dry” setting to 100!

Try the internal sound card

First, we’re going to select the internal sound card: this is the one called “bcm2835 ALSA”.

Now play an eighth-note figure of some sort.  The organ is responsive enough, but we can do better.

Try a USB sound card

Now, select an external audio device.  It should show up as a USB sound card in the pull-down list.  Just select it.
Pi-Organ-Synth will immediately start using this new sound card.

Now play the eighth-note figure again.  You should notice that the organ feels more responsive.  This is because we have reduced latency through the system.  The USB audio device has better latency characteristics than the internal sound device of the Raspberry Pi.

Adjusting Audio Device Parameters

Beneath the “name:” of the output device are two lines.  The first is labeled “actual:”.  This shows the device parameters currently in effect for the output device.
The line

actual:  44100/1024/2

means that the audio card is running at 44100 samples per second, with a period size of 1024 and 2 periods.  But what do these values mean?
The audio buffer of your sound card is divided into groups of samples called “periods”.  A sample is the amplitude of the output signal at a single instant in time.  We know that our USB sound card generates 44100 samples per second.  These are illustrated as the blue bars in the image below.  A period is a group of samples.

The playing of sounds is a delicate balance between writing samples from our software, and reading the sample by the sound card.  The rule is that no period can be used by both the writer and the reader at the same time.  So it is a constant game of chase where the writer (our organ) is writing samples and the sound card is reading samples.

A smaller period size allows the time between the WRITER and the READER to be decreased, leading to a smaller latency.
In the “Output” portion of Pi-Organ-Synth we have provided choosers for the Sample Rate, the Period size and the total number of periods.  The choosers below this line allow you to select each of these three parameters from a drop-down list.  But to do this, you first need to deselect all audio devices.

Deselect Audio Devices

To deselect the audio device, choose the first entry: it has a “–” symbol in it.

Select a smaller Period Size

Before you select an audio device, you can change the audio device parameters request.   Change the period size to 512 samples.

Now select the USB audio device.
Play your eighth note figure again.  You should notice that your organ is really “punchy” now.  It responds almost immediately to your keyboard playing.

Proceed with Caution

If you like the 512 period size setting, you may want to use it all of the time.  That is OK as long as your system remains responsive.  Audio synthesis can place a large load on your Raspberry Pi.  If you are running many other programs besides Pi-Organ-Synth, then this setting may be too aggressive.  You may also notice even smaller settings.  Smaller values may stress out your system to the point that your computer has a problem.
Pi-Organ-Synth was designed to be easy to use, and to choose sensible and SAFE settings by default.  That’s why the default is 1024 — we have found that setting to be safe on a variety of systems, and adequate for many types of playing.  But for LIVE playing of PUNCHY sounds, you might find a period size of 512 to be better.

Summary

Pi-Organ-Synth makes it easy to experiment with sound cards and their settings.  In this article, we talked about how to use an external USB sound card, and also showed how to select a lower period size to reduce playing latency.  This gave us the “punchy” sound that we were looking for!

Further Reading

JACK Latency Tests:
https://wiki.linuxaudio.org/wiki/jack_latency_tests
Real-time Audio on Embedded Linux:
https://www.slideshare.net/trx2001/audio-in-linux-embedded
PortAudio Implementation Notes:
https://app.assembla.com/wiki/show/portaudio/BufferingLatencyAndTimingImplementationGuidelines

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.