Your own Internet Radio on Linux - Updated

I have recently met a new friend who happens to be a DJ for The World Net Radio. I had the opportunity to see this friend set up for a DJ session and what it took to make the radio station work. But my friend is not a Linux user (yet), so I was seeing things from the Windows perspective. Tonight I decided to see if this could be done from a Linux perspective. I've had moderate success, so will detail what I've found seeing as the documents out there are a little sparse on details.

The approach I'm using should work on any Linux system, however both my desktop and server are running Ubuntu. The server is running Ubuntu Feisty (about a year old now), while the desktop is running Gutsy. This approach has NO differences between these versions.

UPDATE: A friend of mine (thanks Nicole!) contacted me and let me know that my statements below regarding Icecast are a little wrong. Icecast CAN do MP3's OR Ogg/vorbis files. It depends on what transmitter tool you use to the streaming server. In this case Ices0 can handle MP3's and Ices2 can handle OGG. The underlying icecast server can handle both. Nicole also pointed me to what she considers THE best HOWTO on setting up Icecast - http://www.gnuware.com/icecast/. I'm still playing with it.

Background

It should be noted that you need to pay attention to copyright issues if you are going to broadcast your music. The solution I'm presenting here are for use on your personal network or workstation. The next step to make this available over the Internet is a simple matter of port forwarding. My station is only intended for my personal use - regardless of where I am.

Google is your friend in most cases. But more so when you know the right incantation (aka search phrase). I eventually arrived at "shoutcast on linux". SHOUTcast is a free (as in beer) audio streaming server. There is an alternative that is free (as in speech) called IceCast. I opted to use SHOUTCast because it could handle MP3 and/or OGG/Vorbis files. The current version of IceCast seems to be limited to OGG. Most of my collection is in MP3 format. Nuff said. Anyways, Google led me to http://www.gentoo.org/doc/en/shoutcast-config.xml, where I was able to ignore the Gentoo installation methods and follow along with the remaining document to learn what I needed.

There are actually two components that are needed to get you broadcasting. The first is the streaming server (SHOUTCast in this case), and the second is a tool to feed the streams to the server. If you want to do live DJ-ing as well as recorded music/commercials, then you'll need a third component to pass the microphone inputs to your streaming server. This last part is where I stumbled a bit. The tool of choice on Linux seems to be Internet DJ Console. This tool looks promising, however it makes use of Jack, and Jack needs some further configurations that are beyond my current knowledge. (i.e. I don't know how to use Jack at this time, or how to setup my box for that level of audio processing). I'll be speaking to a friend to get some assistance here, and if it works out I'll do another post on the topic. In the mean time, I was able to get IDJC running, but it's not stable due to my Jack configuration. Let me know if you come across any other options.

The two tools then that we will be using is the SHOUTCast server, and the SHOUTCast DSP Plugin (which "transmits" the current file to the streaming server).

The DSP plugin reads from a playlist file. It then does the following:

  • determines the next file to be played
  • decodes that file
  • re-encodes that file to the needed settings
  • feeds the file to the streaming server
  • moves to the next file, or ends if it reaches the end of the playlist.

Installation

We will be tackling this from a very simple perspective. We'll assume that you'll be firing up the server manually as needed. In a production environment, you should set up a separate user account for running the ShoutCast server.

  1. create a directory where the server will exist. I chose /opt/shoutcast.
  2. Change into that directory
  3. Download the SHOUTcast server files. You can find them from http://www.shoutcast.com/download/serve.phtml. Click the link that says "PROCEED TO LICENSE AGREEMENT TO DOWNLOAD SHOUTCAST DNAS". You should check here for the latest version. For myself, I did

    wget http://www.shoutcast.com/downloads/sc1-9-8/sc_serv_1.9.8_Linux.tar.gz

    to grab the file into my directory.
  4. Decompress the tar file:

    tar -zxvf sc_serv_1.9.8_Linux.tar.gz

    Note: There are no subdirectories created, so everything is dumped into the current directory. (aren't you glad we cd'd into it first?)
  5. Create a directory here called "trans" (or something meaningful for you).
  6. Change into the "trans" directory
  7. Download the DSP Plugin file. You can get the most recent file at http://www.shoutcast.com/download/broadcast.phtml. I did

    wget http://www.shoutcast.com/downloads/sc_trans_posix_040.tgz
  8. Decompress the DSP Plugin file:

    tar -zxvf sc_trans_posix_040.tgz
  9. Installation is complete. Now we need to configure things.

Configuration

We have two configuration files we need to edit. Use your editor of choice, but we'll start with the sc_serv.conf file located with the streaming server files. (in my case that would be /opt/shoutcast/sc_serv.conf)

