Skip to content

Instantly share code, notes, and snippets.

@syrm
Created November 20, 2018 09:31
Show Gist options
  • Save syrm/2547713fb675ab884d9a3319b944848c to your computer and use it in GitHub Desktop.
Save syrm/2547713fb675ab884d9a3319b944848c to your computer and use it in GitHub Desktop.
class TapTempo
{
Int sample := 5
Duration[] taps := Duration[,]
Duration resetTime := 60sec
Int precision := 2
public Void main()
{
while (true) {
Env.cur.in.readLine()
tap
displayBPM
}
}
private Void tap()
{
currentTap := Duration.now
checkResetTime
if (taps.size == sample)
taps.removeAt(0)
taps.add(currentTap)
}
private Void displayBPM()
{
if (taps.size == 1) {
Env.cur.out.printLine("[Hit a key one more time to start bpm computation...]")
return
}
Env.cur.out.printLine("Tempo: " + getBPM.toLocale("0." + "0" * precision) + " bpm")
}
private Float getBPM()
{
(taps.size - 1f) / (taps[-1] - taps[0]).toMillis * 60000
}
private Void checkResetTime()
{
if (taps.isEmpty)
return
if (Duration.now - taps[-1] > resetTime)
taps.clear
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment