]> git.parisson.com Git - timeside.git/commitdiff
- add example processor illustrating FixedSizeInputAdapter usage
authorOlivier Guilyardi <olivier@samalyse.com>
Wed, 17 Feb 2010 23:16:06 +0000 (23:16 +0000)
committerOlivier Guilyardi <olivier@samalyse.com>
Wed, 17 Feb 2010 23:16:06 +0000 (23:16 +0000)
- require IProcessor.processor() to always return a 2D array (even for 1 channel)
- fix example FileDecoder, so that it properly returns a 2D array when channels=1

api.py
tests/api/examples.py
tests/api/test_pipe.py

diff --git a/api.py b/api.py
index 30a5f1059405b34ab5cb960b6e44cea076c28e16..702f510ac858738e06b71ae3305c5535e14d6ac4 100644 (file)
--- a/api.py
+++ b/api.py
@@ -57,7 +57,7 @@ class IProcessor(Interface):
 
     def process(self, frames=None, eod=False):
         """Process input frames and return a (output_frames, eod) tuple.
-        Both input and output frames are numpy arrays, where columns are 
+        Both input and output frames are 2D numpy arrays, where columns are 
         channels, and containing an undetermined number of frames.  eod=True 
         means that the end-of-data has been reached.
         
index 40ebe7becbfc43eb3dea281fa8a8d473eb5bfd3e..ae1a64a10f44bcb471fbd2b64466f84a19f2529d 100644 (file)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-from timeside.core import Processor, implements, interfacedoc
+from timeside.core import Processor, implements, interfacedoc, FixedSizeInputAdapter
 from timeside.api import *
 from timeside.graph import *
 from timeside import Metadata
@@ -92,6 +92,10 @@ class FileDecoder(Processor):
         eod            = (toread < buffersize)
         self.position += toread
 
+        # audiolab returns a 1D array for 1 channel, need to reshape to 2D: 
+        if self.file.channels == 1:
+            frames = frames.reshape(len(frames), 1)
+
         return frames, eod
 
 class MaxLevel(Processor):
@@ -294,4 +298,42 @@ class Duration(Processor):
     def result(self):
         return self.input_nframes / float(self.input_samplerate)
 
+class FixedInputProcessor(Processor):
+    """Processor which does absolutely nothing except illustrating the use
+    of the FixedInputSizeAdapter. It also tests things a bit."""
+
+    implements(IProcessor)
+
+    BUFFER_SIZE = 1024
+
+    @staticmethod
+    @interfacedoc
+    def id():
+        return "test_fixed"
+
+    @interfacedoc
+    def setup(self, channels, samplerate, nframes):
+        super(FixedInputProcessor, self).setup(channels, samplerate, nframes)
+        self.adapter = FixedSizeInputAdapter(self.BUFFER_SIZE, channels, pad=True)
+
+    @interfacedoc        
+    def nframes(self):
+        return self.adapter.nframes(self.input_nframes)
+
+    @interfacedoc
+    def process(self, frames, eod=False):
+        try:
+            for buffer, end in self.adapter.process(frames, eod):
+                # Test that the adapter is actually doing the job:
+                if len(buffer) != self.BUFFER_SIZE:
+                    raise Exception("Bad buffer size from adapter")
+        except ValueError:
+            print len(frames)
+            raise
+
+        return frames, eod                    
+
+
+
+            
 
index 571646f46c61eca54569653dc17eb3cf860b314f..2296f316545a69bf169580104c1d842512f37be2 100644 (file)
@@ -24,8 +24,9 @@ print "duration: %f %s" % (duration.result(), duration.unit())
 
 gain     = examples.Gain(gain)
 encoder  = examples.WavEncoder("normalized.wav")
+fixed    = examples.FixedInputProcessor()
 
-subpipe  = gain | maxlevel
+subpipe  = gain | fixed | maxlevel
 
 (decoder | subpipe | encoder).run()