A beautiful web radio for a living room music system
Totally Happy?
The solution outlined above has some disadvantages. First, portals look to earn money and therefore regularly treat users to advertising spots. Second, many stations also transmit interesting additional data about the tracks and artists, which a plain vanilla music player does not reproduce (Figure 9). Thirdly, the portals usually offer only very limited options for recording their programs.
In principle, the solution consists of its own collection of links that bypasses portal and station websites and taps directly into the music source. Everything else about program logic is only for convenient control and navigation, as well as the appealing display.
A little research on the web turns up a number of web radios for the Raspberry Pi. The most comprehensive collection is provided by a web radio connoisseur named Bob Rathbone [7]. Besides the software, the project provides a 340-page document that explains the installation and configuration of various hardware and software components on the Raspberry Pi down to the minutest detail. Even if you have never owned an SBC like this, you can build a web radio without any problems, provided you separate the wheat from the chaff, which is not so easy.
However, Rathbone's solution relies on difficult-to-install software with a less than appealing interface, which ultimately pushed my decision in favor of designing a DIY solution.
Installation
For an initial test you will want to use a new, freshly installed Raspberry Pi OS – make sure you have the latest version (at the editorial deadline, this was "Buster"). A Pi Zero with the Lite version is perfectly okay for the project, unless you want an additional screen for the browser, as in the living room radio presented here. You also need the normal desktop version of the operating system and at least a second-generation Raspberry Pi, preferably later, because the graphical interface on the earlier versions is a little rough.
The installation commands are as follows:
$ sudo apt-get -y remove pulseaudio $ git clone https://github.com/bablokb/pi-webradio.git $ cd pi-webradio $ sudo tools/install pi $ sudo systemctl enable pi-webradio.service $ sudo systemctl start pi-webradio.service
You only need the first call for desktop systems, because PulseAudio, which is installed there by default, does not support audio output from a background service. If the default user on your system is not named pi, you also need to add the appropriate user to the installation command (command 4), who needs to be a member of the audio group and have sudo
rights. After the install, activate and start the service (last two commands). After doing so, you can access the web controls in a browser.
To get a landscape aspect, I optimized the responsive interface. On typical desktops, smartphones, and tablets, the system can be operated in portrait format without any problems. By the way, the web server built into radio software delivers exactly one web page to the browser once only. After that, the server and browser only exchange small blocks of data; the web browser takes over the display completely, ensuring that the server part of the software will run without problems, even on SBCs like the Pi Zero. A few details relating to the technical implementation can be found in the "Under the Hood" box.
Under the Hood
The Pi web radio software is based on Python 3 and uses the mpg123
command-line tool to play the audio data. The program provides a remote control mode with the -R
option. You can easily try this out by calling
mpg123 -R
at the command line and then typing h – the program will then display all commands.
The web server used here is Flask, a micro-framework that goes about its task in many Python application servers. The web radio uses only a few of its functions, because the web server just passes API calls like radio_play_channel
to the appropriate Python class, of which there is only one per task (e.g., Radio
, Player
, or Recorder
). Some auxiliary classes are available, as well.
The web interface uses CSS3 and JavaScript; jQuery acts as an auxiliary library. The application doesn't go all out to support all possible browsers, but today's crop of web browsers all offer what it takes.
Configuration
By default, the tool already supports a number of German stations, but for copyright reasons you do not get to see the station logos on launch (Figure 10). The broadcasters probably don't care, but dubious lawyers take every opportunity to earn money, and I don't want to give them any reason to sue me. It takes some configuration work to make the web radio serve your musical tastes and also present the station selection in a visually appealing way.
The channel list itself can be found in the /etc/pi-webradio.channels
file. It stores the name, a URL, and a logo (without path) for each channel. After clicking on the desired station, you can download and save the logos by right-clicking. The files in JPEG or PNG format belong in the /usr/local/lib/pi-webradio/web/images/
directory; you then need to enter the names of the logo files in the station list.
You need a bit more detective work for a broadcaster's URL, because not all broadcasters publish them, and those that do obfuscate the info as well as they can on their websites. The IT horizon of some broadcasters obviously does not go beyond their own web presence. If you can't find the URL despite an intensive search, you can still use the developer tools in your browser. First, navigate to the radio station page that supports live listening, but do not click the play button.
First launch the developer tools from the menu or by pressing F12. Another window opens, in which you navigate to the Network tab. Then press the live play button. The page now fills with all the URLs that the browser retrieves (Figure 11).
When you get there, search for a URL containing mp3
, m3u
, stream
, icecast
, or shoutcast
. The list is not exhaustive, but almost always takes you to the target. Copy the URL and start the playback with mpg123 <URL>
. The mpg123
playback program does not handle encryption, so you need to replace a leading https in the URL with http up front. If you enter the correct URL, you should now hear the station playing. Press Q to cancel the playback.
Besides the /etc/pi-webradio.channels
file, in the /etc/pi-webradio.conf
configuration file you can change the port for the web server (default=8026), for example, or specify the directory in which you will be storing any recordings.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Find SysAdmin Jobs
News
-
OpenMandriva Lx 23.03 Rolling Release is Now Available
OpenMandriva "ROME" is the latest point update for the rolling release Linux distribution and offers the latest updates for a number of important applications and tools.
-
CarbonOS: A New Linux Distro with a Focus on User Experience
CarbonOS is a brand new, built-from-scratch Linux distribution that uses the Gnome desktop and has a special feature that makes it appealing to all types of users.
-
Kubuntu Focus Announces XE Gen 2 Linux Laptop
Another Kubuntu-based laptop has arrived to be your next ultra-portable powerhouse with a Linux heart.
-
MNT Seeks Financial Backing for New Seven-Inch Linux Laptop
MNT Pocket Reform is a tiny laptop that is modular, upgradable, recyclable, reusable, and ships with Debian Linux.
-
Ubuntu Flatpak Remix Adds Flatpak Support Preinstalled
If you're looking for a version of Ubuntu that includes Flatpak support out of the box, there's one clear option.
-
Gnome 44 Release Candidate Now Available
The Gnome 44 release candidate has officially arrived and adds a few changes into the mix.
-
Flathub Vying to Become the Standard Linux App Store
If the Flathub team has any say in the matter, their product will become the default tool for installing Linux apps in 2023.
-
Debian 12 to Ship with KDE Plasma 5.27
The Debian development team has shifted to the latest version of KDE for their testing branch.
-
Planet Computers Launches ARM-based Linux Desktop PCs
The firm that originally released a line of mobile keyboards has taken a different direction and has developed a new line of out-of-the-box mini Linux desktop computers.
-
Ubuntu No Longer Shipping with Flatpak
In a move that probably won’t come as a shock to many, Ubuntu and all of its official spins will no longer ship with Flatpak installed.