How to turn the Raspberry Pi into a music server

I have set up my Raspberry Pi as a music and file server. My requirements were:

  • Big local storage (HD), shared over WiFi
  • Local music playing capability, remotely controllable
  • AirPlay speaker capability
The means by which I fulfilled the requirements were:
  • Platinum 1TB MyDrive and Edimax EW-7711UTn USB wireless adapter
  • mpd Music Player Daemon
  • shairport AirPort emulator
For the first part, I bought a WiFi adapter, the Edimax EW-7711UTn. This one works out of the box with Raspbian, using WPA encryption (Note: I switched to a RTL8188 based dongle by now). It identifies itself with lsusb as:
Bus 001 Device 004: ID 7392:7711 Edimax Technology Co., Ltd EW-7711UTn nLite Wireless Adapter [Ralink RT2870]
This can be easily configured using the wpa_gui tool that comes with the default Raspbian installation. Settings will be saved and restored upon reboot.
Second, I installed samba, samba-common and samba-common-bin for sharing my USB drive. The latter one is a Platinum MyDrive, which is attached to a powered Belkin 7-port USB hub, so that I only need two power supplies. One for the Raspberry Py, and one for the Hub and its attached devices. The MyDrive has been formatted with NTFS, so as to be easily mountable under Linux, OS X and Windows. I mount it using the standard /etc/fstab mechanism. Just added one line to the file:
/dev/sda1       /media/MyDrive  ntfs-3g defaults          0       0
The /etc/samba/smb.conf gets in its first iteration only one additional share, a write-for-all public share, as a big file dump: 
[BigDump]
    comment = Free for all
    read only = no
    path = /media/MyDrive/FreeForAll
    guest ok = yes
Note that literally everybody in your LAN can access this and write to it! You may want to fine tune this…
Now to the mpd. It is easily installed by doing apt-get install mpd. When configuring it via /etc/mpd.conf make sure to change the following lines:
music_directory         “/media/MyDrive/Music/”
password                “MyVeryOwnPassword@read,add,control,admin”
bind_to_address         “any”

Change the directory and password to your liking. Then restart the service or reboot you device. You can control the mpd using a magnitude of clients. For example Theremin for OS X or Mpod for iOS.
Finally, I would like to be able to use the RasPi as an AirPlay target for my Mac and my iOS devices. This can be done via shairport. There are already a lot of good howtos for shairport on the Raspberry Pi. So I refer you to one of those. Two things come to my mind, though:
  1. The Net::SDP library, required by shairport, is not available on Raspbian by default. It is best to clone the github mirror, and go by its installation instructions. Installation via CPAN fails, plus CPAN needs huge amounts of RAM.
  2. The configuration needs to be tweaked a bit. The /etc/init.d/shairport script should be tuned to a sensible name for your RasPi. 
This way, you will be able to see the RasPi in your AirPlay speakers list and it will be happily churning along.

Quoting a set of parameters for program arguments using sed

I have a script which launches another program. That program takes some command line arguments, which in turn can have parameters. Those parameters may include “;”, which has a special meaning in the Bash. To quote these, you can use the following sed command:

$ echo "foo --bar=1 --baz=2" | sed -e 's/(--[^[:space:]]*=)([^[:space:]]*)/1"2"/g'
foo --bar="1" --baz="2"

Improved handling of background GNU Global update

Earlier this year I detailed how to run gtags of GNU Global when Emacs is idle. However, this collides with some special Emacs modes, like ediff. That mode makes its own Emacs frame layout, which can be destroyed by the xgtags incremental update buffer. So I conceived a way to disable gtags temporarily as long as ediff is active:


