Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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)


In reply to MP3 shuffler by rinceWind

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (7)
    As of 2015-07-01 23:16 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (24 votes), past polls