-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscc.py
More file actions
106 lines (88 loc) · 4.11 KB
/
scc.py
File metadata and controls
106 lines (88 loc) · 4.11 KB
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 Leap, sys, pygame
import threading
from Globals import NUM_CONTROLS, Controls, GLOBAL, NUM_TRACKS
class Controller(Leap.Listener):
def setup(self, model, leap_controller):
self.model = model
self.leap_controller = leap_controller
self.thread = threading.Thread(target=self.keyboard_listener)
self.thread.daemon = True
self.thread.start()
self.start_listen = False
self.listening = False
self.stop_listen = False
self.value = 0
self.initial_value = 0
def keyboard_listener(self):
pygame.init()
screen = pygame.display.set_mode((640, 480))
model = self.model
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print "quit"
sys.exit()
# Remove the sample listener when done
self.leap_controller.remove_listener(self)
break
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
print "quit"
# user pressed ESC
sys.exit()
# Remove the sample listener when done
self.leap_controller.remove_listener(self)
break
elif event.key == pygame.K_e:
self.start_listen = True
elif event.key == pygame.K_w:
model.set_control((model.current_control + 1) % NUM_CONTROLS)
print("Control changed to {0}".format(model.current_control))
elif event.key == pygame.K_s:
model.set_control((model.current_control - 1) % NUM_CONTROLS)
print("Control changed to {0}".format(model.current_control))
elif event.key == pygame.K_a:
model.set_track((model.current_track - 1) % NUM_TRACKS)
print("Track changed to {0}".format(model.current_track))
elif event.key == pygame.K_d:
model.set_track((model.current_track + 1) % NUM_TRACKS)
print("Track changed to {0}".format(model.current_track))
elif event.type == pygame.KEYUP:
if event.key == pygame.K_e:
self.stop_listen = True
def on_init(self, controller):
print "Initialized"
#self.lastFrameID = 0
def on_connect(self, controller):
print "Connected"
def on_disconnect(self, controller):
# Note: not dispatched when running in a debugger.
print "Disconnected"
def on_exit(self, controller):
print "Exited"
def on_frame(self, leapController):
frame = leapController.frame()
model = self.model
if len(frame.hands) >= 1:
hand = frame.hands[0]
if self.start_listen:
self.value = hand.palm_position.y
self.initial_value = model.controls[model.current_control][model.current_track]
print("Initial {0}".format(self.value))
self.start_listen = False
self.listening = True
if self.stop_listen:
self.listening = False
self.stop_listen = False
if self.listening:
offset = int(hand.palm_position.y - self.value)
sys.stdout.write('Offset: {0} \r'.format(offset))
if model.current_control == Controls.TRACK:
pass
elif model.current_control == Controls.TEMPO:
model.set_global_value(self.initial_value + offset)
print("Tempo {0}".format(model.controls[Controls.TEMPO][GLOBAL]))
elif model.current_control == Controls.INSTRUMENT:
model.set_value(min(max(self.initial_value + offset, 0), 127))
else:
model.set_value(offset)