;; This stuff is only needed, if you haven't got xgtags loaded yet
(autoload 'gtags-mode "gtags" "" t)
(require 'xgtags)
(add-hook 'c-mode-common-hook (lambda () (xgtags-mode 1)))
(defun global-update-incrementally () (shell-command "global -u -q" "*Messages*" "*Messages*") )

;; Call gtags update when idle for some time
(defcustom my-gtags-update-idle-time 60
"Number of idle seconds before an incremental gtags update is launched"
:group 'my-group
:type 'integer
)

;; initially allow gtags updates
(setq my-gtags-update-active t)

(run-with-idle-timer my-gtags-update-idle-time t
(lambda ()
(if (and my-gtags-update-active
(not (minibufferp) )
)
(progn
(message "Running gtags...")
(global-update-incrementally)
)
)
)
)

(add-hook 'ediff-quit-hook
(lambda ()
(message "Activating gtags update.")
(setq my-gtags-update-active t)
)
)

(add-hook 'ediff-before-setup-hook
(lambda ()
(message "Deactivating gtags update.")
(setq my-gtags-update-active nil)
)
)

Encode image sequence to MPEG4 video

Today something very simple: use ffmpeg to encode an image sequence as a MPEG4 video:

ffmpeg -f image2 -i filename-%04d.png -vcodec mpeg4 -b 6000k output.mp4

I often use blender or custom software to render out a bunch of images. Blender usually names output files filename-0000.png, where 0000 is the frame number.  So ffmpeg can generate those filenames with the usual printf style format. You can of course tweak ffmpeg’s output via a myriad of options. But this here will simply generate a 25fps mpeg4 file, which is easily embedded for example into your Keynote presentation. On OS X you can get ffmpeg for example via MacPorts: sudo port install ffmpeg, and Linux distros usually have a package for ffmpeg as well.

Using Ctrl+Arrow keys in the OS X terminal

The key combination Ctrl + left or right arrow key can be used to go a word left or right in many programs such as GNU Emacs or the bash. In the default configuration of the OS X terminal, this is not the case. That’s because Terminal.app sends the wrong key codes for bash’s default configuration. So you just need to got to the preferences (cmd+,) and set the key codes for Ctrl+left and right to 33b and 33f respectively:

Java Autocompletion for Emacs

The past few weeks I have been using the great eclim, and the corresponding emacseclim package. It allows you to use many of Eclipse’s features in your favourite editor. The installation of emacs-eclim is rather easy, and the one for eclim is even easier, since it comes with a simple clickety-click-installer.

I use emacs-eclim for Java development, and it works rather well. I use the two functions ac-complete-emacs-eclim and ac-complete-eclim-c-dot for auto-completion, which is the most important aspect when using such heavily object oriented languages and when developing large projects. Also useful are the functions eclim-java-import-missing, and eclim-java-remove-unused-imports. The current project can be compiled by calling eclim-project-build.

In theory, you can also use eclim with CDT for C++ development and auto-completion, but I rather stick with my earlier clang based solution, which is very fast and accurate.

An argument for Emacs window-local variables

The Emacs developers used argue that window-local variables are not neccessary, since there are indirect buffers, which can do mostly the same thing. In recent Emacs releases, the window state is anyway mostly hidden from Lisp. However, I think sometimes it is useful to have window-local variables, instead of buffer-local variables. Emacs is very buffer-centric, but sometimes the UI and the user’s view is rather window-centric. Two use-cases for window-local variables are:

I am using ECB, and it would be neat to enable or disable minor modes according to the ECB window I am in. This is also useful for people who manually tile their Emacs frame. In the main edit window, I like to have the tabbar, but in my compilation window, I’d rather not.

The buffer stack for a navigation history definitely should be window specific. Everything else would be very confusing. If a window is split, the new window will inherit its parents history.

Updating GNU Global GTAGS file when Emacs is idle

GNU Global is a very useful code navigation tool. In order for it to work well, it needs to be run regularly. Until now, I used the after-save-hook, to keep it up to date. However, when using emacs-eclim (also see Eclim), saving becomes so frequent that performance takes a massive hit. So I whipped up this small code snippet to run Global only when Emacs is idle for some number of seconds (default is 10):


(defcustom my-gtags-update-idle-time 10
"Number of idle seconds before an incremental gtags update is launched"
:group 'my-group ;; Put whatever customization group you like here
:type 'integer
)
(run-with-idle-timer my-gtags-update-idle-time t
(lambda ()
(if (not (minibufferp) )
(progn
(message "Running gtags...")
(global-update-incrementally)
)
)
)
)