Mercurial > hg > vampy
comparison Example VamPy plugins/PySpectralCentroid.py @ 52:d56f48aafb99
Updated some example plugins.
author | fazekasgy |
---|---|
date | Thu, 08 Oct 2009 08:59:08 +0000 |
parents | |
children | 44d56a3d16b7 |
comparison
equal
deleted
inserted
replaced
51:c1e4f706ca9a | 52:d56f48aafb99 |
---|---|
1 '''PySpectralCentroid.py - Example plugin demonstrates | |
2 how to write a C style plugin using VamPy in pure Python. | |
3 This plugin also introduces the use of the builtin vampy | |
4 extension module. | |
5 | |
6 The plugin has frequency domain input and is using the | |
7 legacy interface: the FFT outpout is passed as a list | |
8 of complex numbers. | |
9 | |
10 Outputs: | |
11 1) Spectral centroid | |
12 | |
13 Note: This is not the adviced way of writing Vampy plugins now, | |
14 since the interfaces provided for Numpy are at least 5 times | |
15 faster. However, this is still a nice and easy to understand | |
16 example, which also shows how can one write a reasonable | |
17 plugin without having Numpy installed. | |
18 | |
19 Warning: Earlier versions of this plugin are now obsolete. | |
20 (They were using the legacy interface of Vampy 1 which | |
21 did not distinquish between time and frequency domain inputs.) | |
22 | |
23 Centre for Digital Music, Queen Mary University of London. | |
24 Copyright (C) 2009 Gyorgy Fazekas, QMUL. (See Vamp sources | |
25 for licence information.) | |
26 | |
27 ''' | |
28 | |
29 # import the names we use from vampy | |
30 from vampy import Feature,FeatureSet,ParameterDescriptor | |
31 from vampy import OutputDescriptor,FrequencyDomain,OneSamplePerStep | |
32 | |
33 from math import sqrt | |
34 | |
35 class PySpectralCentroid: | |
36 | |
37 def __init__(self,inputSampleRate): | |
38 self.m_imputSampleRate = 0.0 | |
39 self.m_stepSize = 0 | |
40 self.m_blockSize = 0 | |
41 self.m_channels = 0 | |
42 self.previousSample = 0.0 | |
43 self.m_inputSampleRate = inputSampleRate | |
44 self.threshold = 0.00 | |
45 | |
46 def initialise(self,channels,stepSize,blockSize): | |
47 self.m_channels = channels | |
48 self.m_stepSize = stepSize | |
49 self.m_blockSize = blockSize | |
50 return True | |
51 | |
52 def getMaker(self): | |
53 return 'Vampy Example Plugins' | |
54 | |
55 def getName(self): | |
56 return 'Spectral Centroid (using legacy process interface)' | |
57 | |
58 def getIdentifier(self): | |
59 return 'vampy-sc3' | |
60 | |
61 def getMaxChannelCount(self): | |
62 return 1 | |
63 | |
64 def getInputDomain(self): | |
65 return FrequencyDomain | |
66 | |
67 def getOutputDescriptors(self): | |
68 | |
69 cod = OutputDescriptor() | |
70 cod.identifier='vampy-sc3' | |
71 cod.name='Spectral Centroid' | |
72 cod.description='Spectral Centroid (Brightness)' | |
73 cod.unit='' | |
74 cod.hasFixedBinCount=True | |
75 cod.binCount=1 | |
76 cod.hasKnownExtents=False | |
77 cod.isQuantized=True | |
78 cod.quantizeStep=1.0 | |
79 cod.sampleType=OneSamplePerStep | |
80 return cod | |
81 | |
82 def getParameterDescriptors(self): | |
83 thd = ParameterDescriptor() | |
84 thd.identifier='threshold' | |
85 thd.name='Noise threshold' | |
86 thd.description='Return null or delete this function if not needed.' | |
87 thd.unit='v' | |
88 thd.minValue=0.0 | |
89 thd.maxValue=0.5 | |
90 thd.defaultValue=0.05 | |
91 thd.isQuantized=False | |
92 return thd | |
93 | |
94 def setParameter(self,paramid,newval): | |
95 if paramid == 'threshold' : | |
96 self.threshold = newval | |
97 return | |
98 | |
99 def getParameter(self,paramid): | |
100 if paramid == 'threshold' : | |
101 return self.threshold | |
102 else: | |
103 return 0.0 | |
104 | |
105 def process(self,inputbuffers,timestamp): | |
106 | |
107 # this is a 1 channel frequency domain plugin, therefore | |
108 # inputbuffers contain (block size / 2) + 1 complex numbers | |
109 # corresponding to the FFT output from DC to Nyquist inclusive | |
110 | |
111 cplxArray = inputbuffers[0][:-1] | |
112 | |
113 prev = self.previousSample | |
114 numLin = 0.0 | |
115 denom = 0.0 | |
116 centroid = 0.0 | |
117 | |
118 output = FeatureSet() | |
119 | |
120 pw = 0 | |
121 for i in xrange(1,len(cplxArray)) : | |
122 pw = pw + abs(cplxArray[i]) | |
123 | |
124 if pw > self.threshold : | |
125 for i in range(1,(len(cplxArray))) : | |
126 | |
127 re = cplxArray[i].real | |
128 im = cplxArray[i].imag | |
129 freq = i * self.m_inputSampleRate / self.m_blockSize | |
130 power = sqrt (re*re + im*im) / (self.m_blockSize/2) | |
131 denom = denom + power | |
132 numLin = numLin + freq * power | |
133 | |
134 if denom != 0 : | |
135 centroid = numLin / denom | |
136 | |
137 else : | |
138 centroid = 0.0 | |
139 | |
140 output[0] = Feature() | |
141 output[0].values = centroid | |
142 output[0].label = str(centroid) | |
143 | |
144 return output |