Play audio streams on OpenWrt (Internet Radio)

 Author:   Posted on:   Updated on:  2020-06-08T18:50:17Z

Add an USB audio card to OpenWrt router and play audio files and internet streams

I own some old ADSL modems that are no longer suitable for modern networks. OpenWrt can be installed on them, but they are limited by hardware to 100 Mbps LAN and 54 Mbps WiFi. Therefore, using these devices as routers, network attached storage or anything else that requires high transfer speeds is no longer wanted. Fortunately, OpenWrt comes with many software packages available to install using its included package manager.

One of the tasks that are suitable for most low-speed OpenWrt routers is audio playing. However, there are some hardware requirements. You need a router on which you can install OpenWrt firmware. It must have at least 8 MB flash storage memory and, the most important: at least one USB port. I haven’t heard of routers with audio output, yet there are plenty with USB ports (for GSM modem or USB storage). With an USB sound card and proper software, you will be able to play audio from any OpenWrt router. In this post I will talk about internet radio streams. However, if you have an extra USB port or you plan to use a hub, you may also play music files from USB drive.

USB audio card plugged in the USB port of the router

USB audio card plugged in the USB port of the router

I will not tell you how to install OpenWrt on your router. This is slightly different depending on your hardware. Follow the instructions for your device from OpenWrt documentation. Besides this you need an USB audio card. As you can see in the photo above, I’m using the cheapest one, which I do not recommend. It is based on C-Media CM108 chipset and the good thing is that it is supported by OpenWrt. However, the audio quality of this card is very poor.

Client Device

So, I’m assuming you got OpenWrt installed and you are now logged in with a password set at the LuCI web interface using the default IP of 192.168.1.1. You have your computer directly connected to OpenWrt device using a LAN cable. If you already had OpenWrt installed, I recommend doing a full reset.

We have to turn this router into a network client. Therefore, no DHCP server and no firewall should be running. Start by going to Network - Interfaces and click the Edit button for the default LAN. There are two ways of proceeding here:

  • DHCP client, where your OpenWrt device gets an IP address from the router it will connect to. I prefer this method, although some may not like the fact that they have to log in to the router and check its clients list to find the IP of the OpenWrt device. In the General Settings tab:
    • Protocol: DHCP client
    • Hostname: NetRadio (or any name you like)
  • Static client, where you pre-set the IP address of the OpenWrt device to one allowed by the router it will connect to. Works very well, unless you change your network configuration or connect the OpenWrt device to another network. In the General Settings tab:
    • Protocol: Static address
    • IPv4 address: an IP that's allowed in the network you will connect this device.
    • IPv4 netmask: same as your main router (usually 255.255.255.0)
    • IPv4 gateway: your main router's IP
    • Use custom DNS servers: usually main router's IP or any other DNS servers advertised by main router. I usually add 8.8.8.8 and 8.8.4.4 and it works fine.
    • Move to DHCP Server tab and check Ignore interface to disable the DHCP server on OpenWrt.
Set up OpenWrt as static client

Set up OpenWrt as static client (modify IPs according to your network)

Do not apply settings yet. Go to System - Startup, look for firewall and click the green Enabled button to disable firewall service. Also, go to System - System, General Settings tab and change Hostname to any name you like.

Save and apply the modified settings (from the top right corner) and be prepared to lose the connection to OpenWrt. That's normal. Leave it for a minute or so to save changes, then power cycle the device and connect it to your LAN using a cable. Wait for it to start, then from a computer in the same LAN, open the main router's administration page, log in and look for the list of LAN/DHCP clients. Here is how it looks on my TP-Link:

DCHP clients list

DCHP clients list

To access LuCI web configuration utility, I have to go to 192.168.2.5 from any device in the same network. Since the firewall is disabled, connection will not be denied. In my list you will see another device, NetRadioW, which is the same OpenWrt device connected to the same network, but this time over WiFi. How it's done? Well, go to Network - Wireles, click Scan next to radio0, choose your WiFi, enter credentials to connect, save and apply new settings. After this, go to Network - Interfaces, but this time edit WWAN (wlan0) interface. Use the same approach as you did with LAN.

