I've recently bought a 1Gb Philips MP3 player, and I'm enjoying much of my music collection whilst enduring the services of London Underground on my journey to and from work.
This player has a "shuffle" feature, which in theory plays the tracks in a random order. I say in theory, because I've found that the player seems to favour certain albums, and sometimes goes into a sequential mode - as if the shuffle wasn't turned on. Taking the battery out seems to restore the shuffle. Anyway I decided I haven't got the time and the anal fixation to gather evidence for this bug and report it to Philips.
So, instead, I've used some Perl to scramble together a random selection of tracks from my whole collection - that which I have so far ripped to my hard drive. The code uses some statistics to try and pick tracks by their length, which should try and give a representative sample of track lengths (it uses the file size to do this). It's using my File::Wildcard to derive the file name in the destination directory. I use the tool below to make a number of selections as directories of symlinks (these can be manually tweaked to remove any tracks you don't like), then use cp -rL to copy to the USB player.
#!/usr/bin/perl
use strict;
use warnings;
use File::Wildcard;
use Getopt::Long;
my $music;
my $output;
my $size = 134217728; # default 128M selection.
GetOptions(
'source=s' => \$music,
'output=s' => \$output,
'size=i' => \$size,
);
$music =~ s!/$!!; #!
my $wc = File::Wildcard->new(
path => "$music///",
match => qr(/\d*\s*([^/]+\.(?:wma|mp3))$),
derive => ["$output/\$1"],
);
my @allsongs;
my $sum=0;
my $sumsq=0;
while (my $found = $wc->next) {
my ($song,$dest) = @$found;
my $siz = (stat $song)[7];
$sum += $siz;
$sumsq += $siz * $siz;
push @allsongs,[$song,$siz,$dest];
}
my $count = scalar @allsongs;
my $mean = $sum / $count;
my $sd = sqrt( $sumsq / $count - $mean * $mean);
my $nsamp = int( $mean / $sd );
my @songs = sort {$a->[1] <=> $b->[1]} @allsongs;
my %seen;
while ($size > 20971520) {
my $pt = 0;
$pt += rand for (1..$nsamp);
my $idx = int( $pt / $nsamp * $count);
my ($song,$ssize,$dest) = @{$songs[$idx]};
next if exists $seen{$song};
$seen{$song}++;
print $song,"\n";
symlink $song, $dest;
$size -= $ssize;
}
--
Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ? (Missquoting Janis Joplin)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|