From: yomguy Date: Fri, 18 Jun 2010 22:54:43 +0000 (+0000) Subject: clean examples, fix test_pipe X-Git-Tag: 0.3.2~131 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=f18888992baf848bf71a3a3a29bdf8e10faa53a3;p=timeside.git clean examples, fix test_pipe --- diff --git a/INSTALL b/INSTALL index c622564..c5adc74 100644 --- a/INSTALL +++ b/INSTALL @@ -72,7 +72,8 @@ And change only the following variables of the GrapherScheme object:: self.color_scheme = { 'waveform': [ # Four (R,G,B) tuples for three main color channels for the spectral centroid method - (50,0,200), (0,220,80), (255,224,0), (255,0,0) + (173,173,173), (147,149,196), (77,80,138), (108,66,0) + # this is a purple one ], 'spectrogram': [ (0, 0, 0), (58/4,68/4,65/4), (80/2,100/2,153/2), (90,180,100), (224,224,44), (255,60,30), (255,255,255) @@ -104,9 +105,11 @@ For example, a normalization and a waveform, from a python shell:: >>> decoder = timeside.decoder.FileDecoder('source.wav') >>> grapher = timeside.grapher.Waveform('image.png') ->>> encoder = timeside.encoder.WavEncoder('normalized.wav') ->>> (decoder | grapher | encoder).run() +>>> analyzer = timeside.analyzer.MaxLevel() +>>> encoder = timeside.encoder.WavEncoder('output.wav') +>>> (decoder | grapher | analyzer | encoder).run() >>> grapher.render() +>>> print 'Level:', analyzer.result() See the website for more examples and information about the TimeSide API: diff --git a/timeside/tests/__init__.py b/timeside/tests/__init__.py index 2a6b9db..09bc2d6 100644 --- a/timeside/tests/__init__.py +++ b/timeside/tests/__init__.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import unittest import sys @@ -17,7 +18,7 @@ class TestCase(unittest.TestCase): for item in list2: if not item in list1: self.fail("%s is not in list1" % str(item)) - + class _TextTestResult(unittest.TestResult): """A test result class that can print formatted text results to a stream. diff --git a/timeside/tests/api/__init__.py b/timeside/tests/api/__init__.py index e69de29..40a96af 100644 --- a/timeside/tests/api/__init__.py +++ b/timeside/tests/api/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/timeside/tests/api/examples.py b/timeside/tests/api/examples.py index d231387..747c197 100644 --- a/timeside/tests/api/examples.py +++ b/timeside/tests/api/examples.py @@ -1,136 +1,9 @@ # -*- coding: utf-8 -*- + from timeside.core import Processor, implements, interfacedoc, FixedSizeInputAdapter from timeside.api import * -from timeside.grapher import * -from timeside import Metadata -#from scikits import audiolab import numpy -class FileAudiolabDecoder(Processor): - """A simple audiolab-based example decoder""" - implements(IDecoder) - - @staticmethod - @interfacedoc - def id(): - return "test_audiolabdec" - - @interfacedoc - def __init__(self, filename): - self.filename = filename - # The file has to be opened here so that nframes(), samplerate(), - # etc.. work before setup() is called. - self.file = audiolab.Sndfile(self.filename, 'r') - self.position = 0 - - @interfacedoc - def setup(self): - super(FileDecoder, self).setup() - if self.position != 0: - self.file.seek(0); - self.position = 0 - - def release(self): - super(FileDecoder, self).release() - if self.file: - self.file.close() - self.file = None - - @interfacedoc - def channels(self): - return self.file.channels - - @interfacedoc - def samplerate(self): - return self.file.samplerate - - @interfacedoc - def nframes(self): - return self.file.nframes - - @interfacedoc - def format(self): - return self.file.file_format - - @interfacedoc - def encoding(self): - return self.file.encoding - @interfacedoc - def resolution(self): - resolution = None - encoding = self.file.encoding - - if encoding == "pcm8": - resolution = 8 - - elif encoding == "pcm16": - resolution = 16 - elif encoding == "pcm32": - resolution = 32 - - return resolution - - @interfacedoc - def metadata(self): - #TODO - return Metadata() - - @interfacedoc - def process(self, frames=None, eod=False): - if frames: - raise Exception("Decoder doesn't accept input frames") - - buffersize = 0x10000 - - # Need this because audiolab raises a bogus exception when asked - # to read passed the end of the file - toread = self.nframes() - self.position - if toread > buffersize: - toread = buffersize - - frames = self.file.read_frames(toread) - eod = (toread < buffersize) - self.position += toread - - # audiolab returns a 1D array for 1 channel, need to reshape to 2D: - if frames.ndim == 1: - frames = frames.reshape(len(frames), 1) - - return frames, eod - -class MaxLevel(Processor): - implements(IValueAnalyzer) - - @interfacedoc - def setup(self, channels=None, samplerate=None, nframes=None): - super(MaxLevel, self).setup(channels, samplerate, nframes) - self.max_value = 0 - - @staticmethod - @interfacedoc - def id(): - return "test_maxlevel" - - @staticmethod - @interfacedoc - def name(): - return "Max level test analyzer" - - @staticmethod - @interfacedoc - def unit(): - # power? amplitude? - return "" - - def process(self, frames, eod=False): - max = frames.max() - if max > self.max_value: - self.max_value = max - - return frames, eod - - def result(self): - return self.max_value class Gain(Processor): implements(IEffect) @@ -152,199 +25,6 @@ class Gain(Processor): def process(self, frames, eod=False): return numpy.multiply(frames, self.gain), eod -class WavEncoder(Processor): - implements(IEncoder) - - def __init__(self, output): - self.file = None - if isinstance(output, basestring): - self.filename = output - else: - raise Exception("Streaming not supported") - - @interfacedoc - def setup(self, channels=None, samplerate=None, nframes=None): - super(WavEncoder, self).setup(channels, samplerate, nframes) - if self.file: - self.file.close() - - format = audiolab.Format("wav", "pcm16") - self.file = audiolab.Sndfile(self.filename, 'w', format=format, channels=channels, - samplerate=samplerate) - - @staticmethod - @interfacedoc - def id(): - return "test_wavenc" - - @staticmethod - @interfacedoc - def description(): - return "Hackish wave encoder" - - @staticmethod - @interfacedoc - def file_extension(): - return "wav" - - @staticmethod - @interfacedoc - def mime_type(): - return "audio/x-wav" - - @interfacedoc - def set_metadata(self, metadata): - #TODO - pass - - @interfacedoc - def process(self, frames, eod=False): - self.file.write_frames(frames) - if eod: - self.file.close() - self.file = None - - return frames, eod - - -class Waveform(Processor): - implements(IGrapher) - - FFT_SIZE = 0x400 - - @interfacedoc - def __init__(self, width=None, height=None, output=None, bg_color=None, color_scheme=None): - if width: - self.width = width - else: - self.width = 1500 - if height: - self.height = height - else: - self.height = 200 - self.bg_color = bg_color - self.color_scheme = color_scheme - self.filename = output - self.graph = None - - @staticmethod - @interfacedoc - def id(): - return "test_waveform" - - @staticmethod - @interfacedoc - def name(): - return "Waveform test" - - @interfacedoc - def set_colors(self, background, scheme): - self.bg_color = background - self.color_scheme = scheme - - @interfacedoc - def setup(self, channels=None, samplerate=None, nframes=None): - super(Waveform, self).setup(channels, samplerate, nframes) - if self.graph: - self.graph = None - self.graph = WaveformImage(self.width, self.height, self.nframes(), self.samplerate(), self.FFT_SIZE, - bg_color=self.bg_color, color_scheme=self.color_scheme, filename=self.filename) - - @interfacedoc - def process(self, frames, eod=False): - self.graph.process(frames, eod) - return frames, eod - - @interfacedoc - def render(self): - if self.filename: - self.graph.save() - return self.graph.image - - -class Spectrogram(Processor): - implements(IGrapher) - - FFT_SIZE = 0x400 - - @interfacedoc - def __init__(self, width=None, height=None, output=None, bg_color=None, color_scheme=None): - if width: - self.width = width - else: - self.width = 1500 - if height: - self.height = height - else: - self.height = 200 - self.bg_color = bg_color - self.color_scheme = color_scheme - self.filename = output - self.graph = None - - @staticmethod - @interfacedoc - def id(): - return "test_spectrogram" - - @staticmethod - @interfacedoc - def name(): - return "Spectrogram test" - - @interfacedoc - def set_colors(self, background, scheme): - self.bg_color = background - self.color_scheme = scheme - - @interfacedoc - def setup(self, channels=None, samplerate=None, nframes=None): - super(Spectrogram, self).setup(channels, samplerate, nframes) - if self.graph: - self.graph = None - self.graph = SpectrogramImage(self.width, self.height, self.nframes(), self.samplerate(), self.FFT_SIZE, - bg_color=self.bg_color, color_scheme=self.color_scheme, filename=self.filename) - - @interfacedoc - def process(self, frames, eod=False): - self.graph.process(frames, eod) - return frames, eod - - @interfacedoc - def render(self): - if self.filename: - self.graph.save() - return self.graph.image - - -class Duration(Processor): - """A rather useless duration analyzer. Its only purpose is to test the - nframes characteristic.""" - implements(IValueAnalyzer) - - @interfacedoc - def setup(self, channels, samplerate, nframes): - if not nframes: - raise Exception("nframes argument required") - super(Duration, self).setup(channels, samplerate, nframes) - - @staticmethod - @interfacedoc - def id(): - return "test_duration" - - @staticmethod - @interfacedoc - def name(): - return "Duration analyzer" - - @staticmethod - @interfacedoc - def unit(): - return "seconds" - - def result(self): - return self.input_nframes / float(self.input_samplerate) class FixedInputProcessor(Processor): """Processor which does absolutely nothing except illustrating the use diff --git a/timeside/tests/api/test_pipe.py b/timeside/tests/api/test_pipe.py index eaa0850..7c2404f 100644 --- a/timeside/tests/api/test_pipe.py +++ b/timeside/tests/api/test_pipe.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- + from timeside.tests.api.examples import Gain from timeside.core import * from timeside.decoder import *