Random Access Science

Geophysics, Glaciology, Computer Science and everything inbetween

XBMC Kitchen Radio

A while back I decided to turn a old Dell Inspiron Mini 10 netbook into a XBMC radio for the kitchen. My requirements where,

  • Stream music from my server
  • Stream online radio
  • Remote control from my other devices
  • Push media to device
  • Low power consumption

Installing XBMC from ppa

First add the XBMC ppa repository to your apt-get list and then install XBMC

1
2
 sudo add-apt-repository ppa:team-xbmc
 sudo apt-get install xbmc xbmc-eventclients-xbmc-send

Configure for low power consumption

I am a big fan of turning off my devices when I do not use them. I am going to use the laptop for less than a hour on average each day so I do not want it running 24/7. In Linux there are two powersaving modes: Suspension and Hibernation. During suspension the machine is still running and uses a bit of power. In hibernation the RAM is dumped to the harddrive and no power is used.

When you use a normal kitchen radio you just flick the power switch when you leave. I really wanted that feature in this device. Since I am using a laptop it can survive on battery power for a couple of hours. So I wrote a small script that detects if there is a active external power connection to the netbook and shutdown if the user turns of the power (on the wall socket).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash
PowerStatus=yes # Is the power on?
LinePower_Path=/org/freedesktop/UPower/devices/line_power_ACAD # Found using upower -e

while [ 1 ]
do
# Check current power status
PowerStatus=$(upower -i $LinePower_Path | grep online | awk '{ print $2}')

if [ "$PowerStatus" == "no" ]; then

# Test by sending a message?
#    notify-send "Power status: $PowerStatus"

# Mute sound to smooth things
xbmc-send --host=127.0.0.1 --port=9777 --action="PlayerControl(Stop)"

# Put into hibernation
sleep 2
sudo pm-hibernate

fi

# Sleep for 10 seconds and check status again
sleep 10

done

Here i use the upower tool in Ubuntu to check current power status. The path (/org/freedesktop/UPower/devices/line_power_ACAD) may vary between systems. You can determine it by running the UPower command,

1
   upower -e

When the power is cut it will stop whatever is playing using the xbmc-send command (Full command list) and put the system into hibernation. The script is run in a while loop so the status is checked every 10 seconds. To run the script on boot save it to disk and edit /etc/rc.local (works on Ubuntu). The file should look something like this,

1
2
3
4
5
   #!/bin/sh -e

   ./home/christian/detect_linepower.sh &
   sudo killall NetworkManager
   exit 0

Setting up streaming and media push functions

Music streaming works out out-of-the-box in XBMC. Just hook-up your server via a NFS or SMB connection to the client. To get streaming Danish TV/radio I normally install the addons developed by Tommy Winther.

If you have several devices you can push content using the UPnP features build into XBMC settings (UPnP/Client). With this I can push music from my laptop/server to the radio with zero setup. I do however need XBMC installed on the device that sends contents.

If you want complete control of the radio without having a monitor attached I suggest using the web interface build into XBMC. There are some nice addons available to improve on the web experience (Chorus).

Saving Matplotlib Figures Using Pickle

Matplotlib is a external library that ships with most bundles of the Python programming language. Providing a huge array of plotting functions is has completely replaced my previous workflow in MATLAB. I have how always missed the *.fig format that exists in MATLAB allowing user to save figures in a native format.

Since version 1.2 matplotlib now allows user to save figures using pickle (Matplotlib: Whats new). Pickle allows python to store most variables directly to a file while retraining the structure. It is similar to the save function in numpy, but more general. It is essentially a dump of python code that can store any kind of information. In this way you can dump the whole matplotlib figure handle into a file and load it on demand. I often work with large datasets that takes time to process. By dumping the figure handle I can quickly make small changes to a figure without processing the raw data behind it.

A Example

The code below illustrates how pickle can be used with python. Using matplotlib a simple sinus function is plotted. Then calling the pickle function dump we save the figure handle to a file called sinus.pickle.

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
import matplotlib.pyplot as plt
import pickle as pl

# Plot simple sinus function
fig_handle = plt.figure()
x = np.linspace(0,2*np.pi)
y = np.sin(x)
plt.plot(x,y)

# Save figure handle to disk
pl.dump(fig_handle,file('sinus.pickle','w'))

In a different python script we can load the figure back into python using the load function.

1
2
3
4
5
6
7
import matplotlib.pyplot as plt
import pickle as pl
import numpy as np

# Load figure from disk and display
fig_handle = pl.load(open('sinus.pickle','rb'))
fig_handle.show()

Running the above codeblock through iPython we can make a completely interactive workflow and just save our figures for later if need be. You can even extract the data behind figures,

1
fig_handle.axes[0].lines[0].get_data()

This will return x- and y-coordinates for the sinus function. This also works for 2D plots create with imshow or pcolormesh.

1
fig_handle.axes[0].images[0].get_data()

I use this feature all the time to combine existing plots and store subsets for my data for later processing. Most figures for publication are very different and using pickle I can maintain general plotting scripts and then later combine the data I need.

Extract Audio From DVD With VLC on Windows

