Skip to content

Commit

Permalink
Add Graph.link_nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Jul 9, 2024
1 parent b98a12e commit 42f3e1e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions av/filter/graph.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Graph:

def __init__(self) -> None: ...
def configure(self, auto_buffer: bool = True, force: bool = False) -> None: ...
def link_nodes(self, *nodes: FilterContext) -> Graph: ...
def add(
self, filter: str | Filter, args: Any = None, **kwargs: str
) -> FilterContext: ...
Expand Down
9 changes: 8 additions & 1 deletion av/filter/graph.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ cdef class Graph:
# We get auto-inserted stuff here.
self._auto_register()

def link_nodes(self, *nodes):
"""
Links nodes together for simple filter graphs.
"""
for c, n in zip(nodes, nodes[1:]):
c.link_to(n)
return self

def add(self, filter, args=None, **kwargs):
cdef Filter cy_filter
Expand All @@ -68,7 +75,7 @@ cdef class Graph:

# There might have been automatic contexts added (e.g. resamplers,
# fifos, and scalers). It is more likely to see them after the graph
# is configured, but we wan't to be safe.
# is configured, but we want to be safe.
self._auto_register()

return ctx
Expand Down
15 changes: 4 additions & 11 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,9 @@ def test_audio_buffer_sink(self):
if e.errno != errno.EAGAIN:
raise

@staticmethod
def link_nodes(*nodes):
for c, n in zip(nodes, nodes[1:]):
c.link_to(n)

def test_audio_buffer_resample(self):
graph = Graph()
self.link_nodes(
graph.link_nodes(
graph.add_abuffer(
format="fltp",
sample_rate=48000,
Expand All @@ -146,8 +141,7 @@ def test_audio_buffer_resample(self):
"aformat", "sample_fmts=s16:sample_rates=44100:channel_layouts=stereo"
),
graph.add("abuffersink"),
)
graph.configure()
).configure()

graph.push(
generate_audio_frame(
Expand All @@ -161,7 +155,7 @@ def test_audio_buffer_resample(self):

def test_audio_buffer_volume_filter(self):
graph = Graph()
self.link_nodes(
graph.link_nodes(
graph.add_abuffer(
format="fltp",
sample_rate=48000,
Expand All @@ -170,8 +164,7 @@ def test_audio_buffer_volume_filter(self):
),
graph.add("volume", volume="0.5"),
graph.add("abuffersink"),
)
graph.configure()
).configure()

input_frame = generate_audio_frame(
0, input_format="fltp", layout="stereo", sample_rate=48000
Expand Down

0 comments on commit 42f3e1e

Please sign in to comment.