Skip to content

Commit

Permalink
Turn none and all/e to regular procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Oct 2, 2023
1 parent e94de43 commit 283eb56
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 31 deletions.
20 changes: 8 additions & 12 deletions auto_editor/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,14 @@ def obj_tag(tag: str, tb: Fraction, obj: dict[str, Any]) -> str:
return key


@dataclass
class Levels:
def __init__(
self, ensure: Ensure, src: FileInfo, tb: Fraction, bar: Bar, temp: str, log: Log
):
self.ensure = ensure
self.src = src
self.tb = tb
self.bar = bar
self.temp = temp
self.log = log
ensure: Ensure
src: FileInfo
tb: Fraction
bar: Bar
temp: str
log: Log

@property
def media_length(self) -> int:
Expand All @@ -167,9 +165,7 @@ def media_length(self) -> int:
return len(arr)

sr, samples = read(
self.ensure.audio(
f"{self.src.path.resolve()}", self.src.label, stream=0
)
self.ensure.audio(f"{self.src.path.resolve()}", self.src.label, 0)
)
samp_count = len(samples)
del samples
Expand Down
19 changes: 18 additions & 1 deletion auto_editor/lang/palet.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ClosingError(MyError):
LPAREN, RPAREN, LBRAC, RBRAC, LCUR, RCUR, EOF = "(", ")", "[", "]", "{", "}", "EOF"
VAL, QUOTE, SEC, DB, DOT, VLIT = "VAL", "QUOTE", "SEC", "DB", "DOT", "VLIT"
SEC_UNITS = ("s", "sec", "secs", "second", "seconds")
METHODS = ("audio:", "motion:", "pixeldiff:", "subtitle:", "none:", "all/e:")
METHODS = ("audio:", "motion:", "pixeldiff:", "subtitle:")
brac_pairs = {LPAREN: RPAREN, LBRAC: RBRAC, LCUR: RCUR}

str_escape = {
Expand Down Expand Up @@ -1334,6 +1334,20 @@ def attr(env: Env, node: list) -> Any:
return my_eval(env, [node[2], node[1]])


def edit_none() -> np.ndarray:
if "@levels" not in env:
raise MyError("Can't use `none` if there's no input media")

return env["@levels"].none()


def edit_all() -> np.ndarray:
if "@levels" not in env:
raise MyError("Can't use `all/e` if there's no input media")

return env["@levels"].all()


def my_eval(env: Env, node: object) -> Any:
if type(node) is Sym:
val = env.get(node.val)
Expand Down Expand Up @@ -1392,6 +1406,9 @@ def my_eval(env: Env, node: object) -> Any:
"true": True,
"false": False,
"all": Sym("all"),
# edit procedures
"none": Proc("none", edit_none, (0, 0)),
"all/e": Proc("all/e", edit_all, (0, 0)),
# syntax
"lambda": Syntax(syn_lambda),
"λ": Syntax(syn_lambda),
Expand Down
39 changes: 24 additions & 15 deletions auto_editor/make_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import numpy as np

from auto_editor.analyze import FileSetup
from auto_editor.analyze import FileSetup, Levels
from auto_editor.ffwrapper import FFmpeg, FileInfo
from auto_editor.lang.palet import Lexer, Parser, env, interpret, is_boolarr
from auto_editor.lib.data_structs import print_str
Expand Down Expand Up @@ -90,31 +90,40 @@ def make_av(
return vtl, atl


def run_interpreter(
text: str,
filesetup: FileSetup,
log: Log,
def run_interpreter_for_edit_option(
text: str, filesetup: FileSetup
) -> NDArray[np.bool_]:
ensure = filesetup.ensure
src = filesetup.src
tb = filesetup.tb
bar = filesetup.bar
temp = filesetup.temp
log = filesetup.log

try:
parser = Parser(Lexer("`--edit`", text))
if log.is_debug:
log.debug(f"edit: {parser}")

env["timebase"] = filesetup.tb
env["@levels"] = Levels(ensure, src, tb, bar, temp, log)
env["@filesetup"] = filesetup

results = interpret(env, parser)
except (MyError, ZeroDivisionError) as e:
log.error(e)

if len(results) == 0:
log.error("Expression in --edit must return a bool-array, got nothing")
if len(results) == 0:
raise MyError("Expression in --edit must return a bool-array, got nothing")

result = results[-1]
if not is_boolarr(result):
log.error(
f"Expression in --edit must return a bool-array, got {print_str(result)}"
)
result = results[-1]
if callable(result):
result = result()

if not is_boolarr(result):
raise MyError(
f"Expression in --edit must return a bool-array, got {print_str(result)}"
)
except MyError as e:
log.error(e)

assert isinstance(result, np.ndarray)
return result
Expand Down Expand Up @@ -276,7 +285,7 @@ def mut_set_range(arr: NDArray, _ranges: list[list[str]], index: Any) -> None:

for i in map(str, inputs):
filesetup = FileSetup(sources[i], ensure, len(inputs) < 2, tb, bar, temp, log)
has_loud = run_interpreter(method, filesetup, log)
has_loud = run_interpreter_for_edit_option(method, filesetup)

if len(mark_loud) > 0:
mut_set_range(has_loud, mark_loud, loud_speed)
Expand Down
7 changes: 4 additions & 3 deletions auto_editor/subcommands/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fractions import Fraction

import auto_editor
from auto_editor.analyze import FileSetup
from auto_editor.analyze import FileSetup, Levels
from auto_editor.ffwrapper import FFmpeg, FileInfo
from auto_editor.lang.palet import ClosingError, Lexer, Parser, env, interpret
from auto_editor.lib.data_structs import print_str
Expand Down Expand Up @@ -77,9 +77,10 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None:
src = sources["0"]
tb = src.get_fps() if args.timebase is None else args.timebase
ensure = Ensure(ffmpeg, src.get_sr(), temp, log)
filesetup = FileSetup(src, ensure, strict, tb, Bar("none"), temp, log)
bar = Bar("none")
env["timebase"] = tb
env["@filesetup"] = filesetup
env["@levels"] = Levels(ensure, src, tb, bar, temp, log)
env["@filesetup"] = FileSetup(src, ensure, strict, tb, bar, temp, log)

print(f"Auto-Editor {auto_editor.version} ({auto_editor.__version__})")
text = None
Expand Down

0 comments on commit 283eb56

Please sign in to comment.