]> git.parisson.com Git - timeside.git/commitdiff
add aubio mfcc and melenergy
authorPaul Brossier <piem@piem.org>
Mon, 15 Apr 2013 01:59:58 +0000 (20:59 -0500)
committerPaul Brossier <piem@piem.org>
Mon, 15 Apr 2013 01:59:58 +0000 (20:59 -0500)
tests/test_aubio_melenergy.py [new file with mode: 0755]
tests/test_aubio_mfcc.py [new file with mode: 0755]
timeside/analyzer/__init__.py
timeside/analyzer/aubio_melenergy.py [new file with mode: 0644]
timeside/analyzer/aubio_mfcc.py [new file with mode: 0644]

diff --git a/tests/test_aubio_melenergy.py b/tests/test_aubio_melenergy.py
new file mode 100755 (executable)
index 0000000..b53eb64
--- /dev/null
@@ -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 (executable)
index 0000000..abdaad6
--- /dev/null
@@ -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())
index 99330d0750dee5dbb4a213a3f423b17a3c023100..8a50f369d6c9252faa1832a8a113c7743871a50b 100644 (file)
@@ -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 (file)
index 0000000..17fa893
--- /dev/null
@@ -0,0 +1,81 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2013 Paul Brossier <piem@piem.org>
+
+# 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 <http://www.gnu.org/licenses/>.
+
+# Author: Paul Brossier <piem@piem.org>
+
+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 (file)
index 0000000..4dd77b4
--- /dev/null
@@ -0,0 +1,80 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2013 Paul Brossier <piem@piem.org>
+
+# 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 <http://www.gnu.org/licenses/>.
+
+# Author: Paul Brossier <piem@piem.org>
+
+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])