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

Source Code for Module timeside.analyzer.level

 1  # -*- coding: utf-8 -*- 
 2  # 
 3  # Copyright (c) 2007-2009 Guillaume Pellerin <yomguy@parisson.com> 
 4  # Copyright (c) 2009 Olivier Guilyardi <olivier@samalyse.com> 
 5   
 6  # This file is part of TimeSide. 
 7   
 8  # TimeSide is free software: you can redistribute it and/or modify 
 9  # it under the terms of the GNU General Public License as published by 
10  # the Free Software Foundation, either version 2 of the License, or 
11  # (at your option) any later version. 
12   
13  # TimeSide is distributed in the hope that it will be useful, 
14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
16  # GNU General Public License for more details. 
17   
18  # You should have received a copy of the GNU General Public License 
19  # along with TimeSide.  If not, see <http://www.gnu.org/licenses/>. 
20   
21  # Author: Guillaume Pellerin <yomguy@parisson.com> 
22   
23  from timeside.core import implements, interfacedoc 
24  from timeside.analyzer.core import Analyzer 
25  from timeside.api import IValueAnalyzer 
26  import numpy as np 
27 28 29 -class Level(Analyzer):
30 implements(IValueAnalyzer) 31 32 @interfacedoc
33 - def setup(self, channels=None, samplerate=None, blocksize=None, 34 totalframes=None):
35 super(Level, self).setup(channels, samplerate, blocksize, totalframes) 36 # max_level 37 self.max_value = 0 38 # rms_level 39 self.mean_values = np.array([])
40 41 @staticmethod 42 @interfacedoc
43 - def id():
44 return "level"
45 46 @staticmethod 47 @interfacedoc
48 - def name():
49 return "Level Analyzer"
50 51 @staticmethod 52 @interfacedoc
53 - def unit():
54 return "dBFS"
55
56 - def process(self, frames, eod=False):
57 if frames.size: 58 # max_level 59 max_value = frames.max() 60 if max_value > self.max_value: 61 self.max_value = max_value 62 # rms_level 63 self.mean_values = np.append(self.mean_values, 64 np.mean(np.square(frames))) 65 return frames, eod
66
67 - def post_process(self):
68 # Max level 69 max_level = self.new_result(data_mode='value', time_mode='global') 70 71 max_level.id_metadata.id += '.' + "max" 72 max_level.id_metadata.name += ' ' + "Max" 73 74 max_level.data_object.value = np.round(20*np.log10(self.max_value), 3) 75 self.process_pipe.results.add(max_level) 76 77 # RMS level 78 rms_level = self.new_result(data_mode='value', time_mode='global') 79 rms_level.id_metadata.id += '.' + "rms" 80 rms_level.id_metadata.name += ' ' + "RMS" 81 82 rms_level.data_object.value = np.round(20*np.log10( 83 np.sqrt(np.mean(self.mean_values))), 3) 84 self.process_pipe.results.add(rms_level)
85