-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryKeyer.py
106 lines (86 loc) · 2.46 KB
/
MemoryKeyer.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
# MemoryKeyer - Record/Replay real time keying
import time
import KeyingControl as key
import CwUtilities as utl
recording=False
tstamp =[] # sequence of time stamp
keystat =[] # sequence of key operation
maxdelay =600 # Too long mark/space (sec) is truncated.
#(disabled if maxdelay is non-positive)
# start recording
#
def recstart():
global recording, tstamp, keystat
if recording:
print('? Already recording...')
return False
print('Start recording ...')
recording=True
tstamp=[]
keystat=[]
return True
# stop recording
#
def recstop():
global recording, tstamp, keystat
if not recording:
print('? Not recording, yet.')
return False
recording=False
try:
tstamp.append(tstamp[-1]+0.1)
keystat.append(key.RELEASED)
except IndexError:
pass
print('Stop recording.')
return True
# replay recording
#
def replay(speed, barlen=0):
if recording:
print("? Now recording, can't replay")
return False
if not (0.0<speed and speed<=10):
print('? Replay speed is too fast or too slow.')
return False
if not tstamp:
print('? nothing to play')
return False
print('Replay keying:', len(tstamp), 'marks and spaces...')
print('(Dots or marks longer than', maxdelay, 'seconds will be trucated)')
# setups for progress bar
#
if 1<=barlen:
progbar=utl.ProgressBar(barlen, int(len(tstamp)))
progbar.begin()
barstep=int(len(tstamp)/barlen/2)+1 # frequency of bar update
barcount=0
try:
# replay loop
#
for i in range(len(tstamp)-1):
if key.abort_requested():
key.space()
progbar.end(False)
return False
elif keystat[i]==key.PRESSED:
key.mark()
elif keystat[i]==key.RELEASED:
key.space()
if 1<=barlen:
barcount += 1
if barstep<barcount:
progbar.update(i)
barcount=0
if 0<=maxdelay:
time.sleep(min(maxdelay, (tstamp[i+1]-tstamp[i])/speed))
else:
time.sleep((tstamp[i+1]-tstamp[i])/speed)
if 1<=barlen:
# complete progressbar
progbar.end(True)
except KeyboardInterrupt:
if 1<=barlen:
print()
key.space()
return True