diff options
author | Ben Connors <benconnors@outlook.com> | 2019-02-22 15:35:50 -0500 |
---|---|---|
committer | Ben Connors <benconnors@outlook.com> | 2019-02-22 23:13:47 -0500 |
commit | 08805d7464dd3e8930ce730f84a2056e31684042 (patch) | |
tree | 745aa405b802f4d135e636eaa6839b6144d77f6a | |
parent | 422b8f7485b95278d0207a7749577dd960f5f206 (diff) |
Add pre-rendering utilities
- prstore uses pickle to store a pre-render to a file
- prcons loads and renders the pickled pre-renders
-rwxr-xr-x | prcons.py | 72 | ||||
-rwxr-xr-x | prstore.py | 46 |
2 files changed, 118 insertions, 0 deletions
diff --git a/prcons.py b/prcons.py new file mode 100755 index 0000000..d2c9b8d --- /dev/null +++ b/prcons.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 + +"""Basic console interface for running pre-rendered functions. + +Supports either pre-rendering a function from a workspace or loading a pickled pre-render from +the disk. +""" + +import pickle +import sys +import time + +from blc.workspace import Workspace, PreRenderable +from blc.audio import DefaultAudioPlayer +from blc.output import LightingOutput + +__version__ = "0.0.1" + +class DummyOutput(LightingOutput): + """Output that prints changed values to the console.""" + def set_values(self, values): + for c,v in values: + univ, addr = c + if self.values[addr] != v: + print("U%1d C%3d to %3d" % (univ, addr, v)) + self.values[addr] = v + + def __init__(self): + self.values = {c: 0 for c in range(1,513)} + +minnx = 10 +aminnx = minnx/1000 + +print("PRCons v%s" % __version__) +print() +if len(sys.argv) == 3: + w = Workspace.load(sys.argv[1]) + f = w.functions[int(sys.argv[2])] + + if not issubclass(type(f), PreRenderable): + raise ValueError("Function must be PreRenderable!") + + print("Rendering...") + render = f.render_all(minnx=minnx) +elif len(sys.argv) == 2: + print("Loading from %s..." % sys.argv[1]) + with open(sys.argv[1], "rb") as f: + render = pickle.load(f) +else: + print("Usage: %s <workspace> <function id>" % sys.argv[0]) + print(" %s <pickled render>" % sys.argv[0]) + sys.exit(1) + +input("Ready, press enter to start: ") +print("Starting show") +output = DummyOutput() +for n, (values, acues) in enumerate(render, 1): + values = list(values) + acues = list(acues) + print("Step", n) + start = time.monotonic() + while values or acues: + t = 1000*(time.monotonic() - start) + while values and values[0][0] < t: + output.set_values(values.pop(0)[1]) + while acues and acues[0][0] < t: + fname = acues.pop(0)[2] + print("Playing", fname) + DefaultAudioPlayer(fname).play() + time.sleep(aminnx) + print("Done step", n) + input("Press enter to continue: ") diff --git a/prstore.py b/prstore.py new file mode 100755 index 0000000..06d1016 --- /dev/null +++ b/prstore.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +"""Pre-render functions into a file. + +Uses the pickle module to serialize the render. + +There are some caveats to this: + +- Audio filepaths are stored absolutely, so this should be run on the system that will be + running the show, or take care to ensure that the directory structure is identical +- Audio lengths are ignored +""" + +import pickle +import sys + +from blc.workspace import Workspace, PreRenderable + +if len(sys.argv) not in (3,4,): + print("Usage: %s <workspace file> <prerenderable id> [output file]" % sys.argv[0]) + sys.exit(1) + +minnx = 10 + +w = Workspace.load(sys.argv[1]) +func = w.functions[int(sys.argv[2])] +if not issubclass(type(func), PreRenderable): + raise ValueError("The given function may not be rendered") + +if len(sys.argv) == 4: + ofname = sys.argv[3] +else: + ofname = func.name+".pickle" + print("Storing to", ofname) + +with open(ofname, "wb") as f: + print("Rendering", func.name) + p = pickle.Pickler(f) + try: + render = func.render_all(minnx=minnx) + except ValueError: + print("Cannot render", func.name) + else: + print("Storing render") + p.dump(render) + print("Done!") |