From a4cbd7797061013930b41a723470a2d412e5b13c Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Sun, 7 Jul 2013 16:35:45 +0200 Subject: [PATCH] * fix some variable names fot overhaul coherence * fix yafee.results container --- tests/test_analyzer_dc.py | 16 ++++---- tests/test_analyzer_level.py | 22 +++++------ tests/test_yaafe.py | 20 +++++----- timeside/analyzer/aubio_melenergy.py | 16 ++++---- timeside/analyzer/aubio_mfcc.py | 16 ++++---- timeside/analyzer/aubio_pitch.py | 18 ++++----- timeside/analyzer/aubio_specdesc.py | 18 ++++----- timeside/analyzer/aubio_temporal.py | 20 +++++----- timeside/analyzer/core.py | 50 +++++++++++------------ timeside/analyzer/dc.py | 10 ++--- timeside/analyzer/level.py | 12 +++--- timeside/analyzer/yaafe.py | 59 ++++++++++++++-------------- 12 files changed, 138 insertions(+), 139 deletions(-) diff --git a/tests/test_analyzer_dc.py b/tests/test_analyzer_dc.py index 99bd450..e4cf7bf 100755 --- a/tests/test_analyzer_dc.py +++ b/tests/test_analyzer_dc.py @@ -17,10 +17,10 @@ class TestAnalyzerDC(TestCase): attributes=AnalyzerAttributes(name="Mean DC shift", unit="%", id="mean_dc_shift", - sampleRate=44100, - blockSize=None, - stepSize=None) - + samplerate=44100, + blocksize=None, + stepsize=None) + self.expected = AnalyzerResult(data=-0.000, attributes=attributes) def testOnGuitar(self): @@ -29,10 +29,10 @@ class TestAnalyzerDC(TestCase): attributes=AnalyzerAttributes(name="Mean DC shift", unit="%", id="mean_dc_shift", - sampleRate=44100, - blockSize=None, - stepSize=None) - self.expected = AnalyzerResult(data=0.054, attributes=attributes) + samplerate=44100, + blocksize=None, + stepsize=None) + self.expected = AnalyzerResult(data=0.054, attributes=attributes) def tearDown(self): decoder = FileDecoder(self.source) diff --git a/tests/test_analyzer_level.py b/tests/test_analyzer_level.py index 10248e1..7d9d8c1 100755 --- a/tests/test_analyzer_level.py +++ b/tests/test_analyzer_level.py @@ -14,39 +14,39 @@ class TestAnalyzerLevel(TestCase): def testOnSweep(self): "runs on sweep" self.source = os.path.join (os.path.dirname(__file__), "samples", "sweep.wav") - + # Max level attr = AnalyzerAttributes(id="max_level", name="Max level", unit = "dBFS", - sampleRate=44100) + samplerate=44100) max_level = AnalyzerResult(-6.021, attr) - + # RMS level attr = AnalyzerAttributes(id="rms_level", name="RMS level", unit="dBFS", - sampleRate=44100) - rms_level = AnalyzerResult(-9.856, attr) + samplerate=44100) + rms_level = AnalyzerResult(-9.856, attr) self.expected = AnalyzerResultContainer([max_level,rms_level]) - + def testOnGuitar(self): "runs on guitar" self.source = os.path.join (os.path.dirname(__file__), "samples", "guitar.wav") - + # Max level attr = AnalyzerAttributes(id="max_level", name="Max level", unit = "dBFS", - sampleRate=44100) + samplerate=44100) max_level = AnalyzerResult(-4.258, attr) - + # RMS level attr = AnalyzerAttributes(id="rms_level", name="RMS level", unit="dBFS", - sampleRate=44100) - rms_level = AnalyzerResult(-21.945, attr) + samplerate=44100) + rms_level = AnalyzerResult(-21.945, attr) self.expected = AnalyzerResultContainer([max_level,rms_level]) def tearDown(self): diff --git a/tests/test_yaafe.py b/tests/test_yaafe.py index 0f6ccf3..daaa0a3 100755 --- a/tests/test_yaafe.py +++ b/tests/test_yaafe.py @@ -9,9 +9,9 @@ class TestYaafe(TestCase): def setUp(self): self.sample_rate = 16000 - + def testOnSweepWithFeaturePlan(self): - "runs on sweep and define feature plan manualy" + "runs on sweep and define feature plan manually" self.source = os.path.join (os.path.dirname(__file__), "samples", "sweep.wav") # Setup Yaafe Analyzer @@ -21,8 +21,8 @@ class TestYaafe(TestCase): fp.addFeature('mfcc: MFCC blockSize=512 stepSize=256') fp.addFeature('mfcc_d1: MFCC blockSize=512 stepSize=256 > Derivate DOrder=1') fp.addFeature('mfcc_d2: MFCC blockSize=512 stepSize=256 > Derivate DOrder=2') - - # Setup a new Yaafe TimeSide analyzer + + # Setup a new Yaafe TimeSide analyzer # from FeaturePlan self.analyzer = Yaafe(fp) @@ -33,22 +33,22 @@ class TestYaafe(TestCase): # Load Yaafe Feature Plan fp = FeaturePlan(sample_rate=self.sample_rate) fp_file = os.path.join (os.path.dirname(__file__), "yaafe_config", "yaafeFeaturePlan") - + fp.loadFeaturePlan(fp_file) - # Setup a new Yaafe TimeSide analyzer + # Setup a new Yaafe TimeSide analyzer # from FeaturePlan self.analyzer = Yaafe(fp) - + def testOnGuitarWithDataFlow(self): "runs on guitar and load Yaafe dataflow from file" self.source = os.path.join (os.path.dirname(__file__), "samples", "guitar.wav") # Setup Yaafe Analyzer # Load DataFlow from file - df = DataFlow() + df = DataFlow() df_file = os.path.join (os.path.dirname(__file__), "yaafe_config", "yaafeDataFlow") df.load(df_file) - - # Setup a new Yaafe TimeSide analyzer + + # Setup a new Yaafe TimeSide analyzer # from DataFlow self.analyzer = Yaafe(df) diff --git a/timeside/analyzer/aubio_melenergy.py b/timeside/analyzer/aubio_melenergy.py index bec64df..b99aa25 100644 --- a/timeside/analyzer/aubio_melenergy.py +++ b/timeside/analyzer/aubio_melenergy.py @@ -63,21 +63,21 @@ class AubioMelEnergy(Processor): container = AnalyzerResultContainer() melenergy = AnalyzerResult() - + # Get attributes - sampleRate = self.samplerate() - blockSize = self.win_s - stepSize = self.hop_s + samplerate = self.samplerate() + blocksize = self.win_s + stepsize = self.hop_s parameters = dict(n_filters= self.n_filters, n_coeffs= self.n_coeffs) # Set attributes melenergy.attributes = AnalyzerAttributes(id="aubio_melenergy", name="melenergy (aubio)", unit='', - sampleRate = sampleRate, - blockSize = blockSize, - stepSize = stepSize, - parameters = parameters) + samplerate = samplerate, + blocksize = blocksize, + stepsize = stepsize, + parameters = parameters) # Set Data melenergy.data = self.melenergy_results container.add_result(melenergy) diff --git a/timeside/analyzer/aubio_mfcc.py b/timeside/analyzer/aubio_mfcc.py index 06fbbbe..510bf53 100644 --- a/timeside/analyzer/aubio_mfcc.py +++ b/timeside/analyzer/aubio_mfcc.py @@ -65,18 +65,18 @@ class AubioMfcc(Processor): def results(self): # MFCC mfcc = AnalyzerResult() - sampleRate = self.samplerate() - blockSize = self.win_s - stepSize = self.hop_s + samplerate = self.samplerate() + blocksize = self.win_s + stepsize = self.hop_s parameters = dict(n_filters= self.n_filters, n_coeffs= self.n_coeffs) - mfcc.attributes = AnalyzerAttributes(id = "aubio_mfcc", + mfcc.attributes = AnalyzerAttributes(id = "aubio_mfcc", name = "mfcc (aubio)", unit = "", - sampleRate = sampleRate, - blockSize = blockSize, - stepSize = stepSize, + samplerate = samplerate, + blocksize = blocksize, + stepsize = stepsize, parameters = parameters) mfcc.data = [list(line) for line in self.mfcc_results] # TODO : type ? list list ? - + return AnalyzerResultContainer(mfcc) diff --git a/timeside/analyzer/aubio_pitch.py b/timeside/analyzer/aubio_pitch.py index 8a290ae..2f5697f 100644 --- a/timeside/analyzer/aubio_pitch.py +++ b/timeside/analyzer/aubio_pitch.py @@ -66,23 +66,23 @@ class AubioPitch(Processor): container = AnalyzerResultContainer() pitch = AnalyzerResult() - + # Get attributes - sampleRate = self.samplerate() - blockSize = self.win_s - stepSize = self.hop_s + samplerate = self.samplerate() + blocksize = self.win_s + stepsize = self.hop_s # parameters : None # TODO check with Piem "default" and "freq" in setup - + # Set attributes pitch.attributes = AnalyzerAttributes(id="aubio_pitch", name="f0 (aubio)", unit='Hz', - sampleRate = sampleRate, - blockSize = blockSize, - stepSize = stepSize) + samplerate = samplerate, + blocksize = blocksize, + stepsize = stepsize) # Set Data self.pitches = numpy.array(self.pitches) pitch.data = self.pitches container.add_result(pitch) - + return container diff --git a/timeside/analyzer/aubio_specdesc.py b/timeside/analyzer/aubio_specdesc.py index e23f4af..fadf75e 100644 --- a/timeside/analyzer/aubio_specdesc.py +++ b/timeside/analyzer/aubio_specdesc.py @@ -66,9 +66,9 @@ class AubioSpecdesc(Processor): container = AnalyzerResultContainer() # Get common attributes - sampleRate = self.samplerate() - blockSize = self.win_s - stepSize = self.hop_s + samplerate = self.samplerate() + blocksize = self.win_s + stepsize = self.hop_s unit = "" # For each method store results in container for method in self.methods: @@ -76,16 +76,16 @@ class AubioSpecdesc(Processor): # Set attributes id = '_'.join(["aubio_specdesc", method]) name = ' '.join(["spectral descriptor", method, "(aubio)"]) - + specdesc.attributes = AnalyzerAttributes(id = id, name = name, unit = unit, - sampleRate = sampleRate, - blockSize = blockSize, - stepSize = stepSize) - - # Set Data + samplerate = samplerate, + blocksize = blocksize, + stepsize = stepsize) + + # Set Data specdesc.data = numpy.array(self.specdesc_results[method]) container.add_result(specdesc) diff --git a/timeside/analyzer/aubio_temporal.py b/timeside/analyzer/aubio_temporal.py index eb5b251..deff4ce 100644 --- a/timeside/analyzer/aubio_temporal.py +++ b/timeside/analyzer/aubio_temporal.py @@ -68,11 +68,11 @@ class AubioTemporal(Processor): def results(self): # Get common attributes - commonAttr = dict(sampleRate=self.samplerate(), - blockSize=self.win_s, - stepSize=self.hop_s) + commonAttr = dict(samplerate=self.samplerate(), + blocksize=self.win_s, + stepsize=self.hop_s) # FIXME : Onsets, beat and onset rate are not frame based Results - # sampleRate, blockSize, etc. are not appropriate here + # samplerate, blocksize, etc. are not appropriate here # Those might be some kind of "AnalyzerSegmentResults" #--------------------------------- @@ -90,19 +90,19 @@ class AubioTemporal(Processor): #--------------------------------- # Onset Rate #--------------------------------- - onsetRate = AnalyzerResult() + onsetrate = AnalyzerResult() # Set attributes - onsetRateAttr = dict(id="aubio_onset_rate", + onsetrateAttr = dict(id="aubio_onset_rate", name="onset rate (aubio)", unit="bpm") - onsetRate.attributes = dict(onsetRateAttr.items() + commonAttr.items()) + onsetrate.attributes = dict(onsetrateAttr.items() + commonAttr.items()) # Set Data if len(self.onsets) > 1: #periods = [60./(b - a) for a,b in zip(self.onsets[:-1],self.onsets[1:])] periods = 60. / numpy.diff(self.onsets) - onsetRate.data = periods + onsetrate.data = periods else: - onsetRate.data = [] + onsetrate.data = [] #--------------------------------- # Beats @@ -133,4 +133,4 @@ class AubioTemporal(Processor): else: bpm.data = [] - return AnalyzerResultContainer([onsets, onsetRate, beats, bpm]) \ No newline at end of file + return AnalyzerResultContainer([onsets, onsetrate, beats, bpm]) \ No newline at end of file diff --git a/timeside/analyzer/core.py b/timeside/analyzer/core.py index ae28251..0569c63 100644 --- a/timeside/analyzer/core.py +++ b/timeside/analyzer/core.py @@ -57,14 +57,14 @@ class AnalyzerAttributes(object): id : string name : string unit : string - sampleRate : int or float - blockSize : int - stepSize : int + samplerate : int or float + blocksize : int + stepsize : int parameters : dict Methods ------- - asdict() + as_dict() Return a dictionnary representation of the AnalyzerAttributes """ from collections import OrderedDict @@ -73,9 +73,9 @@ class AnalyzerAttributes(object): _default_value = OrderedDict([('id', ''), ('name', ''), ('unit', ''), - ('sampleRate', None), - ('blockSize', None), - ('stepSize', None), + ('samplerate', None), + ('blocksize', None), + ('stepsize', None), ('parameters', {}) ]) # TODO : rajouter @@ -95,9 +95,9 @@ class AnalyzerAttributes(object): id : string name : string unit : string - sampleRate : int or float - blockSize : int - stepSize : int + samplerate : int or float + blocksize : int + stepsize : int parameters : dict Returns @@ -121,7 +121,7 @@ class AnalyzerAttributes(object): (name, self.__class__.__name__)) super(AnalyzerAttributes, self).__setattr__(name, value) - def asdict(self): + def as_dict(self): return dict((att, getattr(self, att)) for att in self._default_value.keys()) @@ -131,10 +131,10 @@ class AnalyzerAttributes(object): ', '.join('{}={}'.format( att, repr(getattr(self, att))) for att in self._default_value.keys())) - + def __eq__(self,other): return (isinstance(other, self.__class__) - and self.asdict() == other.asdict()) + and self.as_dict() == other.as_dict()) class AnalyzerResult(object): @@ -195,19 +195,19 @@ class AnalyzerResult(object): # return self[name] # return super(AnalyzerResult, self).__getattr__(name) - def asdict(self): - return(dict(data=self.data, attributes=self.attributes.asdict())) + def as_dict(self): + return(dict(data=self.data, attributes=self.attributes.as_dict())) def to_json(self): import simplejson as json - return json.dumps(self.asdict()) + return json.dumps(self.as_dict()) def __repr__(self): return self.to_json() - + def __eq__(self,other): return (isinstance(other, self.__class__) - and self.asdict() == other.asdict()) + and self.as_dict() == other.as_dict()) def __ne__(self, other): return not self.__eq__(other) @@ -226,7 +226,7 @@ class AnalyzerResultContainer(object): return len(self.results) def __repr__(self): - return [res.asdict() for res in self.results] + return [res.as_dict() for res in self.results] def __eq__(self, other): if hasattr(other, 'results'): @@ -235,9 +235,9 @@ class AnalyzerResultContainer(object): if a != b: return False return True - + def __ne__(self, other): - return not self.__eq__(other) + return not self.__eq__(other) def add_result(self, analyzer_result): if type(analyzer_result) == list: @@ -267,7 +267,7 @@ class AnalyzerResultContainer(object): data_node.text = repr(result.data) # Serialize Attributes attr_node = ET.SubElement(res_node, 'attributes') - for (name, val) in result.attributes.asdict().items(): + for (name, val) in result.attributes.as_dict().items(): # TODO reorder keys child = ET.SubElement(attr_node, name) if name == 'parameters': @@ -316,7 +316,7 @@ class AnalyzerResultContainer(object): result.data = ast.literal_eval(result_child.find('data').text) except: result.data = result_child.find('data').text - + # Get attributes for attr_child in result_child.find('attributes'): name = attr_child.tag @@ -358,7 +358,7 @@ class AnalyzerResultContainer(object): def to_json(self): #if data_list == None: data_list = self.results import simplejson as json - return json.dumps([res.asdict() for res in self]) + return json.dumps([res.as_dict() for res in self]) def from_json(self, json_str): import simplejson as json @@ -373,7 +373,7 @@ class AnalyzerResultContainer(object): def to_yaml(self): #if data_list == None: data_list = self.results import yaml - return yaml.dump([res.asdict() for res in self]) + return yaml.dump([res.as_dict() for res in self]) def from_yaml(self, yaml_str): import yaml diff --git a/timeside/analyzer/dc.py b/timeside/analyzer/dc.py index f22c395..dff87de 100644 --- a/timeside/analyzer/dc.py +++ b/timeside/analyzer/dc.py @@ -50,14 +50,14 @@ class MeanDCShift(Processor): def results(self): result = AnalyzerResult() # Set attributes - # FIXME : blockSize and stepSize are not appropriate here + # FIXME : blocksize and stepsize are not appropriate here result.attributes = AnalyzerAttributes(id="mean_dc_shift", name = "Mean DC shift", unit = "%", - sampleRate=self.samplerate(), - blockSize=None, - stepSize=None) - + samplerate=self.samplerate(), + blocksize=None, + stepsize=None) + # Set Data result.data = numpy.round(numpy.mean(100*self.values),3) return AnalyzerResultContainer(result) diff --git a/timeside/analyzer/level.py b/timeside/analyzer/level.py index bf061f4..eef9085 100644 --- a/timeside/analyzer/level.py +++ b/timeside/analyzer/level.py @@ -59,21 +59,21 @@ class Level(Processor): def results(self): # Max level - # FIXME : blockSize and stepSize are not appropriate here + # FIXME : blocksize and stepsize are not appropriate here attr = AnalyzerAttributes(id="max_level", name="Max level", unit = "dBFS", - sampleRate=self.samplerate()) + samplerate=self.samplerate()) data = numpy.round(20*numpy.log10(self.max_value), 3) max_level = AnalyzerResult(data, attr) - + # RMS level - # FIXME : blockSize and stepSize are not appropriate here + # FIXME : blocksize and stepsize are not appropriate here attr = AnalyzerAttributes(id="rms_level", name="RMS level", unit="dBFS", - sampleRate=self.samplerate()) + samplerate=self.samplerate()) data = numpy.round(20*numpy.log10(numpy.sqrt(numpy.mean(self.mean_values))), 3) rms_level = AnalyzerResult(data, attr) - + return AnalyzerResultContainer([max_level, rms_level]) diff --git a/timeside/analyzer/yaafe.py b/timeside/analyzer/yaafe.py index 4887920..4914cd4 100644 --- a/timeside/analyzer/yaafe.py +++ b/timeside/analyzer/yaafe.py @@ -19,7 +19,7 @@ # Author : Thomas Fillon """ -Module Yaafe Analyzer +Module Yaafe Analyzer Created on Thu Jun 13 16:05:02 2013 @author: Thomas Fillon @@ -27,13 +27,13 @@ Created on Thu Jun 13 16:05:02 2013 from timeside.core import Processor, implements, interfacedoc, FixedSizeInputAdapter from timeside.analyzer.core import * from timeside.api import IValueAnalyzer -# from yaafelib import * -# import numpy + class Yaafe(Processor): implements(IValueAnalyzer) + def __init__(self, yaafeSpecification): # Check arguments if isinstance(yaafeSpecification,DataFlow): @@ -43,7 +43,7 @@ class Yaafe(Processor): self.dataFlow = self.featurePlan.getDataFlow() else: raise TypeError("'%s' Type must be either '%s' or '%s'" % (str(yaafeSpecification),str(DataFlow),str(FeaturePlan))) - + @interfacedoc def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): super(Yaafe, self).setup(channels, samplerate, blocksize, totalframes) @@ -51,6 +51,8 @@ class Yaafe(Processor): self.yaafe_engine = Engine() self.yaafe_engine.load(self.dataFlow) self.yaafe_engine.reset() + self.samplerate = samplerate + self.blocksize = blocksize @staticmethod @interfacedoc @@ -64,47 +66,44 @@ class Yaafe(Processor): def process(self, frames, eod=False): # do process things... - # Downmixing to mono and convert to float64 for compatibility with Yaafe + # Downmixing to mono and convert to float64 for compatibility with Yaafe yaafe_frames = frames.sum(axis=-1,dtype=numpy.float64) / frames.shape[-1] - # Reshape for compatibility with Yaafe input format - yaafe_frames.shape = (1,yaafe_frames.shape[0]) + # Reshape for compatibility with Yaafe input format + yaafe_frames.shape = (1,yaafe_frames.shape[0]) # write audio array on 'audio' input - self.yaafe_engine.writeInput('audio',yaafe_frames) - # process available data - self.yaafe_engine.process() + self.yaafe_engine.writeInput('audio',yaafe_frames) + # process available data + self.yaafe_engine.process() if eod: # flush yaafe engine to process remaining data - self.yaafe_engine.flush() - + self.yaafe_engine.flush() + return frames, eod def results(self): # Get back current container container = AnalyzerResultContainer() # Get feature extraction results from yaafe - map_keys = {'sampleRate': 'sampleRate', - 'frameLength': 'blockSize', - 'sampleStep': 'stepSize', - 'parameters': 'parameters', - } featNames = self.yaafe_engine.getOutputs().keys() for featName in featNames: - # Map Yaafe attributes into AnalyzerResults dict - res_dict = {map_keys[name]: self.yaafe_engine.getOutputs()['mfcc'][name] for name in map_keys.keys()} - # Define ID fields - res_dict['id'] = 'yaafe_' + featName - res_dict['name'] = 'Yaafe ' + featName - res_dict['unit'] = '' - # create AnalyzerResult and set its attributes - result = AnalyzerResult(attributes=res_dict) + # Define ID fields + id = 'yaafe_' + featName + name = 'Yaafe ' + featName + unit = '' + # Get results from Yaafe engine - result.data = self.yaafe_engine.readOutput(featName) # Read Yaafe Results + result = AnalyzerResult() + result.attributes = AnalyzerAttributes(id = id, + name = name, + unit = unit, + samplerate = self.samplerate, + blocksize = self.blocksize, + stepsize = None) + + result.data = self.yaafe_engine.readOutput(featName) # Read Yaafe Results # Store results in Container if len(result.data): container.add_result(result) - - return container - - + return container -- 2.39.5