//practical sound analysis - fredrik olofsson

//organized by lullcec 28-30oct 2011, hangar, barcelona


SynthDef(\taptempo, {|out= 0, thresh= 0.9|

var src, chain, buf, trig, bpm;

var numTaps= 5;

var minBpm= 30;

var maxBpm= 300;

buf= LocalBuf(512);

src= SoundIn.ar;

chain= FFT(buf, src);

trig= Onsets.kr(chain, thresh);

trig= Trig1.kr(trig, 60/maxBpm);

bpm= Median.kr(numTaps, 60/Timer.kr(trig).clip(60/maxBpm, 60/minBpm)).poll(trig);

Out.kr(out, 60/bpm); //output beats-per-seconds on a control bus



Synth(\taptempo, [\out, 0])

//run this at the same time as the taptempo above.  reads tempo from control bus 0

a= {SinOsc.ar([400, 404], 0, Decay.kr(Impulse.kr(1/In.kr(0)), 0.1))}.play(outbus:1)