annotate Example VamPy plugins/PySpectralCentroid.py @ 24:7d28bed0864e

* Rearrange Python plugin construction. Formerly, the PyPluginAdapter has retained a single plugin instance pointer for each plugin found, and its createPlugin method has simply returned a new PyPlugin object wrapping the same instance pointer. This has a couple of negative consequences: - Because construction of the actual Python instance occurred before the wrapper was constructed, it was not possible to pass arguments (i.e. the sample rate) from the wrapper constructor to the Python plugin instance constructor -- they had to be passed later, to initialise, disadvantaging those plugins that would like to use the sample rate for parameter & step/block size calculations etc - Because there was only a single Python plugin instance, it was not possible to run more than one instance at once with any isolation This rework instead stores the Python class pointer (rather than instance pointer) in the PyPluginAdapter, and each PyPlugin wrapper instance creates its own Python plugin instance. What could possibly go wrong?
author cannam
date Mon, 17 Aug 2009 15:22:06 +0000
parents 535d559300dc
children
rev   line source
fazekasgy@9 1 '''PySpectralCentroid.py - Example plugin demonstrates'''
fazekasgy@9 2 '''how to write a C style plugin using VamPy.'''
fazekasgy@9 3
fazekasgy@9 4 from numpy import *
fazekasgy@9 5
fazekasgy@9 6 class PySpectralCentroid:
fazekasgy@9 7
cannam@24 8 def __init__(self,inputSampleRate):
cannam@24 9 self.m_inputSampleRate = inputSampleRate
fazekasgy@9 10 self.m_stepSize = 0
fazekasgy@9 11 self.m_blockSize = 0
fazekasgy@9 12 self.m_channels = 0
fazekasgy@9 13 self.previousSample = 0.0
cannam@23 14 self.threshold = 0.05
fazekasgy@9 15
cannam@24 16 def initialise(self,channels,stepSize,blockSize):
fazekasgy@9 17 self.m_channels = channels
fazekasgy@9 18 self.m_stepSize = stepSize
fazekasgy@9 19 self.m_blockSize = blockSize
fazekasgy@9 20 return True
fazekasgy@9 21
fazekasgy@9 22 def getMaker(self):
fazekasgy@9 23 return 'VamPy Example Plugins'
fazekasgy@9 24
fazekasgy@9 25 def getName(self):
fazekasgy@9 26 return 'Spectral Centroid (VamPy Legacy Interface)'
fazekasgy@9 27
fazekasgy@9 28 def getIdentifier(self):
fazekasgy@9 29 return 'python-sf1'
fazekasgy@9 30
fazekasgy@9 31 def getMaxChannelCount(self):
fazekasgy@9 32 return 1
fazekasgy@9 33
fazekasgy@9 34 def getInputDomain(self):
fazekasgy@9 35 return 'FrequencyDomain'
fazekasgy@9 36
fazekasgy@9 37 def getOutputDescriptors(self):
fazekasgy@9 38
fazekasgy@9 39 #descriptors are python dictionaries
fazekasgy@9 40 output0={
fazekasgy@9 41 'identifier':'vampy-sf1',
fazekasgy@9 42 'name':'Spectral Centroid',
fazekasgy@9 43 'description':'Spectral Centroid (Brightness)',
fazekasgy@9 44 'unit':' ',
fazekasgy@9 45 'hasFixedBinCount':True,
fazekasgy@9 46 'binCount':1,
fazekasgy@9 47 #'binNames':['1 Hz',1.5,'2 Hz',3,'4 Hz'],
fazekasgy@9 48 'hasKnownExtents':False,
fazekasgy@9 49 #'minValue':0.0,
fazekasgy@9 50 #'maxValue':0.0,
fazekasgy@9 51 'isQuantized':True,
fazekasgy@9 52 'quantizeStep':1.0,
fazekasgy@9 53 'sampleType':'OneSamplePerStep'
fazekasgy@9 54 #'sampleRate':48000.0
fazekasgy@9 55 }
fazekasgy@9 56
fazekasgy@9 57 #return a list of dictionaries
fazekasgy@9 58 return [output0]
fazekasgy@9 59
fazekasgy@9 60 def getParameterDescriptors(self):
fazekasgy@9 61 paramlist1={
fazekasgy@9 62 'identifier':'threshold',
fazekasgy@9 63 'name':'Noise threshold: ',
fazekasgy@9 64 'description':'Return null or delete this function if not needed.',
fazekasgy@9 65 'unit':'v',
fazekasgy@9 66 'minValue':0.0,
fazekasgy@9 67 'maxValue':0.5,
fazekasgy@9 68 'defaultValue':0.05,
fazekasgy@9 69 'isQuantized':False
fazekasgy@9 70 }
fazekasgy@9 71 return [paramlist1]
fazekasgy@9 72
fazekasgy@9 73 def setParameter(self,paramid,newval):
fazekasgy@9 74 if paramid == 'threshold' :
fazekasgy@9 75 self.threshold = newval
fazekasgy@9 76 return
fazekasgy@9 77
fazekasgy@9 78 def getParameter(self,paramid):
fazekasgy@9 79 if paramid == 'threshold' :
fazekasgy@9 80 return self.threshold
fazekasgy@9 81 else:
fazekasgy@9 82 return 0.0
fazekasgy@9 83
fazekasgy@13 84 def process(self,inbuf,timestamp):
fazekasgy@9 85 inArray = array(inbuf[0])
fazekasgy@9 86 crossing = False
fazekasgy@9 87 prev = self.previousSample
fazekasgy@9 88 count = 0.0
fazekasgy@9 89 numLin = 0.0
fazekasgy@9 90 denom = 0.0
fazekasgy@9 91 centroid = 0.0
fazekasgy@9 92
fazekasgy@9 93
fazekasgy@9 94 re = array(inbuf[2:len(inArray):2])
fazekasgy@9 95 im = array(inbuf[3:len(inArray):2])
fazekasgy@9 96 #we have two outputs defined thus we have to declare
fazekasgy@9 97 #them as empty dictionaries in our output list
fazekasgy@9 98 #in order to be able to return variable rate outputs
fazekasgy@9 99 output0=[]
fazekasgy@9 100 output1=[]
fazekasgy@9 101
fazekasgy@9 102 if sum(abs(inArray)) > self.threshold :
fazekasgy@9 103 for i in range(1,(len(inArray)/2)) :
fazekasgy@9 104
fazekasgy@9 105 re = inArray[i*2]
fazekasgy@9 106 im = inArray[i*2+1]
fazekasgy@9 107 freq = i * self.m_inputSampleRate / self.m_blockSize
fazekasgy@9 108 power = sqrt (re*re + im*im) / (self.m_blockSize/2)
fazekasgy@9 109 denom = denom + power
fazekasgy@9 110 numLin = numLin + freq * power
fazekasgy@9 111
fazekasgy@9 112 if denom != 0 :
fazekasgy@9 113 centroid = numLin / denom
fazekasgy@9 114
fazekasgy@9 115 else :
fazekasgy@9 116 centroid = 0.0
fazekasgy@9 117
fazekasgy@9 118 feature0={
fazekasgy@9 119 'hasTimestamp':False,
fazekasgy@9 120 'values':[centroid], #strictly must be a list
fazekasgy@9 121 'label':str(centroid)
fazekasgy@9 122 }
fazekasgy@9 123 output0.append(feature0)
fazekasgy@9 124
fazekasgy@9 125 #return a LIST of list of dictionaries
fazekasgy@9 126 return [output0]