This December my dad wanted to copy the sound from a couple of DVD concerts he had. Then he could hear it in his office or in the car. After a bit googling around I saw that VLC (Videolan.org) is able to extract the data. The problem was that VLC is only able to extract all the audio in one go. So all music is in one file… No way to skip from one track to the next. Not really what I wanted… The way out of this is to call VLC from the commandline and tell it to only play one chapter.

1
vlc dvd:///O:/1:2-1:2

This will play from title 1 and chapter 2 to title 1 and chapter 2. In this way you are able to open a single track at a time. But going in and using the GUI to record one track at a time is a bit cumbersome on a DVD with 32 tracks. So I ended up writing a script. My dad uses windows so it is in batch (sorry), but POSIX should work in a similar way,

1
2
3
4
5
set title=%1
set chapters=%2
set name=%3

FOR /L %%G in (1,1,%chapters%) DO CALL "C:\Programmer\VideoLAN\VLC\vlc" -I dummy -vvv dvd:///O:/#%title%:%%G-%title%:%%G --sout=#transcode{vcodec=none,acodec=mp3,ab=192,channels=2}:std{access="file",mux=raw,dst="g:\RIPDVD\%name%_%%G.mp3"}} vlc://quit

The above script assumes that the DVD is in the drive with letter O, the file is saved to “g:\RIPDVD\”, the output file is a mp3 file with bitrate 192.

To use the script edit the above parts to work with your system. When calling the script you must give it a couple of commandline arguments,

  1. The title to use (this can be found from a program like Handbrake)
  2. Number of chapters to extract (i.e. number of tracks in the concert. Can also be found using Handbrake)
  3. The base filename of the output file (i.e. filename_trackNumber.mp3)

If you decide to call the script rip-audioDVD.bat then you would call the script from the Windows commandline (cmd.exe) with

1
rip-audioDVD.bat 1 10 mymusic

This would rip tracks 1-10 from title 1 on the current DVD. The output would be call mymusic_1.mp3, mymusic_2.mp3, …, mymusic_10mp3.

So the script will run through all the chapters in the title and create one .mp3 file per track in the folder given. There may be a easy way to do this without scripting but this was the quickest way I could find.

Trying Out the Alfa AWUS036NH on Crunchbang Linux

Background

I travel a lot for conferences and workshops as part of my PhD. Most hotels claim to have “High-speed internet” and WIFI but most of the time that is a overstatement. So recently I bought the Alfa AWUS036NH long-range USB adapter. It is simply a USB network card with a 5dB antenna that will boost the number of networks you can access. So now even if you get the room farthest away from the hotel access point you may still have a change to connect.

It’s plug and play

On the website (bought if off amazon.co.uk) they claimed it would work on Linux 2.4/2.6. My current kernel is 3.10 so I was a bit afraid it would not work. Plugged it in and it was ready… That was a first. Depending on your system you may need to install the Ralink firmware (on debian: apt-get install firmware-ralink). I did have some problems getting access to networks on kernel 3.2.0 but after the switch to 3.10 everything was fine.

The card takes a minute or so to “warm up”. After that I was able to see about three times the number of networks with the 5 dB antenna. I also bought a 9.5 dB antenna just because I had to try it out. It is massive (There wasn’t any scale on the promotion pictures). It is massive! About the same length as my 13” macbook air. If you didn’t look like someone trying to crack WIFI passwords before you will with this.

Does it work?

I am quite impressed with the device. Allowing the adapter to be on for about 5 minutes I got the following statistics using the list in the network manager.

  • build-in Macbook Air WIFI: 7 network,
  • Using the 5 dB antenna about 31 network.
  • Using the 9.5dB antenna: 39 networks

So not that big a difference with the large antenna but it still makes a difference. The list of available networks does seem a bit unstable. When connected to a network the list shrinks but that is perhaps because some power is used on the current connection.

Conclusion

Do I recommend the Alfa AWUS036NH for linux users? YES, it great. Do I recommend the 9.5dB antenna? Well if you think you need it then yes otherwise it is quite large and removed a lot of the portability of the device. Also with the large antenna the device will tip over because of the weight. You can find a “stable” position where it will stand but the smallest push will tip it. But that is a small price to pay and can be fixed with a piece of paper under the antenna. I also do not quite know how it is to fly with the device. I am sure it will not be a problem but you may get pulled over in security to explain the large antenna you have in your carry-in :)

Update: I have now tried the device a couple of places. At a random coffee house in Helsinki (Finland) I got 14 networks using the build-in device and 27 networks using the 5dB antenna. So quite a difference. I also tried putting the device in my carry-on and did not have any problems. I also experinced that the device uses a lot of power. So if you run in on a laptop expect that you battery life will decrease by perhaps 30% – 50%.

NFS on Debian

There are plenty of guides on the internet about NFS. Still it took me ~2 hour and 15 different website to get NFS working on my home server. The reason? Well firstly, a lot of NFS posts are outdated because of a switch from portmap to rpcbind. Secondly, NFS was never properly installed. Because NFS is part of the kernel the nfs-kernel-server and nfs-common files can still be on the system even without the packages being installed.

The best source I found to setup NFS on debian is: http://debian-handbook.info/browse/stable/sect.nfs-file-server.html