comparison examples/SpectralCentroid.cpp @ 0:6479539d1b32

* Importing first cut of Sonic Visualiser's Vamp plugin format SDK
author cannam
date Fri, 31 Mar 2006 14:21:51 +0000
parents
children 8f10d35a4090
comparison
equal deleted inserted replaced
-1:000000000000 0:6479539d1b32
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Vamp
5
6 An API for audio analysis and feature extraction plugins.
7
8 Centre for Digital Music, Queen Mary, University of London.
9 Copyright 2006 Chris Cannam.
10
11 Permission is hereby granted, free of charge, to any person
12 obtaining a copy of this software and associated documentation
13 files (the "Software"), to deal in the Software without
14 restriction, including without limitation the rights to use, copy,
15 modify, merge, publish, distribute, sublicense, and/or sell copies
16 of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 Except as contained in this notice, the names of the Centre for
31 Digital Music; Queen Mary, University of London; and Chris Cannam
32 shall not be used in advertising or otherwise to promote the sale,
33 use or other dealings in this Software without prior written
34 authorization.
35 */
36
37 #include "SpectralCentroid.h"
38
39 /*
40 #include "dsp/transforms/FFT.h"
41 #include "base/Window.h"
42 */
43
44 using std::string;
45 using std::vector;
46 using std::cerr;
47 using std::endl;
48
49
50 SpectralCentroid::SpectralCentroid(float inputSampleRate) :
51 Plugin(inputSampleRate),
52 m_stepSize(0),
53 m_blockSize(0),
54 m_workBuffer(0)
55 {
56 }
57
58 SpectralCentroid::~SpectralCentroid()
59 {
60 delete m_workBuffer;
61 }
62
63 string
64 SpectralCentroid::getName() const
65 {
66 return "spectralcentroid";
67 }
68
69 string
70 SpectralCentroid::getDescription() const
71 {
72 return "Spectral Centroid";
73 }
74
75 string
76 SpectralCentroid::getMaker() const
77 {
78 return "QMUL";
79 }
80
81 int
82 SpectralCentroid::getPluginVersion() const
83 {
84 return 2;
85 }
86
87 string
88 SpectralCentroid::getCopyright() const
89 {
90 return "GPL";
91 }
92
93 bool
94 SpectralCentroid::initialise(size_t channels, size_t stepSize, size_t blockSize)
95 {
96 if (channels < getMinChannelCount() ||
97 channels > getMaxChannelCount()) return false;
98
99 m_stepSize = stepSize;
100 m_blockSize = blockSize;
101
102 delete m_workBuffer;
103 m_workBuffer = new double[m_blockSize * 4];
104
105 return true;
106 }
107
108 void
109 SpectralCentroid::reset()
110 {
111 delete m_workBuffer;
112 m_workBuffer = new double[m_blockSize * 4];
113 }
114
115 size_t
116 SpectralCentroid::getPreferredStepSize() const
117 {
118 return 2048; // or whatever -- parameter?
119 }
120
121 size_t
122 SpectralCentroid::getPreferredBlockSize() const
123 {
124 return getPreferredStepSize();
125 }
126
127 SpectralCentroid::OutputList
128 SpectralCentroid::getOutputDescriptors() const
129 {
130 OutputList list;
131
132 OutputDescriptor d;
133 d.name = "logcentroid";
134 d.unit = "Hz";
135 d.description = "Log Frequency Centroid";
136 d.hasFixedValueCount = true;
137 d.valueCount = 1;
138 d.hasKnownExtents = false;
139 d.isQuantized = false;
140 d.sampleType = OutputDescriptor::OneSamplePerStep;
141 list.push_back(d);
142
143 d.name = "linearcentroid";
144 d.description = "Linear Frequency Centroid";
145 list.push_back(d);
146
147 return list;
148 }
149
150 SpectralCentroid::FeatureSet
151 SpectralCentroid::process(float **inputBuffers, Vamp::RealTime)
152 {
153 if (m_stepSize == 0) {
154 cerr << "ERROR: SpectralCentroid::process: "
155 << "SpectralCentroid has not been initialised"
156 << endl;
157 return FeatureSet();
158 }
159
160 /*
161 for (size_t i = 0; i < m_blockSize; ++i) {
162 m_workBuffer[i] = inputBuffers[0][i];
163 m_workBuffer[i + m_blockSize] = 0.0;
164 }
165
166 Window<double>(HanningWindow, m_blockSize).cut(m_workBuffer);
167
168 FFT::process(m_blockSize, false,
169 m_workBuffer,
170 m_workBuffer + m_blockSize,
171 m_workBuffer + m_blockSize*2,
172 m_workBuffer + m_blockSize*3);
173 */
174
175 double numLin = 0.0, numLog = 0.0, denom = 0.0;
176
177 for (size_t i = 1; i < m_blockSize/2; ++i) {
178 double freq = (double(i) * m_inputSampleRate) / m_blockSize;
179 double real = inputBuffers[0][i*2];
180 double imag = inputBuffers[0][i*2 + 1];
181 double power = sqrt(real * real + imag * imag) / (m_blockSize/2);
182 numLin += freq * power;
183 numLog += log10f(freq) * power;
184 denom += power;
185 }
186
187 FeatureSet returnFeatures;
188
189 std::cerr << "power " << denom << ", block size " << m_blockSize << std::endl;
190
191 if (denom != 0.0) {
192 float centroidLin = float(numLin / denom);
193 float centroidLog = exp10f(float(numLog / denom));
194 Feature feature;
195 feature.hasTimestamp = false;
196 feature.values.push_back(centroidLog);
197 returnFeatures[0].push_back(feature);
198 feature.values.clear();
199 feature.values.push_back(centroidLin);
200 returnFeatures[1].push_back(feature);
201 }
202
203 return returnFeatures;
204 }
205
206 SpectralCentroid::FeatureSet
207 SpectralCentroid::getRemainingFeatures()
208 {
209 return FeatureSet();
210 }
211