Even mid-range systems were £300 per room.

Read more Raspberry Pi stories on Electronics Weekly »

Raspberry Pi has changed all that, and now the only qualification for such a multi-room audio system is the ability to follow step-by-step instructions – and maybe £100 per room for good sound through existing loudspeakers, and under ~£30 if you have an old set of computer speakers lying around and are really not fussed about quality.

Even though they are credit card sized, some Raspberry Pi hi-fi add-ons include Class-D power amplifiers delivering tens of watts. And it is not only makers that turn to Raspberry Pi, there are professional bespoke systems out there constructed around the little educational computer.

Hardware

Raspberry Pi has had PWM-based on-board audio output capability since it was first released. It is great for the computer’s educational remit, perfect for beeps and tones, and will play music through active PC loudspeakers, but will never set the audio world on fire.

Almost as soon as the audio output limitations were noticed, members of the open-source community looked for ways – both hardware and software – to add external music-quality DACs.

Initially, these had to be USB DACs because that was the only available interface, but the first major revision of Raspberry Pi’s PCB bought the host chip’s existing I2S audio bus out to an 8pin header (P5), and these connections moved to the main 40pin I/O when it replaced the original 26pin I/O connector when the ‘plus’ and subsequent ‘2’ Raspberry Pis emerged.

Invented by Philips, I2S is a three-signal (clock, data and left/right) serial interface dedicated to digital stereo. It is agnostic with respect to sample rate or sample word length and was intended as a way to send data to dumb two-channel DACs. Provided the word length (usually 16bit) matches, the DAC will play at whatever sample rate data is sent.

There are also smart audio chips that take I2S as input and emit analogue audio. These generally need a separate bus connection to set them in the right mode to function like a dumb DAC.

Several companies have taken DACs and built them into audio cards that sit on top of Raspberry Pi and connect to the I2S bus.

Amongst them are cards from Element14/Cirrus/Wolfson, Modul 9 (HiFiBerry) of Switzerland and IQaudIO.

Gordon Garrity is founder of IQaudIO, which both designs and makes its Hi-Fi audio boards in the UK.

“People can plug in USB sound card, and there are good ones for under £200, but the [Raspberry Pi] USB chip shares a bus with Ethernet and an overloaded bus was a problem with original Pi for HD audio,” he said. “This should be less of a problem with the quad core [Raspberry Pi 2].”

Another issue, according to Garrity, is that a lot of USB sound cards cannot convert faster than 96kbit/s as this is the maximum for Windows drivers. Expensive USB DACs have 192kbit/s chipsets and by-pass the Windows drivers. Linux and Macs will handle these 192kbit/s files and DACs.

And they protrude outside Pi’s svelte credit card profile.

All of which is why his company uses the I2S connection.

Getting clean sound when bolted to a computer can be tricky – Raspberry Pi is quite noisy, for example.

“We try to lay boards out to get the best audio we can,” said Garrity. “The PCB needs a good ground plane, and we have a lot more components than some audio cards to stopping us from being susceptible. There are some on the headphone output, and some stopping conducted noise.

Grounding is another issue that has had careful attention to avoid loops – made more complex from the Pi ‘+’ versions onwards because the computer can be powered directly or powered from its add-on board.

And however careful you are trying to be, some things will still show up if you have an audio analyser to hand.

“RaspPi is the I2S master and its clock is not brilliant. There is a bit of jitter, and you are not going to get jitter-free with a sub-£100 device,” said Garrity. “The Raspberry Pi market cannot sustain a £150 selling price and the target price has to be as close to the Raspberry Pi price as possible.”

Farnell (Cirrus/Wolfson), IQaudIO and HiFiBerry sell DAC add-on boards, with IQaudIO and HiFiBerry also selling single boards that combine a DAC and Class-D amplifier delivering over 20W/channel stereo. Some of these have headphone outputs as well.

Farnell’s offerings include a 2+2W power amp, and differ from the others in having high-quality audio input ADCs, mems microphones and sophisticated mixers, plus SPDIF digital interfaces.

Low-level software

None of these audio products can be usefully attached to a Raspberry Pi without some modification to the Pi’s software. Even a dumb DAC can need a minimal driver, just to say it has been attached. More complex DAC hardware may need data loaded into internal registers to set it into the appropriate mode – I2C bus is a favourite amongst chip makers for this, and Raspberry Pi’s Broadcom chip has an I2C bus, brought out onto the 26/40 way connector.

The Cirrus/Wolfson boards are supplied with scripts to run which set them into various modes via I2C. IQaudio hardware also uses I2C to configure the DACs, with the necessary I2C device drivers built into the Raspberry Pi Linux release (Raspbian).

