From: Paul Brossier Date: Thu, 27 Sep 2012 21:28:35 +0000 (-0600) Subject: added two simple analyzer examples X-Git-Tag: 0.4.3~4^2~4 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=8969b39948a85313580289e95ee792267d637127;p=timeside.git added two simple analyzer examples --- diff --git a/timeside/analyzer/__init__.py b/timeside/analyzer/__init__.py index 92d3bf9..504d50f 100644 --- a/timeside/analyzer/__init__.py +++ b/timeside/analyzer/__init__.py @@ -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 index 0000000..ad5c817 --- /dev/null +++ b/timeside/analyzer/aubio_bpm.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2012 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 +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 index 0000000..35df60a --- /dev/null +++ b/timeside/analyzer/aubio_onsetrate.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2012 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 +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)