Stories
Slash Boxes
Comments
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)

pudge
  (email not shown publicly)
http://pudge.net/
AOL IM: Crimethnk (Add Buddy, Send Message)

I run this joint, see?

Journal of pudge (1)

Friday April 30, 2004
05:24 PM

BPM

[ #18569 ]

I wanted to tap out a beat and find out how many beats per minute it is, and a quick glance turned up nothing too good for Mac OS X (though I am sure I'd seen something like it before); so, as I am wont to do, I wrote my own. Just run it in the terminal, tap out your rhythm. It only calculates for the last five beats (modify to your pleasure ...).

#!/usr/local/bin/perl
use warnings;
use strict;
use Time::HiRes 'time';
 
print "Hit '.' for each beat.  Hit '.' to start, and <enter> or ^C to end.\n";
print "Keep going until you reach about 10 beats, and are satisfied with the result.\n";
 
$| = 1;
my @times;
1 until getc();
push @times, time();
while (my $c = getc()) {
    push @times, time();
    printf "\rBPM: %d\n", ($#times * 60) / ($times[-1] - $times[0]);
    shift @times while @times > 4;
    last if $c eq "\n";
}
 
BEGIN {
    system 'stty', '-icanon', 'eol', "\cA";
}
 
END {
    system 'stty', 'icanon', 'eol', "\c@";
}

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Cool. You were probably thinking of BPM Inspector [blacktree.com], from the awesome dude(s) who brought us Quicksilver.
    --
    rjbs
    • Yeah, that's the one. I was looking at some others that didn't do what I wanted. Thanks! But mine's smaller. :-) I could make mine set the value, too, with less than 10 more lines of code. But I don't think I will, unless someone wants me to, since I don't care about that.
  • When did that sneak into perl? (It's basically while(!something)... but very pretty.)
    • It's been there a "while". HA!

      Seriously, as long as I can remember. I dunno.
    • It's been there from the beginning. From the perl 1.0 man page:

      If the word while is replaced by the word until, the sense of the test is reversed, but the conditional is still tested before the first iteration.