Skip to content

Commit

Permalink
Remove uint?, add nat1?
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Sep 28, 2023
1 parent a34de29 commit 27e2450
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 52 deletions.
22 changes: 11 additions & 11 deletions auto_editor/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from auto_editor.lib.contracts import (
is_bool,
is_nat,
is_nat1,
is_str,
is_threshold,
is_uint,
is_void,
orc,
)
Expand Down Expand Up @@ -46,28 +46,28 @@
audio_builder = pAttrs(
"audio",
pAttr("threshold", 0.04, is_threshold),
pAttr("stream", 0, orc(is_uint, Sym("all"), "all")),
pAttr("mincut", 6, is_uint),
pAttr("minclip", 3, is_uint),
pAttr("stream", 0, orc(is_nat, Sym("all"), "all")),
pAttr("mincut", 6, is_nat),
pAttr("minclip", 3, is_nat),
)
motion_builder = pAttrs(
"motion",
pAttr("threshold", 0.02, is_threshold),
pAttr("stream", 0, is_uint),
pAttr("blur", 9, is_uint),
pAttr("width", 400, is_nat),
pAttr("stream", 0, is_nat),
pAttr("blur", 9, is_nat),
pAttr("width", 400, is_nat1),
)
pixeldiff_builder = pAttrs(
"pixeldiff",
pAttr("threshold", 1, is_uint),
pAttr("stream", 0, is_uint),
pAttr("threshold", 1, is_nat),
pAttr("stream", 0, is_nat),
)
subtitle_builder = pAttrs(
"subtitle",
pAttr("pattern", Required, is_str),
pAttr("stream", 0, is_uint),
pAttr("stream", 0, is_nat),
pAttr("ignore-case", False, is_bool),
pAttr("max-count", None, orc(is_uint, is_void)),
pAttr("max-count", None, orc(is_nat, is_void)),
)

