Archive for March 2012

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!

Process substitution, Apache’s Velocity, PHP Eval, and Hexagonal Game Boards and More

We held a DCS meeting on the 23rd of March and had the following presentations:

Jason Schollenberger presented on some of the different features he’s added to his IRC bot, and on a quick-and-dirty script for parsing Google widget data. His PHP code takes a Javascript array, prepends a sigil ($) and appends a semicolon. At this point, the array is a valid PHP script. This string is passed to PHP’s eval function to become a valid PHP array. From there, the code is able to choose the desired element. Jason also presented an overview of recent improvements to his IRC bot that allow users to query several JSON-based APIs.

Stan Schwertly presented on several different topic and prepared slides, which are available at:

http://wiki.schwertly.com/everyone/dcs-mar-23-12.html

Among the topics presented are a song downloader for wearehunted.com, Bash’s process substitution, a scraper for stat information for etsy.com, an Apache HTTPD directory size counter, strace example, and an explanation of improvements to his IRC bot.

Aniello DiSpigna presented a demonstration detailing the usage of Apache’s Velocity, a “simple yet powerful template language to reference objects defined in Java code.” Aniello is a long-standing attendee of DCS and this marks his first presentation and meeting as member of the group.

Thomas Rush presented on his work constructing a hexagonal gameboard in Python using pygame, along with some of the struggles that come along with using this style of map. He reviewed some existing perspectives on coordinate systems for hexagonal maps, and discussed his own algorithms for line-of-sight detection and pathfinding.

Cheers to the Dead Coder Society