The Nokia N800 internet tablet has a feature you may not know about: a built-in FM radio tuner that you can script using Python. Here’s how it works.
First, let’s take a quick look at the radio. Go to “Application Manager,” select “Browse installable applications” and find “fmradio.” Install it. If you can’t find it, take a look in “Show installed applications” instead—you might have it installed already. Or you may need to get into developer “Red Pill” mode on your N800. See my previous article about running simple Python scripts for instructions.
Because the radio is a desktop applet, you’ll need to close or reduce all of the windows until you see the N800 desktop. Pull down the desktop menu and choose “Select applets…” Choose “FM Radio” and drag it somewhere to get started.
Playing with this application can give you a sense of what the FM tuner is capable of. Remember to plug something into your N800’s headphone jack to get a signal, or else you’re just going to get a bunch of static. Also make sure to tap the little antenna icon in the lower right corner of the main radio application window. With this, all of your local radio stations will load up automatically.
Now that you’ve seen that your N800’s FM tuner works, let’s dig into pyFMRadio.
Martin Grimme, a developer out of Germany, has written a Python module called pyFMRadio that makes it quite easy to control using Python. Go here and download the most recent version onto your computer (or download it straight to the N800). Unzip the latest version of pyFMRadio and send the unzipped files “FMradio.py” and “example.py” to your N800 via Bluetooth. Remember to keep your files on your N800 somewhat organized. I put all of my Python scripts in a folder called “Python” inside of my main Documents folder. The paths I use in this article refer to that directory. If you’re putting these files elsewhere, you’ll need to adjust paths accordingly. Regardless, both files need to be in the same folder.
Now, let’s run FMradio.py. Type this into the command line:
cd /home/user/MyDocs/.documents/Python - Change to your Python directory.
python example.py - Run the pyFMRadio example script.
When you run this script, you will see output as the script scans all frequencies for available channels. It will then go through and play each channel that it finds for a few seconds. (Remember to have something plugged into the stereo jack on your N800 or you’ll just get static.)
When you’ve had enough of this, take a closer look at example.py by opening it up on your computer. It’s fairly short, and even if you don’t know Python very well you can probably make some sense out of it.
What we are going to do is very simple. We are going to modify the “Hello World!” script I wrote about in my previous article so that it plays a radio station of your choice and displays simple details about that station on the screen. This is going to be the most featureless radio ever—you won’t even be able to change channels—but it will get us comfortable using pyFMRadio. (If you have a more extravagant idea, please share it in the comments section.)
Here’s the Hello World script again:
#!/usr/bin/env python2.5
import gtk
import hildon
window = hildon.Window()
window.connect(”destroy”, gtk.main_quit) label = gtk.Label(”Hello World!”)
window.add(label)
label.show()
window.show()
gtk.main()
All this does is display “Hello World!” in an N800-style window. If you did the previous tutorial, it’s still probably in your Python folder.
Here’s the code for a new file called “hello_radio.py”:
#!/usr/bin/env python2.5
import gtk
import hildon
from FMRadio import FMRadio, FMRadioUnavailableError
# set your variables
volume = 50
freq = 97.9
label = gtk.Label(”Playing %250.1f FM.” %25 freq)
# start the radio
try:
radio = FMRadio()
except FMRadioUnavailableError:
label = gtk.Label(”Oop! Your device has no radio.”)
radio.set_volume(volume)
radio.set_frequency(freq*1000)
window = hildon.Window()
window.connect(”destroy”, gtk.main_quit)
window.add(label)
label.show()
window.show()
gtk.main()
radio.close()
What’s going on here? It should look very similar to your “hello_world.py”, but with some new radio stuff in the mix. You’ll note that we have to import the pyFMRadio modules on line 5 (”from FMRadio import FMRadio, FMRadioUnavailableError”). We then set our volume and frequency to whatever we would like and set the label to show the station we have chosen on line 11. If you’re used to Java or PHP, you may find the syntax to be a bit odd. Basically the “%250.1f” means we’re going to display a float out just one decimal place. “%25 freq” tells Python which variable to display.
Lines 13-17 are the tricky part. Basically, these tell your N800 to *try* loading up a radio object for use. If there’s a problem (a.k.a. an exception), then it will display an error on screen instead of the FM frequency. PHP programmers don’t deal with this sort of error-catching structure very often. Java and C programmers will be more used to it.
Then, lines 19 and 20 turn the radio to the right volume and frequency—the radio will start playing automatically. And that’s it! You have a fully scriptable radio you can access via Python scripts.
The only remaining piece is to make sure to close the radio before fully exiting the script. If you don’t do this, your radio will continue to run even if you quit the application, which could be annoying. The radio.close() function on the last line of the script takes care of it.
As you see, scripting the FM tuner on your N800 opens up a lot of possibilities. By adding a few widgets—maybe a slider to choose the station and one to adjust the volume—you could make your own full-featured FM radio. You could even use the audio recording tools to make a radio TiVo or something cool like that. If you do, share your ideas and developments in the comments section.