How to Turn a Raspberry Pi 3 into a Darkice Stream Encoder

Since signing on Classic Pop Radio a couple of years ago, I have used two computers to run the radio station. The first is a CentOS box running the Rivendell Radio Automation system. The second was a Windows XP laptop with a dead LCD screen running AltaCast. (Why not do everything on the CentOS box? I like running outboard audio processing, and I like having the ability to plug in an audio console when I want to go live and it’s easier to do both with a separate streaming box.)

I’ve always looked at the laptop as a temporary solution and I’ve longingly looked at the Raspberry Pi as the device to become my streaming appliance. Since all I want it to do is encode a stream and send it to my streaming host, I don’t need fancy, and a fanless low power box is perfect to sit in the corner of my home office.

There are several blog posts on the internet that detail how to do this project. Several of them are old and state that the Pi is not robust enough to handle a stereo stream. With the advent of newer Pi hardware such as the Pi 3 this is no longer the case. You can stream in stereo to your heart’s content with the current hardware. Also, several of those tutorials are aimed at people who stream scanner audio. If you want a great sounding internet radio station, this is the tutorial for you.

What You Need

  1. A Raspberry Pi 3. I bought the Raspberry Pi 3 Model B at MCM Electronics. Tip: If you buy their bundle (Pi, case, power supply) you also get a Micro SD card that’s pre-loaded with NOOBS (New Out Of Box Software) that lets you download the OS of your choice.I did things the hard way and bought the Pi, case, and power supply separately because I wanted a different case – and found out that the Micro SD card didn’t come along for the ride. One Sunday morning trip to the nearest big box store for a Micro SD card and a quick download of NOOBS from raspberrypi.org later I was ready to go. Buying the bundle would have saved about an hour of my time.
  2. A USB sound card that supports Linux. There is a 3.5 mm jack on the side of the Pi that has audio and composite video on it, but that’s output only. The only way to get audio into the Pi is via the USB port. If this is for an internet radio station, don’t go for the cheap $3 USB sound card from Ebay if you want it to sound good. But since internet radio rarely makes a profit, don’t go nuts spending money on the hardware, either. I have had good experiences with the Behringer UCA-202 and UCA-222; I used the UCA-202 here.

Getting Started

  1. Boot up the Pi and select Raspbian as your operating system. After it finishes installing the OS, it will boot to the desktop. Before you get too deep, now would be a good time to change the default password for the “pi” user. If you haven’t blocked ssh on your firewall, you should, because most people know the easiest way into a Pi is via ssh, so an open port plus default password is a bad thing. (Firewall your network and use a VPN to get to your Pi if you need remote access. Don’t leave ports open or do port forwarding on your firewall.) If you’re going to have a keyboard, mouse, and monitor plugged into your Pi, you could even turn off ssh on the Pi if you really want to lock things down. There’s a checkbox to do this in the Raspberry Pi config menu in the X (windows-like) interface.
  2. Compile and install Darkice. This is the software used to encode your stream and send it to an Icecast, Icecast 2, or Shoutcast server. Steffen Müller has written the definitive blog post on this *and* he has built his own package with MP3 support. So click over there and follow his steps for compiling and installing Darkice with MP3. When you get to the section on creating the rules file, follow his suggestion on downloading his file and copying it as opposed to copying and pasting to avoid errors (the rules file is expecting tabs, which does not copy and paste well).
  3. After you have compiled Darkice per Steffen’s instructions, you need to set up the config file. Copy the template into your /etc/ folder:
    $ sudo cp /usr/share/doc/darkice/examples/darkice.cfg /etc/

    You will need to know the address of your USB soundcard in ALSA when you edit the config. You can get this from arecord:

    $ arecord -l
    **** List of CAPTURE Hardware Devices ****
    card 1: U0x46d0x825 [USB Device 0x46d:0x825], device 0: USB Audio [USB Audio]
      Subdevices: 1/1
      Subdevice #0: subdevice #0

    This is card 1, device 0, which translates to hw1,0 in the config file.

    The config template has sections for Icecast, Icecast 2, and Shoutcast. My station uses Icecast 2, so I cut out the sections that didn’t apply to me. Here’s my config, with my server info removed. You’ll use your own settings from your stream host.
    darkice
    Note that if your mount point has a leading slash (ie /live) you will OMIT the leading slash in the config file. I find that a 2 second buffer is sufficient.

  4. Now it’s time to run Darkice. IMPORTANT: you will want to run this as super user. The standard user (pi) doesn’t have access to the realtime OS, but super user does.
    $ sudo darkice

    If you just launch it by typing darkice and omit the sudo, you will get clicks, pops, squeals, and pauses. Run it as super user.

    If you get a sink error like this, you probably have a mistyped something in your login information.

    DarkIce: LameLibEncoder.cpp:75: lame lib opening underlying sink error [0]

    If you don’t get audio at all, check to make sure that you didn’t put a leading slash in for your mount point.

  5. You will want to add a command to fire up Darkice on boot. I like using rc.local for this because everything it starts runs as root (aka the super user).Enjoy!