[chuck-users] Timing with ChucK

Tom Lieber lieber at princeton.edu
Tue Feb 17 16:20:44 EST 2009

2009/2/17 rkramer <rkramer at gmail.com>:
> Being a relative non-programmer, I'm not quite sure if this is even
> possible...  My goal is a quick and dirty program that will measure the time
> between two large audio events (in the input sound buffer, live recording.)
> Essentially what I want to do is use ChucK for practical demonstrations of
> simple physics...  IE demonstrating average velocity of a falling object
> (that generates a sufficiently loud event when it is released and when it
> hits the floor.)
> Any tips where to start on this project?

This can be really easy depending on how shady you mind the results
being. For example, this seems to be a pretty good hand-clap detector
with my MacBook mic:

now => time lastnoise;
while(1::samp => now) {
    if(adc.last() > 0.8 && now - lastnoise > second/10) {
        <<< "bang!", (now - lastnoise)/second >>>;
        now => lastnoise;

That just waits for the microphone to report a value greater than 0.8
(where 1.0 is considered the "max" even though it can sometimes be
higher). It will print "bang!" and the number of seconds since the
last bang, if such a high value is heard and it's been at least 1/10
of a second since the last clap (so that the clap isn't detected in
the room's echo or in repeated oscillations of the sound).

I am usually satisfied with the easiest, shady solution, but if this
kind of detection isn't good enough, there are plenty of ways to make
it more robust which we can help you with if you need it. There's no
"correct" solution that I know of, though.

Tom Lieber

More information about the chuck-users mailing list