BPM

pudge on 2004-04-30T22:24:27

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 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@"; }


bpm inspector

rjbs on 2004-05-01T01:18:31

Cool. You were probably thinking of BPM Inspector, from the awesome dude(s) who brought us Quicksilver.

Re:bpm inspector

pudge on 2004-05-01T02:13:52

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.

until

Robrt on 2004-05-01T06:12:20

When did that sneak into perl? (It's basically while(!something)... but very pretty.)

Re:until

pudge on 2004-05-01T06:27:57

It's been there a "while". HA!

Seriously, as long as I can remember. I dunno.

Only about 16 years ago

vsergu on 2004-05-01T20:33:52

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.