Streaming Server Configuration
The configuration options are well documented within the config file. The items we need to change are:

  • MaxUser - set this to something reasonable. If you are dealing with your local network, then you can likely set this to what you need. If you are accessing the streams over the Internet then you'll need to do a little math. If you are on a cable modem with a 760 Mbps upload rate, you don't want this set to 100 - you'll max out your bandwidth quickly. You can do a quick calculation with something like this: Upload Mbps divided by File Bitrate = rough approximation of how many users your bandwidth will support. Being on cable I know I have at least 760 Mbps upload. So I picked an arbitrary value of 500 Mbps to leave some room for the other services on my server box. I then decided I wanted a bit rate of 24000. 500 divided by 24 gives me a value of 20.833. So I know I can set my MaxUsers to 20 and still not be abusing my bandwidth to the max. I chose to set it to 10 for starting out and working through the rest of the setup.
  • Password - this is the password to connect to the streaming server (as a service or as a DJ). You'll need this password if you are going to set or change the streams going to your streaming server. A regular user shouldn't know this password.
  • Port Base - this defaults to 8000. This is the TCP/IP port your server will listen for connections on. It will be used in your connection URL (i.e. http://yourserver:8000).
  • --- OPTIONAL Items ---
  • RealTime - I set this to "0" (that's a zero) to disable the realtime updates. This applies to the web page.
  • ScreenLog - This was also set to 0. If not, then when you start the service manually it tells you how many listeners are connected every second - in the terminal session. Gets annoying when you are still working. Not to mention it is meaningless if you run the server without a terminal (as a service maybe?)

All the other settings should be reviewed and set to something meaningful for your own use. General rule of thumb - if you don't understand the setting, don't change it.

DSP Plugin Configuration
Change into the "trans" directory (or whatever you chose to call it), and open up the sc_trans.conf file for editing.

  • PlaylistFile - set this to the absolute path to the file that contains your playlist. If you are just starting out, set this to "music.lst", and we'll set this up in a moment.
  • Server IP / Server Port - set these to point to where the streaming server is located. Use the "Port Base" value you specified above for the port. If the DSP plugin is on the same computer as the streaming server (it doesn't need to be), then set the IP address to 127.0.0.1.
  • StreamTitle, StreamURL, Genre - These are arbitrary values, except for the StreamURL. Set the title and genre to something suitable for your server. (Mine currently says "Grover's Tunes" and "Music that Grover likes" respectively. The StreamURL would be the URL the users need to use to connect to listen to your stream. This should look something like "http://yourserver:8000/listen.pls". Make sure the "8000" reflects the port you chose to use. You need the listen.pls as the file name.
  • Shuffle - set this to one if you want the next song to be a random selection from your play list. Set this to zero if you want the next song to be the next listed in the play list.
  • Bitrate/SampleRate/Channels/Quality - Set these to something that makes sense for your server. Keep in mind that the higher the values, the more bandwidth you'll need (except for Quality). Set the Channels to one if you want a mono transmission, or two if you want stereo transmissions. Set Quality to 1 if you want highest quality, or 10 if you want fastest speed, or someplace in between. If you have it set to 1 you'll see a few seconds delay between the DSP Plugin processing a file and when that file/change is played.

The remaining settings are set to reasonable defaults, but review them and make any changes you'd like.

Build the Playlist

Our playlist file is a simple text file with one song per line. Each song entry is an absolute path to the song file. I cheated a little and used the following command:

find /my/mp3/files -type f -name "*.mp3" | sort > /music.lst

This command finds all the .mp3 files in the /my/mp3/files directory AND subdirectories, then it sorts them alphabetically, and writes the results out to music.lst. I then edited the file to remove songs I didn't want to hear. I used the sort option to make this task easier, and because I opted to play the files in a random order (shuffled).

Where things get interesting is that you can easily write a script to add things to add entries to this file. The sc_trans_linux executable can be "bumped" to reload the playlist without affecting the current file. So in theory it should be possible to write a script to periodically insert a commercial, or pull a song from a different source. (details how to "bump" the executable are in the sc_trans.conf file - something along the lines of "kill -usr1 $PID" should do the trick.

Let the streams flow!

We're all set. We just need to start our services and connect a client tool to them to listen in. Run the following command, changing the paths as needed to match your setup:

cd /opt/shoutcast
./sc_serv &
cd trans
./sc_trans_linux &

If all goes well, you should start seeing the output from sc_trans_linux. (you can send that process a HUP signal to stop it from sending output to stdout.) If you encountered errors, make sure the sc_serv file and the sc_trans_linux file are executable.

Next we need to connect to the stream server. Open your browser, or Amarok, or what ever stream player you'd like. Point them at http://yourserverIP:8000/listen.pls. Again, if all goes well, you are listening to your music being streamed over the network. I have an occasional hiccup with Amarok where it won't play a stream. When this happens, I can just stop Amarok (ctrl-Q) and restart it, then try again. 9 times out of 10 this works fine. If that doesn't work, then my sound system is in an odd state, log out and/or restart to clean this up. (no just restarting Alssa wasn't doing it for me.. :)

As an added bonus you can view some information about your server by pointing your browser to http://yourserverIP:8000. There is a login option where you can use the username of "admin" and the password you setup in the sc_serv.conf file. Once logged in, you can kick or ban listeners, or add them to the Reserved IP list (which means a user will be bumped if needed to let the reserved IP connect).

Where to next?

Next on my list is to try to figure out how to get the DJ features working, and/or write some scripts to better manage the play list. I'm now having visions of my own personal radio station where I can play the music I like, and even promote the community groups I'm involved with (or reminding me of upcoming events with the groups). Heck, if you are feeling really energetic, you can easily turn this type of server into a business and setup commercials for an income stream. Eitherway, it'd be fun just getting everything running right. And then perhaps I can convert that friend of mine to use Linux full time.