This is a journey into sound...

So I decided to try and get some sound out of my Vocore tiny linux box. It doesn’t come with any audio support, so I decided to add a USB sound card. I bought one of these, which I knew worked on the Raspberry Pi, so I knew had Linux support. They’re also pretty small:

I simply followed these instructions describing how to add USB Audio support to OpenWrt. At first, I chose to install sox as the player (I originally intended to use Madplay, but I don’t think it is supported on current versions of OpenWrt anymore).

I did not have much success playing mp3 files straight away, I kept getting sox WARN alsa: under-run messages when I tried to use the following command:

sox myfile.mp3 -d

Although sound was coming out … in chunks, so it was a partial success. But when I added a “-G” parameter to use temporary files (supposed to guard against clipping) like this:

sox myfile.mp3 -d -G

…then it worked fine for some lower quality mp3 files, but with a considerable delay before playing anything out the speaker. If I tried to play some better quality mp3 files then it was still stuttery. So then I started to experiment with reducing the sound quality, which sox can do easily. So I tried this:

sox myfile.mp3 -r 8000 -e unsigned -b 8 -c 1 test.raw

…which results in a raw file that can be played with the aplay command that comes with the ALSA soundcard drivers (specifically, the command above gives mono unsigned 8 bit with a rate of 8000 Hz). Whilst this results in a poor quality file, I found that the Vocore would happily play these raw files. In some ways it was kinda cool to hear my original mp3 file playing with a hiss in the background. But then I decided to see what happens if I create a file in “CD” quality, so I tried this:

sox myfile.mp3 -r 44100 -e signed -b 16 -c 2 test.raw trim 0 00:30

Note, that to save space on the Vocore I used the trim parameter to just convert the first 30 seconds. Playing that “CD” quality file on the Vocore also worked perfectly, with this command:

aplay test.raw -f cd

Passing the parameter -f cd means 16 bit little endian, 44100 Hz, stereo and is the same as passing -f S16_LE -c2 -r44100. Here’s a screenshot of a CD quality raw file playing on the Vocore with the aplay command.

So… what I’m doing now is using sox on another Linux box to convert the mp3 files and then just using aplay on the Vocore to play them. It should mean that sox is not actually required on my Vocore. It also means that playback is immediate and the quality is good. I will need to find a means to store the files, because they’re much bigger than mp3, but that’s a problem for another day… I’ll probably see if I can mount a USB stick or something.

Music by numbers - more notes

I blogged before about building a Raspberry Pi music player by numbers thingy and I said that I’d mention some more details about the software used. I did write up the notes, but then forgot to post it on the blog, so here goes, better late than never…

I had already mentioned the USB Message Board linux driver and the MP3 player that I used. The USB Message Board also requires the libusb-1.0-dev package to be installed, like this:

sudo apt-get install libusb-1.0-0-dev

But before I got that far, I needed an OS … and I wanted a version that wouldn’t corrupt the filesystem on the SD card if I just switched the power off at the mains. I have ended up running something called IPE - Industrial Perennial Environment of which I’m using the R1 edition, which is based on Raspbian. It seems to work a treat: I don’t need to worry about clean shutdowns, the filesystem runs in read-only mode. The R2 version of IPE looks even better, but for the time being I’ve stuck with the Raspbian derived version which is more familiar to me.

It goes without saying, that the webserver I used to provide the web-based API was my own dweb lightweight webserver which seems to have done the job very well.

I also wanted the Raspberry Pi to offer its own WiFi hotspot, for which I followed these instructions. After that I also followed this advice and disabled ifplugd for the wireless lan.

So … putting those things together and with a simple C program, I have:

  • a version of Raspbian which can be powered off without a safe shutdown
  • a Raspberry Pi which operates a WiFi hotspot with a DHCP server
  • an MP3 player which shows the track number on an LED display
  • a web based user interface and API

Normally, I just use Safari on my iPhone to call up a song over WiFi, but I added a numeric keypad as a backup, in case of WiFi interference or other problems. I’m sure that I could really go to town and make a much more complicated wireless MP3 player out of this … but what I have just seems to work perfectly, so at the moment I’m leaving it well alone.

Music by numbers

I wanted to use a Raspberry Pi to play some songs (mp3s) through a very simple interface. Something where you can just type in a number and a corresponding mp3 file would play. This is what I've come up with:

The songs play with mpg321, a simple and lightweight mp3 player. I've used a spare "Dream Cheeky" LED display to show which mp3 is being played (by number). Obviously in the photo shown above there is nothing plugged into the audio socket ... but it was easier to take the picture this way.

The LED display is driven by the excellent dcled utility which works perfectly on the Raspberry Pi. A cheap numeric keypad allows input (although I have also made a web-based interface too).

I'll probably describe some of the software in more detail, when I have time.