From: Paul Brossier Date: Mon, 15 Apr 2013 01:59:58 +0000 (-0500) Subject: add aubio mfcc and melenergy X-Git-Tag: 0.5.0~115^2~30 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=933f5fd7ba43360c6ade0d47e90631972968c557;p=timeside.git add aubio mfcc and melenergy --- diff --git a/tests/test_aubio_melenergy.py b/tests/test_aubio_melenergy.py new file mode 100755 index 0000000..b53eb64 --- /dev/null +++ b/tests/test_aubio_melenergy.py @@ -0,0 +1,30 @@ +#! /usr/bin/env python + +from unit_timeside import * +from timeside.decoder import * +from timeside.analyzer.aubio_melenergy import AubioMelEnergy + +class TestAubioMelEnergy(TestCase): + + def setUp(self): + self.analyzer = AubioMelEnergy() + + 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_aubio_mfcc.py b/tests/test_aubio_mfcc.py new file mode 100755 index 0000000..abdaad6 --- /dev/null +++ b/tests/test_aubio_mfcc.py @@ -0,0 +1,30 @@ +#! /usr/bin/env python + +from unit_timeside import * +from timeside.decoder import * +from timeside.analyzer.aubio_mfcc import AubioMfcc + +class TestAubioMfcc(TestCase): + + def setUp(self): + self.analyzer = AubioMfcc() + + 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 99330d0..8a50f36 100644 --- a/timeside/analyzer/__init__.py +++ b/timeside/analyzer/__init__.py @@ -5,3 +5,5 @@ from mean_level import * from dc import * from aubio_temporal import * from aubio_pitch import * +from aubio_mfcc import * +from aubio_melenergy import * diff --git a/timeside/analyzer/aubio_melenergy.py b/timeside/analyzer/aubio_melenergy.py new file mode 100644 index 0000000..17fa893 --- /dev/null +++ b/timeside/analyzer/aubio_melenergy.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2013 Paul Brossier + +# 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: Paul Brossier + +from timeside.core import Processor, implements, interfacedoc, FixedSizeInputAdapter +from timeside.analyzer.core import * +from timeside.api import IValueAnalyzer + +import numpy +from aubio import filterbank, pvoc + +from math import isnan + +class AubioMelEnergy(Processor): + implements(IValueAnalyzer) + + @interfacedoc + def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): + super(AubioMelEnergy, self).setup(channels, samplerate, blocksize, totalframes) + self.win_s = 1024 + self.hop_s = self.win_s / 4 + self.n_filters = 40 + self.n_coeffs = 13 + self.pvoc = pvoc(self.win_s, self.hop_s) + self.melenergy = filterbank(self.n_filters, self.win_s) + self.melenergy.set_mel_coeffs_slaney(samplerate) + self.block_read = 0 + self.melenergy_results = numpy.zeros([self.n_filters, ]) + + @staticmethod + @interfacedoc + def id(): + return "aubio_mel_analyzer" + + @staticmethod + @interfacedoc + def name(): + return "Mel Energy analysis (aubio)" + + def process(self, frames, eod=False): + i = 0 + while True: + downmixed_samples = frames[i:i+self.hop_s, :].sum(axis = -1) + time = self.block_read * self.hop_s * 1. / self.samplerate() + fftgrain = self.pvoc(downmixed_samples) + self.melenergy_results = numpy.vstack( [ self.melenergy_results, self.melenergy(fftgrain) ]) + i += self.hop_s + self.block_read += 1 + if self.hop_s + i < frames.shape[0]: break + return frames, eod + + def results(self): + + melenergy = AnalyzerResult(id = "aubio_melenergy", name = "melenergy (aubio)", unit = "") + melenergy.value = [list(line) for line in self.melenergy_results] + + melenergy_mean = AnalyzerResult(id = "aubio_melenergy_mean", name = "melenergy mean (aubio)", unit = "") + melenergy_mean.value = list(self.melenergy_results.mean(axis=0)) + + melenergy_median = AnalyzerResult(id = "aubio_melenergy_median", name = "melenergy median (aubio)", unit = "") + melenergy_median.value = list(numpy.median(self.melenergy_results,axis=0)) + + return AnalyzerResultContainer([melenergy, melenergy_median, melenergy_mean]) + diff --git a/timeside/analyzer/aubio_mfcc.py b/timeside/analyzer/aubio_mfcc.py new file mode 100644 index 0000000..4dd77b4 --- /dev/null +++ b/timeside/analyzer/aubio_mfcc.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2013 Paul Brossier + +# 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: Paul Brossier + +from timeside.core import Processor, implements, interfacedoc, FixedSizeInputAdapter +from timeside.analyzer.core import * +from timeside.api import IValueAnalyzer + +import numpy +from aubio import mfcc, pvoc + +from math import isnan + +class AubioMfcc(Processor): + implements(IValueAnalyzer) + + @interfacedoc + def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): + super(AubioMfcc, self).setup(channels, samplerate, blocksize, totalframes) + self.win_s = 1024 + self.hop_s = self.win_s / 4 + self.n_filters = 40 + self.n_coeffs = 13 + self.pvoc = pvoc(self.win_s, self.hop_s) + self.mfcc = mfcc(self.win_s, self.n_filters, self.n_coeffs, samplerate) + self.block_read = 0 + self.mfcc_results = numpy.zeros([self.n_coeffs, ]) + + @staticmethod + @interfacedoc + def id(): + return "aubio_mfcc_analyzer" + + @staticmethod + @interfacedoc + def name(): + return "MFCC analysis (aubio)" + + def process(self, frames, eod=False): + i = 0 + while True: + downmixed_samples = frames[i:i+self.hop_s, :].sum(axis = -1) + time = self.block_read * self.hop_s * 1. / self.samplerate() + fftgrain = self.pvoc(downmixed_samples) + coeffs = self.mfcc(fftgrain) + self.mfcc_results = numpy.vstack((self.mfcc_results, coeffs)) + i += self.hop_s + self.block_read += 1 + if self.hop_s + i < frames.shape[0]: break + return frames, eod + + def results(self): + + mfcc = AnalyzerResult(id = "aubio_mfcc", name = "mfcc (aubio)", unit = "") + mfcc.value = [list(line) for line in self.mfcc_results] + + mfcc_mean = AnalyzerResult(id = "aubio_mfcc_mean", name = "mfcc mean (aubio)", unit = "") + mfcc_mean.value = list(self.mfcc_results.mean(axis=0)) + + mfcc_median = AnalyzerResult(id = "aubio_mfcc_median", name = "mfcc median (aubio)", unit = "") + mfcc_median.value = list(numpy.median(self.mfcc_results,axis=0)) + + return AnalyzerResultContainer([mfcc, mfcc_median, mfcc_mean])