Live mp3 streaming from audio-in with DarkIce and Icecast2 on Raspberry Pi
As a response to my lastest article about Streaming audio with MPD and Icecast2 on Raspberry Pi, I was asked for some ideas about live streaming. I took the chance to plug in an usb audio device with microphone to RasPi and tried to get it working.
Update (2012/08/11): Added a step to update package version number and added "devscripts" to the list of suggested packages .
What you need to have
- Raspberry Pi with Raspbian installed,
- USB sound stick with an input device,
- DarkIce to capture audio from the audio input and send it to Icecast2,
- Icecast2 to distribute the audio stream.
Compiling and installing DarkIce
The default darkice package comes without mp3 support. Since mp3 is the most widespread codec, I decided to build my own package of darkice with mp3 support.
Add a deb-src repository to your sources list at /etc/apt/sources.list:
$ sudo sh -c "echo 'deb-src http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi' >> /etc/apt/sources.list"
$ sudo apt-get update
To fullfill the build dependencies, you have to install some additional packages:
$ sudo apt-get --no-install-recommends install build-essential devscripts autotools-dev fakeroot dpkg-dev debhelper autotools-dev dh-make quilt ccache libsamplerate0-dev libpulse-dev libaudio-dev lame libjack-jackd2-dev libasound2-dev libtwolame-dev libfaad-dev libflac-dev libmp4v2-dev libshout3-dev libmp3lame-dev
(Not sure if you need all these packages. But it won't hurt.)
Create a working directory:
$ mkdir src && cd src/
Get the source package of darkice:
$ apt-get source darkice
Change the compile configuration to match Raspbian environment:
$ cd darkice-1.0/
$ vi debian/rules
ln -s /usr/share/misc/config.guess .
ln -s /usr/share/misc/config.sub .
dh_auto_configure -- --prefix=/usr --sysconfdir=/usr/share/doc/darkice/examples --with-vorbis-prefix=/usr/lib/arm-linux-gnueabihf/ --with-jack-prefix=/usr/lib/arm-linux-gnueabihf/ --with-alsa-prefix=/usr/lib/arm-linux-gnueabihf/ --with-faac-prefix=/usr/lib/arm-linux-gnueabihf/ --with-aacplus-prefix=/usr/lib/arm-linux-gnueabihf/ --with-samplerate-prefix=/usr/lib/arm-linux-gnueabihf/ --with-lame-prefix=/usr/lib/arm-linux-gnueabihf/ CFLAGS='-march=armv6 -mfpu=vfp -mfloat-abi=hard'
Please consider to download the rules file from here instead of copy&paste the lines above. The build will fail if line beginnings contains spaces instead of tabs.
Before you start to build the package, change the version of the package to reflect mp3 support. debchange will ask you to add some comments to the changelog.
$ debchange -v 1.0-999~mp3+1
darkice (1.0-999~mp3+1) UNRELEASED; urgency=low
* New build with mp3 support
-- <pi@raspberrypi> Sat, 11 Aug 2012 13:35:06 +0000
Build and install the darkice package:
$ dpkg-buildpackage -rfakeroot -uc -b
$ sudo dpkg -i ../darkice_1.0-999~mp3+1_armhf.deb
Preparing to replace darkice 1.0-999 (using .../darkice_1.0-999~mp3+1_armhf.deb) ...
Unpacking replacement darkice ...
Setting up darkice (1.0-999~mp3+1) ...
Now you have installed DarkIce with mp3 support.
Add a configuration by copying the template file:
$ sudo cp /usr/share/doc/darkice/examples/darkice.cfg /etc/
Here's an example configuration to have:
- read input from an usb device using the alsa audio device hw1,0
- stream mp3 output to an icecast2 server
# see the darkice.cfg man page for details
# this section describes general aspects of the live streaming session
duration = 0 # duration of encoding, in seconds. 0 means forever
bufferSecs = 5 # size of internal slip buffer, in seconds
reconnect = yes # reconnect to the server(s) if disconnected
# this section describes the audio input that will be streamed
device = hw:1,0 # Alsa soundcard device for the audio input
sampleRate = 44100 # sample rate in Hz. try 11025, 22050 or 44100
bitsPerSample = 16 # bits per sample. try 16
channel = 1 # channels. 1 = mono, 2 = stereo
# this section describes a streaming connection to an IceCast2 server
# there may be up to 8 of these sections, named [icecast2-0] ... [icecast2-7]
# these can be mixed with [icecast-x] and [shoutcast-x] sections
bitrateMode = vbr # variable bit rate
format = mp3 # format of the stream: mp3
quality = 0.6 # quality of the stream sent to the server
server = localhost # host name of the server
port = 8000 # port of the IceCast2 server, usually 8000
password = SOURCE_PASSWORD # source password to the IceCast2 server
mountPoint = raspi # mount point of this stream on the IceCast2 server
name = RasPi # name of the stream
description = DarkIce on RasPi # description of the stream
url = http://localhost # URL related to the stream
genre = my genre # genre of the stream
public = no # advertise this stream?
localDumpFile = recording.mp3 # Record also to a file
This is just an example and neither optimized nor secure! Learn more about darkice in the documentation section below /usr/share/doc/darkice/. The configuration options are explained in the manpage:
$ man darkice.cfg
Btw., to find your alsa input devices, ask arecord:
$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: U0x46d0x825 [USB Device 0x46d:0x825], device 0: USB Audio [USB Audio]
Subdevice #0: subdevice #0
This particular input device is the microphone of a Logitech H270 webcam. It can be addressed using hw:1,0 (Card 1, Subdevice 0).
Keep in mind that Raspberry Pi has no input audio devices on board. You have to plug additional soundcard hardware to usb.
If you don't have an icecast2 server running somewhere, you could set one up on RasPi.
$ sudo aptitude install icecast2
The package manager will ask you to configure Icecast2. You should do so and set a hostname and passwords for source, relay and administration. Needless to say to use strong passwords. The source password will be needed in the darkice.cfg configuration. (See SOURCE_PASSWORD in the above example)
Running DarkIce to stream audio
Using config file: /etc/darkice.cfg
Using ALSA DSP input device: hw:1,0
Could not set POSIX real-time scheduling, this may cause recording skips.
Try to run darkice as the super-user.
When you run into the following "sink error" message, check if icecast is running and passwords are matching:
DarkIce: LameLibEncoder.cpp:75: lame lib opening underlying sink error 
Now grab your favorite instrument and start a live jam. To listen to it, open your browser, navigate to the Raspberry Pi host at http://your.raspberry.hostname:8000/raspi and hook up into the stream with your favorite player.
I have done some very basic measurements. With the above configurations, the CPU load is about 50%. Very nice!
Did this article help you? Become a sponsor!
If you liked this article, I invite you to sponsor me one or two license keys for my Raspberry Pi. The key for MPEG2 costs £2.40, the key for VC-1 is £1.20. You can order the MPEG-2 key and the VC-1 key directly at the Raspberry Pi store. Once you received the key(s), send it per mail to firstname.lastname@example.org. That would be awesome. Thanks!
My serial number is: 00000000b0cd0a01
Thanks to Patrick Adams for inspiration. Looking forward for a turntable session to listen to ;-)
This article is licensed under the Creative Commons License CC BY-SA 3.0. You are free to share (copy, distribute and transmit) and to remix (to adapt) the work under the following conditions:
- You must attribute the work by mentioning the name of the author (Steffen Müller) and setting a link back to the original article using its URL.
- If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.
Leave a comment
Sorry, due to technical issues, commenting is disabled at the moment. I'm on it.