builder_map = {
Expand Down
20 changes: 10 additions & 10 deletions auto_editor/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@
Editing Methods:
- audio ; Audio silence/loudness detection
- threshold threshold? : 4%
- stream (or/c uint? 'all "all") : 0
- mincut uint? : 6
- minclip uint? : 3
- stream (or/c nat? 'all "all") : 0
- mincut nat? : 6
- minclip nat? : 3
- motion ; Motion detection specialized for noisy real-life videos
- threshold threshold? : 2%
- stream uint? : 0
- blur uint? : 9
- width nat? : 400
- stream nat? : 0
- blur nat? : 9
- width nat1? : 400
- none ; Do not modify the media in anyway; mark all sections as "loud" (1).
- all/e ; Cut out everything out; mark all sections as "silent" (0).
- pixeldiff ; Detect when a certain amount of pixels have changed between frames.
- threshold uint? : 1
- stream uint? : 0
- threshold nat? : 1
- stream nat? : 0
- subtitle ; Detect when subtitle matches pattern as a RegEx string.
- pattern string?
- stream uint? : 0
- stream nat? : 0
- ignore-case bool? : #f
- max-count (or/c uint? void?) : (void)
- max-count (or/c nat? void?) : (void)
Command-line Examples:
--edit audio
Expand Down
61 changes: 48 additions & 13 deletions auto_editor/lang/palet.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

if TYPE_CHECKING:
from collections.abc import Callable
from typing import Any, NoReturn
from typing import Any, Literal, NoReturn

from numpy.typing import NDArray

Expand Down Expand Up @@ -479,6 +479,31 @@ def __str__(self) -> str:
is_keyw = Contract("keyword?", lambda v: type(v) is QuotedKeyword)


@dataclass(slots=True)
class OutputPort:
name: str
port: Any
write: Any
closed: bool

def close(self) -> None:
if not self.closed:
self.port.close()

def __str__(self) -> str:
return f"#<output-port:{self.name}>"

__repr__ = __str__


def initOutPort(name: str) -> OutputPort | Literal[False]:
try:
port = open(name, "w", encoding="utf-8")
except Exception:
return False
return OutputPort(name, port, port.write, False)


def raise_(msg: str) -> None:
raise MyError(msg)

Expand Down Expand Up @@ -1382,8 +1407,8 @@ def my_eval(env: Env, node: object) -> Any:
"number?": is_num,
"real?": is_real,
"int?": is_int,
"uint?": is_uint,
"nat?": is_nat,
"nat1?": is_nat1,
"float?": is_float,
"frac?": is_frac,
"threshold?": is_threshold,
Expand Down Expand Up @@ -1485,7 +1510,7 @@ def my_eval(env: Env, node: object) -> Any:
# vectors
"vector": Proc("vector", lambda *a: list(a), (0, None)),
"make-vector": Proc(
"make-vector", lambda size, a=0: [a] * size, (1, 2), is_uint, any_p
"make-vector", lambda size, a=0: [a] * size, (1, 2), is_nat, any_p
),
"vector-append": Proc("vector-append", vector_append, (0, None), is_vector),
"vector-pop!": Proc("vector-pop!", list.pop, (1, 1), is_vector),
Expand All @@ -1496,21 +1521,21 @@ def my_eval(env: Env, node: object) -> Any:
"sort!": Proc("sort!", list.sort, (1, 1), is_vector),
# arrays
"array": Proc("array", array_proc, (2, None), is_symbol, is_real),
"make-array": Proc("make-array", make_array, (2, 3), is_symbol, is_uint, is_real),
"make-array": Proc("make-array", make_array, (2, 3), is_symbol, is_nat, is_real),
"array-splice!": Proc(
"array-splice!", splice, (2, 4), is_array, is_real, is_int, is_int
),
"array-copy": Proc("array-copy", np.copy, (1, 1), is_array),
"count-nonzero": Proc("count-nonzero", np.count_nonzero, (1, 1), is_array),
# bool arrays
"bool-array": Proc(
"bool-array", lambda *a: np.array(a, dtype=np.bool_), (1, None), is_uint
"bool-array", lambda *a: np.array(a, dtype=np.bool_), (1, None), is_nat
),
"margin": Proc("margin", margin, (2, 3)),
"mincut": Proc("mincut", mincut, (2, 2), is_boolarr, is_uint),
"minclip": Proc("minclip", minclip, (2, 2), is_boolarr, is_uint),
"maxcut": Proc("maxcut", maxcut, (2, 2), is_boolarr, is_uint),
"maxclip": Proc("maxclip", maxclip, (2, 2), is_boolarr, is_uint),
"mincut": Proc("mincut", mincut, (2, 2), is_boolarr, is_nat),
"minclip": Proc("minclip", minclip, (2, 2), is_boolarr, is_nat),
"maxcut": Proc("maxcut", maxcut, (2, 2), is_boolarr, is_nat),
"maxclip": Proc("maxclip", maxclip, (2, 2), is_boolarr, is_nat),
# ranges
"range": Proc("range", range, (1, 3), is_int, is_int, int_not_zero),
# generic iterables
Expand All @@ -1533,14 +1558,24 @@ def my_eval(env: Env, node: object) -> Any:
"hash-update!": UserProc(env, "hash-update!", ["h", "v", "up"], (is_hash, any_p),
[[Sym("hash-set!"), Sym("h"), Sym("v"), [Sym("up"), [Sym("hash-ref"), Sym("h"), Sym("v")]]]],
),
# i/o
"open-output-file": Proc("open-output-file", initOutPort, (1, 1), is_str),
"output-port?": (op := Contract("output-port?", lambda v: type(v) is OutputPort)),
"close-port": Proc("close-port", OutputPort.close, (1, 1), op),
"closed?": Proc("closed?", lambda o: o.closed, (1, 1), op),
# printing
"display": Proc("display",
lambda v, f=None: print(display_str(v), end="", file=f), (1, 2), any_p, op),
"displayln": Proc("displayln",
lambda v, f=None: print(display_str(v), file=f), (1, 2), any_p, op),
"print": Proc("print",
lambda v, f=None: print(print_str(v), end="", file=f), (1, 2), any_p, op),
"println": Proc("println",
lambda v, f=None: print(print_str(v), file=f), (1, 2), any_p, op),
# actions
"assert": Proc("assert", palet_assert, (1, 2), any_p, orc(is_str, False)),
"display": Proc("display", lambda v: print(display_str(v), end=""), (1, 1)),
"displayln": Proc("displayln", lambda v: print(display_str(v)), (1, 1)),
"error": Proc("error", raise_, (1, 1), is_str),
"sleep": Proc("sleep", sleep, (1, 1), is_int_or_float),
"print": Proc("print", lambda v: print(print_str(v), end=""), (1, 1)),
"println": Proc("println", lambda v: print(print_str(v)), (1, 1)),
"system": Proc("system", palet_system, (1, 1), is_str),
# conversions
"number->string": Proc("number->string", number_to_string, (1, 1), is_num),
Expand Down
4 changes: 2 additions & 2 deletions auto_editor/lib/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def is_contract(c: object) -> bool:

is_bool = Contract("bool?", lambda v: type(v) is bool)
is_int = Contract("int?", lambda v: type(v) is int)
is_uint = Contract("uint?", lambda v: type(v) is int and v > -1)
is_nat = Contract("nat?", lambda v: type(v) is int and v > 0)
is_nat = Contract("nat?", lambda v: type(v) is int and v > -1)
is_nat1 = Contract("nat1?", lambda v: type(v) is int and v > 0)
int_not_zero = Contract("(or/c (not/c 0) int?)", lambda v: v != 0 and is_int(v))
is_num = Contract("number?", lambda v: type(v) in (int, float, Fraction, complex))
is_real = Contract("real?", lambda v: type(v) in (int, float, Fraction))
Expand Down
32 changes: 16 additions & 16 deletions auto_editor/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,59 +100,59 @@ class TlEllipse(_Visual):

video_builder = pAttrs(
"video",
pAttr("start", Required, is_uint),
pAttr("dur", Required, is_uint),
pAttr("start", Required, is_nat),
pAttr("dur", Required, is_nat),
pAttr("src", Required, is_str),
pAttr("offset", 0, is_int),
pAttr("speed", 1, is_real),
pAttr("stream", 0, is_uint),
pAttr("stream", 0, is_nat),
)
audio_builder = pAttrs(
"audio",
pAttr("start", Required, is_uint),
pAttr("dur", Required, is_uint),
pAttr("start", Required, is_nat),
pAttr("dur", Required, is_nat),
pAttr("src", Required, is_str),
pAttr("offset", 0, is_int),
pAttr("speed", 1, is_real),
pAttr("volume", 1, is_real),
pAttr("stream", 0, is_uint),
pAttr("stream", 0, is_nat),
)
text_builder = pAttrs(
"text",
pAttr("start", Required, is_uint),
pAttr("dur", Required, is_uint),
pAttr("start", Required, is_nat),
pAttr("dur", Required, is_nat),
pAttr("content", Required, is_str),
pAttr("x", 0.5, is_real),
pAttr("y", 0.5, is_real),
pAttr("font", "Arial", is_str),
pAttr("size", 55, is_uint),
pAttr("size", 55, is_nat),
pAttr("align", "left", is_str),
pAttr("opacity", 1, is_threshold),
pAttr("anchor", "ce", is_str),
pAttr("rotate", 0, is_real),
pAttr("fill", "#FFF", is_str),
pAttr("stroke", 0, is_uint),
pAttr("stroke", 0, is_nat),
pAttr("strokecolor", "#000", is_str),
)

img_builder = pAttrs(
"image",
pAttr("start", Required, is_uint),
pAttr("dur", Required, is_uint),
pAttr("start", Required, is_nat),
pAttr("dur", Required, is_nat),
pAttr("src", Required, is_str),
pAttr("x", 0.5, is_real),
pAttr("y", 0.5, is_real),
pAttr("opacity", 1, is_threshold),
pAttr("anchor", "ce", is_str),
pAttr("rotate", 0, is_real),
pAttr("stroke", 0, is_uint),
pAttr("stroke", 0, is_nat),
pAttr("strokecolor", "#000", is_str),
)

rect_builder = pAttrs(
"rect",
pAttr("start", Required, is_uint),
pAttr("dur", Required, is_uint),
pAttr("start", Required, is_nat),
pAttr("dur", Required, is_nat),
pAttr("x", Required, is_real),
pAttr("y", Required, is_real),
pAttr("width", Required, is_real),
Expand All @@ -161,7 +161,7 @@ class TlEllipse(_Visual):
pAttr("anchor", "ce", is_str),
pAttr("rotate", 0, is_real),
pAttr("fill", "#c4c4c4", is_str),
pAttr("stroke", 0, is_uint),
pAttr("stroke", 0, is_nat),
pAttr("strokecolor", "#000", is_str),
)
ellipse_builder = rect_builder
Expand Down

0 comments on commit 27e2450

Please sign in to comment.