This post is for all the folks who, like me, have tried (and failed out of frustration) 20 different times to stream local audio from an ubuntu machine to some other thing on the network via HTTP. Today my goal was to get the Sonos in the office to stream from my machine — aka whatever was playing on my machine should come out of sonos. I finally sat down and got it done with icecast and darkice today. The basics for this article come from here.
OK, so without further ado, the basics:
sudo apt-get install icecast2 darkice
sudo nano /etc/default/icecast2
Change ENABLE=false to ENABLE=true. The reason this is false by default is that icecast is actually a community radio streaming thing that is intended for multiple people to use. Therefore it has some passwords in the default configuration file (/etc/icecast2/icecast.xml) that they want you to change before turning the thing on at first. Since this is just us using icecast and we’re all on the local network, I chose to go with simplicity and defaults here.
Now, create a new config file someplace you like (perhaps ~/.darkice.cfg) which contains the following:
duration = 0 # duration of encoding, in seconds. 0 means forever
bufferSecs = 1 # size of internal slip buffer, in seconds
reconnect = yes # reconnect to the server(s) if disconnected
device = pulse
sampleRate = 44100 # sample rate in Hz. try 11025, 22050 or 44100
bitsPerSample = 16 # bits per sample. try 16
channel = 2 # channels. 1 = mono, 2 = stereo
bitrateMode = vbr # variable bit rate (cbr for constant)
quality = 1.0 # 1.0 is best quality
format = mp3
bitrate = 256 # bitrate
server = localhost # or IP
port = 8000 # port for IceCast2
accesspassword = hackme # source password to the IceCast2
servermountPoint = mystream.mp3 # mount point on the IceCast2 server or any name
name = mystream
Now bootup icecast
sudo /etc/init.d/icecast2 start
Now start darkice (the thing which actually reads pulse and sends it to icecast. Icecast then provides an HTTP URI you can use to give out to others)
darkice -c ~/.darkice.cfg
Now head on over to your consuming device and give it the url http://MY_IP:8000/mystream.mp3 (replacing MY_IP with your machines ip, aka 192.168.1.105). In sonos you go to the “Radio” tab, hit “Add” then type in that url and a fancy-shamancy name. Click the radio you created and shazam, good to go!
Note: there is a 3-4 second delay using icecast. I’m not sure how much of that is icecast vs. sonos, but for just audio streaming its great. That is, until I decide I really want to watch a youtube video with the audio coming out of sonos and I spend a few hours putting jack in the system…
Note: this was all done on Ubuntu 11.10 using standard repo versions of icecast, darkice, pulseaudio etc.