-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbehaviours.py
111 lines (87 loc) · 3.17 KB
/
behaviours.py
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
105
106
107
108
109
110
111
import midi_message
def null_behaviour():
"""
This is the default behaviour for all footswitches and will not do anything when called.
"""
def null_behaviour_call(midi_out, delta):
print("Do nothing.")
return null_behaviour_call
def toggle_behaviour(channel=0, control=20, on_cc_value=127, off_cc_value=0):
state = False
def toggle_behaviour_call(midi_out, delta):
nonlocal state
state = not state
if state:
msg = midi_message.control_change(channel, control, on_cc_value)
midi_out.send_message(msg)
print(msg)
else:
msg = midi_message.control_change(channel, control, off_cc_value)
midi_out.send_message(msg)
print(msg)
return toggle_behaviour_call
def trigger_note_behaviour(channel=0, note=midi_message.MIDDLE_C, velocity=127, release_velocity=127):
def trigger_note_behaviour_call(midi_out, delta):
msg = midi_message.note_on(channel, note, velocity)
midi_out.send_message(msg)
msg = midi_message.note_off(channel, note, release_velocity)
midi_out.send_message(msg)
print("[TRIGGER NOTE]", msg)
return trigger_note_behaviour_call
def toggle_note_behaviour(channel=0, note=midi_message.MIDDLE_C, velocity=127, release_velocity=127):
state = False
def toggle_note_behaviour_call(midi_out, delta):
nonlocal state
state = not state
if state:
msg = midi_message.note_on(channel, note, velocity)
midi_out.send_message(msg)
print(msg)
else:
msg = midi_message.note_off(channel, note, release_velocity)
midi_out.send_message(msg)
print(msg)
return toggle_note_behaviour_call
def tap_tempo_behaviour(channel=0, control=20, taps=4, reset_after=1, transform=None):
"""
Allows using the footswitch as a tap tempo control.
"""
deltas = []
def tap_tempo_behaviour_call(midi_out, delta):
nonlocal deltas
if delta > reset_after:
deltas.clear()
if deltas:
deltas.append(delta)
else:
deltas.append(0)
if len(deltas) == taps:
print(deltas)
mean = sum(filter(lambda d : d > 0, deltas)) / (taps - 1)
bpm = int(600 / mean) / 10
if transform:
cc_value = transform(mean)
msg = midi_message.control_change(channel, control, cc_value)
midi_out.send_message(msg)
else:
print(f'{bpm} BPM')
deltas.clear()
return tap_tempo_behaviour_call
def keyboard_behaviour(key, ctrl=False, shift=False):
"""
Allows using the footswitch to perform keypresses
"""
from pynput.keyboard import Key, Controller
keyboard = Controller()
def keyboard_behaviour_call(midi_out, delta):
if ctrl:
keyboard.press(Key.ctrl)
if shift:
keyboard.press(Key.shift)
keyboard.press(key)
keyboard.release(key)
if ctrl:
keyboard.release(Key.ctrl)
if shift:
keyboard.release(Key.shift)
return keyboard_behaviour_call