Package timeside :: Package analyzer :: Module spectrogram
[hide private]
[frames] | no frames]

Source Code for Module timeside.analyzer.spectrogram

 1  # -*- coding: utf-8 -*- 
 2  # 
 3  # Copyright (c) 2013 Paul Brossier <piem@piem.org> 
 4   
 5  # This file is part of TimeSide. 
 6   
 7  # TimeSide is free software: you can redistribute it and/or modify 
 8  # it under the terms of the GNU General Public License as published by 
 9  # the Free Software Foundation, either version 2 of the License, or 
10  # (at your option) any later version. 
11   
12  # TimeSide is distributed in the hope that it will be useful, 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
15  # GNU General Public License for more details. 
16   
17  # You should have received a copy of the GNU General Public License 
18  # along with TimeSide.  If not, see <http://www.gnu.org/licenses/>. 
19   
20  # Author: Paul Brossier <piem@piem.org> 
21   
22  from timeside.core import implements, interfacedoc 
23  from timeside.analyzer.core import Analyzer 
24  from timeside.api import IAnalyzer 
25  from preprocessors import downmix_to_mono, frames_adapter 
26  import numpy as np 
27 28 29 -class Spectrogram(Analyzer):
30 implements(IAnalyzer) 31
32 - def __init__(self, blocksize=2048, stepsize=None):
33 super(Spectrogram, self).__init__() 34 35 self.input_blocksize = blocksize 36 if stepsize: 37 self.input_stepsize = stepsize 38 else: 39 self.input_stepsize = blocksize / 2
40 41 @interfacedoc
42 - def setup(self, channels=None, samplerate=None, 43 blocksize=None, totalframes=None):
44 super(Spectrogram, self).setup(channels, samplerate, 45 blocksize, totalframes) 46 47 self.values = [] 48 self.FFT_SIZE = 2048
49 50 @staticmethod 51 @interfacedoc
52 - def id():
53 return "spectrogram_analyzer"
54 55 @staticmethod 56 @interfacedoc
57 - def name():
58 return "Spectrogram Analyzer"
59 60 @staticmethod 61 @interfacedoc
62 - def unit():
63 return ""
64 65 @downmix_to_mono 66 @frames_adapter
67 - def process(self, frames, eod=False):
68 self.values.append(np.abs(np.fft.rfft(frames, self.FFT_SIZE))) 69 return frames, eod
70
71 - def post_process(self):
72 spectrogram = self.new_result(data_mode='value', time_mode='framewise') 73 spectrogram.parameters = {'FFT_SIZE': self.FFT_SIZE} 74 spectrogram.data_object.value = self.values 75 self.process_pipe.results.add(spectrogram)
76