Stream Audio With Ubuntu
A cancer patient can't get to church, so church is getting brought to her. Now that I've figured out how to stream audio with Ubuntu into her living from, she can enjoy the music and the message that the rest of us hear in the sanctuary.
April 23, 2012
Let me start off by saying that I hope you found this easily enough. Googling didn't help me a whole lot, and even searching the Ubuntu forums sent me down a lot of dead ends. This is the link that finally got me over the hump though: http://wiki.radioreference.com/index.php/Live_Audio/Ubuntu_Darkice; after a side trip into different VNC options (long story — suffice to say I got sick of running back and forth between the server and client) and some chatting in IRC, this setup finally got hammered out. So…
As you perhaps read earlier, there's a woman who can't get to church due to issues resulting from cancer. As luck would have it, the church meets on her property in a converted building. There had been talk, before I started attending the church, of running a line (coax or some such) from the "sanctuary" into her living room. While this is probably the best way (reliability-wise) to do it, this route means punching holes through exterior walls. I wondered if looking at it from a streaming server/client perspective might help. Turns out that we're in wireless range, and streaming audio with Ubuntu from A to B is a sound (oops, too punny — I meant plausible) idea.
The tools I needed were few. Alsamixer was handy (but perhaps unnecessary), darkice, and icecast2. Pulseaudio is also one I needed, but it was already installed. If you don't have it, running sudo apt-get install pulseaudio pavucontrol should do it for you. To get the other packages you'll need, a simple sudo apt-get install icecast2 darkice will get them. As far as an OS, I'm currently running Ubuntu 12.04 with the XFCE desktop added after the fact. A second install was done with a 10.04 Lubuntu install.
First I had to make sure that sound was coming in the microphone jack. This one took me a while… I forgot that the M at the bottom of a fader in alsamixer meant MUTED (actually I didn't even SEE the M for the longest time), and when I finally hit M I got blasted by the music coming in that jack (The Woodstock version of Ten Years After's Going Home — yes, it was a bit startling when it came blaring out all of a sudden like that)
Next I had to figure out what Ubuntu thinks of my sound card, and this is done with aplay -l:
craig@cp-lap:~$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: SB [HDA ATI SB], device 0: ALC272X Analog [ALC272X Analog]
Subdevice #0: subdevice #0
Card and device are what's important here (both 0) for use later with darkice.
In the /etc/icecast2/icecast.xml file, I really only had to edit the authentication section. This is what I ended up with:
<!– Sources log in with username 'source' –>
<!– Relays log in username 'relay' –>
<!– Admin logs in with the username given below –>
After restarting the daemon, I could see it up by browsing to http://localhost:8000
Configuring darkice was way more of a pain. After a few hours, Here's the /etc/darkice.cfg I finally ended up with:
duration = 0 # duration of encoding, in seconds. 0 means forever
bufferSecs = 15 # 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 = default
device = hw:0,0
#device = /dev/dsp # OSS DSP soundcard device for the audio input
sampleRate = 44100
#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
format = mp3
bitrateMode = cbr
bitrate = 128
quality = 0.8
server = 192.168.2.5
mountPoint = high
port = 8000
password = MYPASSWORD
name = CP_LAP
description = Whatever is running on my laptop
url = http://192.168.2.5:8000
genre = Several
public = yes
Remember that the lines starting with # are comments. Some are left over from failed attempts, and I just hadn't deleted them from the file yet…
After firing it up with a sudo darkice -c /etc/darkice.cfg, I saw the stream show up in my browser (http://loclhost:8000) and was able to launch it. You may want to play with the buffer. You might also notice I have two device lines, and that one is commented out. For whatever reason (I've not bothered looking into it yet) using default works better when I'm streaming audio that already resides on my laptop (like mp3 and ogg files). hw:0,0 works better when I'm listening to the mic. Actually, no sound at all comes out with the mic jack if I don't set it this way. I'm all ears if someone can tell me why.
Also, I've got the sample rate at 41000. If I had it lower to converse bandwith, the sound was lower pitched and slow, just like resting your finger on a spinning record. I listened to Statesboro Blues about an octave lower than normal. Weird. But this was only when using the mic jack; the sample rate didn't seem to matter when I played a local mp3.
Because I'd be running it on a different network at church, I wrote a script so I wouldn't have to keep typing in commands. Well, mostly it was so I wouldn't have to remember them…
This bash script gets run AFTER I've gotten onto the wireless network in question:
read -p " press Enter to continue"
sudo /etc/init.d/icecast2 restart
sudo darkice -c /etc/darkice-mic.cfg
Notice my last line refers to a different config file. This is because that if I'm using a mic on my laptop, I need this one. If I'm just streaming music that's already on my laptop (oggs, mp3s, whatever) then I need a different config file. That one has the device = default (it's commented out in the example above) instead of device = hw:0,0.
That's pretty much it folks. I also ran an Android phone as a client and that played fine. What ended up happening is that we used my phone as a client and ran it into a tv in the house, as I don't believe the woman at church has a laptop or a tablet. We're hopefully running an on site test Wednesday (4/25/2012) and she should be able to listen on Sunday.
We didn't have a test run. I ran "fire drills" on my own so I could practice getting things up quickly from an "everything shut down" state, and set it up this morning. It went fine, other than the regular trouble I have with this laptop switching wireless networks on the fly. After we work a couple of kinks out (like what happens when I'm not there) everything should be hunky dorey.
Remember kids, NONE of this software costs anything. Not the web server, not the streamer, not the operating system, NOTHING.
Have a nice day. :)
Another update (5/2/2012)
I'm sure I will not be at church some Sunday, so I got a computer to do this all if I happen to be absent. If I'd sat down at it the whole time, it'd have taken me maybe two hours from blank hard drive to streaming server. I followed my tutorial (having already forgotten what I need to do) and found a couple of vague spots that I've already fixed.
I'm going to try running this headless, so I've also had to put on openssh-server and vim; I always forget vim until after I'm a few frantic seconds into editing a text file with the regular vi. I also installed lamp-server^ My laptop is already a lamp server, so I didn't mention it in the initial article.
I'll have to show someone how to putty in (there are two guys that bring laptops to church) and fire things up.
Good luck with this. I can already think of a few other uses for a streaming server of my very own.
Need a hand? Looking for something you didn't see?
Give a rundown of what kind of trouble you're having, and we'll see if we can help.
2 responses to “Stream Audio With Ubuntu”