Broadcast Sound over Network from RaspberryPi with Rasbian Stretch, DarkIce and Icecast2
Introduction
This procedure describes the installation of a RaspberryPi to broadcast your turntable or any analog device over the local network. Darkice will be use to encode the incoming analog feed to MP3 and will send it to Icecast for publication.
Icecast will be installed on the Raspberry, but you could have it on another machine should you already have one.
Similar Article
I found the following article from HiFi Berry. They make audio cards (aka DAC) for Raspberry Pi that can be used for sound output and also input which is what you could use in your project.
What is not described
These configurations are not described here.
- DarkIce could be replace by LiquidSoap.
- IceCast could be replace by ShoutCast.
- You could also decide to publish the casting server over the internet but you will have to take care of the security.
Required Material
- RaspberryPi
- SD Card 8BG+ (with Rasbian Stretch Lite)
- A compatible USB Sound Card for acquisition.
Installation
Update System
sudo apt update
sudo apt upgrade
Check Recording Device
arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: USB [phono PreAmp USB], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
Install DarkIce
Install dependencies
sudo apt install apt-utils unzip autoconf libtool libtool-bin checkinstall libssl-dev libasound2-dev libmp3lame-dev libpulse-dev alsa-utils avahi-daemon
Check that you have the non-free package activated
grep ^[[:alnum:]] /etc/apt/sources.list
Add Source package repo
echo “deb-src http://archive.raspbian.org/raspbian/ stretch main contrib non-free rpi” | sudo tee –append /etc/apt/sources.list
Update Repositories
sudo apt update
Get Darkice Sources and Compile with MP3 support
apt source darkice
Go into the subdirectory that has been created
cd darkice-1.3/
Configure
./configure –prefix=/usr –sysconfdir=/usr/share/doc/darkice/examples –with-lame –with-lame-prefix=/usr/lib/arm-linux-gnueabihf –with-alsa –with-alsa-prefix=/usr/lib/arm-linux-gnueabihf
Make
make
Make Install
make install
Install IceCast2
sudo apt install icecast2
Upon installtion you will be asked to configure it. Please proceed and save the password for later use.
“hackme” is the default password. Make sure you’ve change it and keep it safe. It could also been change later in the configuration file (/etc/icecast2/icecast.xml)
Configure DarkIce to Stream data to IceCast2
sudo nano /etc/darkice.cfg
# this section describes general aspects of the live streaming session
[general]
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
[input]
device = plughw:1,0# OSS DSP soundcard device for the audio input
sampleRate = 22050 # sample rate in Hz. try 11025, 22050 or 44100
bitsPerSample = 16 # bits per sample. try 16
channel = 2 # 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
[icecast2-0]
bitrateMode = abr # average bit rate
format = mp3 # format of the stream: ogg vorbis
bitrate = 96 # bitrate of the stream sent to the server
server = localhost # host name of the server
port = 8000 # port of the IceCast2 server, usually 8000
password = hackme # source password to the IceCast2 server
mountPoint = mic # mount point of this stream on the IceCast2 server
name = Microphone Raspberry Pi # name of the stream
description = Broadcast from 2nd room # description of the stream
url = http://example.com/ # URL related to the stream
genre = my own # genre of the stream
public = yes # advertise this stream?
Change the device name.
The device name is composed of the card and the device : plughw:[card_id],[device_id])
So for my card….
card 1: USB [phono PreAmp USB], device 0: USB Audio
… it means the line is as follow
plughw:1,0
Update the IceCast password
Use the one you’ve set during IceCast2 installation.
Configure Service (not working as it is)
Create the service file
sudo nano /etc/systemd/system/darkice.service
Paste the below code (should you use a separate user)
[Unit]
Description=DarkIce live audio streamer
Documentation=http://www.darkice.org/ man:darkice(1) man:darkice.cfg(5)
Requires=sound.target
Wants=network-online.target
After=icecast.service network-online.target sound.target
[Service]
Type=simple
ExecStart=/usr/bin/darkice -c /etc/darkice.cfg
User=darkice
Group=darkice
# DarkIce uses POSIX real-time scheduling to keep up with sound card input.
# Let systemd change the CPU scheduler policy and priority beforhand, so that
# root privilegs can be avoided.
CPUSchedulingPolicy=fifo
CPUSchedulingPriority=4
[Install]
WantedBy=multi-user.target
Use the code below to run it as root
[Unit]
Description=DarkIce live audio streamer
After=icecast.service
Wants=icecast.service
[Service]
Restart=always
RestartSec=3
ExecStart=/usr/local/bin/darkice -c /etc/darkice.cfg
ExecStop=/usr/bin/pkill darkice
CPUSchedulingPolicy=fifo
CPUSchedulingPriority=4
[Install]
WantedBy=multi-user.target
Enable service
sudo systemctl enable darkice.service
Start Service
sudo systemctl start darkice.service
Sources
- https://technic2radio.fr/darkice-toutes-librairies-raspberry-pi/
- http://code-injection.blogspot.com/2014/05/broadcasting-with-raspberry-pi.html
- https://maker.pro/raspberry-pi/projects/how-to-build-an-internet-radio-station-with-raspberry-pi-darkice-and-icecast
- https://gist.github.com/vees/b52fcf0cccde403472a4058761aa3b50
- https://stmllr.net/blog/live-mp3-streaming-from-audio-in-with-darkice-and-icecast2-on-raspberry-pi/
- https://github.com/coreyk/darkice-libaacplus-rpi-guide
- https://stmllr.net/blog/live-mp3-streaming-from-audio-in-with-darkice-and-icecast2-on-raspberry-pi/