1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
import tkinter as tk
import jack
client = jack.Client("py_hihat")
outport = client.midi_outports.register("out")
send_queue = []
@client.set_process_callback
def process(frames):
outport.clear_buffer()
for event in send_queue:
outport.write_midi_event(0, event)
send_queue.clear()
class HiHatApp:
def __init__(self, root):
self.root = root
self.root.title("Hi-hat Controller")
# Set a fixed size for the window
self.root.geometry("200x500")
self.state = 64
self.max_state = 127
self.instruction_label = tk.Label(root, text="Scroll the mouse wheel to change the hi-hat position. Click to hit. Press 'q' to quit.", font=("Helvetica", 10), wraplength=180, justify="center")
self.instruction_label.pack(pady=10)
self.position_label = tk.Label(root, text=f"Hi-hat position: {self.state}", font=("Helvetica", 14))
self.position_label.pack(pady=5)
self.canvas = tk.Canvas(root, width=100, height=300, bg='white')
self.canvas.pack(pady=10)
self.hit_label = tk.Label(root, text="", font=("Helvetica", 24), fg='red')
self.hit_label.pack()
self.update_progress_bar()
# Bind mouse wheel events and button click event
self.canvas.bind("<Button-1>", self.on_left_click)
self.root.bind("<Key>", self.on_key_press)
self.canvas.bind("<MouseWheel>", self.on_mouse_wheel) # For Windows and MacOS
self.canvas.bind("<Button-4>", self.on_mouse_wheel_up) # For Linux
self.canvas.bind("<Button-5>", self.on_mouse_wheel_down) # For Linux
def update_progress_bar(self):
self.canvas.delete("all")
progress_height = int((self.state / self.max_state) * 300)
self.canvas.create_rectangle(40, 300 - progress_height, 60, 300, fill="blue")
self.hit_label.config(text="")
def transmit(self, msg):
send_queue.append(msg)
def transmit_hit(self):
self.transmit_state()
self.transmit((0x90, 26, 100))
def transmit_state(self):
self.transmit((0xB0, 4, self.state))
def adjust_openness(self, amount):
self.state = max(0, min(self.max_state, self.state + amount))
def on_mouse_wheel(self, event):
if event.delta > 0:
self.adjust_openness(5)
else:
self.adjust_openness(-5)
self.transmit_state()
self.position_label.config(text=f"Hi-hat position: {self.state}")
self.update_progress_bar()
def on_mouse_wheel_up(self, event):
self.adjust_openness(5)
self.transmit_state()
self.position_label.config(text=f"Hi-hat position: {self.state}")
self.update_progress_bar()
def on_mouse_wheel_down(self, event):
self.adjust_openness(-5)
self.transmit_state()
self.position_label.config(text=f"Hi-hat position: {self.state}")
self.update_progress_bar()
def on_left_click(self, event):
self.transmit_hit()
self.hit_label.config(text="hit")
self.root.after(200, self.clear_hit_label)
def clear_hit_label(self):
self.hit_label.config(text="")
def on_key_press(self, event):
if event.char == 'q':
self.root.quit()
if __name__ == "__main__":
with client:
root = tk.Tk()
app = HiHatApp(root)
root.mainloop()
|