Stream audio from an analog turntable to Sonos speakers using a RaspberryPi

I recently got an old turntable and some records for free. Since I only have two Sonos speakers without analog inputs in my apartment, I needed a simple and cheap way of streaming the audio from the player to my speakers

Parts List

  • RaspberryPi 3
  • Behringer U-Control UFO202 USB Audio Interface
  • Stereo cinch cable

Wiring

Install Icecast2

sudo apt update
sudo apt install icecast2
sudo nano /etc/icecast2/icecast.xml

Change

<hostname>localhost</hostname> to your IP address, for example
<hostname>192.168.1.102</hostname>

Start and enable the icecast2 service

sudo systemctl start icecast2
sudo systemctl enable icecast2

You should now be able to access the icecast2 web UI at
http://your-ip:8000

Install Darkice

sudo apt install darkice
sudo nano /etc/darkice.cfg

Paste the following basic configuration:

[general]
duration        = 0
bufferSecs      = 5
reconnect       = yes
realtime        = yes
rtprio          = 3

[input]
device          = hw:1,0
sampleRate      = 44100
bitsPerSample   = 16
channel         = 2

[icecast2-0]
bitrateMode     = cbr
format          = mp3
bitrate         = 320
server          = localhost                         
port            = 8000
password        = hackme
mountPoint      = listen.mp3
name            = Turntable.fm
description     = Turntable.fm
url             = http://localhost
genre           = music
public          = no
localDumpFile	= dump.mp3

If the alsa device hw:1,0 is not working, you can find the id of your USB audio interface using aplay -l

Install Sonosplay

sudo apt install git
git clone https://github.com/drudv/sonosplay
cd sonosplay
pip3 install .

Write the control script
The following simple python script starts icecast2 and darkice and sends the mp3 stream to the Sonos speakers:

#!/usr/bin/env python3

import os
import time
import signal

# make sure that icecast2 is started
os.system('sudo systemctl start icecast2')

# wait 5s
time.sleep(5)

# Start darkice in a new process
pid = os.fork()

if pid == 0:
  os.system('darkice')
else:
  # Stream to sonos
  os.system('sonosplay --ip 192.168.1.121 http://192.168.1.102:8000/listen.mp3')
  os.system('sonosplay --ip 192.168.1.115 http://192.168.1.102:8000/listen.mp3')

signal.pause()

You will have to change the IP addresses of your Sonos speakers.If you run the script using
python3 stream.py the music should start playing on your Sonos speakers.

Systemd Service

To run the script on system startup, you have to create the file stream.service in /etc/systemd/system:

[Unit]
Description=Sonos stream
After=network.target auditd.service

[Service]
ExecStart=python3 /home/pi/stream.py

[Install]
WantedBy=multi-user.target
Alias=stream.service

and enable it using sudo systemctl enable stream

Show Comments