comparison Example VamPy plugins/obsolete/PySpectralCentroid.py @ 37:27bab3a16c9a vampy2final

new branch Vampy2final
author fazekasgy
date Mon, 05 Oct 2009 11:28:00 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 37:27bab3a16c9a
1 '''PySpectralCentroid.py - Example plugin demonstrates
2 how to write a C style plugin using VamPy.
3
4 Obsolete warning: this plugin will no longer be supported
5 since the legacy interface should pass a list of complex
6 numbers for frequency domain plugins and a list of floats
7 for time domin plugins.
8
9 '''
10
11 from numpy import *
12
13 class PySpectralCentroid:
14
15 def __init__(self,inputSampleRate):
16 self.m_imputSampleRate = 0.0
17 self.m_stepSize = 0
18 self.m_blockSize = 0
19 self.m_channels = 0
20 self.previousSample = 0.0
21 self.m_inputSampleRate = inputSampleRate
22 self.threshold = 0.00
23
24 def initialise(self,channels,stepSize,blockSize):
25 self.m_channels = channels
26 self.m_stepSize = stepSize
27 self.m_blockSize = blockSize
28 return True
29
30 def getMaker(self):
31 return 'Vampy Example Plugins'
32
33 def getName(self):
34 return 'Spectral Centroid (legacy process interface)'
35
36 def getIdentifier(self):
37 return 'vampy-sc2'
38
39 def getMaxChannelCount(self):
40 return 1
41
42 def getInputDomain(self):
43 return 'FrequencyDomain'
44
45 def getOutputDescriptors(self):
46
47 output0={
48 'identifier':'vampy-sf1',
49 'name':'Spectral Centroid',
50 'description':'Spectral Centroid (Brightness)',
51 'unit':' ',
52 'hasFixedBinCount':True,
53 'binCount':1,
54 'hasKnownExtents':False,
55 'isQuantized':True,
56 'quantizeStep':1.0,
57 'sampleType':'OneSamplePerStep'
58 }
59
60 return [output0]
61
62 def getParameterDescriptors(self):
63 paramlist1={
64 'identifier':'threshold',
65 'name':'Noise threshold: ',
66 'description':'Return null or delete this function if not needed.',
67 'unit':'v',
68 'minValue':0.0,
69 'maxValue':0.5,
70 'defaultValue':0.05,
71 'isQuantized':False
72 }
73 return [paramlist1]
74
75 def setParameter(self,paramid,newval):
76 if paramid == 'threshold' :
77 self.threshold = newval
78 return
79
80 def getParameter(self,paramid):
81 if paramid == 'threshold' :
82 return self.threshold
83 else:
84 return 0.0
85
86 def process(self,inbuf,timestamp):
87
88 inArray = array(inbuf[0])
89 crossing = False
90 prev = self.previousSample
91 count = 0.0
92 numLin = 0.0
93 denom = 0.0
94 centroid = 0.0
95
96 # re = array(inbuf[2:len(inArray):2])
97 # im = array(inbuf[3:len(inArray):2])
98
99 output0=[]
100 output1=[]
101
102 # pw = 0
103 # for i in xrange(1,len(inbuf[0])) :
104 # pw = pw + abs(inbuf[0][i])
105
106 if sum(abs(inArray)) > self.threshold :
107 for i in range(1,(len(inArray)/2)) :
108 # for i in range(1,len(inbuf[0])) :
109
110 re = inArray[i*2]
111 im = inArray[i*2+1]
112 # re = inbuf[0][i].real
113 # im = inbuf[0][i].imag
114 freq = i * self.m_inputSampleRate / self.m_blockSize
115 power = sqrt (re*re + im*im) / (self.m_blockSize/2)
116 denom = denom + power
117 numLin = numLin + freq * power
118
119 if denom != 0 :
120 centroid = numLin / denom
121
122 else :
123 centroid = 0.0
124
125 feature0={
126 'hasTimestamp':False,
127 'values':[centroid], #strictly must be a list
128 'label':str(centroid)
129 }
130 output0.append(feature0)
131
132 return [output0]