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

Source Code for Module timeside.analyzer.yaafe

  1  # -*- coding: utf-8 -*- 
  2  # 
  3  # Copyright (c) 2013 Thomas Fillon <thomas@parisson.com> 
  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 : Thomas Fillon <thomas@parisson.com> 
 21  """ 
 22  Module Yaafe Analyzer 
 23  Created on Thu Jun 13 16:05:02 2013 
 24   
 25  @author: Thomas Fillon 
 26  """ 
 27  from timeside.core import implements, interfacedoc 
 28  from timeside.analyzer.core import Analyzer 
 29  from timeside.api import IAnalyzer 
 30  from yaafelib import * 
 31  import numpy 
32 33 34 -class Yaafe(Analyzer):
35 implements(IAnalyzer) 36
37 - def __init__(self, yaafeSpecification=None):
38 super(Yaafe,self).__init__() 39 40 # Check arguments 41 if yaafeSpecification is None: 42 yaafeSpecification = FeaturePlan(sample_rate=32000) 43 # add feature definitions manually 44 yaafeSpecification.addFeature('mfcc: MFCC blockSize=512 stepSize=256') 45 46 if isinstance(yaafeSpecification, DataFlow): 47 self.dataFlow = yaafeSpecification 48 elif isinstance(yaafeSpecification, FeaturePlan): 49 self.featurePlan = yaafeSpecification 50 self.dataFlow = self.featurePlan.getDataFlow() 51 else: 52 raise TypeError("'%s' Type must be either '%s' or '%s'" % 53 (str(yaafeSpecification), 54 str(DataFlow), 55 str(FeaturePlan))) 56 self.yaafe_engine = None
57 58 59 @interfacedoc
60 - def setup(self, channels=None, samplerate=None, 61 blocksize=None, totalframes=None):
62 super(Yaafe, self).setup(channels, samplerate, blocksize, totalframes) 63 # Configure a YAAFE engine 64 self.yaafe_engine = Engine() 65 self.yaafe_engine.load(self.dataFlow) 66 self.yaafe_engine.reset() 67 self.input_samplerate = samplerate 68 self.input_blocksize = blocksize
69 70 @staticmethod 71 @interfacedoc
72 - def id():
73 return "yaafe"
74 75 @staticmethod 76 @interfacedoc
77 - def name():
78 return "Yaafe Descriptor"
79 80 @staticmethod 81 @interfacedoc
82 - def unit():
83 return ''
84
85 - def process(self, frames, eod=False):
86 # do process things... 87 # Downmixing to mono and convert to float64 for compatibility with 88 # Yaafe 89 yaafe_frames = frames.sum( 90 axis=-1, dtype=numpy.float64) / frames.shape[-1] 91 # Reshape for compatibility with Yaafe input format 92 yaafe_frames.shape = (1, yaafe_frames.shape[0]) 93 # write audio array on 'audio' input 94 self.yaafe_engine.writeInput('audio', yaafe_frames) 95 # process available data 96 self.yaafe_engine.process() 97 if eod: 98 # flush yaafe engine to process remaining data 99 self.yaafe_engine.flush() 100 101 return frames, eod
102
103 - def post_process(self):
104 # Get feature extraction results from yaafe 105 featNames = self.yaafe_engine.getOutputs().keys() 106 if len(featNames) == 0: 107 raise KeyError('Yaafe engine did not return any feature') 108 for featName in featNames: 109 110 result = self.new_result(data_mode='value', time_mode='framewise') 111 result.id_metadata.id += '.' + featName 112 result.id_metadata.name += ' ' + featName 113 # Read Yaafe Results 114 result.data_object.value = self.yaafe_engine.readOutput(featName) 115 # Store results in Container 116 if len(result.data_object.value): 117 self.process_pipe.results.add(result)
118