Friday, 25 April 2014

Tagging a music library for use with a Sonos system

I migrated my iTunes music library to a NAS built with a Raspberry Pi so it could be shared with my Sonos system. Before this the only thing reading any metadata on the files was iTunes which can sometimes do weird things. Coupled with that, I've previously used TuneUp to fix broken metadata within iTunes. It works very well in that context but it seems that the metadata it writes is non-standard in some way and other programs will not read it. All this manifests in some of my music only appearing in the track listing part of the Sonos interface, being listed by filename rather than track name and not being listed by album or artist at all.

What do I need?

Sonos reads standard id3 tags - apparently v1 or any of the v2 options. The files need to be tagged in one of these formats.

Fixing things up

My files are kept in a consistent directory structure looking like this:

blah/Music/Gun/Swagger/01 Stand in line.mp3

My first plan was to use a Linux command line utility for reading and writing id3 tags and pull the required information from the directory path using some clever scripting. I've been away from work for over a week so my brain isn't really up to the required "clever" scripting at the moment. Fortunately, when building the NAS as per my last post I made all my music available over a samba share so it was easy to mount that on my desktop machine and get access to the files that way.

There are dozens of GUI id3 editors. I chose to use mp3tag (my desktop is a Windows machine) and hoped I could somehow convert the tags produced by TuneUp and used in iTunes to something more standard. Unfortunely mp3tag lists the TuneUp tags as "Bad Tags". Drat. Well, at least that explains the problem.

So, armed with my new clicky it's back to plan 1. Having found the files which needed attention I used the "filename-tag" converter with this pattern:

%artist%\%album%\%track% %title%

Slashes reversed because we're in a Windows environment now.

This solved the vast majority of the problems. There were still a few albums tagged in a bizarre way - with no track numbers and a title of something like "01 Intro". I didn't want to completely nuke the existing tags for these as they included Album Art so it was time to break out the "tag-tag" converter which lets you use existing information in the tag to populate other fields.

We need two different filters here, applied in this order because the second removes information needed for the first.

Field: TRACK
Pattern: $left(%TITLE%,2)


Field: TITLE
Pattern: $cutLeft(%TITLE%,3)

These methods came from the scripting section of the mp3tag documentation. It turns out it's quite powerful, even if the methods don't always do exactly what the docs suggest they will do.

Job done.

Next steps

My album art is all extremely patchy. Sonos is now my primary playout system at home and it takes an age to load album art so I'm not too bothered about fixing it, however if I change my mind there are a couple of options. Firstly, mp3tag lets you paste album art into the box rather than having to use a presaved photo. I can look up album art in iTunes and copy from there for the occasional simple fix.

If I want to do a proper, automatic job I'll probably take a look at Album Art Downloader which is apparently very good and can be run as a tool within mp3tag. That's fun for another day though.

Monday, 21 April 2014

Using a Raspberry Pi as a headless media store on a Sonos network

This is to build a headless media centre and is also being built that way - that is, I'm not going to plug a screen or keyboard/mouse directly into the Pi. This means that I can install the version of Debian without the window manager which makes it far lighter on the processor.

It's not difficult, but I've compiled my notes and written some English to link them together as someone may find them useful. I'm going to assume some basic Linux and home networking knowledge.

You will need

Sourced from Amazon, total cost (at time of writing, without hunting around) is: £47.60

You'll also need some USB hard drives. The Pi (model B here) only has USB 2.0 ports so you can use older drives if you're not planning on plugging them in anywhere else. Also, the Pi runs on very low power so you really want powered drives or a powered USB hub to sit between them. If you want to back up your data, you're going to need two drives. If you don't, you can ignore all the stuff about rsync that comes up later.

Let's go

Up and running

  1. download Raspbian
    Sadly, the NOOBS install method requires the use of a screen so wont work for us.
  2. install Raspbian onto the SD card
    These are the instructions for prepping the card on a Windows machine.
  3. put the Pi in the case, plug in the ethernet cable and put in the SD card
  4. finally plug in the power cable and we're off

Connect and update

Find the IP address of your Pi, shell to it and log in (default username / password is pi / raspberry). You need to run:
sudo raspi-config
and select the option to expand the root filesystem to fill the SD card. It's important you do this before any of the other steps or you will be starting again.

Next update the operating system via:
sudo apt-get update
sudo apt-get upgrade
This will likely take a while.

Connect the drives

Plugging the drives in will auto-mount them, however if we are using one drive as a backup to the other we need to ensure they are always mounting the same way round on restart. To do this we will identify the drives via their UUID in /etc/fstab:
UUID=0AC4D607C4D5F543 /mount/location ntfs rw,defaults 0 0
To find the UUID of the drives:
blkid /dev/sda1
blkid /dev/sdb1
Remember that if you format a drive, the UUID will change.

Install the software

Time to install some useful software.

