Upload player.py
Browse files
lib/python3.10/site-packages/dspy/player.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from Queue import PriorityQueue, Queue, Full
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
from dspy.generator import Generator
|
6 |
+
from dspy.lib import rechannel, t2f
|
7 |
+
|
8 |
+
|
9 |
+
class Player(Generator):
|
10 |
+
def __init__(self, sequence=[], channels=2, live=True, loop=False, clip=True, max_size=0):
|
11 |
+
Generator.__init__(self)
|
12 |
+
self.max_size = max_size
|
13 |
+
self._generators = PriorityQueue(max_size)
|
14 |
+
self._finished = Queue()
|
15 |
+
self._gain = 0.1
|
16 |
+
self._live = live
|
17 |
+
self._length_cache = max([f+g.length() for (f, g) in sequence] + [0])
|
18 |
+
self.loop = loop
|
19 |
+
self.clip = clip
|
20 |
+
self.num_channels = channels
|
21 |
+
|
22 |
+
self.num_gens = 0
|
23 |
+
|
24 |
+
if sequence:
|
25 |
+
for (f, g) in sequence:
|
26 |
+
self._append_gen(f, g)
|
27 |
+
|
28 |
+
if live:
|
29 |
+
assert(not loop)
|
30 |
+
|
31 |
+
|
32 |
+
self.auto_reset = loop
|
33 |
+
|
34 |
+
def _append_gen(self, frame, gen):
|
35 |
+
try:
|
36 |
+
self._generators.put((t2f(frame), gen), False)
|
37 |
+
except Full:
|
38 |
+
print('Too many generators to append another')
|
39 |
+
return
|
40 |
+
|
41 |
+
def add(self, gen, time=None):
|
42 |
+
if time is None:
|
43 |
+
frame = self.frame
|
44 |
+
else:
|
45 |
+
frame = t2f(time)
|
46 |
+
self._length_cache = max(self._length_cache, gen.length() + frame)
|
47 |
+
|
48 |
+
if self.num_gens < self.max_size or self.max_size == 0:
|
49 |
+
self.num_gens += 1
|
50 |
+
self._append_gen(frame, gen)
|
51 |
+
|
52 |
+
def _reset(self):
|
53 |
+
if self._live:
|
54 |
+
raise Exception('Cannot reset if Player is live')
|
55 |
+
|
56 |
+
sequence = []
|
57 |
+
while not self._finished.empty():
|
58 |
+
frame, gen = self._finished.get()
|
59 |
+
gen.reset()
|
60 |
+
sequence.append((frame, gen))
|
61 |
+
|
62 |
+
while not self._generators.empty():
|
63 |
+
frame, gen = self._generators.get()
|
64 |
+
gen.reset()
|
65 |
+
sequence.append((frame, gen))
|
66 |
+
|
67 |
+
for frame, gen in sequence:
|
68 |
+
self._append_gen(frame, gen)
|
69 |
+
|
70 |
+
def _length(self):
|
71 |
+
if self._live:
|
72 |
+
return float('inf')
|
73 |
+
|
74 |
+
return self._length_cache
|
75 |
+
|
76 |
+
@property
|
77 |
+
def gain(self):
|
78 |
+
return self._gain
|
79 |
+
|
80 |
+
@gain.setter
|
81 |
+
def gain(self, value):
|
82 |
+
self._gain = np.clip(value, 0, 1)
|
83 |
+
|
84 |
+
def _generate(self, frame_count):
|
85 |
+
output = np.zeros(frame_count * self.num_channels, dtype=np.float32)
|
86 |
+
not_done = []
|
87 |
+
while not self._generators.empty():
|
88 |
+
frame, gen = self._generators.get()
|
89 |
+
if frame > self.frame + frame_count:
|
90 |
+
not_done.append((frame, gen))
|
91 |
+
break
|
92 |
+
|
93 |
+
delay = 0
|
94 |
+
if frame > self.frame:
|
95 |
+
delay = frame - self.frame
|
96 |
+
|
97 |
+
signal, continue_flag = gen.generate(frame_count - delay)
|
98 |
+
signal = rechannel(signal, gen.num_channels, self.num_channels)
|
99 |
+
output[delay * self.num_channels:] += signal
|
100 |
+
if continue_flag:
|
101 |
+
not_done.append((frame, gen))
|
102 |
+
else:
|
103 |
+
if not self._live:
|
104 |
+
self._finished.put((frame, gen))
|
105 |
+
self.num_gens -= 1
|
106 |
+
|
107 |
+
for frame, gen in not_done:
|
108 |
+
self._append_gen(frame, gen)
|
109 |
+
|
110 |
+
output *= self.gain
|
111 |
+
if self.clip:
|
112 |
+
output = np.clip(output, -1, 1)
|
113 |
+
return output
|