comparison examples/SpectralCentroid.cpp @ 227:6b30e064cab7 distinct-libraries

* more moving
author cannam
date Thu, 06 Nov 2008 14:13:12 +0000
parents src/SpectralCentroid.cpp@14029eb08472
children 3cf5bd155e5b
comparison
equal deleted inserted replaced
226:14029eb08472 227:6b30e064cab7
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 AUTHORS 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 using std::string;
40 using std::vector;
41 using std::cerr;
42 using std::endl;
43
44 #include <math.h>
45
46 #ifdef WIN32
47 #define isnan(x) false
48 #define isinf(x) false
49 #endif
50
51 SpectralCentroid::SpectralCentroid(float inputSampleRate) :
52 Plugin(inputSampleRate),
53 m_stepSize(0),
54 m_blockSize(0)
55 {
56 }
57
58 SpectralCentroid::~SpectralCentroid()
59 {
60 }
61
62 string
63 SpectralCentroid::getIdentifier() const
64 {
65 return "spectralcentroid";
66 }
67
68 string
69 SpectralCentroid::getName() const
70 {
71 return "Spectral Centroid";
72 }
73
74 string
75 SpectralCentroid::getDescription() const
76 {
77 return "Calculate the centroid frequency of the spectrum of the input signal";
78 }
79
80 string
81 SpectralCentroid::getMaker() const
82 {
83 return "Vamp SDK Example Plugins";
84 }
85
86 int
87 SpectralCentroid::getPluginVersion() const
88 {
89 return 2;
90 }
91
92 string
93 SpectralCentroid::getCopyright() const
94 {
95 return "Freely redistributable (BSD license)";
96 }
97
98 bool
99 SpectralCentroid::initialise(size_t channels, size_t stepSize, size_t blockSize)
100 {
101 if (channels < getMinChannelCount() ||
102 channels > getMaxChannelCount()) return false;
103
104 m_stepSize = stepSize;
105 m_blockSize = blockSize;
106
107 return true;
108 }
109
110 void
111 SpectralCentroid::reset()
112 {
113 }
114
115 SpectralCentroid::OutputList
116 SpectralCentroid::getOutputDescriptors() const
117 {
118 OutputList list;
119
120 OutputDescriptor d;
121 d.identifier = "logcentroid";
122 d.name = "Log Frequency Centroid";
123 d.description = "Centroid of the log weighted frequency spectrum";
124 d.unit = "Hz";
125 d.hasFixedBinCount = true;
126 d.binCount = 1;
127 d.hasKnownExtents = false;
128 d.isQuantized = false;
129 d.sampleType = OutputDescriptor::OneSamplePerStep;
130 list.push_back(d);
131
132 d.identifier = "linearcentroid";
133 d.name = "Linear Frequency Centroid";
134 d.description = "Centroid of the linear frequency spectrum";
135 list.push_back(d);
136
137 return list;
138 }
139
140 //static int scount = 0;
141
142 SpectralCentroid::FeatureSet
143 SpectralCentroid::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
144 {
145 if (m_stepSize == 0) {
146 cerr << "ERROR: SpectralCentroid::process: "
147 << "SpectralCentroid has not been initialised"
148 << endl;
149 return FeatureSet();
150 }
151
152 // std::cerr << "SpectralCentroid::process: count = " << scount++ << ", timestamp = " << timestamp << ", total power = ";
153
154 double numLin = 0.0, numLog = 0.0, denom = 0.0;
155
156 for (size_t i = 1; i <= m_blockSize/2; ++i) {
157 double freq = (double(i) * m_inputSampleRate) / m_blockSize;
158 double real = inputBuffers[0][i*2];
159 double imag = inputBuffers[0][i*2 + 1];
160 double power = sqrt(real * real + imag * imag) / (m_blockSize/2);
161 numLin += freq * power;
162 numLog += log10f(freq) * power;
163 denom += power;
164 }
165
166 // std::cerr << denom << std::endl;
167
168 FeatureSet returnFeatures;
169
170 if (denom != 0.0) {
171 float centroidLin = float(numLin / denom);
172 float centroidLog = powf(10, float(numLog / denom));
173
174 Feature feature;
175 feature.hasTimestamp = false;
176 if (!isnan(centroidLog) && !isinf(centroidLog)) {
177 feature.values.push_back(centroidLog);
178 }
179 returnFeatures[0].push_back(feature);
180
181 feature.values.clear();
182 if (!isnan(centroidLin) && !isinf(centroidLin)) {
183 feature.values.push_back(centroidLin);
184 }
185 returnFeatures[1].push_back(feature);
186 }
187
188 return returnFeatures;
189 }
190
191 SpectralCentroid::FeatureSet
192 SpectralCentroid::getRemainingFeatures()
193 {
194 return FeatureSet();
195 }
196