From 7398bf2df8369b144a1401487ee639531d7ad901 Mon Sep 17 00:00:00 2001 From: Olivier Guilyardi Date: Thu, 3 Dec 2009 10:06:05 +0000 Subject: [PATCH] support process pipes concatenation (ie: pipe1 | pipe2) --- core.py | 27 ++++++++++++++++++--------- tests/api/test_pipe.py | 7 ++++++- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/core.py b/core.py index e27200a..9a6f449 100644 --- a/core.py +++ b/core.py @@ -80,8 +80,8 @@ class Processor(Component): def release(self): pass - def __or__(self, item): - return ProcessPipe(self, item) + def __or__(self, other): + return ProcessPipe(self, other) def processors(interface=IProcessor, recurse=True): """Returns the processors implementing a given interface and, if recurse, @@ -100,14 +100,23 @@ def get_processor(processor_id): class ProcessPipe(object): """Handle a pipe of processors""" - def __init__(self, *processors): - self.processors = processors + def __init__(self, *others): + self.processors = [] + for p in others: + self |= p - def __or__(self, processor): - p = [] - p.extend(self.processors) - p.append(processor) - return ProcessPipe(*p) + def __or__(self, other): + return ProcessPipe(self, other) + + def __ior__(self, other): + if isinstance(other, Processor): + self.processors.append(other) + elif isinstance(other, ProcessPipe): + self.processors.extend(other.processors) + else: + raise Error("Piped item is neither a Processor nor a ProcessPipe") + + return self def run(self): """Setup/reset all processors in cascade and stream audio data along diff --git a/tests/api/test_pipe.py b/tests/api/test_pipe.py index cb56802..7465090 100644 --- a/tests/api/test_pipe.py +++ b/tests/api/test_pipe.py @@ -6,6 +6,7 @@ from sys import stdout import os source=os.path.dirname(__file__) + "../samples/guitar.wav" +print "Normalizing %s" % source decoder = examples.FileDecoder(source) maxlevel = examples.MaxLevel() @@ -21,6 +22,10 @@ print "gain: %f" % gain gain = examples.Gain(gain) encoder = examples.WavEncoder("normalized.wav") -(decoder | gain | maxlevel | encoder).run() +subpipe = gain | maxlevel + +(decoder | subpipe | encoder).run() print "output maxlevel: %f" % maxlevel.result() + + -- 2.39.5