Secret Rabbit Code Upsampling – Digital Audio Done Right!

What if cd replayWelcome.

In this article I would like to publicise a feature of Linux audio that I have not seen mentioned anywhere, but to me is completely amazing! I found this while researching the Linux sound system and I think it should be widely known as it could lead to generally higher quality audio and possibly even reduce some hearing related problems.

Anyway, bear with me and I’ll show you how you can make your audio become more natural and musically satisfying. In fact, this principle can be extended to any digital audio replay mechanism, as long as it incorporates a high-res digital-to-analogue converter (DAC).

Who?

Inevitably, not everyone will find this useful, but I’m hoping that some will be as impressed as I have been. I think this should be of great benefit to audiophiles and that this technology should work itself into any CD player – but we’ll see why this might be a bit of a problem. To appreciate this fully, you really need a good quality hi-fi stereo on which you can distinguish between HD audio and CD quality. I’m afraid that there is unlikely to be much difference with cheap earbuds. It is possible to tell the difference on a laptop and I can get significantly more enjoyment on mine, but I do use reasonable headphones (Sennheiser HD25s are a great choice).

I have done tests with a number of people and pretty well everyone is able to tell the difference on the hi-fi, but this hasn’t been true of the laptop. It may be more obvious to me because I have a little hearing loss and have become quite sensitive to distortion in digital music. I grew up with vinyl and have always been searching for ways to get back to a cleaner sound, and now this is it!

This may not be useful for those with more profound hearing issues and there may even be those who have become accustomed to the sound of digital distortion and resent when it is gone! Also, you can’t please everyone and there may be those who just don’t like it. However, I’m not trying to sell anything and am genuinely just trying to spread the word.

What?

Sine1So what is actually happening here? You cannot create something out of nothing, so this is not a way to create information that is not present on the CD.  I will go into the theory in more depth later on, but in a nutshell, all music is composed of sine waves (Fourier Analysis can reveal these). When music is sampled for CD, it is sampled 44,100 times a second (the sampling frequency). The reason for this comes from Shannon’s sampling theorem which states that to recreate a particular frequency, you need to sample it at a bit more than twice that frequency. The range of human hearing is normally given as 20 to 20,000 Hz (cycles per second), so twice 20,000 is 40,000 and 44,100 is a bit more so there you go. The big problem with this is it does not tell you how to recreate the original frequency and that is where things start to go wrong.

Essentially, a single frequency is a continuously changing waveform with no breaks in it (discontinuities). However, this is exactly what digital music is not. If a cycle of a sine wave is at 20 Hz, the 44,100 / 20 sample points along the wave can track it very accurately. At 20,000 Hz, you will only get two or three data points in each cycle. These will result in distortion as the DAC just outputs a voltage for each data point rather than recreating the sine wave.

The way to fix this is to recreate the original waveform more accurately – we have enough information due to Shannon’s theorem, but how do we do it? The answer is to upsample using a sinc based algorithm. There is a linux library libsamplerate from the fantastically named mega-nerd.com (which also has a nice introduction to computer audio) that does just this. The library is also known as Secret Rabbit Code, having an acronym of SRC as that is the normal acronym used for Sample Rate Conversion programs.

Upsampling is the process of converting to a higher sampling rate. In the case I will be discussing here, we are moving from 44,100 Hz to 192,000 Hz. The default upsampling algorithm is not very good, the key here is to use this special version which knows that music is made from sine waves and attempts to find samples on the only possible sine wave implied by those samples. This essentially adds more samples along the curves for each frequency in your music, but it is mainly noticeable at higher frequencies as they have the largest differences when rendered by the DAC.

Requirements

What do you need to get this working? There are many ways, some of which I hope to explore in future articles, but this is what I used to get this working on my laptop:

1) A Linux computer containing the pulseaudio sound system. This is a fantastic piece of software that I am still exploring, sitting between your sound producing programs and the audio hardware. It allows all sorts of manipulations including sample rate conversion using the Secret Rabbit Code libsamplerate library. It is provided on many flavours of Linux including Ubuntu. Configuration is done via the command line and configuration files. I am currently using Kubuntu 14.04 and it is installed by default. If you don’t have it, it can be brought in as a dependency when installing certain music packages.

2) Audio hardware in the computer able to work at a significantly higher rate than CD. I have had success with both 96,000 and 192,000 samples per sec. I found some information about what sample rates (and bit depths) were available by interrogating the /proc virtual directories. However, I think this is particular example is telling me about the codec and not necessarily the hardware. On a different device, I could see that this did match the hardware but with lower values:

martin@martin-U36JC:/proc/asound/card0$ cat codec#0
Codec: Realtek ALC269VB
Address: 0
AFG Function Id: 0x1 (unsol 1)
Vendor Id: 0x10ec0269
Subsystem Id: 0x104310b3
Revision Id: 0x100100
No Modem Function Group found
Default PCM:
    rates [0x560]: 44100 48000 96000 192000
    bits [0xe]: 16 20 24
...

3) A DAC that works at this higher sample rate. This can be the internal DAC when using headphones, or a digital output (SPDIF) to an external DAC, typically feeding into a hi-fi system. There are two types of digital cables commonly in use, coaxial and optical. Laptops often have a hidden optical output in the headphone jack. This is the case for the U36J. You can use special optical cables with this termination or, as I do, use a very cheap adapter and normal optical (TOSLINK) cables:

P1020530_1 P1020528_1

How?

Ok, if you have all the required hardware, and are convinced enough to give this a go, what software settings do you need to use to get this to work? Really this is just two things – set the output to digital audio and then configure pulse audio.

