[linux-audio-dev] tempo estimator!

New Message Reply About this list Date view Thread view Subject view Author view Other groups

Subject: [linux-audio-dev] tempo estimator!
From: Paul Winkler (slinkp_AT_ulster.net)
Date: la joulu  11 1999 - 00:20:46 EST


Hi folks,

here's a utility that guesses a tempo based on your taps on the
console kbd.
it just occurred to me today how easy this would be to do in pretty
much any language; I used python 'cause it's what I like for quick
stuff. a little searching in the python faq turned up exactly what I
needed, and voila...
feel free to modify / use / dispose of this anywhere it might be
handy (sequencer-like apps?)
 
................ paul winkler ..................
slinkP arts: music, sound, illustration, design, etc.
A member of ARMS -----> http://www.reacharms.com
or http://www.mp3.com/arms or http://www.amp3.com/arms
personal page ----> http://www.ulster.net/~abigoo

here's the code:

#!/usr/bin/python

print """
Tap a steady beat on any key.
Hit 'q' to stop and print the estimated tempo of your beat.
"""

## This is almost entirely based on the 'keypress' entry in python
FAQ, by
## Andrew Kuchling.

import termios, TERMIOS, sys, os, time

fd = sys.stdin.fileno()
# Get 2 copies of termios info... one to modify, one to restore
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
# turn off "canonical mode" & "echo"
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
# I have no idea what these do-- not in python docs or 'man termios'
new[6][TERMIOS.VMIN] = 1
new[6][TERMIOS.VTIME] = 0
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)

times = [] # save the time when keys are pressed.
try:
    while 1:
        c = os.read(fd, 1)
        when = time.time()
        if c == "q": break
        else:
            times.append(when)
            print "%s %f" % (c, when)
finally:
    # be sure to restore tty attributes no matter what!
    termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)

diffs = []
for i in range(len(times)):
    try:
        diffs.append(times[i + 1] - times[i])
    except IndexError: pass #

sum = 0
## print "diffs:"
for d in diffs:
## print d,
     sum = sum + d

estimate = 60.0 / (sum / len(diffs))
print "\n\nestimate is: %f" % estimate


New Message Reply About this list Date view Thread view Subject view Author view Other groups

This archive was generated by hypermail 2b28 : pe maalis 10 2000 - 07:23:26 EST