From: Paul Brossier Date: Mon, 15 Apr 2013 22:35:10 +0000 (-0500) Subject: timeside/analyzer/: factorise max_level and rms_level X-Git-Tag: 0.5.0~115^2~8 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=4eeded14f1c36972023a35428444f84ba7ffa913;p=timeside.git timeside/analyzer/: factorise max_level and rms_level --- diff --git a/tests/test_analyzer_level.py b/tests/test_analyzer_level.py new file mode 100755 index 0000000..d1e4553 --- /dev/null +++ b/tests/test_analyzer_level.py @@ -0,0 +1,30 @@ +#! /usr/bin/env python + +from unit_timeside import * +from timeside.decoder import * +from timeside.analyzer.level import Level + +class TestAnalyzerLevel(TestCase): + + def setUp(self): + self.analyzer = Level() + + def testOnSweep(self): + "runs on sweep" + self.source = os.path.join (os.path.dirname(__file__), "samples", "sweep.wav") + + def testOnGuitar(self): + "runs on guitar" + self.source = os.path.join (os.path.dirname(__file__), "samples", "guitar.wav") + + def tearDown(self): + decoder = FileDecoder(self.source) + (decoder | self.analyzer).run() + results = self.analyzer.results() + print results + #print results.to_yaml() + #print results.to_json() + #print results.to_xml() + +if __name__ == '__main__': + unittest.main(testRunner=TestRunner()) diff --git a/tests/test_max_level.py b/tests/test_max_level.py deleted file mode 100755 index d55f6f7..0000000 --- a/tests/test_max_level.py +++ /dev/null @@ -1,30 +0,0 @@ -#! /usr/bin/env python - -from unit_timeside import * -from timeside.decoder import * -from timeside.analyzer.max_level import MaxLevel - -class TestAubioMelEnergy(TestCase): - - def setUp(self): - self.analyzer = MaxLevel() - - def testOnSweep(self): - "runs on sweep" - self.source = os.path.join (os.path.dirname(__file__), "samples", "sweep.wav") - - def testOnGuitar(self): - "runs on guitar" - self.source = os.path.join (os.path.dirname(__file__), "samples", "guitar.wav") - - def tearDown(self): - decoder = FileDecoder(self.source) - (decoder | self.analyzer).run() - results = self.analyzer.results() - print results - #print results.to_yaml() - #print results.to_json() - #print results.to_xml() - -if __name__ == '__main__': - unittest.main(testRunner=TestRunner()) diff --git a/timeside/analyzer/__init__.py b/timeside/analyzer/__init__.py index 8a50f36..95001c3 100644 --- a/timeside/analyzer/__init__.py +++ b/timeside/analyzer/__init__.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- -from max_level import * -from mean_level import * +from level import * from dc import * from aubio_temporal import * from aubio_pitch import * diff --git a/timeside/analyzer/level.py b/timeside/analyzer/level.py new file mode 100644 index 0000000..ed585ce --- /dev/null +++ b/timeside/analyzer/level.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2007-2009 Guillaume Pellerin +# Copyright (c) 2009 Olivier Guilyardi + +# This file is part of TimeSide. + +# TimeSide is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. + +# TimeSide is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with TimeSide. If not, see . + +# Author: Guillaume Pellerin + +from timeside.core import Processor, implements, interfacedoc, FixedSizeInputAdapter +from timeside.analyzer.core import * +from timeside.api import IValueAnalyzer +import numpy + + +class Level(Processor): + implements(IValueAnalyzer) + + @interfacedoc + def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): + super(Level, self).setup(channels, samplerate, blocksize, totalframes) + # max_level + self.max_value = 0 + # rms_level + self.mean_values = numpy.array([]) + + @staticmethod + @interfacedoc + def id(): + return "level_analyzer" + + @staticmethod + @interfacedoc + def name(): + return "level analyzer" + + def process(self, frames, eod=False): + if frames.size: + # max_level + max_value = frames.max() + if max_value > self.max_value: + self.max_value = max_value + # rms_level + self.mean_values = numpy.append(self.mean_values, numpy.mean(numpy.square(frames))) + return frames, eod + + def results(self): + max_level = AnalyzerResult(id = "max_level", name = "Max level", unit = "dBFS") + max_level.value = numpy.round(20*numpy.log10(self.max_value), 3) + rms_level = AnalyzerResult(id = "rms_level", name = "RMS level", unit = "dBFS") + rms_level.value = numpy.round(20*numpy.log10(numpy.sqrt(numpy.mean(self.mean_values))), 3) + return AnalyzerResultContainer([max_level, rms_level]) diff --git a/timeside/analyzer/max_level.py b/timeside/analyzer/max_level.py deleted file mode 100644 index d280326..0000000 --- a/timeside/analyzer/max_level.py +++ /dev/null @@ -1,57 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (c) 2007-2009 Guillaume Pellerin -# Copyright (c) 2009 Olivier Guilyardi - -# This file is part of TimeSide. - -# TimeSide is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 2 of the License, or -# (at your option) any later version. - -# TimeSide is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with TimeSide. If not, see . - -# Author: Guillaume Pellerin - -from timeside.core import Processor, implements, interfacedoc, FixedSizeInputAdapter -from timeside.analyzer.core import * -from timeside.api import IValueAnalyzer -import numpy - - -class MaxLevel(Processor): - implements(IValueAnalyzer) - - @interfacedoc - def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): - super(MaxLevel, self).setup(channels, samplerate, blocksize, totalframes) - self.value = 0 - - @staticmethod - @interfacedoc - def id(): - return "maxlevel_analyzer" - - @staticmethod - @interfacedoc - def name(): - return "Max level analyzer" - - def process(self, frames, eod=False): - if frames.size: - max = frames.max() - if max > self.value: - self.value = max - return frames, eod - - def results(self): - result = AnalyzerResult(id = "maxlevel", name = "Max level", unit = "dBFS") - result.value = numpy.round(20*numpy.log10(self.value), 3) - return AnalyzerResultContainer([result]) diff --git a/timeside/analyzer/mean_level.py b/timeside/analyzer/mean_level.py deleted file mode 100644 index 33bae26..0000000 --- a/timeside/analyzer/mean_level.py +++ /dev/null @@ -1,60 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (c) 2007-2009 Guillaume Pellerin - -# This file is part of TimeSide. - -# TimeSide is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 2 of the License, or -# (at your option) any later version. - -# TimeSide is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with TimeSide. If not, see . - -# Author: Guillaume Pellerin - -from timeside.core import Processor, implements, interfacedoc, FixedSizeInputAdapter -from timeside.analyzer.core import * -from timeside.api import IValueAnalyzer -import numpy - - -class MeanLevel(Processor): - implements(IValueAnalyzer) - - @interfacedoc - def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): - super(MeanLevel, self).setup(channels, samplerate, blocksize, totalframes) - self.values = numpy.array([]) - - @staticmethod - @interfacedoc - def id(): - return "rmslevel" - - @staticmethod - @interfacedoc - def name(): - return "RMS level" - - @staticmethod - @interfacedoc - def unit(): - return "dBFS" - - def __str__(self): - return "%s %s" % (str(self.value), unit()) - - def process(self, frames, eod=False): - if frames.size: - self.values = numpy.append(self.values, numpy.mean(numpy.square(frames))) - return frames, eod - - def result(self): - return numpy.round(20*numpy.log10(numpy.sqrt(numpy.mean(self.values))), 3)