You can disconnect LAN cable now, find the new IP assigned over WiFi and access LuCI at that address (or go directly to the static IP if that's what you have chosen). You should be done by now. Let's make one last test: go to Network - Diagnostics and run the ping command. You should get a valid response. If not, verify DNS servers (in the case of static IP) and make sure firewall is stopped.

Play audio

You have to install some software packages. It can be done from LuCI, System - Software. Click on Update lists first, then you're ready to install packages. Since there are seven to install, I prefer the command line over SSH. Use PuTTY to connect to OpenWrt device IP and install multiple packages at once. For hardware support:

opkg update
opkg install kmod-usb-audio kmod-sound-core alsa-utils usbutils
opkg install kmod-sound-hda-codec-cmedia

The last package is specific to my audio card. Yours might by different and you should install the proper driver. Search in LuCI, System - Software for kmod-sound-hda-codec. If it displays nothing, update lists again and be sure you get Signature check passed. output for each of the downloaded lists (otherwise you're offline). At the time of writing this, for mips32 platform, on OpenWrt 19.07, there are 11 sound codecs available to install (Analog Devices, Creative CA0110, Creative CA0132, Cirrus Logic, C-Media, Conexant, audio over HDMI, Sigmatel IDT, Realtek, Silicon Labs 3054 and VIA). Check this list if you're going to buy a supported audio card.

Once installed, let's check everything works. If you're not on the command line and you installed those packages from the web interface, now you have to. I forgot to mention that you can get a serial command line to with some USB to TTL serial adapter wired to the device's serial port. Check the presence of audio card and adjust its volume:

root@NetRadio:/# lsusb
Bus 002 Device 002: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
root@NetRadio:/#
root@NetRadio:/# alsamixer

┌────────────────────────────── AlsaMixer v1.1.7 ──────────────────────────────┐
│ Card: USB PnP Sound Device                           F1:  Help               │
│ Chip: USB Mixer                                      F2:  System information │
│ View: F3:[Playback] F4: Capture  F5: All             F6:  Select sound card  │
│ Item: Speaker [dB gain: -11.31, -11.31]              Esc: Exit               │
│                                                                              │
│                  ┌──┐               ┌──┐                                     │
│                  │  │               │  │                                     │
│                  │  │               │  │                                     │
│                  │  │               │  │                                     │
│                  │  │               │  │                                     │
│                  │  │               │  │                                     │
│                  │  │               │  │                                     │
│                  │▒▒│               │  │                                     │
│                  │▒▒│               │  │                                     │
│                  │▒▒│               │  │                                     │
│                  │▒▒│               │  │                                     │
│                  │▒▒│               │  │                                     │
│                  ├──┤               ├──┤               ┌──┐                  │
│                  │OO│               │MM│               │OO│                  │
│                  └──┘               └──┘               └──┘                  │
│                 47<>47               0                                       │
│          <     Speaker      >       Mic         Auto Gain Control            │
└──────────────────────────────────────────────────────────────────────────────┘

From what I've noticed, the volume seems to reset to maximum after each boot. And that crappy audio card of mine outputs a very loud signal (I have to set volume to 3-4 percent to listen with the headphones). Anyway, we'll see later how to change volume from command line.

For the audio playing software, I chose mpd/mpc because they support various files and streams types. I was able to play AAC internet radio only with mpd. No other OpenWrt player supported this format.

opkg update
opkg install mpd mpc

Now, either reboot OpenWrt, or go to LuCI web interface, System - Startup, find mpd and start it. There is also the command line approach:

/etc/init.d/mpd start

It's easy to play a stream now. Here are two examples (first is AAC, second is MP3):

root@NetRadio:/# mpc add http://live.radiocafe.ro:8048/live.aac
root@NetRadio:/# mpc play
http://live.radiocafe.ro:8048/live.aac
[playing] #1/1   0:00/0:00 (0%)
volume: n/a   repeat: off   random: off   single: off   consume: off
root@NetRadio:/# mpc current
Radio Cafe Romania: GLORIA ESTEFAN - CONGA
root@NetRadio:/#
root@NetRadio:/# mpc clear
volume: n/a   repeat: off   random: off   single: off   consume: off
root@NetRadio:/# mpc add http://stream2.srr.ro:8370/tunein/acirtina.asx
root@NetRadio:/# mpc play
http://stream2.srr.ro:8370/tunein/acirtina.asx
[playing] #1/1   0:00/0:00 (0%)
volume: n/a   repeat: off   random: off   single: off   consume: off
root@NetRadio:/# mpc current
Radio Oltenia Craiova: Radio Romania Oltenia Craiova
root@NetRadio:/#

When it runs, mpd has a temporary playlist (queue). When you use mpc add, you add an entry to that playlist. If you add multiple entries, you can use mpc prev and mpc next to switch to another playlist entry. mpc clear stops current playing entry and deletes all entries from playlist. You may be able to control volume from mpc, using mpc volume [+-] (it doesn't work for me). There are many other options available, including local files and playlist management (read the documentation). Note that you cannot add and play playlist URLs (ending in m3u or similar). Paste such a link in browser, download the playlist, open it with Notepad and copy the direct stream URL for mpc add.

Volume can be changed using amixer command:

amixer sset 'Speaker' 1%+
amixer sset 'Speaker' 1%-

Absolute values are accepted without +/- sign. Decibel values are accepted too, as confirmed by amixer help. Where did I came up with 'Speaker'? Well have a look at the Alsa Mixer screen above and modify with what you have.

Conclusion

If you followed this, your OpenWrt router is now a network client able to play audio files and internet streams. What's missing? A hardware addition that will improve usability: there will be some follow-up posts, where I will attempt to add a serial "controller" - a microcontroller with buttons and LCD which will send commands and parse their outputs. As it is, you can control mpd with an Android smartphone connected to the same WiFi network (hint: M.A.L.P. - MPD Client). There is also the possibility of adding an USB HDD where you can store and play music from.

References

No comments :

Post a Comment

Please read the comments policy before publishing your comment.