Samba shares specified parts of your filesystem on the network. It will allow you to mount the drive on another computer which will let you put files on your media server when it's ready. It is also the shared area that Sonos will be able to search for music files.

Minidlna shares an area of your filesystem via DLNA. This means a DLNA-ready device can locate the share and request any files in it. I use this for watching video on my iPad (my viewing program of choice being 8player).
# install samba
sudo apt-get install samba samba-common-bin

# install minidlna
sudo apt-get install minidlna
I also installed some extra tools to make other tasks easier. You don't need them for this build.
sudo apt-get install tree locate chkconfig

Configure samba

To configure samba you need to edit /etc/samba/smb.conf. You'll want something like this:
    netbios name = NETBIOS_NAME
    workgroup = WORKGROUP
    security = user
    encrypt passwords = yes
    smb passwd file = /etc/samba/smbpasswd

    comment = My media
    path = /PATH/TO/SHARE
    writeable = yes
    create mask = 0770
    force create mode = 0770
    locking = yes
Mixed to taste, of course.

Then add the user to the samba password file:
sudo smbpasswd -L -a USERNAME
Restart samba and you should then be able to mount the drive on other machines on the network.

Configure minidlna

The minidlna config file is found at /etc/minidlna.conf

Docs on configuring minidlna are available on the minidlna site. The important bits are:
media_dir=A,/home/user/Music      # Music directory
media_dir=P,/home/user/Pictures   # Pictures directory
media_dir=V,/home/user/Videos     # Video directory
friendly_name=Laptop              # Name of the share
db_dir=/var/cache/minidlna        # Index files (make sure location is writeable)
log_dir=/var/log                  # Log files (make sure location is writeable)
inotify=yes                       # Index new files as they are added
It's important to make sure the minidlna user can read and write to the index and log directories and read the media files. Alternately, if you're lazy like me you can make minidlna run as the main user by editing /etc/init.d/minidlna (around line 68). Obviously this is not recommended for any significant install of anything, but I'm assuming we're at home on a very limited access network.

When starting minidlna you may get an error message claiming to be exceeding the watch limit. To increase this limit, add to /etc/sysctl.d/90-inotify.conf
fs.inotify.max_user_watches = 100000
and restart.

Note that there is a new version of minidlna on the way called ReadyMedia possibly rendering this completely obsolete.

Back up the drives

I wanted to keep a backup of my media on the second drive. I could have used RAID 1 but reading around suggested that the amount of read/write information going across the USB bus would cause a bottleneck and therefore problems serving the files. To get around this, I use rsync to copy everything across on a nightly basis.

Something like this in the crontab will sync the drives each night at 3am:
0 3 * * * rsync /mountpoint/maindrive /mountpoint/backupdrive >> ~/rsync_log.txt 2>&1
And with this, the setup is complete.

Getting the media files in order

Sonos will read the ID3 tags on the music files so they need to correct. Due to the fun of an iTunes crash some time in the past mine are a mess so it's time for some scripting. More on this in a future post.

Saturday, 19 April 2014

On a list

Recent statistics from Steam say that approximately a third of owned games remain unplayed. I'm as bad as anyone with this - between sales, Humble Bundles and people sending me keys from THEIR bundles I've got a long list of the things I'm sure I'll get around to playing one day. Obviously, when someone says "Hey, Splinter Cell: Blacklist is on sale - why don't we pick it up?" there is only one response: why not?

I've got a splinter

I'm not a long-term fan of the Splinter Cell series. Slightly odd national-paranoia storyline and slow slide into mediocre FPS territory aside, I've never really gotten into the gameplay. This is probably my own fault as typically I've played the games around five years after release and they really haven't stood up to the ravages of time. It's not that I dislike the games - it's more that each time I've sat down to play one I've found better things to do with my time. This time, though, there was something different. Blacklist has co-op.

Just the two of us

Things are better with a friend and gaming is (usually) no exception. Assuming you have a friend to play with, co-op can be seen as a magic bullet that can improve any game. Of course if you have to find someone via a matchmaking system it can be the most irritating thing in the world.

Personally, I like to be able to play whatever the game has as a single player campaign in co-op mode. Blacklist goes down the cheap and annoying route of having a single player campaign with some bonus missions in which you can bring a friend. Normally this leaves co-op as a bolt-on to be enjoyed for half an hour before you move on to the inevitable multiplayer. Blacklist manages to avoid short-changing you by making the bonus missions roughly 75% of the game content and this means many happy hours yelling at your buddy for tripping alarms and forcing a restart of the entire mission. It really is a lot of fun and substantial - which is good, because it was the main reason I bought the game in the first place.