Deep within the Linux Kernel is an audio subsystem called ALSA (Advanced Linux Sound Architecture which can receive and send on multiple channels of digital music and mix between them.

We have German student Florian Meier to thank for working out how to enable the I2S hardware within the Broadcom chip at the heart of Raspberry Pi, and get it to work with ALSA. His code has been in the Linux main line kernel since version 3.14.

There is also something called ASoC (ALSA System on a Chip), which is a subsystem that provides a framework for audio in embedded systems running Linux.

Other people have created device drivers for specific chips. According to Garrity there are some 100 chip drivers shipped along with Linux, some of them supplied by Edinburgh-based Wolfson, now absorbed into Cirrus logic.

IQaudIO uses a Texas Instruments audio DAC/codec in its Pi-DAC+ product. “We created a driver for the TI PCM5122. I wrote a proof of concept driver and Mark Brown [ex Wolfson] and Florian generously created a full device driver [PCM5122x.c],” said Garrity. “As it’s open source, others have continue to add new features and improve the quality.” Pi-DAC+ has line-level and headphone outputs.

IQaudIO’s 20W+20W DAC/amplifier called Pi-DigiAMP+ uses the same driver because it is based around TI’s TAS5756m which shares a register configuration with the PCM5122 – it is functionally equivalent to a 5122 followed by a TPA3118 Class-D amplifier. As it happens, IQaudIO also uses the 3118 in a product called Pi-AMP+ which fits over the Pi-DAC+ to add loudspeaker driving capability without losing line out and headphone support.

“The Raspberry Pi went through changes in January. We wanted to make sure our device drivers were in the standard kernel, then you only need to configure the through a text file,” said Garrity – whose boards are the ones that can be found built into Abacus Electronic’s bespoke audio products in Germany.

Raspbian

Having a Pi, and audio board, a kernel and drivers means you have a computer capable of outputting sound, but the computer has no idea what file to play and where to get it from.

If you are just playing music while using a Pi as a desktop computer, Raspbian with suitable drivers will do the job.

In general, most drivers for the various audio add-on boards for Raspberry Pi are supplied as part of the Raspbian operating system, but not for all.

This gives you two choices: add the drivers to Raspbian or download a pre-modified SD Card image from the board maker – in this last case, you have to keep your fingers crossed that the board maker is updating their image with each new Raspbian release.

GitHub is your friend for obtaining compile-it-yourself drivers. And some of the manufacturers have step-by-step instructions to walk you through the compilation process.

Be warned, executing a sudo apt-get update is likely to remove all the customisation.

The Raspberry Pi Foundation included an EEPROM in the specification of the latest generation of add-on boards (HATs – hardware attached on top), that will one day mean updates will automatically retrieve packages suitable for attached hardware.

“We have the EEPROM on our boards, but it is blank at moment. The Foundation pretty much has it all worked out now. It is not something that is blocking anything.”

Special music playing software

Up to this point, the computer is desktop PC-like, with hardware set up through the command line.

Raspberry Pi comes into its own as a music player when pre-compiled custom Linux media players are installed – see the list.

These have been created by people from the open-source community and can include all the features expected from an advance sound system – playback of local and network music files, streaming from on-line services like Spotify, Internet radio, remote control from phones and tablets, and ‘headless’ playback where the Pi is a network black box with loudspeakers but no local keyboard or display. Some software solutions even emulate other manufacturer’s consumer electronic solutions such as Apple Airplay and Logitech’s squeezebox. Take a look at the list at the end of this article.

Frequently in these cases, setting up for the appropriate DAC hardware is just a case of selecting it on a drop-down menu.

Hardware makers and software creators know each other.

“A bunch of people around the world are writing nice interfaces for audio play-back,” said Garrity. “The software providers are not monitising – it’s free to download. They are doing something to help me, so I provide them with hardware free-of-charge, and we support the guys who take donations.”

Electronics Weekly just happened to have a Raspberry Pi Model B (P5 connector version) and a compatible Wolfson Audio card from element14 in its sample cupboard.After downloading suitably-modified Raspbian from element 14’s website and programming an SD Card, it only took a matter of minutes to get it working – you have to run a supplied configuration script on first use.Included in the download were some HD audio files, and these sounded great through a fair amplifier and a nice pair of speakers – clear, sparkling, no hum, no hiss. On-line reports suggest IQaudIO and HiFiBerry products also sound great, and that people have underestimated just how good Raspberry Pi can sound with the right add-on.