tapThis block outputs a bpm value when triggered by a bang that can come from a MIDI message (eg a drum pad). When it loses track because the trigger is not periodic enough, it restarts and for a moment (2 taps) holds the previous tempo.
versionv1.0: built 14/05/2007 with Pd v0.40-2 [pd][pdf]
based on http://www.parasitaere-kapazitaeten.net/Pd/pdpatches
interfaceInput 1: the inlet that receives triggers, anything counts: a bang, a value,...
Input 2: a reset knob: resets the counting when tempo is lost. Also happens automatically.
Output 1: the tempo in beats per minute (bpm)
Output 2: a bang when tempo is lost. It's not necessary to connect this with the reset input. It's taken care of inside
To calculate the tempo, this algorithm uses three steps: it calculates the interval between the previous click and the current click, it calculates the mean inter-click interval time and it computes the bpm value of this. Then as an extra step the last value before losing the tempo and dropping to 0bpm is remembered by only updating the outlet if it's not 0 (0 can never be a tempo of a song, therefor it's allowed)
Then the mean is calculated by adding the new value to the sum of the previous values (this last sum is made by multiplying the last mean value by the last counter index, so a small backward recalculation is done). This is each time divided by the current counter value (indicating the number of terms of the sum).
Another step decides if the tempo is between the right values, that it doesn't deviate too much. If it does, the whole thing is reset.