]> git.parisson.com Git - timeside.git/commitdiff
added two simple analyzer examples
authorPaul Brossier <piem@piem.org>
Thu, 27 Sep 2012 21:28:35 +0000 (15:28 -0600)
committerPaul Brossier <piem@piem.org>
Thu, 27 Sep 2012 21:28:35 +0000 (15:28 -0600)
timeside/analyzer/__init__.py
timeside/analyzer/aubio_bpm.py [new file with mode: 0644]
timeside/analyzer/aubio_onsetrate.py [new file with mode: 0644]

index 92d3bf96a2161c4fe42aeb07bb76cbf33db409e7..504d50f7780a62bfc0881f35129489ebb746e378 100644 (file)
@@ -6,4 +6,6 @@ from core import *
 from max_level import *
 from mean_level import *
 from dc import *
+from aubio_bpm import *
+from aubio_onsetrate import *
 
diff --git a/timeside/analyzer/aubio_bpm.py b/timeside/analyzer/aubio_bpm.py
new file mode 100644 (file)
index 0000000..ad5c817
--- /dev/null
@@ -0,0 +1,69 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2012 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
+from aubio import tempo
+
+class AubioBPM(Processor):
+    implements(IValueAnalyzer)
+
+    @interfacedoc
+    def setup(self, channels=None, samplerate=None, nframes=None):
+        super(AubioBPM, self).setup(channels, samplerate, nframes)
+        self.win_s = 512
+        self.hop_s = self.win_s / 2
+        self.t = tempo("default", self.win_s, self.hop_s, 1)
+        self.block_read = 0
+        self.beats = []
+
+    @staticmethod
+    @interfacedoc
+    def id():
+        return "aubio_bpm"
+
+    @staticmethod
+    @interfacedoc
+    def name():
+        return "aubio bpm"
+
+    @staticmethod
+    @interfacedoc
+    def unit():
+        return "bpm"
+
+    def __str__(self):
+        return "%s %s" % (str(self.value), unit())
+
+    def process(self, frames, eod=False):
+        i = 0
+        while not eod and i < frames.shape[0]:
+          isbeat = self.t(frames[0,i:i+self.hop_s])
+          if isbeat: self.beats += [(isbeat[0] + self.block_read * self.hop_s ) / 44100. ]
+          i += self.hop_s
+          self.block_read += 1
+        return frames, eod
+
+    def result(self):
+        periods = [60./(b - a) for a,b in zip(self.beats[:-1],self.beats[1:])]
+        from numpy import median
+        return median (periods)
diff --git a/timeside/analyzer/aubio_onsetrate.py b/timeside/analyzer/aubio_onsetrate.py
new file mode 100644 (file)
index 0000000..35df60a
--- /dev/null
@@ -0,0 +1,69 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2012 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
+from aubio import onset
+
+class AubioBPM(Processor):
+    implements(IValueAnalyzer)
+
+    @interfacedoc
+    def setup(self, channels=None, samplerate=None, nframes=None):
+        super(AubioBPM, self).setup(channels, samplerate, nframes)
+        self.win_s = 512
+        self.hop_s = self.win_s / 2
+        self.t = onset("default", self.win_s, self.hop_s, 1)
+        self.block_read = 0
+        self.onsets = []
+
+    @staticmethod
+    @interfacedoc
+    def id():
+        return "aubio_onsetrate"
+
+    @staticmethod
+    @interfacedoc
+    def name():
+        return "aubio onset rate"
+
+    @staticmethod
+    @interfacedoc
+    def unit():
+        return "bpm"
+
+    def __str__(self):
+        return "%s %s" % (str(self.value), unit())
+
+    def process(self, frames, eod=False):
+        i = 0
+        while not eod and i < frames.shape[0]:
+          isonset = self.t(frames[0,i:i+self.hop_s])
+          if isonset: self.onsets += [(isonset[0] + self.block_read * self.hop_s ) / 44100. ]
+          i += self.hop_s
+          self.block_read += 1
+        return frames, eod
+
+    def result(self):
+        periods = [60./(b - a) for a,b in zip(self.onsets[:-1],self.onsets[1:])]
+        from numpy import mean
+        return mean(periods)