forked from talonvoice/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoise.py
59 lines (53 loc) · 1.92 KB
/
noise.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
import time
from talon import ctrl
from talon import tap
from talon.audio import noise
from talon.track.geom import Point2d
class NoiseModel:
def __init__(self):
self.hiss_start = 0
self.hiss_last = 0
self.button = 0
self.mouse_origin = Point2d(0, 0)
self.mouse_last = Point2d(0, 0)
self.dragging = False
tap.register(tap.MMOVE, self.on_move)
noise.register('noise', self.on_noise)
def on_move(self, typ, e):
if typ != tap.MMOVE: return
self.mouse_last = pos = Point2d(e.x, e.y)
if self.hiss_start and not self.dragging:
if (pos - self.mouse_origin).len() > 10:
self.dragging = True
self.button = 0
x, y = self.mouse_origin.x, self.mouse_origin.y
ctrl.mouse(x, y)
ctrl.mouse_click(x, y, button=0, down=True)
def on_noise(self, noise):
now = time.time()
if noise == 'pop':
ctrl.mouse_click(button=0, hold=16000)
elif noise == 'hiss_start':
if now - self.hiss_last < 0.25:
ctrl.mouse_click(button=self.button, down=True)
self.hiss_last = now
self.dragging = True
else:
self.mouse_origin = self.mouse_last
self.hiss_start = now
elif noise == 'hiss_end':
if self.dragging:
ctrl.mouse_click(button=self.button, up=True)
self.dragging = False
else:
duration = time.time() - self.hiss_start
if duration > 0.5:
self.button = 1
ctrl.mouse_click(button=1)
self.hiss_last = now
elif duration > 0.2:
self.button = 0
ctrl.mouse_click(button=0)
self.hiss_last = now
self.hiss_start = 0
model = NoiseModel()