Cheap MP3 players and Linux

Armijn Hemel, April 2, 2009, 4504 views.

Tags: , ,

A few years ago I got an MP3-player. One thing I noticed when I copied music to it is that the running order of MP3s was not correct. I did not give it much thought, because it did not annoy me. In fact, there are people who are willing to pay to have music played in a random order (hello iPod Shuffle owners!). Usually it's not really an issue, unless you play albums that tell a story and you need to listen to it in a certain order, or albums where there is no clear distinction where one song starts and the other one ends. Not much later I gave my parents MP3 players as well and they did notice, so I decided to dig a little bit deeper into it.

As it turns out it has everything to do with the order in which the files are copied to the device. The MP3 devices don't sort by looking at the name of the file. There is a good reason for this: sorting is complex if you really want to do it right and make it work for various locales. The languages the average el cheapo MP3 player supports are usually English, French, German and Chinese (both simplified and traditional). The reason that it is tough is that it every locale has different rules. Since the MP3 players simply don't have enough computing power to support all these different sortings the makers chose to use the creation time to determine the order of the files and let whoever transfers the songs to the device care about the order.

Windows transfers the files in sorted order, Mac OS X does so too, but Unix(-like) systems (except Mac OS X) don't.

A simple example demonstrates this:

$ mkdir test
$ cd test/
$ touch 1 2 3 4 5 6 7 8 9
$ cd ..
$ find test/
test/
test/3
test/7
test/5
test/4
test/9
test/2
test/1
test/6
test/8

As you can see, find does not find the files in sorted order. It becomes clear if we look at the actual inodes of the files:

$ ls -li
total 0
1605873 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 1
1605872 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 2
1605867 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 3
1605870 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 4
1605869 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 5
1605874 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 6
1605868 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 7
1605875 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 8
1605871 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 9

The inodes appear to be unsorted.

If we sort by inode number we get the following:

$ ls -li | sort
1605867 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 3
1605868 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 7
1605869 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 5
1605870 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 4
1605871 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 9
1605872 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 2
1605873 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 1
1605874 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 6
1605875 -rw-rw-r-- 1 tux tux 0 2008-12-26 15:42 8
total 0

Looking the output of the earlier find command, you can see the exact same order. This also happens to be the order that cp and graphical file managers use when copying the files to the MP3 player, where the MP3s appear to be unsorted and will play in seemingly random order.

The solution is to make sure that the files are copied to the MP3 player in sorted order. Commandline tools as find, sort and xargs can help you there.

Social networking: Tweet this article on Twitter Pass on this article on LinkedIn Bookmark this article on Google Bookmark this article on Yahoo! Bookmark this article on Technorati Bookmark this article on Delicious Share this article on Facebook Digg this article on Digg Submit this article to Reddit Thumb this article up at StumbleUpon Submit this article to Furl

Talkback

respond to this article