Digital Audio SetupThis is the Audio Hardware Setup of the Audio Setup function, accessible in KDE by right clicking on the volume symbol in the system tray. Here, you can see I’ve selected the Digital Stereo output and it has chosen the SPDIF connector – so connect the adapter into the headphone socket and then the other end to a DAC. Alternatively, just use headphones and set the profile to Analogue Stereo Output or Analogue Stereo Duplex.

The most complicated part is configuring pulseaudio, but even so, this is not hard. There are per user override files, but if you are just on your own, you can just edit /etc/pulse/daemon.conf (I take a backup first in case I mess something up). This is a system file so you will need to edit using sudo or some other similar means. There is a man page at man pulse-daemon.conf explaining the options in more detail, but the settings we are mainly interested in are:

  • resample-method: this is the key to the whole process, it has a default of a speex algorithm which is optimised for efficient transmission of speech. This is fine for general PC use where you don’t want to clog the bandwidth up, but not for us! There are three options to choose from: src-sinc-best-quality, src-sinc-medium-quality and src-sinc-fastest. Which one you go for depends on your hardware capabilities, but I have experimented with all three and there are noticeable improvements between each one. You can tell if your hardware can’t cope as the audio fails catastrophically. On the U36J laptop, I am able to run the medium quality algorithm. The fastest algorithm (least cpu) gives excellent results too. The best quality is just that but uses up more cpu.
  • default-sample-rate: this must be changed. Use the highest value your DAC can handle. For headphones I use 192000 but my external DAC can only do 96000. Even that level, my hi-fi DAC is still much better than the, presumably cheap, laptop DAC. If you do not raise this setting above 44100, you are not upsampling!
  • default-sample-format: this may not need to change. It controls the bit depth. This is how much data is used to represent each sample. The default for CD is s16le which means stereo, 16 bits per sample, little endian. This controls the dynamic range of the music (how loud and soft it can go). This figure is fine for our purposes as you can’t increase the information here, once the music has been encoded as CD, any greater dynamic range is lost. I set this to the lower of the audio processing system in the computer and the DAC’s values. For listening with headphones directly, use the same as the computer’s hardware which here is 24 bit. Two useful other values are s24le which is 24 bit and s32le which is a floating point 24 bit where the values can go above 100% without clipping. Resampling can produce values above 100% resulting in possible distortion.
  • realtime-scheduling and realtime-priority: your mileage may vary with these. I prefer with this left at default, which is on / 5. These should theoretically make things better as the audio subsystem needs to deliver samples at exactly the right time to control jitter.

For each setting you wish to change, remove the preceeding comment (“;”) to enable it.

To bounce pulseaudio (in order to pick up configuration changes), you can use these commands:

pulseaudio -k
pulseaudio --start

Even so, you may find changes not being picked up correctly. I have had quite a few issues here, one thing that can help a bit is to turn off the autospawn setting in /etc/pulse/client.conf. Then other things complain though. You may have to also bounce any music playing software that is running.

Watch out if you also want to use the computer for voice / video calls as high res audio settings thrash the connection and you will lose friends. I have set up a mechanism to be able to swap between settings.

Of course you now need a digital source, preferably of CD quality. This could be an external CD player (my laptop has no built in drive), a download or just a file you’ve transferred from elsewhere. My recommendation is to use EAC (Windows only, sadly) and rip to flac.

So how does it sound?

Hearing is subjective but as there are unlikely to be many who enjoy the sound of a road drill, I can point to some general improvements which hopefully others will be able to confirm. This moves digital audio back towards the sound of good vinyl, while remaining practical (aside – good vinyl can still be significantly better, but that is heavily dependent on the pressing and replay).

The main improvements I hear are:

  • A reduction in harshness; the better the resampling algorithm, the less harsh the music. This is a big deal for me as I was seriously considering changing my amplifier as CD was becoming too painful. Even the lowest quality algorithm is very good.
  • Openness and clarity in the high end. As expected from the theory, high frequencies benefit most. Cymbals now sound like cymbals rather than splashes of aggression. Drums have significant HF content and are an obviously improved area. Brass instruments can now sing smoothly, orchestral music can shine.
  • General improvements in tone and therefore naturalness in music reproduction. Almost all instruments have high frequency components to their sound, even when not playing high pitched notes (Fourier analysis again) , just look at a spectrum analyser such as a graphic equaliser to see that there are high frequencies everywhere. Some instruments come out particularly badly in digital and this helps.

How does this compare to HD audio such as that provided by HDtracks.com? Very well actually, it seems like the extra bit depth is not anywhere as important as the sample rate is. I guess the recordings there are taken from the original masters and re-sampled at higher resolutions. A lot of the catalogue is typical hi-fi reissue material and to get the quality improvement, you have to purchase them again. I have some HD recordings so it is a subject that I would like to look into more deeply.

The sinc upsampling technique here works on existing digitally created material. There is no extra cost.

Finally

My goal is for others to try this and release the music already contained in their existing collections. These improvements, for me, give greater enjoyment. I just wonder why I have not heard of this before? Is it because of the different worlds of professional / hi-fi audio, Linux and signal processing? If this was well known, why would it not be standard? I really hope that this does get universally adopted so that average music replay improves. That said, there may be processing limitations to this due to the greater expense of running the algorithm.

I did a quick search to see if there was anything equivalent available on Windows, but I couldn’t see anything. SRC has a foobar2000 plugin but it was never updated and is no longer compatible (the blog has a nice rant about it). I would genuinely like to thank Erik de Castro Lopo, the author of SRC for making this open source software.

I’d also like to thank Prof Sergio Velastin and Dr Chris Bore of Kingston Uni for teaching me about DSP, enabling me to know what to look out for here. Happy birthday Chris!

I am hoping to do a few more articles on related topics such as more theory and different replay mechanisms. Please let me know if you get any joy using this technique and also any albums that come across particularly well. Good luck!

 

%d bloggers like this: