Created
July 17, 2022 19:46
-
-
Save plasmatic1/dfdbd781805bf1aabb1dbae4567fd3b5 to your computer and use it in GitHub Desktop.
Estimate BPM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import * | |
from tkinter import ttk | |
import math | |
import time | |
root = Tk() | |
root.title('BPM Estimator') | |
root.geometry('300x300') | |
# math and stuff | |
clicks = [] | |
def calculate(): | |
n = 1 | |
tot_sum = 0 | |
tot_weight = 0 | |
for i in range(len(clicks) - 1, 0, -1): | |
weight = 1 / n | |
tot_sum += weight / (clicks[i] - clicks[i - 1]) | |
tot_weight += weight | |
n += 1 | |
bpm_text = '?' if tot_weight == 0 else f'{60 * (tot_sum / tot_weight):.1f}' | |
b_text.set(f'Click!\n{bpm_text} BPM\n{len(clicks)} Click{"s" if len(clicks) != 1 else ""}') | |
def do_reset(): | |
global clicks | |
clicks.clear() | |
calculate() | |
def do_click(): | |
global clicks | |
clicks.append(time.time()) | |
calculate() | |
# setup UI | |
ui = ttk.Frame(root, padding=10) | |
ui.pack(fill=BOTH, expand=True) | |
b_text = StringVar() | |
button = ttk.Button(ui, command=do_click, textvariable=b_text) | |
button.pack(anchor=CENTER, expand=True, fill=BOTH) | |
reset_button = ttk.Button(ui, command=do_reset, text='Reset') | |
reset_button.pack(anchor=CENTER, expand=True, fill=BOTH) | |
calculate() | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment