Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

pudge (1)

  (email not shown publicly)
AOL IM: Crimethnk (Add Buddy, Send Message)

I run this joint, see?

Journal of pudge (1)

Wednesday July 21, 2004
05:54 PM

Stupid Mac::Glue Tricks: Name That Tune

[ #19985 ]

The iPod has a Name That Tune game in it, where it shows you a list of five songs, and it plays a song from your iPod, and you guess which one it is. Here's a perl/iTunes version. It has a crummy UI, and it doesn't keep score, but the guts all seem to work. Just type the number of the song you think is playing.

One slightly dangerous part is that it sets the "start" time of the file, so that it begins playing in the middle of the song, then it sets it back. It could, of course, fail to set it back if there's a crash of some kind. Oh, the risks we take in life!

use strict;
use warnings;
use Mac::Glue ':all';
use Term::ReadKey;
my $itunes = new Mac::Glue 'iTunes';
# interval between guesses
my $guess_sleep = 3;
# tracks to guess
my $guesses = 5;
# define the songs you want to get
my $any = $itunes->obj(track => gAny, tracks => whose(bit_rate => g_e => 128), playlist => 1);
play_game() for 0..2;
sub play_game {
    my(@tracks, %tracks);
    until (@tracks == $guesses) {
        my $track = $any->get;
        my($artist, $name, $duration, $start) = map { $track->prop($_)->get || '' }
            qw(artist name duration start);
        next unless $artist && $name;
        next if $tracks{$artist, $name}++;
        push @tracks, [$track, @tracks + 1, $artist, $name, $duration, $start];
    my $selected = my $winner = int rand(@tracks);
    $winner += 1;
    my $playing = $tracks[$selected];
    my $newstart = int($playing->[4]/3 + rand($playing->[4]/10) - rand($playing->[4]/10));
    if ($newstart > $playing->[4]) {
        $newstart = int($playing->[4] - $guesses*$guess_sleep);
        $newstart = 0 if $newstart < 0;
    $playing->[0]->prop('start')->set(to => $newstart);
    OUTER: while (@tracks > 1) {
        my $now = time();
        INNER: while (($now + $guess_sleep) > time()) {
            my $c = ReadKey(-1);
            next unless $c && $c =~ /\d/;
            if ($c == $winner) {
                print "Right-o!\n\n\n";
                last OUTER;
            } else {
                last INNER;
        my $remove = int rand(@tracks);
        next if $remove == $selected;
        $selected-- if $remove < $selected;
        splice(@tracks, $remove, 1);
    $playing->[0]->prop('start')->set(to => $playing->[5]);
sub print_tracks {
    my($tracks) = @_;
    for (@$tracks) {
        print "$$_[1]. $$_[2], '$$_[3]'\n";
    print "\n\n";

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.