]> git.parisson.com Git - timeside.git/commitdiff
support process pipes concatenation (ie: pipe1 | pipe2)
authorOlivier Guilyardi <olivier@samalyse.com>
Thu, 3 Dec 2009 10:06:05 +0000 (10:06 +0000)
committerOlivier Guilyardi <olivier@samalyse.com>
Thu, 3 Dec 2009 10:06:05 +0000 (10:06 +0000)
core.py
tests/api/test_pipe.py

diff --git a/core.py b/core.py
index e27200ae8f9addb6d27f6b0f3bbe133b786e7a9a..9a6f44965e4ba3a4d9ac7dd5aa83efdd576874a3 100644 (file)
--- 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
index cb56802fd5a8dae44fbbff62e866eedb70222731..746509038734ded57d30bbaa5ffb507d3d909f07 100644 (file)
@@ -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()
+
+