aboutsummaryrefslogtreecommitdiff
path: root/site/udo/bpmdetect.udo
diff options
context:
space:
mode:
authorRichard <q@1bpm.net>2025-04-13 18:48:02 +0100
committerRichard <q@1bpm.net>2025-04-13 18:48:02 +0100
commit9fbf91db06a6d4f4b5cd8bb45389a731bb86bf22 (patch)
tree291bd79ce340e67affa755a8a6b4f6a83cce93ea /site/udo/bpmdetect.udo
downloadapps.csound.1bpm.net-9fbf91db06a6d4f4b5cd8bb45389a731bb86bf22.tar.gz
apps.csound.1bpm.net-9fbf91db06a6d4f4b5cd8bb45389a731bb86bf22.tar.bz2
apps.csound.1bpm.net-9fbf91db06a6d4f4b5cd8bb45389a731bb86bf22.zip
initial
Diffstat (limited to 'site/udo/bpmdetect.udo')
-rwxr-xr-xsite/udo/bpmdetect.udo57
1 files changed, 57 insertions, 0 deletions
diff --git a/site/udo/bpmdetect.udo b/site/udo/bpmdetect.udo
new file mode 100755
index 0000000..6eb0f60
--- /dev/null
+++ b/site/udo/bpmdetect.udo
@@ -0,0 +1,57 @@
+#ifndef UDO_BPMDETECT
+#define UDO_BPMDETECT ##
+
+/*
+ BPM detection from trigger
+
+ This file is part of the SONICS UDO collection by Richard Knight 2021
+ License: GPL-2.0-or-later
+ http://1bpm.net
+*/
+
+
+/*
+ Detect a BPM given a stream of pulses
+
+ kdone, kbpm bpmdetect kpulse [, imaxpulses, imintime]
+
+ kdone outputs 1 when a new BPM has been ascertained
+ kbpm the detected BPM
+ kpulse trigger input
+ imaxpulses number of pulses to consider: will output BPM after this many
+ imintime minimum time between beats to normalise signal
+*/
+opcode bpmdetect, kk, kjj
+ kpulse, imaxpulses, imintime xin
+ imax = (imaxpulses == -1) ? 4 : imaxpulses
+ imintime = (imintime == -1) ? 0.2 : imintime
+ kcycle init 0
+ ktimesum init 0
+ ktime timeinsts
+ klasttime init 0
+ kbpm init 0
+ kdone init 0
+ if (kpulse == 1 && klasttime != 0) then
+ kelapsed = ktime - klasttime
+ if (kelapsed > imintime) then
+ ktimesum += kelapsed
+ klasttime = ktime
+ kcycle += 1
+ endif
+ elseif (klasttime == 0) then
+ klasttime = ktime
+ endif
+
+ if (kcycle >= imax) then
+ kcycle = 0
+ kbpm = 60 / (ktimesum / imax)
+ ktimesum = 0
+ kdone = 1
+ else
+ kdone = 0
+ endif
+ xout kdone, kbpm
+endop
+
+#end
+