You are not logged in.

#1 2010-08-15 01:57:06

cteampride
Member
Registered: 2010-01-10
Posts: 15

BPM counter script

just needed a manual bpm counter, partially to practice perl, but also cuz i dont like the audacity auto bpm counter.  I like to manually click to the beat... but thats just me.

It works, but the output isnt rly pretty.  should i print another line that just displays the instantaneous bpm (between the two most recent clicks) or would that just be to much info?  In my opinion i think the avg bpm is all ya need, and if you screw your bpm up by missing a few beats just rerun the app.

#! usr/bin/perl

#Beat Counter Program
#By William Gur
#2010

use strict;
use warnings;
use Time::HiRes;
use 5.010;


sub timediff{
    state @time;
    state $total_time;
    state $focus_element;
        my $timeval=$_[0];
    my $returnval;
    #return time elaped in mins. ex: .35 mins
    #use state to keep value between calls

    push @time, $timeval;

    if(!defined($total_time)){
        $total_time=0;
    }
    if(!defined($focus_element)){
        $focus_element=0;
    }
    if($focus_element>=1){
        $total_time=($total_time+($time[$focus_element]-$time[($focus_element-1)])); 
        $returnval=($total_time/60);
        $focus_element+=1;
        return $returnval;
    }
    else{
    $focus_element+=1;
    return 1;
    }
}

print "hit the return button to the beat!\n";
print "press any other key to exit\n";
my $input="", my $counter=0, my $time_now, my $time_elapsed=1;
    #get the local time as a start val so you know how long has passed
    #in total... compare that to number of clicks so far.  get average bpm from this data.
    while($input eq ""){
         $input=<STDIN>;
         chomp($input);
        $time_now = Time::HiRes::time();
        $time_elapsed = &timediff($time_now);
                if($time_elapsed != 0 && $counter != 0){
            print $counter/$time_elapsed;
            print " avg bpm";
            }
        $counter+=1;
    }
print "\ngoodbye\n";

Comes in handy covering songs smile

Offline

Board footer

Powered by FluxBB