Building a Streaming Radio Station, Part 1: Icecast, MPD, and Python


I run an anonymous message board for Stockton College – stkbuzz.com. It’s fairly active, and due to the nature of the site, it gets some pretty amazing posts. I had the idea of recording dramatic readings of some select posts, but the problem was that I didn’t have a good way of delivering audio content to my audience. After bouncing some ideas off of my co-workers, I decided that the best way of doing so would be via an online streaming radio station, accessible from a player embedded into the bottom of the website – enter stk.fm, the Buzz.

Icecast and MPD

I researched my options for streaming audio in a radio-like fashion, and it seemed like Shoutcast and Icecast were the two most popular. After previously having a less-than-stellar experience with Shoutcast, I decided to give Icecast a shot. I’m happy to report that so far, it’s working great – I haven’t had a single problem with it yet.

I planned the “structure” of the content that would be played fairly early on. The structure looked something like this:

  • An intro segment (sometimes known as a radio bump)
  • A talk segment (such as a dramatic reading)
  • Another bump
  • Either one or two songs
  • Repeat!

This would obviously require a rather fine-grained level of control. The nice thing about Icecast is that it can use MPD as an audio source. MPD (Music Player Daemon) is, as the name might suggest, simply a daemon that plays music from a library. It doesn’t play music directly, however; it acts as a server which requires a client. In this case, Icecast is our client (this was incredibly easy to configure). Icecast streams whatever the source gives it, and since we can control MPD directly, we can essentially control Icecast directly.

Serving Content

The next step was to automate adding tracks to the MPD playlist. I wrote a Python script that acts as a DJ, playing tracks in the structure I originally settled on. The script does the following:

  • First, it looks at a set of directories that I’ve specified and generates a “segment” for each MP3 it finds – a segment simply being an ordered list of MP3s that will be added to the MPD playlist. Typically, a segment will consist of a randomly chosen bump, followed by the content. However, I built in a simple regex check that looks at the filename of the content and can prepend or append specific bumps (for example, any filename that begins with the string “read_” is a dramatic reading, and should therefore always be prepended by the bump that says “and now, a dramatic reading…”).
  • After the segments are generated, they are dumped into a pool with segments of their own type (talk or music) to be later pulled out and copied into the MPD playlist when MPD is playing the last song in the list (from within Python, I used MPC to interact with MPD). At that point, the segment is put into a queue and will be placed back into the pool of playable content after a certain number of other segments have been played (this allows for the content to be continuously shuffled).

 

Part 2 of this blog post will cover the client side player as well as streaming analytics. For now, you can check out stk.fm on stkbuzz.com!


Leave a Reply

Your email address will not be published.