annotate vamp-sdk/hostext/PluginSummarisingAdapter.cpp @ 175:4811fb599a97

* summarising adapter might sort of work now -- quite untested though
author cannam
date Tue, 05 Aug 2008 15:15:37 +0000
parents a6346812db44
children adfb6348881c
rev   line source
cannam@173 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@173 2
cannam@173 3 /*
cannam@173 4 Vamp
cannam@173 5
cannam@173 6 An API for audio analysis and feature extraction plugins.
cannam@173 7
cannam@173 8 Centre for Digital Music, Queen Mary, University of London.
cannam@173 9 Copyright 2006-2008 Chris Cannam and QMUL.
cannam@173 10
cannam@173 11 Permission is hereby granted, free of charge, to any person
cannam@173 12 obtaining a copy of this software and associated documentation
cannam@173 13 files (the "Software"), to deal in the Software without
cannam@173 14 restriction, including without limitation the rights to use, copy,
cannam@173 15 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@173 16 of the Software, and to permit persons to whom the Software is
cannam@173 17 furnished to do so, subject to the following conditions:
cannam@173 18
cannam@173 19 The above copyright notice and this permission notice shall be
cannam@173 20 included in all copies or substantial portions of the Software.
cannam@173 21
cannam@173 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@173 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@173 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@173 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@173 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@173 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@173 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@173 29
cannam@173 30 Except as contained in this notice, the names of the Centre for
cannam@173 31 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@173 32 shall not be used in advertising or otherwise to promote the sale,
cannam@173 33 use or other dealings in this Software without prior written
cannam@173 34 authorization.
cannam@173 35 */
cannam@173 36
cannam@173 37 #include "PluginSummarisingAdapter.h"
cannam@173 38
cannam@174 39 #include <map>
cannam@175 40 #include <cmath>
cannam@174 41
cannam@173 42 namespace Vamp {
cannam@173 43
cannam@173 44 namespace HostExt {
cannam@173 45
cannam@173 46 class PluginSummarisingAdapter::Impl
cannam@173 47 {
cannam@173 48 public:
cannam@173 49 Impl(Plugin *plugin, float inputSampleRate);
cannam@173 50 ~Impl();
cannam@173 51
cannam@173 52 FeatureSet process(const float *const *inputBuffers, RealTime timestamp);
cannam@173 53 FeatureSet getRemainingFeatures();
cannam@173 54
cannam@173 55 void setSummarySegmentBoundaries(const SegmentBoundaries &);
cannam@173 56
cannam@175 57 FeatureList getSummary(int output, SummaryType type);
cannam@173 58
cannam@173 59 protected:
cannam@174 60 Plugin *m_plugin;
cannam@174 61
cannam@173 62 SegmentBoundaries m_boundaries;
cannam@174 63
cannam@174 64 typedef std::vector<float> ValueList;
cannam@174 65 typedef std::map<int, ValueList> BinValueMap;
cannam@174 66
cannam@174 67 struct OutputAccumulator {
cannam@174 68 int count;
cannam@174 69 BinValueMap values;
cannam@174 70 };
cannam@174 71
cannam@174 72 typedef std::map<int, OutputAccumulator> OutputAccumulatorMap;
cannam@174 73 OutputAccumulatorMap m_accumulators;
cannam@174 74
cannam@174 75 struct OutputBinSummary {
cannam@174 76 float minimum;
cannam@174 77 float maximum;
cannam@174 78 float median;
cannam@174 79 float mode;
cannam@174 80 float sum;
cannam@174 81 float variance;
cannam@174 82 int count;
cannam@174 83 };
cannam@174 84
cannam@174 85 typedef std::map<int, OutputBinSummary> OutputSummary;
cannam@174 86 typedef std::map<RealTime, OutputSummary> SummarySegmentMap;
cannam@174 87 typedef std::map<int, SummarySegmentMap> OutputSummarySegmentMap;
cannam@174 88
cannam@174 89 OutputSummarySegmentMap m_summaries;
cannam@174 90
cannam@174 91 RealTime m_lastTimestamp;
cannam@174 92
cannam@174 93 void accumulate(const FeatureSet &fs, RealTime);
cannam@174 94 void accumulate(int output, const Feature &f, RealTime);
cannam@174 95 void reduce();
cannam@173 96 };
cannam@173 97
cannam@173 98 PluginSummarisingAdapter::PluginSummarisingAdapter(Plugin *plugin) :
cannam@173 99 PluginWrapper(plugin)
cannam@173 100 {
cannam@173 101 m_impl = new Impl(plugin, m_inputSampleRate);
cannam@173 102 }
cannam@173 103
cannam@173 104 PluginSummarisingAdapter::~PluginSummarisingAdapter()
cannam@173 105 {
cannam@173 106 delete m_impl;
cannam@173 107 }
cannam@173 108
cannam@173 109 Plugin::FeatureSet
cannam@173 110 PluginSummarisingAdapter::process(const float *const *inputBuffers, RealTime timestamp)
cannam@173 111 {
cannam@173 112 return m_impl->process(inputBuffers, timestamp);
cannam@173 113 }
cannam@173 114
cannam@174 115 Plugin::FeatureSet
cannam@174 116 PluginSummarisingAdapter::getRemainingFeatures()
cannam@174 117 {
cannam@174 118 return m_impl->getRemainingFeatures();
cannam@174 119 }
cannam@174 120
cannam@175 121 Plugin::FeatureList
cannam@175 122 PluginSummarisingAdapter::getSummary(int output, SummaryType type)
cannam@175 123 {
cannam@175 124 return m_impl->getSummary(output, type);
cannam@175 125 }
cannam@173 126
cannam@173 127 PluginSummarisingAdapter::Impl::Impl(Plugin *plugin, float inputSampleRate) :
cannam@174 128 m_plugin(plugin)
cannam@173 129 {
cannam@173 130 }
cannam@173 131
cannam@173 132 PluginSummarisingAdapter::Impl::~Impl()
cannam@173 133 {
cannam@173 134 }
cannam@173 135
cannam@174 136 Plugin::FeatureSet
cannam@174 137 PluginSummarisingAdapter::Impl::process(const float *const *inputBuffers, RealTime timestamp)
cannam@174 138 {
cannam@174 139 FeatureSet fs = m_plugin->process(inputBuffers, timestamp);
cannam@174 140 accumulate(fs, timestamp);
cannam@174 141 m_lastTimestamp = timestamp;
cannam@174 142 return fs;
cannam@174 143 }
cannam@174 144
cannam@174 145 Plugin::FeatureSet
cannam@174 146 PluginSummarisingAdapter::Impl::getRemainingFeatures()
cannam@174 147 {
cannam@174 148 FeatureSet fs = m_plugin->getRemainingFeatures();
cannam@174 149 accumulate(fs, m_lastTimestamp);
cannam@174 150 reduce();
cannam@174 151 return fs;
cannam@174 152 }
cannam@174 153
cannam@175 154 Plugin::FeatureList
cannam@175 155 PluginSummarisingAdapter::Impl::getSummary(int output, SummaryType type)
cannam@175 156 {
cannam@175 157 //!!! need to ensure that this is only called after processing is
cannam@175 158 //!!! complete (at the moment processing is "completed" in the
cannam@175 159 //!!! call to getRemainingFeatures, but we don't want to require
cannam@175 160 //!!! the host to call getRemainingFeatures at all unless it
cannam@175 161 //!!! actually wants the raw features too -- calling getSummary
cannam@175 162 //!!! should be enough -- we do need to ensure that all data has
cannam@175 163 //!!! been processed though!)
cannam@175 164 FeatureList fl;
cannam@175 165 for (SummarySegmentMap::const_iterator i = m_summaries[output].begin();
cannam@175 166 i != m_summaries[output].end(); ++i) {
cannam@175 167 Feature f;
cannam@175 168 f.hasTimestamp = true;
cannam@175 169 f.timestamp = i->first;
cannam@175 170 f.hasDuration = false;
cannam@175 171 for (OutputSummary::const_iterator j = i->second.begin();
cannam@175 172 j != i->second.end(); ++j) {
cannam@175 173
cannam@175 174 // these will be ordered by bin number, and no bin numbers
cannam@175 175 // will be missing except at the end (because of the way
cannam@175 176 // the accumulators were initially filled in accumulate())
cannam@175 177
cannam@175 178 const OutputBinSummary &summary = j->second;
cannam@175 179 float result = 0.f;
cannam@175 180
cannam@175 181 switch (type) {
cannam@175 182
cannam@175 183 case Minimum:
cannam@175 184 result = summary.minimum;
cannam@175 185 break;
cannam@175 186
cannam@175 187 case Maximum:
cannam@175 188 result = summary.maximum;
cannam@175 189 break;
cannam@175 190
cannam@175 191 case Mean:
cannam@175 192 if (summary.count) {
cannam@175 193 result = summary.sum / summary.count;
cannam@175 194 }
cannam@175 195 break;
cannam@175 196
cannam@175 197 case Median:
cannam@175 198 result = summary.median;
cannam@175 199 break;
cannam@175 200
cannam@175 201 case Mode:
cannam@175 202 result = summary.mode;
cannam@175 203 break;
cannam@175 204
cannam@175 205 case Sum:
cannam@175 206 result = summary.sum;
cannam@175 207 break;
cannam@175 208
cannam@175 209 case Variance:
cannam@175 210 result = summary.variance;
cannam@175 211 break;
cannam@175 212
cannam@175 213 case StandardDeviation:
cannam@175 214 result = sqrtf(summary.variance);
cannam@175 215 break;
cannam@175 216
cannam@175 217 case Count:
cannam@175 218 result = summary.count;
cannam@175 219 break;
cannam@175 220 }
cannam@175 221 }
cannam@175 222
cannam@175 223 fl.push_back(f);
cannam@175 224 }
cannam@175 225 return fl;
cannam@175 226 }
cannam@175 227
cannam@174 228 void
cannam@174 229 PluginSummarisingAdapter::Impl::accumulate(const FeatureSet &fs,
cannam@174 230 RealTime timestamp)
cannam@174 231 {
cannam@174 232 for (FeatureSet::const_iterator i = fs.begin(); i != fs.end(); ++i) {
cannam@174 233 for (FeatureList::const_iterator j = i->second.begin();
cannam@174 234 j != i->second.end(); ++j) {
cannam@174 235 accumulate(i->first, *j, timestamp);
cannam@174 236 }
cannam@174 237 }
cannam@174 238 }
cannam@174 239
cannam@174 240 void
cannam@174 241 PluginSummarisingAdapter::Impl::accumulate(int output,
cannam@174 242 const Feature &f,
cannam@174 243 RealTime timestamp)
cannam@174 244 {
cannam@174 245 //!!! use timestamp to determine which segment we're on
cannam@174 246 m_accumulators[output].count++;
cannam@174 247 for (int i = 0; i < int(f.values.size()); ++i) {
cannam@174 248 m_accumulators[output].values[i].push_back(f.values[i]);
cannam@174 249 }
cannam@174 250 }
cannam@174 251
cannam@174 252 void
cannam@174 253 PluginSummarisingAdapter::Impl::reduce()
cannam@174 254 {
cannam@174 255 RealTime segmentStart = RealTime::zeroTime; //!!!
cannam@174 256
cannam@174 257 for (OutputAccumulatorMap::iterator i = m_accumulators.begin();
cannam@174 258 i != m_accumulators.end(); ++i) {
cannam@174 259
cannam@174 260 int output = i->first;
cannam@174 261 OutputAccumulator &accumulator = i->second;
cannam@174 262
cannam@174 263 for (BinValueMap::iterator j = accumulator.values.begin();
cannam@174 264 j != accumulator.values.end(); ++j) {
cannam@174 265
cannam@174 266 int bin = j->first;
cannam@174 267 ValueList &values = j->second;
cannam@174 268
cannam@174 269 OutputBinSummary summary;
cannam@174 270 summary.minimum = 0.f;
cannam@174 271 summary.maximum = 0.f;
cannam@174 272 summary.median = 0.f;
cannam@174 273 summary.mode = 0.f;
cannam@174 274 summary.sum = 0.f;
cannam@174 275 summary.variance = 0.f;
cannam@174 276 summary.count = accumulator.count;
cannam@174 277 if (summary.count == 0 || values.empty()) continue;
cannam@174 278
cannam@174 279 std::sort(values.begin(), values.end());
cannam@174 280 int sz = values.size();
cannam@174 281
cannam@174 282 summary.minimum = values[0];
cannam@174 283 summary.maximum = values[sz-1];
cannam@174 284
cannam@174 285 if (sz % 2 == 1) {
cannam@174 286 summary.median = values[sz/2];
cannam@174 287 } else {
cannam@174 288 summary.median = (values[sz/2] + values[sz/2 + 1]) / 2;
cannam@174 289 }
cannam@174 290
cannam@174 291 std::map<float, int> distribution;
cannam@174 292
cannam@174 293 for (int k = 0; k < sz; ++k) {
cannam@174 294 summary.sum += values[k];
cannam@174 295 ++distribution[values[k]];
cannam@174 296 }
cannam@174 297
cannam@174 298 int md = 0;
cannam@174 299
cannam@174 300 //!!! I don't like this. Really the mode should be the
cannam@174 301 //!!! value that spans the longest period of time, not the
cannam@174 302 //!!! one that appears in the largest number of distinct
cannam@175 303 //!!! features. I suppose that a median by time rather
cannam@175 304 //!!! than number of features would also be useful.
cannam@174 305
cannam@174 306 for (std::map<float, int>::iterator di = distribution.begin();
cannam@174 307 di != distribution.end(); ++di) {
cannam@174 308 if (di->second > md) {
cannam@174 309 md = di->second;
cannam@174 310 summary.mode = di->first;
cannam@174 311 }
cannam@174 312 }
cannam@174 313
cannam@174 314 distribution.clear();
cannam@174 315
cannam@174 316 float mean = summary.sum / summary.count;
cannam@174 317
cannam@174 318 for (int k = 0; k < sz; ++k) {
cannam@174 319 summary.variance += (values[k] - mean) * (values[k] - mean);
cannam@174 320 }
cannam@174 321 summary.variance /= summary.count;
cannam@174 322
cannam@174 323 m_summaries[output][segmentStart][bin] = summary;
cannam@174 324 }
cannam@174 325 }
cannam@175 326
cannam@175 327 m_accumulators.clear();
cannam@174 328 }
cannam@174 329
cannam@174 330
cannam@174 331 }
cannam@174 332
cannam@174 333 }
cannam@174 334