I'm told the side missions are divided into four types which will be recognisable to fans of the older games. I can't comment on how good these missions are at evoking the spirit of the older games, but I can say that they all play differently and are a good way of keeping the game fresh and interesting. There are the "normal" missions, with normal defined as being similar in playstyle to the single player game. Then there are the full-stealth missions where the whole thing is failed if anyone catches sight of you. These are great as the tension ramps up towards the end and you risk losing the last hour of game time to a mistimed run from cover to cover. Next up there are the violent missions in which your job is to murder everyone in an area - ideally without being seen - which are a pleasant change in pace to the uber-stealth missions. Finally there are the survival missions, in which you need to hold an area using guns and gadgets until you've worked through the waves of attackers then either bail or stick around for more mayhem.

I've not seen co-op implemented quite like this before. It's a fantastic idea, giving me the co-op experience I like while leaving the main campaign alone so the writers can tell the story they want to tell without being hamstrung by the constant requirement of a second protagonist.

All by myself

So yes, there's also a solo campaign. It seems to do a good job of continuing the existing story while not alienating newcomers to the series like me. I found it easy enough to get a feel for the established characters - not the most challenging thing, but they are more rounded than most of hardened military or espionage types you typically find in settings like this.

The plot deals with a massive terrorist operation on US soil. It's heavy on the argh-foreigners paranoia, but interestingly you'll spend much of your time crossing swords with other US intelligence agencies and doing some pretty dubious stuff to get around them. It's a little odd to see a turf war breaking out while tens of thousands of civilians are at risk but it's credible and it makes for some interesting caveats on some of the missions. Nothing says "be careful" like the game failing you the moment you cut down an unsuspecting friendly trooper with a hail of silenced machine gun fire because you were too ham-fisted to sneak across a compound without them seeing you.

I also appreciate the game letting me do these things myself. It's depressingly common that a game's story is told through non-interactive cutscenes or via quick time events (note - a non-interactive cutscene does not become gameplay just because I have to lunge for a random button on my keyboard 3 minutes in) but in Blacklist the game does not feel in the way of the film the writers want you to watch. You get short cutscenes before and after missions and everything else is told in-mission. Like the extra work which has gone into making the co-op mission structure, this makes a big difference to the game.


Oh yes, except the end (bit of a spoiler warning for some of the game's ending here).

I assume there must be a QTE guy somewhere involved in this project. Maybe they sent him out for coffee every day so he couldn't interfere. Maybe he was on holiday. Either way, they managed to distract him for most of the game's production. Then at the last minute he was allowed back in and the FINALLY BOSS is defeated by a series of bloody quick time events - and not just ordinary events either. They are obnoxiously difficult and cause you to restart the entire boss fight (the rest of which is oddly mechanical and doesn't really flow properly but at least is gameplay) when you inevitably mess them up because after 20-odd hours of a proper game you really aren't expecting them.


I don't normally have a problem with QTEs (passing them, that is, not appreciating them) but the only way I could get through these was to learn the sequence and anticipate which button to hit. Thanks guys. It certainly doesn't ruin the game, but it does have a good go at ruining the ending.

Always a system

Back to something positive. The upgrade system is extensive but because of the variance in the game types in the extra content most of what is on offer is actually useful. The problem I had with something like Dishonored is that it gave you a big pile of toys with which you could cause mayhem then slapped your hands if you actually used them. In this you get everything from knockout gas to land mines and then the game lets you get on with it. Obviously you can't use hand grenades in the stealth-only missions, but you can go nuts in the survival modes. The guns aren't quite as generous as the gadgets - the silenced weapons are definitely more useful in all modes - but you can still find a use for the assault rifles if you try.

The only exceptions are the breaching charges (I carried these through SO MANY MISSIONS because they sound cool and didn't manage to use one once) and the final goggles (which appear to be the same as the second to last goggle but with a stylish chinstrap). Bonus points are awarded for not ruining the upgrade system with the DLC kit. Although it is very good there are still normal unlocks which are better so there is still something to work towards.

All the people

There's also a competitive multiplayer mode. It's another interesting Ubisoft game, with unbalanced teams fighting very different battles, and works very well. I haven't found it compelling enough to play for hours but what I did play was a lot of fun.

And so

I like Blacklist. I'm genuinely surprised to be writing that. In fact, I like it a lot and I'm sad that I've now played through all the content. I'd like a little more co-op, but frankly I'm always going to say that and despite completing it I can see myself going back to play more of the survival games. I hope the sequel is structured in the same way - if I can get another fix of sneaky co-op fun then you can finally me to the list of people who are excited by this series.

Saturday, 12 April 2014

When drives don't mount nicely

While rebuilding my media sharing Raspberry Pi I've come across a small problem with usb drives. If you don't unmount a drive from a Windows machine properly, the Pi will mount it read-only. A simple solution for this is:
  1. install the ntfs-3g package:
    sudo apt-get install ntfs-3g
  2. mount the drive using this utility:
    sudo ntfs-3g /dev/sda1 media
    (to mount drive at sda1 to directory media) which clears up all the locks for you
  3. then simply umount and mount and things are back to normal