From: Olivier Guilyardi Date: Wed, 17 Feb 2010 23:16:06 +0000 (+0000) Subject: - add example processor illustrating FixedSizeInputAdapter usage X-Git-Tag: 0.3.2~194 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=1476d28048bbab7e37fbf98e29d1d1177c2fd21e;p=timeside.git - add example processor illustrating FixedSizeInputAdapter usage - 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 --- diff --git a/api.py b/api.py index 30a5f10..702f510 100644 --- 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. diff --git a/tests/api/examples.py b/tests/api/examples.py index 40ebe7b..ae1a64a 100644 --- a/tests/api/examples.py +++ b/tests/api/examples.py @@ -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 + + + + diff --git a/tests/api/test_pipe.py b/tests/api/test_pipe.py index 571646f..2296f31 100644 --- a/tests/api/test_pipe.py +++ b/tests/api/test_pipe.py @@ -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()