-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayback.py
executable file
·83 lines (69 loc) · 2.32 KB
/
playback.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
#!/usr/bin/python
import time
import threading
class Playback(threading.Thread):
debug = False
def __init__(self, recordings,gap):
threading.Thread.__init__(self)
self.gap = gap
self.stopped = False
self.recordings = self.harmonize(recordings)
if Playback.debug:
print("init playbackThread")
def harmonize(self, recordings):
eps = 0.05
harmonized = list(recordings)
total = sum(recordings)
for i in range(len(harmonized)):
f = harmonized[i]
best = total/4.0
if abs(best-f) < eps:
harmonized[i] = best
print recordings[i], harmonized[i], recordings[i] - harmonized[i], "1/4"
continue
best = total/8.0
if abs(best-f) < eps:
harmonized[i] = best
print recordings[i], harmonized[i], recordings[i] - harmonized[i], "1/8"
continue
best = total/12.0
if abs(best-f) < eps:
harmonized[i] = best
print recordings[i], harmonized[i], recordings[i] - harmonized[i], "1/12"
continue
best = total/16.0
if abs(best-f) < eps:
harmonized[i] = best
print recordings[i], harmonized[i], recordings[i] - harmonized[i], "1/16"
continue
return harmonized
def run(self):
if Playback.debug:
print "playback start", self.recordings
time.sleep(max(0,self.recordings[0]-self.gap)) # close gap
i=1
while True:
if i == len(self.recordings):
i = 0
if self.stopped:
if Playback.debug:
print("playback stopped")
return
if Playback.debug:
print("playback ",i)
h = self.recordings[i]
t1 = time.time()
self.play(h)
t2 = time.time()
rest = h - (t2-t1)
if rest > 0:
time.sleep(h - (t2-t1))
i = i+1
if Playback.debug:
print("playback finished")
self.stopped = True
def stop(self):
self.stopped = True
def play(self,duration):
if Playback.debug:
print duration