From 3d56eb0681f4d3a74cc9fa1052ec19966b580f26 Mon Sep 17 00:00:00 2001 From: Olivier Guilyardi Date: Wed, 2 Dec 2009 16:10:40 +0000 Subject: [PATCH] add IProcessor.release() --- api.py | 4 ++++ core.py | 4 ++++ tests/api/examples.py | 26 ++++++++++++++------------ tests/api/test_lolevel.py | 11 ++++++++--- tests/api/test_pipe.py | 2 ++ 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/api.py b/api.py index ae9c1b0..e7db2bb 100644 --- a/api.py +++ b/api.py @@ -58,6 +58,10 @@ class IProcessor(Interface): Warning: it is required to call setup() before this method.""" + def release(self): + """Release resources owned by this processor. The processor cannot + be used anymore after calling this method.""" + class IEncoder(IProcessor): """Encoder driver interface. Each encoder is expected to support a specific format.""" diff --git a/core.py b/core.py index 57f4405..e27200a 100644 --- a/core.py +++ b/core.py @@ -76,6 +76,10 @@ class Processor(Component): def process(self, frames): return frames + @interfacedoc + def release(self): + pass + def __or__(self, item): return ProcessPipe(self, item) diff --git a/tests/api/examples.py b/tests/api/examples.py index 5502bf6..15afed2 100644 --- a/tests/api/examples.py +++ b/tests/api/examples.py @@ -16,15 +16,22 @@ class FileDecoder(Processor): @interfacedoc def __init__(self, filename): self.filename = filename - self.file = None + # The file has to be opened here so that nframes(), samplerate(), + # etc.. work before setup() is called. + self.file = audiolab.sndfile(self.filename, 'read') + self.position = 0 @interfacedoc def setup(self, channels=None, samplerate=None): Processor.setup(self, channels, samplerate) + if self.position != 0: + self.file.seek(0); + self.position = 0 + + def release(self): if self.file: - self.file.close(); - self.file = audiolab.sndfile(self.filename, 'read') - self.position = 0 + self.file.close() + self.file = None @interfacedoc def channels(self): @@ -82,16 +89,10 @@ class FileDecoder(Processor): if toread > buffersize: toread = buffersize - frames = self.file.read_frames(toread) - + frames = self.file.read_frames(toread) + eod = (toread < buffersize) self.position += toread - eod = False - if toread < buffersize: - self.file.close() - self.file = None - eod = True - return frames, eod class MaxLevel(Processor): @@ -201,3 +202,4 @@ class WavEncoder(Processor): self.file = None return frames, eod + diff --git a/tests/api/test_lolevel.py b/tests/api/test_lolevel.py index 9c341e3..1c87aba 100644 --- a/tests/api/test_lolevel.py +++ b/tests/api/test_lolevel.py @@ -3,10 +3,10 @@ import os source=os.path.dirname(__file__) + "../samples/guitar.wav" -Decoder = examples.AudiolabDecoder +Decoder = examples.FileDecoder print "Creating decoder with id=%s for: %s" % (Decoder.id(), source) decoder = Decoder(source) -analyzer = examples.MaxLevelAnalyzer() +analyzer = examples.MaxLevel() decoder.setup() nchannels = decoder.channels() samplerate = decoder.samplerate() @@ -24,6 +24,8 @@ while True: max_level = analyzer.result() print "Max level: %f" % max_level +analyzer.release() + destination = "normalized.wav" Encoder = examples.WavEncoder print "Creating encoder with id=%s for: %s" % (Encoder.id(), destination) @@ -33,7 +35,7 @@ gain = 1 if max_level > 0: gain = 0.9 / max_level -effect = examples.GainEffect(gain) +effect = examples.Gain(gain) decoder.setup() effect.setup(decoder.channels(), decoder.samplerate()) @@ -47,3 +49,6 @@ while True: if eod: break +decoder.release() +effect.release() +encoder.release() diff --git a/tests/api/test_pipe.py b/tests/api/test_pipe.py index d139840..cb56802 100644 --- a/tests/api/test_pipe.py +++ b/tests/api/test_pipe.py @@ -1,5 +1,7 @@ from timeside.tests.api import examples from timeside.core import * +from timeside.api import * +from sys import stdout import os source=os.path.dirname(__file__) + "../samples/guitar.wav" -- 2.39.5