17 Oct 2009

Streaming audio from line in

After playing around with a product-that-shall-remain-eye-queue-nameless today, I found a nice way to stream the audio coming out of an audio source online through Linux:

  1. Firstly, if the device you’re getting audio from doesn’t have a 3.5mm stereo jack for output (commonly known as a headphone jack), you will need to connect a 3.5mm stereo to RCA cable from the audio output of the device to the Line in on your PC. Don’t use the microphone input (it typically is only mono and has a lower impedance)—stick with the line in.

  2. emerge -a icecast ezstream sox

    Icecast is an open source alternative for Nullsoft’s proprietary SHOUTcast software which will be using to stream the audio online. The sox package includes a program called rec which is capable of capturing and encoding the input from line in. ezstream is the glue: it takes the encoded audio and pushes it to the Icecast server.

  3. alsamixer -V capture

    Switch Input Source to Line (it defaults to Mic), and increase its volume to approximately 70%. After much testing this seems to be about the best setting (feel free to experiment, it won’t be the same on your PC).

  4. alsamixer -V playback

    Increase the Line slider to approximately 70%.

  5. Run rec test.wav for 10 seconds or so and ^C it. All going well, you should have just captured 10 seconds worth of audio from the source in 8-bit mono! Confirm this with your favourite media player (or mplayer test.wav). You can remove this file now, as we won’t be capturing to file for the real thing.

To configure Icecast, you will want to make a few changes to the default configuration. Edit /etc/icecast/icecast.xml and change the following settings:

  1. The default maximum client count is a bit much (100), so drop it down to 10:

    <clients>10</clients>
    
  2. Change the default passwords from hackme (lulz) to a more meaningful (and secure) setting. There are a few different sections that need changing, so just /hackme in vim.

  3. The <hostname> is set to localhost by default, which may not be useful if you intend to access the streaming server by its FQDN or IP. Change this as appropriate (it makes no difference unless you use the web interface).

  4. Start the server: /etc/init.d/icecast start.

Next, we will configure ezstream. This is very easy as one of the provided configuration files does exactly what we want (taking input from stdin in the Vorbis format and send it to Icecast). We’re using Vorbis (.ogg) because encoding as it is less CPU-intensive than MPEG-2 (MP3).

  1. Copy the example configuration to Icecast’s configuration directory (which isn’t strictly required but it’s a good a place as any to store it):

    bzcat /usr/share/doc/ezstream-0.5.3/examples/ezstream_stdin_vorbis.xml.bz2 > /etc/icecast2/ezstream_stdin_vorbis.xml
    
  2. Edit the newly created configuration and change hostname in <url> to whatever you picked before (the default is localhost). Replace vorbis.ogg with a more meaningful name. An example <url> value might be:

    <url>http://my-server:8000/ipod</url>
    
  3. Change each of the svr items at the bottom to your liking. This is all metadata for the server to add to the stream. Leave the sample/bitrate/etc settings as their defaults.

  4. Start sending the audio to Icecast:

    rec -c 2 -r 44k -t ogg - | ezstream -c /etc/icecast2/ezstream_stdin_vorbis.xml
    

    These commands tell rec to take the input from the default capture device (line in) and encode it using the Vorbis format, 2-channel, 44 kHz (standard for CD-quality audio) and write it to stdout. ezstream takes its input from stdout and sends it to Icecast as per its given configuration. This must stay running for as long as you want to capture/encode and push audio to Icecast.

  5. Test the stream by opening http://my-server:8000/ipod in mplayer. You should hear the output from your audio device being played through your computer with a lag of approximately 3-4 seconds.

Congratulations, you can now stream the current audio to anyone in the world! Just make sure the source device’s volume is set to maximum for the best listening experience.