Skip to content

Instantly share code, notes, and snippets.

@kylemarsh
Last active May 17, 2023 22:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylemarsh/1cb34f2ce4e39dbd1840481ebe1a942b to your computer and use it in GitHub Desktop.
Save kylemarsh/1cb34f2ce4e39dbd1840481ebe1a942b to your computer and use it in GitHub Desktop.
Use a Raspberry Pi to stream to sonos via Airplay

Airplay to Sonos through Raspberry Pi

Links

Testing Sound Cards

Testing Notes

Create the loopback device:

sudo modprobe snd-aloop
sudo echo "snd-aloop" >> /etc/modules

View sound stuff:

  • cat /proc/asound/modules
  • cat /proc/asound/cards (show sound cards; first number is device id, followed by [DEVICE_NAME ])
  • cat /proc/asound/card1/pcm0p/info (show info for card 1, device 0, playback)
  • cat /proc/asound/card1/pcm0c/info (show info for card 1, device 0, capture)
  • aplay -l show all playback devices
  • arecord -l show all capture (recording) devices

Test sound stuff: speaker-test sends test noises to playback devices. We can test our loopback by setting up speaker-test to play on the loopback's playback device, and setting arecord to record from the loopback capture device simultaneously. First set up arecord to listen to the capture devce for a signed, 16-bit, little-endian stream at 48000 Hz (speaker test's default) (your device may be different; use the above notes to determine it):

arecord -f S16_LE -d 10 -r 48000 --device="hw:Loopback,1,0" test-lo.wav

Then start playing a test tone on one channel of the playback device:

speaker-test -Dhw:Loopback,0,0 -c1 -twav

Finally play your recording with aplay:

aplay test-lo.wav

Later once you've got darkice and icecast2 set up you can use the speaker test to test those two pieces without shairport by running the test on two channels at 44100 Hz (use -tsine because the wav test isn't 44100 Hz):

speaker-test -Dhw:Loopback,0,0 -c2 -tsine -r44100

Setting up the software

Installation:

sudo apt install shairport-sync darkice icecast2

Configuration:

  • shairport-sync
    • Mostly the default config is sufficient?
    • For some reason it doesn't start whenever I try to change the name in the config's general stanza.
    • set interpolation to basic insted of soxr if CPU usage is too high.
    • In the alsa config stanza set output_device = "hw:Loopback,0,0" (or whatever you got working with speaker-test earlier)
  • icecast
    • you should probably change the passwords during the installation config.
    • "source" is things sending audio to icecast; you'll use this password in the darkice config.
    • set up the actual stream in the <listen-socket> section
      • Uncommenting the <bind-port> breaks it. I don't know why.
      • I think you need to uncomment the <shoutcast-mount> bit and give it a name. This name is what you put in the darkice config.
  • darkice
    • Server, port, password, mountPoint, and name under the [icecast2-0] stanza have to match the icecast config (what is mountPoint vs name?)
    • /etc/darkice.cfg:
    [general]
    duration        = 0
    bufferSecs      = 1
    reconnect       = yes
    realtime        = yes
    rtprio          = 3
    
    [input]
    device          = hw:Loopback,1,0
    sampleRate      = 44100
    bitsPerSample   = 16
    channel         = 2
    
    [icecast2-0]
    bitrateMode     = vbr
    format          = mp3
    quality         = 0.8
    server          = localhost
    port            = 8000
    password        = <pwd>
    mountPoint      = sonos
    name            = sonos
    
    • /etc/systemd/system/darkice.service:
    [Unit]
    Description=DarkIce Live audio streamer
    After=icecast2.service
    
    [Service]
    ExecStart=/usr/bin/darkice
    
    [Install]
    WantedBy=multi-user.target
    

Launch, and Start on Reboot:

sudo systemctl daemon-reload # so that it picks up the darkice service we wrote
sudo systemctl start shairport-sync.service
sudo systemctl start icecast2 # this has to start before darkice
sudo systemctl start darkice.service
sudo systemctl enable shairport-sync.service
sudo systemctl enable icecast2
sudo systemctl enable darkice.service

Sonos Setup

Add the icecast url to sonos: Manage -> Add Radio Station...use the pi's IP address (probably should make it static in the router!) and the port and shoutcast-mount name from the icecast2 config (ex):

http://192.168.1.65/sonos

TODO

Instead of (in addition to?) shairport-sync, do we have somethign that'll do this via chromcast?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment