def addFeature(self, definition): """ Add a feature defined according the :ref:`feature definition syntax <featplan>`. :param definition: feature definition :type definition: string :rtype: True on success, False on fail. """ data = definition.split(':') if not len(data) == 2: print 'Syntax error in "%s"' % definition return False name, featdef = data dataflow = DataFlow() dataflow.createInput('audio', self.audio_params) if featdef.strip(): for s in featdef.split('>'): s = s.strip() bb = s.split(' ') feat = AudioFeatureFactory.get_feature(bb[0]) if not feat: return False params = {} for d in bb[1:]: if len(d) == 0: continue if not '=' in d: print 'Invalid feature parameter "%s"' % d return False dd = d.split('=') if not len(dd) == 2: print 'Syntax error in feature parameter "%s"' % d return False params[dd[0]] = dd[1] dataflow.append(feat.get_dataflow(params, self.sample_rate)) fNode = dataflow.finalNodes()[0] feat_attrs = self.out_attrs.copy() feat_attrs['yaafedefinition'] = featdef.strip() outNode = dataflow.createOutput(name, feat_attrs) dataflow.link(fNode, '', outNode, '') self.dataflow.merge(dataflow) return True
def get_dataflow(cls, params, samplerate): nboct = decimal.Decimal(params['CQTNbOctaves']) original_fs = samplerate original_step = int(params['stepSize']) samplerate = decimal.Decimal(samplerate) minFreq = decimal.Decimal(params['CQTMinFreq']) * (2 ** (nboct - 1)) bandwidth = decimal.Decimal('0.95') # create dataflow df = DataFlow() signal_node = None # decimate until first octave to analyse nbdecim = 0 while ((bandwidth * samplerate / 8) > minFreq): s = df.createNode('Decimate2', {}) if signal_node: df.link(signal_node, '', s, '') signal_node = s samplerate = samplerate / 2 nbdecim = nbdecim + 1 # check stepsize is OK with decimation # stepSize = int(params['stepSize']) # if (stepSize % (2**(nbdecim+nboct-1)))!=0: # stepSize = stepSize - stepSize % (2**(nbdecim+nboct-1)) # print('WARNING: adjust stepSize to %i to make it compatible with successive decimation'%stepSize) # compute octave CQT parameters Q = 2 / (2 ** (1.0 / int(params['CQTBinsPerOctave'])) - 1) fftLen = Q * float(samplerate / minFreq) fftLen = pow(2, math.ceil(math.log(fftLen) / math.log(2))) # current_stepSize = stepSize / (2**nbdecim) oct_params = {'CQTBinsPerOctave': params['CQTBinsPerOctave'], 'CQTAlign': params['CQTAlign'], 'CQTMinFreq': '%s' % str(minFreq / samplerate), 'CQTMaxFreq': '%s' % str(2 * minFreq / samplerate - decimal.Decimal('1e-14'))} # for each octave, analysis, concatenate and decimation concat_node = df.createNode('Concatenate', {}) for oct in range(int(nboct), 0, -1): frames = df.createNode('AdvancedFrameTokenizer', {'blockSize': '%i' % fftLen, 'outStepSize': '%i' % original_step, 'outSampleRate': '%f' % original_fs}) if signal_node: df.link(signal_node, '', frames, '') cspec = df.createNode( 'FFT', {'FFTLength': '%i' % fftLen, 'FFTWindow': 'None'}) df.link(frames, '', cspec, '') oct_cq = df.createNode('CQT', oct_params) df.link(cspec, '', oct_cq, '') df.link(oct_cq, '', concat_node, '%i' % (oct - 1)) if (oct == 1): # no more octave to analyze, no need to decimate any more break # decimation for next actave analysis s = df.createNode('Decimate2', {}) if signal_node: df.link(signal_node, '', s, '') signal_node = s # current_stepSize = current_stepSize / 2 minFreq = minFreq / 2 return df
def get_dataflow(cls, params, samplerate): nboct = decimal.Decimal(params['CQTNbOctaves']) original_fs = samplerate original_step = int(params['stepSize']) samplerate = decimal.Decimal(samplerate) minFreq = decimal.Decimal(params['CQTMinFreq']) * (2 ** (nboct - 1)) bandwidth = decimal.Decimal('0.95') # create dataflow df = DataFlow() signal_node = None # decimate until first octave to analyse nbdecim = 0 while ((bandwidth * samplerate / 8) > minFreq): s = df.createNode('Decimate2', {}) if signal_node: df.link(signal_node, '', s, '') signal_node = s samplerate = samplerate / 2 nbdecim = nbdecim + 1 # check stepsize is OK with decimation # stepSize = int(params['stepSize']) # if (stepSize % (2**(nbdecim+nboct-1)))!=0: # stepSize = stepSize - stepSize % (2**(nbdecim+nboct-1)) # print 'WARNING: adjust stepSize to %i to make it compatible with successive decimation'%stepSize # compute octave CQT parameters Q = 2 / (2 ** (1.0 / int(params['CQTBinsPerOctave'])) - 1) fftLen = Q * float(samplerate / minFreq) fftLen = pow(2, math.ceil(math.log(fftLen) / math.log(2))) # current_stepSize = stepSize / (2**nbdecim) oct_params = {'CQTBinsPerOctave': params['CQTBinsPerOctave'], 'CQTAlign': params['CQTAlign'], 'CQTMinFreq': '%s' % str(minFreq / samplerate), 'CQTMaxFreq': '%s' % str(2 * minFreq / samplerate - decimal.Decimal('1e-14'))} # for each octave, analysis, concatenate and decimation concat_node = df.createNode('Concatenate', {}) for oct in range(nboct, 0, -1): frames = df.createNode('AdvancedFrameTokenizer', {'blockSize': '%i' % fftLen, 'outStepSize': '%i' % original_step, 'outSampleRate': '%f' % original_fs}) if signal_node: df.link(signal_node, '', frames, '') cspec = df.createNode( 'FFT', {'FFTLength': '%i' % fftLen, 'FFTWindow': 'None'}) df.link(frames, '', cspec, '') oct_cq = df.createNode('CQT', oct_params) df.link(cspec, '', oct_cq, '') df.link(oct_cq, '', concat_node, '%i' % (oct - 1)) if (oct == 1): # no more octave to analyze, no need to decimate any more break # decimation for next actave analysis s = df.createNode('Decimate2', {}) if signal_node: df.link(signal_node, '', s, '') signal_node = s # current_stepSize = current_stepSize / 2 minFreq = minFreq / 2 return df
def get_dataflow(cls, params, samplerate): nboct = decimal.Decimal(params["CQTNbOctaves"]) original_fs = samplerate original_step = int(params["stepSize"]) samplerate = decimal.Decimal(samplerate) minFreq = decimal.Decimal(params["CQTMinFreq"]) * (2 ** (nboct - 1)) bandwidth = decimal.Decimal("0.95") # create dataflow df = DataFlow() signal_node = None # decimate until first octave to analyse nbdecim = 0 while (bandwidth * samplerate / 8) > minFreq: s = df.createNode("Decimate2", {}) if signal_node: df.link(signal_node, "", s, "") signal_node = s samplerate = samplerate / 2 nbdecim = nbdecim + 1 # check stepsize is OK with decimation # stepSize = int(params['stepSize']) # if (stepSize % (2**(nbdecim+nboct-1)))!=0: # stepSize = stepSize - stepSize % (2**(nbdecim+nboct-1)) # print 'WARNING: adjust stepSize to %i to make it compatible with successive decimation'%stepSize # compute octave CQT parameters Q = 2 / (2 ** (1.0 / int(params["CQTBinsPerOctave"])) - 1) fftLen = Q * float(samplerate / minFreq) fftLen = pow(2, math.ceil(math.log(fftLen) / math.log(2))) # current_stepSize = stepSize / (2**nbdecim) oct_params = { "CQTBinsPerOctave": params["CQTBinsPerOctave"], "CQTAlign": params["CQTAlign"], "CQTMinFreq": "%s" % str(minFreq / samplerate), "CQTMaxFreq": "%s" % str(2 * minFreq / samplerate - decimal.Decimal("1e-14")), } # for each octave, analysis, concatenate and decimation concat_node = df.createNode("Concatenate", {}) for oct in range(nboct, 0, -1): frames = df.createNode( "AdvancedFrameTokenizer", {"blockSize": "%i" % fftLen, "outStepSize": "%i" % original_step, "outSampleRate": "%f" % original_fs}, ) if signal_node: df.link(signal_node, "", frames, "") cspec = df.createNode("FFT", {"FFTLength": "%i" % fftLen, "FFTWindow": "None"}) df.link(frames, "", cspec, "") oct_cq = df.createNode("CQT", oct_params) df.link(cspec, "", oct_cq, "") df.link(oct_cq, "", concat_node, "%i" % (oct - 1)) if oct == 1: # no more octave to analyze, no need to decimate any more break # decimation for next actave analysis s = df.createNode("Decimate2", {}) if signal_node: df.link(signal_node, "", s, "") signal_node = s # current_stepSize = current_stepSize / 2 minFreq = minFreq / 2 return df