How to stream FM Radio using the Command Line in Linux
With all the advancements in IT, one piece of technology has persisted and seems not ready to die yet: FM radio. Unlike its companion AM, FM radio continues to be the primary source of information and entertainment. For example, in the United States, FM radio is still alive, and is used by many as a discovery medium. Most people especially teenagers will tell you that they first heard a song on terrestrial FM radio, before they went on to streaming sites such as YouTube, Spotify etc. In Uganda, like elsewhere in Africa, expect traditional FM radio to continue being king of information. Our limited Internet infrastructure is still very limited to a small section of the population, speeds are still horrible, so on-line radio is still not an option for many.
As I work on my upcoming digital product, I debated a lot about whether to include FM radio as a feature on this device. With all the above said, there was no doubt, FM radio would quickly become one of the most requested for features. So I got down to playing, recording, and streaming Live FM radio using the Linux command line. The cool bit is that you can stream a live FM radio show to all your devices in the house or office. No need for you to buy a radio.
What you need:
a) Analog Video for Linux (V4L2) FM radio tuner. You can either use a USB stick or PCI-E based card.
b) Linux distribution – Am using Fedora 20. But any Linux distro will work, although you will need to adjust the install commands where necessary.
Available Linux CLI tools:
There are a whole lot of GUI based applications that you can use to play radio in Linux, For example Gnomeradio, Kradio, gradio etc. However, using the command line gives you much more flexibility, as the following commands illustrate. You can control an FM radio tuner using CLI tools like MPlayer (which is installed on most Linux distros), ivtv-radio, and fmtools. In this ‘how to,’ I use the latter, fmtools, which comes with a set of tools (fm and fmscan) for controlling V4l2 based tuners.
Installation:
1. Install fmtools
yum install fmtools
(On Red-Hat based Distros such as Fedora, CentOS)
2. Install SoX
yum install sox
(On Red-Hat based Distros such as Fedora, CentOS)
3. Install VLC
yum install vlc
(On Red-Hat based Distros such as Fedora, CentOS)
FM Radio Commands
The basic arguments for the fm command line tool are:
fm -d [device] -T [time] [freq] [on|off] [percentage]
-d The radio device created by your USB or PCI based tuner
-T The duration of play
-q quiet | No verbose
[Freq] The radio channel
[on|off] Turn on or mute radio
[Volume] The desired volume
Tune and play FM 91.3
fm -q -d /dev/radio0 -T forever 91.3 100%
Mute:
fm -q -d /dev/radio0 off
Unmute:
fm -q -d /dev/radio0 on
Recording FM Radio
To record FM radio, use parec a utility that is packaged with pulseaudio, the default sound subsystem on most Linux distros.
1. First tune to the desired radio station as shown above.
2. Capture the audio:
parec -d alsa_output.pci-0000_01_01.0.analog-stereo.monitor | sox -t raw -r 43000 -e signed -L -b 16 -c 2 - /var/tmp/fmradio.wav
NOTE: You will need to change the variable -d i.e. output pulse device for your system. To find yours, just run the command:
pactl list | grep -A2 '^Source #' | grep 'Name: .*\.monitor$' | awk '{print $NF}' | tail -1
The recording will be stored at /var/tmp/fmradio.wav. You can change that to another location if you wish.
Stream FM Radio to other PCs or devices (such as smart phones)
Start the capture process as shown above, and proceed immediately to the stream command below:
vlc -I dummy -I http --daemon --no-video --no-sout-display-video --verbose 2 /var/tmp/fmradio.wav --sout #duplicate{dst=standard{access=http,mux=asf{title='JambulaRadio',author=FM-91.3-Radio-Kampala(Uganda),copyright=2015-2016-Joseph-Zikusooka-All-rights-reserved,comment=Broadcasting-Live-from-Kampala-Uganda,rating=PG14},dst=192.168.0.10:8090},dst=standard{access=http,mux=asf{title='FM-Radio',author=FM-91.3-Radio-Kampala(Uganda),copyright=2015-2016-Joseph-Zikusooka-All-rights-reserved,comment=Broadcasting-Live-from-Kampala-Uganda,rating=PG14},dst=172.16.0.1:8080}"}
NOTE: Here, I’m streaming on both wired and wireless LAN, and my machine IP addresses are 192.168.0.10, and 172.16.0.1 respectively. The http stream port is 8080. Change these to suit your environment.
If you are using a wired connection, use VLC on your client PC or mobile phone to access the stream as follows:
vlc http://192.168.0.10:8080
If on Wireless, use vlc on your client PC or mobile phone to access the stream as follows:
vlc http://172.16.0.1:8080
Enjoy!