Randomizing music files on an USB flash drive

I recently bought a Libratone ZIPP v1 wireless speaker, which lets me play music via UPnP, Bluetooth, 3.5mm audio jack and USB socket.

In preparation for a garden party, I browsed the music library and copied all the files onto a FAT32-formatted USB drive. Then I plugged the drive into the Zipp, and it played the files in the order I copied them onto the USB stick. No randomization.

Solution 1: Randomize filenames

One solution is to randomize the filenames with fileshuffle. It prepends numbers so the order of files when alphabetical sorted is random:

000. Die Ärzte - Jazz ist anders - 01 - Himmelblau.ogg
001. Freundeskreis - Quadratur Des Kreises - 06 - A-N-N-A.ogg
002. Wincent Weiss - Irgendwas gegen die Stille - 06 - Nur ein Herzschlag entfernt.ogg
003. 04 - Sixpence None The Richer - Sixpence None The Richer - Kiss Me.ogg
004. Sportfreunde Stiller - Die gute Seite - 01 - Wie lange sollen wir noch warten.ogg

This means spending some extra time coping the files onto the flash drive again, because the Zipp plays them in the order that they were created. Renaming files afterwards does not affect the playing order.

Solution 2: Randomize FAT file table entries

A directory with files on a FAT-formatted partition consists of a directory table, an index with meta data for each file in that directory (directory entry). Part of those meta data is the location of the actual file data on disk.

The idea is now to shuffle the contents of the directory table, without moving the file data around. This should be much faster, since it's only a couple of kilobytes to modify instead of writing a gigabyte of music files.

Such a tool already exists: Mike Kazantsev wrote vfat_shuffler that takes a directory, and shuffles the entries in the directory table.

I stumbled across some bugs that he fixed literally within minutes, and now I have a tool in my ~/bin that takes only seconds to randomize the music files:

$ sudo /home/cweiske/bin/vfat_shuffler --umount --list /dev/sdb1 /
[  50758] 2raumwohnung - kommt zusammen - 12 - Wir trafen uns in einem Garten.ogg
[ 127272] Wincent Weiss - Irgendwas gegen die Stille - 11 - Wir sind.ogg
[  83266] Annett Louisan - In meiner Mitte - 07 - Auf der Jagd nach Mr. Big.ogg
[  62764] 05 - Positively Somewhere.ogg
[  99569] Liquido - At the rocks - 03 - catch me.mp3
[  84098] Bad Lamps - Soultronic EP - 01 Never Know the Difference.ogg
[  93508] Green Day - Dookie - 07 - Basket Case.mp3
[ 126940] Wincent Weiss - Irgendwas gegen die Stille - 10 - Wenn mir die Worte fehlen.ogg
[ 105385] Milow - North & South - 01 - You and Me (in my pocket).mp3

Written by Christian Weiske.

Comments? Please send an e-mail.