Raspberry Pi Streaming Radio Player: Marginally Viable Product

The least horrible way to get events from the keypad turned out to be a simple non-blocking poll from Python’s select library, then sucking the event input queue dry; the main loop now does what might be grandiosely overstated as cooperative multitasking. Well, hey, it reads lines from mplayer’s output pipe and processes keypad events and doesn’t stall (for very long) and that’s multi enough for me.

It extracts the stream title from the ICY Info line, but I still haven’t bothered with a display. It may well turn out that this thing doesn’t need a display. The stream title will be enclosed in single quotes, but it may also contain non-escaped and non-paired single quotes (a.k.a. apostrophes): the obvious parsing strategy doesn’t work. I expect titles can contain non-escaped semicolons, too, which will kill the algorithm I’m using stone cold dead. Some try - except armor may be appropriate.

This code does not tolerate a crappy WiFi connection very well at all. I eventually replaced a long-antenna WiFi adapter with an actual Ethernet cable and all the mysterious problems at the far end of the house Went Away. Soooo this code won’t tolerate random network stream dropouts very well, either; we’ll see how poorly that plays out in practice.

The hackery to monitor / kill / restart / clean up after mplayer and its pipes come directly from seeing what failed, then whacking that mole in the least intrusive manner possible. While it would be better to wrap a nice abstract model around what mplayer is (assumed to be) doing, it’s not at all clear to me that I can build a sufficiently durable model to be worth the effort. Basically, trying to automate a program designed to be human-interactive is always a recipe for disaster.

The option for the Backspace / Del key lets you do remote debugging by editing the code to just bail out of the loop instead of shut down. Unedited, it’s a power switch: the Pi turns off all the peripherals and shuts itself down. The key_hold conditional means you must press-and-hold that button to kill the power, but don’t run this on your desktop PC, OK?

Autostarting the program requires one line in /etc/rc.local:

sudo -u pi python /home/pi/Streamer.py &

AFAICT, using cron with an @REBOOT line has timing issues with the network being available, but I can’t point to any solid evidence that hacking rc.local waits until the network is up, either. So far, so good.

I make no apologies for any of the streams; I needed streams behind all the buttons and picked stuff from Xiph’s listing. The AAC+ streams from the Public Domain Project give mplayer a bad bellyache; I think its codecs can’t handle the “+” part of AAC+.

All in all, not bad for a bit over a hundred lines of code, methinks…

More fiddling will happen, but we need some continuous experience for that; let the music roll!

The Python program as a GitHub Gist:



  1. #1 by madbodger on 2016-03-11 - 09:41

    The stream title will be enclosed in single quotes, but it may also contain non-escaped and non-paired single quotes (a.k.a. apostrophes): the obvious parsing strategy doesn’t work.

    To me, “the obvious strategy” is to use the CSV library. It deals with all of that splitting/quoting/escaping stuff for you.

    • #2 by Ed on 2016-03-11 - 10:06

      Hadn’t thought of that library, but the incoming metadata definitely isn’t valid CSV-format text: they’re not using it on the source end!

      As nearly as I can tell, the “format” consists of wrapping single quotes around whatever the stream title text might be, then sending that mess right out:

      ICY Info: StreamTitle='I'm a title; with "punctuation', y'know, because!';StreamUrl='';

      I haven’t (yet) seen a semicolon in a title, but any number and variation of quote characters within the outer pair is a given. It’s not UTF-8 text, either, but some gibberish that looked like mis-escaped junk from MS Word floated past.

      Some titles come from the Million Monkey Project’s slush pile…

      • #3 by Red County Pete on 2016-03-11 - 11:08

        Didn’t take long to find a semicolon. Roger Norrington conducting the London Classical Players:

        Beethoven: Symphony No. 7; Overtures (EMI Classics CDC 7 49816 2).

        It’s the only example I found, but I could imagine some stuff titled like line noise.

        • #4 by Ed on 2016-03-11 - 12:53

          My case, it is rested…

          On the other paw, we have WordPress relentlessly escaping every special character it sees, even ampersands that have already been escaped. Venturing down that rat hole doesn’t work, either.

  2. #5 by Keith Neufeld on 2016-03-11 - 14:18

    Clarification — which part(s) didn’t work well over wireless — the keypad control, extracting titles, or receiving audio?

    • #6 by Ed on 2016-03-11 - 14:48

      WiFi RF flat-out doesn’t make it from the far end of the house, Avahi / Bonjour / whatever can’t handle intermittent dropouts, and mplayer gives up when its buffer drains. All those problems Went Away when I removed the WiFi radio and plugged in an Ethernet cable: solved!

      FWIW, our Larval Engineer snaked that cable from a spare port in the hub over the Sherline in the basement, through the plumbing access hatch (her closet shares a wall with the tub), then under the door to her bed for better reliability and more bandwidth. Didn’t ask for permission, either; I was so proud …

  1. Streaming Player: NFS Program Distribution | The Smell of Molten Projects in the Morning