Skip to content

Commit

Permalink
Refactor NRT rendering (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
josiah-wolf-oberholtzer authored Nov 28, 2022
1 parent 943f8f1 commit 4836d31
Show file tree
Hide file tree
Showing 14 changed files with 607 additions and 899 deletions.
12 changes: 9 additions & 3 deletions supriya/ext/book.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import base64
import hashlib
import pathlib
Expand Down Expand Up @@ -68,9 +69,14 @@ def render(cls, node, output_path):
renderable, render_kwargs = pickle.loads(
base64.b64decode("".join(node[0].split()))
)
return websafe_audio(
renderable.__render__(render_directory_path=output_path, **render_kwargs)
)
if callable(renderable):
render_function, path = renderable()
else:
render_function, path = renderable.__render__(
render_directory_path=output_path, **render_kwargs
)
asyncio.run(render_function())
return websafe_audio(path)

@staticmethod
def visit_block_html(self, node):
Expand Down
8 changes: 5 additions & 3 deletions supriya/intervals/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""
Tools for modeling overlapping time structures with timespans.
"""
from .Interval import Interval # noqa
from .IntervalTree import IntervalTree # noqa
from .Interval import Interval
from .IntervalTree import IntervalTree
from .IntervalTreeDriver import IntervalTreeDriver # noqa
from .Moment import Moment # noqa
from .Moment import Moment

try:
from .IntervalTreeDriverEx import IntervalTreeDriverEx # noqa
except ModuleNotFoundError:
pass

__all__ = ["Interval", "IntervalTree", "Moment"]
38 changes: 24 additions & 14 deletions supriya/io.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import asyncio
import dataclasses
import datetime
import hashlib
import pathlib
import platform
import subprocess
from os import PathLike
from typing import Optional
from pathlib import Path
from typing import Callable, Coroutine, Optional, Tuple

from uqbar.graphs import Grapher
from uqbar.io import open_path
Expand All @@ -20,25 +21,26 @@ class PlayMemo:
contents: bytes
suffix: str

def __render__(
def __call__(
self,
output_file_path: Optional[PathLike] = None,
render_directory_path: Optional[PathLike] = None,
**kwargs,
) -> pathlib.Path:
) -> Tuple[Callable[[], Coroutine[None, None, int]], Path]:
async def render_function():
path.write_bytes(self.contents)
return 0

if output_file_path is None:
hexdigest = hashlib.sha1(self.contents).hexdigest()
file_name = f"audio-{hexdigest}{self.suffix}"
file_path = (
pathlib.Path(render_directory_path or supriya.output_path) / file_name
)
path = Path(render_directory_path or supriya.output_path) / file_name
else:
file_path = pathlib.Path(output_file_path)
file_path.write_bytes(self.contents)
return file_path
path = Path(output_file_path)
return render_function, path

@classmethod
def from_path(cls, path: pathlib.Path) -> "PlayMemo":
def from_path(cls, path: Path) -> "PlayMemo":
return cls(contents=path.read_bytes(), suffix=path.suffix)


Expand Down Expand Up @@ -71,7 +73,9 @@ def render(self):
result = self.renderable.__render__(**self.render_kwargs)
if hasattr(result, "__render__"):
result = result.__render__(**self.render_kwargs)
return result
coroutine, path = result
exit_code = asyncio.run(coroutine)
return exit_code, path


class Plotter:
Expand Down Expand Up @@ -129,12 +133,18 @@ def render(
output_file_path: Optional[PathLike] = None,
render_directory_path: Optional[PathLike] = None,
**kwargs,
):
return renderable.__render__(
) -> Tuple[int, Path]:
result = renderable.__render__(
output_file_path=output_file_path,
render_directory_path=render_directory_path,
**kwargs,
)
if callable(result):
render_function, path = result()
else:
render_function, path = result
exit_code = asyncio.run(render_function())
return exit_code, path


__all__ = ["Player", "Plotter", "graph", "play", "plot", "render"]
5 changes: 2 additions & 3 deletions supriya/nonrealtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from .buffers import Buffer, BufferGroup
from .buses import AudioInputBusGroup, AudioOutputBusGroup, Bus, BusGroup
from .nodes import Group, Node, RootNode, Synth
from .renderer import SessionRenderer
from .sessions import Session
from .sessions import Renderer, Session
from .states import DoNotPropagate, Moment, NodeTransition, State

__all__ = [
Expand All @@ -21,10 +20,10 @@
"Moment",
"Node",
"NodeTransition",
"Renderer",
"RootNode",
"Session",
"SessionObject",
"SessionRenderer",
"State",
"Synth",
]
Loading

0 comments on commit 4836d31

Please sign in to comment.