summaryrefslogtreecommitdiff
path: root/interface/render.py
blob: e6f0e64974c35685c5532afe674f66a24d405119 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import threading
import time

import mpv

from blc2.workspace import Workspace
from blc2.topology import Fixture
from blc2.constants import ONESHOT

class Renderer:
    def hold(self, values):
        with self._lock:
            if isinstance(values, dict):
                values = values.items()
            for c, v in values:
                if v is None:
                    if c in self._hold:
                        del self._hold[c]
                else:
                    self._hold[c] = v
            ## Rely on the run thread to update when possible
            if not self._running:
                ## If not, just update now
                self._update()

    def clear_hold(self):
        with self._lock:
            self._hold = {}
            self.hold(())

    def _update(self):
        with self._lock:
            self._values = {c: (v if c not in self._hold else self._hold[c]) for c, v in self._last.items()}
            self.output.set_values(self._values)

    def start(self):
        with self._lock:
            if self._run_thread is not None:
                raise ValueError("Already running")
            self._run_thread = threading.Thread(target=self._run)
            self._running = True
            self._run_thread.start()

    def stop(self):
        with self._lock:
            self._running = False


    def set_functions(self, *args):
        with self._lock:
            if self._running:
                raise ValueError("Can't change while running")
            self._functions, self._data = [i[0] for i in args], [i[1] for i in args]

    @property
    def time(self):
        with self._lock:
            return self._current

    def _run(self):
        audio_cache = {}

        sleep = 1/60
        next_ap = []
        ap = {}
        running_ap = set()
        t = 0
        start = time.monotonic()
        while True:
            ## w_lock here is a formality: by assumption, we're not editing while we're 
            ## running a show
            with self._lock, self.w_lock:
                self._update()
                for a in next_ap:
                    a.pause = False
                next_ap = []
                
                if self._callback is not None:
                    self._callback(self._current, self._values)

                ## FIXME: Cleanup finished audio players?
                ## FIXME: Handle audio fades and jumps

                next_t = sleep + time.monotonic()
                self._current = next_t - start
                t = int(1000*self._current)
                _last = {c: 0 for c in self._channels}
                this_ap = set()
                for n, (f, d) in enumerate(zip(self._functions, self._data)):
                    lc, ac, self._data[n] = f.render(t, d)
                    for c, v in lc:
                        if _last[c] < v:
                            _last[c] = v

                    for guid, filename, start_t, fin, fstart, fout in ac:
                        this_ap.add(guid)
                        fstart += start_t
                        if guid in running_ap:
                            mul = 100
                            if t < fin:
                                mul = max(0, int(100*(t/fin)))
                            elif t > fstart+fout:
                                mul = -1
                            elif t > fstart:
                                mul = max(0, int(100*(1 - (t-fstart)/fout)))

                            if mul == -1:
                                ap[guid].pause = True
                            else:
                                ap[guid].volume = mul
                        else:
                            running_ap.add(guid)
                            nap = mpv.MPV()
                            nap.pause = True
                            nap.play(filename)
                            next_ap.append(nap)
                            ap[guid] = nap

                for a, p in tuple(ap.items()):
                    if a not in this_ap:
                        p.pause = True
                        del ap[a]
                        running_ap.remove(a)

                self._last = _last
                self._update()

                if not self._running:
                    ## We're done, clean up
                    for a in ap.values():
                        a.pause = True
                        del a
                    self._last = {c: 0 for c in self._channels}
                    self._current = 0
                    self._update()
                    if self._callback is not None:
                        self._callback(0, self._values)
                    self._run_thread = None
                    break

            ## END locked block
            time.sleep(max(0, next_t - time.monotonic()))

    def advance(self, *args):
        with self._lock:
            if self._run_thread is None:
                raise ValueError("Not running")
            for a in args:
                if isinstance(a, int):
                    p = None
                else: 
                    a, p = a
                f = self._functions[a]
                t = int(1000*self._current)
                if f.advance_mode == ONESHOT and self._data[a].steps and self._data[a].steps[-1].index+1 == len(f.steps):
                    continue
                try:
                    d2 = f.advance(t, self._data[a], n=p)
                except ValueError:
                    ## Done
                    pass
                else:
                    d = d2
                _, _, self._data[a] = f.render(t, d)

    def __init__(self, w: Workspace, w_lock: threading.RLock, output, callback=None):
        self.output = output
        self.w = w
        self.w_lock = w_lock 

        self._lock = threading.RLock()
        self._stop_lock = threading.Lock()
        self._hold = {}
        self._channels = frozenset().union(*((c for c in f.channels) for f in w.fixtures.values()))
        self._last = {c: 0 for c in self._channels}
        self._functions = []
        self._data = []
        self._current = 0
        self._running = False
        self._run_thread = None
        self._values = {}

        self._callback = callback