annotate src/vamp-hostsdk/PluginSummarisingAdapter.cpp @ 248:c88a3cdb0215

* Tidy up --list-full output a bit * Some documentation, including docs for PluginSummarisingAdapter
author cannam
date Tue, 11 Nov 2008 12:07:09 +0000
parents 521734d2b498
children 4454843ff384
rev   line source
cannam@233 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@233 2
cannam@233 3 /*
cannam@233 4 Vamp
cannam@233 5
cannam@233 6 An API for audio analysis and feature extraction plugins.
cannam@233 7
cannam@233 8 Centre for Digital Music, Queen Mary, University of London.
cannam@233 9 Copyright 2006-2008 Chris Cannam and QMUL.
cannam@233 10
cannam@233 11 Permission is hereby granted, free of charge, to any person
cannam@233 12 obtaining a copy of this software and associated documentation
cannam@233 13 files (the "Software"), to deal in the Software without
cannam@233 14 restriction, including without limitation the rights to use, copy,
cannam@233 15 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@233 16 of the Software, and to permit persons to whom the Software is
cannam@233 17 furnished to do so, subject to the following conditions:
cannam@233 18
cannam@233 19 The above copyright notice and this permission notice shall be
cannam@233 20 included in all copies or substantial portions of the Software.
cannam@233 21
cannam@233 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@233 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@233 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@233 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@233 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@233 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@233 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@233 29
cannam@233 30 Except as contained in this notice, the names of the Centre for
cannam@233 31 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@233 32 shall not be used in advertising or otherwise to promote the sale,
cannam@233 33 use or other dealings in this Software without prior written
cannam@233 34 authorization.
cannam@233 35 */
cannam@233 36
cannam@233 37 #include <vamp-hostsdk/PluginSummarisingAdapter.h>
cannam@233 38
cannam@233 39 #include <map>
cannam@233 40 #include <algorithm>
cannam@233 41 #include <cmath>
cannam@233 42 #include <climits>
cannam@233 43
cannam@233 44 #define DEBUG_PLUGIN_SUMMARISING_ADAPTER 1
cannam@233 45 //#define DEBUG_PLUGIN_SUMMARISING_ADAPTER_SEGMENT 1
cannam@233 46
cannam@233 47 namespace Vamp {
cannam@233 48
cannam@233 49 namespace HostExt {
cannam@233 50
cannam@233 51 class PluginSummarisingAdapter::Impl
cannam@233 52 {
cannam@233 53 public:
cannam@233 54 Impl(Plugin *plugin, float inputSampleRate);
cannam@233 55 ~Impl();
cannam@233 56
cannam@233 57 bool initialise(size_t channels, size_t stepSize, size_t blockSize);
cannam@233 58
cannam@233 59 FeatureSet process(const float *const *inputBuffers, RealTime timestamp);
cannam@233 60 FeatureSet getRemainingFeatures();
cannam@233 61
cannam@233 62 void setSummarySegmentBoundaries(const SegmentBoundaries &);
cannam@233 63
cannam@233 64 FeatureList getSummaryForOutput(int output,
cannam@233 65 SummaryType type,
cannam@233 66 AveragingMethod avg);
cannam@233 67
cannam@233 68 FeatureSet getSummaryForAllOutputs(SummaryType type,
cannam@233 69 AveragingMethod avg);
cannam@233 70
cannam@233 71 protected:
cannam@233 72 Plugin *m_plugin;
cannam@233 73 float m_inputSampleRate;
cannam@233 74 size_t m_stepSize;
cannam@233 75 size_t m_blockSize;
cannam@233 76
cannam@233 77 SegmentBoundaries m_boundaries;
cannam@233 78
cannam@233 79 typedef std::vector<float> ValueList;
cannam@233 80
cannam@233 81 struct Result { // smaller than Feature
cannam@233 82 RealTime time;
cannam@233 83 RealTime duration;
cannam@233 84 ValueList values; // bin number -> value
cannam@233 85 };
cannam@233 86
cannam@233 87 typedef std::vector<Result> ResultList;
cannam@233 88
cannam@233 89 struct OutputAccumulator {
cannam@233 90 int bins;
cannam@233 91 ResultList results;
cannam@233 92 OutputAccumulator() : bins(0) { }
cannam@233 93 };
cannam@233 94
cannam@233 95 typedef std::map<int, OutputAccumulator> OutputAccumulatorMap;
cannam@233 96 OutputAccumulatorMap m_accumulators; // output number -> accumulator
cannam@233 97
cannam@233 98 typedef std::map<RealTime, OutputAccumulator> SegmentAccumulatorMap;
cannam@233 99 typedef std::map<int, SegmentAccumulatorMap> OutputSegmentAccumulatorMap;
cannam@233 100 OutputSegmentAccumulatorMap m_segmentedAccumulators; // output -> segmented
cannam@233 101
cannam@233 102 typedef std::map<int, RealTime> OutputTimestampMap;
cannam@233 103 OutputTimestampMap m_prevTimestamps; // output number -> timestamp
cannam@233 104 OutputTimestampMap m_prevDurations; // output number -> durations
cannam@233 105
cannam@233 106 struct OutputBinSummary {
cannam@233 107
cannam@233 108 int count;
cannam@233 109
cannam@233 110 // extents
cannam@233 111 double minimum;
cannam@233 112 double maximum;
cannam@233 113 double sum;
cannam@233 114
cannam@233 115 // sample-average results
cannam@233 116 double median;
cannam@233 117 double mode;
cannam@233 118 double variance;
cannam@233 119
cannam@233 120 // continuous-time average results
cannam@233 121 double median_c;
cannam@233 122 double mode_c;
cannam@233 123 double mean_c;
cannam@233 124 double variance_c;
cannam@233 125 };
cannam@233 126
cannam@233 127 typedef std::map<int, OutputBinSummary> OutputSummary;
cannam@233 128 typedef std::map<RealTime, OutputSummary> SummarySegmentMap;
cannam@233 129 typedef std::map<int, SummarySegmentMap> OutputSummarySegmentMap;
cannam@233 130
cannam@233 131 OutputSummarySegmentMap m_summaries;
cannam@233 132
cannam@233 133 bool m_reduced;
cannam@233 134 RealTime m_endTime;
cannam@233 135
cannam@233 136 void accumulate(const FeatureSet &fs, RealTime, bool final);
cannam@233 137 void accumulate(int output, const Feature &f, RealTime, bool final);
cannam@233 138 void accumulateFinalDurations();
cannam@233 139 void findSegmentBounds(RealTime t, RealTime &start, RealTime &end);
cannam@233 140 void segment();
cannam@233 141 void reduce();
cannam@233 142
cannam@233 143 std::string getSummaryLabel(SummaryType type, AveragingMethod avg);
cannam@233 144 };
cannam@233 145
cannam@233 146 static RealTime INVALID_DURATION(INT_MIN, INT_MIN);
cannam@233 147
cannam@233 148 PluginSummarisingAdapter::PluginSummarisingAdapter(Plugin *plugin) :
cannam@233 149 PluginWrapper(plugin)
cannam@233 150 {
cannam@233 151 m_impl = new Impl(plugin, m_inputSampleRate);
cannam@233 152 }
cannam@233 153
cannam@233 154 PluginSummarisingAdapter::~PluginSummarisingAdapter()
cannam@233 155 {
cannam@233 156 delete m_impl;
cannam@233 157 }
cannam@233 158
cannam@233 159 bool
cannam@233 160 PluginSummarisingAdapter::initialise(size_t channels,
cannam@233 161 size_t stepSize, size_t blockSize)
cannam@233 162 {
cannam@233 163 return
cannam@233 164 PluginWrapper::initialise(channels, stepSize, blockSize) &&
cannam@233 165 m_impl->initialise(channels, stepSize, blockSize);
cannam@233 166 }
cannam@233 167
cannam@233 168 Plugin::FeatureSet
cannam@233 169 PluginSummarisingAdapter::process(const float *const *inputBuffers, RealTime timestamp)
cannam@233 170 {
cannam@233 171 return m_impl->process(inputBuffers, timestamp);
cannam@233 172 }
cannam@233 173
cannam@233 174 Plugin::FeatureSet
cannam@233 175 PluginSummarisingAdapter::getRemainingFeatures()
cannam@233 176 {
cannam@233 177 return m_impl->getRemainingFeatures();
cannam@233 178 }
cannam@233 179
cannam@233 180 void
cannam@233 181 PluginSummarisingAdapter::setSummarySegmentBoundaries(const SegmentBoundaries &b)
cannam@233 182 {
cannam@233 183 m_impl->setSummarySegmentBoundaries(b);
cannam@233 184 }
cannam@233 185
cannam@233 186 Plugin::FeatureList
cannam@233 187 PluginSummarisingAdapter::getSummaryForOutput(int output,
cannam@233 188 SummaryType type,
cannam@233 189 AveragingMethod avg)
cannam@233 190 {
cannam@233 191 return m_impl->getSummaryForOutput(output, type, avg);
cannam@233 192 }
cannam@233 193
cannam@233 194 Plugin::FeatureSet
cannam@233 195 PluginSummarisingAdapter::getSummaryForAllOutputs(SummaryType type,
cannam@233 196 AveragingMethod avg)
cannam@233 197 {
cannam@233 198 return m_impl->getSummaryForAllOutputs(type, avg);
cannam@233 199 }
cannam@233 200
cannam@233 201 PluginSummarisingAdapter::Impl::Impl(Plugin *plugin, float inputSampleRate) :
cannam@233 202 m_plugin(plugin),
cannam@233 203 m_inputSampleRate(inputSampleRate),
cannam@233 204 m_reduced(false)
cannam@233 205 {
cannam@233 206 }
cannam@233 207
cannam@233 208 PluginSummarisingAdapter::Impl::~Impl()
cannam@233 209 {
cannam@233 210 }
cannam@233 211
cannam@233 212 bool
cannam@233 213 PluginSummarisingAdapter::Impl::initialise(size_t channels,
cannam@233 214 size_t stepSize, size_t blockSize)
cannam@233 215 {
cannam@233 216 m_stepSize = stepSize;
cannam@233 217 m_blockSize = blockSize;
cannam@233 218 return true;
cannam@233 219 }
cannam@233 220
cannam@233 221 Plugin::FeatureSet
cannam@233 222 PluginSummarisingAdapter::Impl::process(const float *const *inputBuffers,
cannam@233 223 RealTime timestamp)
cannam@233 224 {
cannam@233 225 if (m_reduced) {
cannam@233 226 std::cerr << "WARNING: Cannot call PluginSummarisingAdapter::process() or getRemainingFeatures() after one of the getSummary methods" << std::endl;
cannam@233 227 }
cannam@233 228 FeatureSet fs = m_plugin->process(inputBuffers, timestamp);
cannam@233 229 accumulate(fs, timestamp, false);
cannam@233 230 m_endTime = timestamp +
cannam@233 231 RealTime::frame2RealTime(m_stepSize, m_inputSampleRate);
cannam@233 232 return fs;
cannam@233 233 }
cannam@233 234
cannam@233 235 Plugin::FeatureSet
cannam@233 236 PluginSummarisingAdapter::Impl::getRemainingFeatures()
cannam@233 237 {
cannam@233 238 if (m_reduced) {
cannam@233 239 std::cerr << "WARNING: Cannot call PluginSummarisingAdapter::process() or getRemainingFeatures() after one of the getSummary methods" << std::endl;
cannam@233 240 }
cannam@233 241 FeatureSet fs = m_plugin->getRemainingFeatures();
cannam@233 242 accumulate(fs, m_endTime, true);
cannam@233 243 return fs;
cannam@233 244 }
cannam@233 245
cannam@233 246 void
cannam@233 247 PluginSummarisingAdapter::Impl::setSummarySegmentBoundaries(const SegmentBoundaries &b)
cannam@233 248 {
cannam@233 249 m_boundaries = b;
cannam@233 250 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 251 std::cerr << "PluginSummarisingAdapter::setSummarySegmentBoundaries: boundaries are:" << std::endl;
cannam@233 252 for (SegmentBoundaries::const_iterator i = m_boundaries.begin();
cannam@233 253 i != m_boundaries.end(); ++i) {
cannam@233 254 std::cerr << *i << " ";
cannam@233 255 }
cannam@233 256 std::cerr << std::endl;
cannam@233 257 #endif
cannam@233 258 }
cannam@233 259
cannam@233 260 Plugin::FeatureList
cannam@233 261 PluginSummarisingAdapter::Impl::getSummaryForOutput(int output,
cannam@233 262 SummaryType type,
cannam@233 263 AveragingMethod avg)
cannam@233 264 {
cannam@233 265 if (!m_reduced) {
cannam@233 266 accumulateFinalDurations();
cannam@233 267 segment();
cannam@233 268 reduce();
cannam@233 269 m_reduced = true;
cannam@233 270 }
cannam@233 271
cannam@233 272 bool continuous = (avg == ContinuousTimeAverage);
cannam@233 273
cannam@233 274 FeatureList fl;
cannam@233 275 for (SummarySegmentMap::const_iterator i = m_summaries[output].begin();
cannam@233 276 i != m_summaries[output].end(); ++i) {
cannam@233 277
cannam@233 278 Feature f;
cannam@233 279
cannam@233 280 f.hasTimestamp = true;
cannam@233 281 f.timestamp = i->first;
cannam@233 282
cannam@233 283 f.hasDuration = true;
cannam@233 284 SummarySegmentMap::const_iterator ii = i;
cannam@233 285 if (++ii == m_summaries[output].end()) {
cannam@233 286 f.duration = m_endTime - f.timestamp;
cannam@233 287 } else {
cannam@233 288 f.duration = ii->first - f.timestamp;
cannam@233 289 }
cannam@233 290
cannam@233 291 f.label = getSummaryLabel(type, avg);
cannam@233 292
cannam@233 293 for (OutputSummary::const_iterator j = i->second.begin();
cannam@233 294 j != i->second.end(); ++j) {
cannam@233 295
cannam@233 296 // these will be ordered by bin number, and no bin numbers
cannam@233 297 // will be missing except at the end (because of the way
cannam@233 298 // the accumulators were initially filled in accumulate())
cannam@233 299
cannam@233 300 const OutputBinSummary &summary = j->second;
cannam@233 301 double result = 0.f;
cannam@233 302
cannam@233 303 switch (type) {
cannam@233 304
cannam@233 305 case Minimum:
cannam@233 306 result = summary.minimum;
cannam@233 307 break;
cannam@233 308
cannam@233 309 case Maximum:
cannam@233 310 result = summary.maximum;
cannam@233 311 break;
cannam@233 312
cannam@233 313 case Mean:
cannam@233 314 if (continuous) {
cannam@233 315 result = summary.mean_c;
cannam@233 316 } else if (summary.count) {
cannam@233 317 result = summary.sum / summary.count;
cannam@233 318 }
cannam@233 319 break;
cannam@233 320
cannam@233 321 case Median:
cannam@233 322 if (continuous) result = summary.median_c;
cannam@233 323 else result = summary.median;
cannam@233 324 break;
cannam@233 325
cannam@233 326 case Mode:
cannam@233 327 if (continuous) result = summary.mode_c;
cannam@233 328 else result = summary.mode;
cannam@233 329 break;
cannam@233 330
cannam@233 331 case Sum:
cannam@233 332 result = summary.sum;
cannam@233 333 break;
cannam@233 334
cannam@233 335 case Variance:
cannam@233 336 if (continuous) result = summary.variance_c;
cannam@233 337 else result = summary.variance;
cannam@233 338 break;
cannam@233 339
cannam@233 340 case StandardDeviation:
cannam@233 341 if (continuous) result = sqrtf(summary.variance_c);
cannam@233 342 else result = sqrtf(summary.variance);
cannam@233 343 break;
cannam@233 344
cannam@233 345 case Count:
cannam@233 346 result = summary.count;
cannam@233 347 break;
cannam@233 348
cannam@233 349 case UnknownSummaryType:
cannam@233 350 break;
cannam@233 351
cannam@233 352 default:
cannam@233 353 break;
cannam@233 354 }
cannam@233 355
cannam@233 356 f.values.push_back(result);
cannam@233 357 }
cannam@233 358
cannam@233 359 fl.push_back(f);
cannam@233 360 }
cannam@233 361 return fl;
cannam@233 362 }
cannam@233 363
cannam@233 364 Plugin::FeatureSet
cannam@233 365 PluginSummarisingAdapter::Impl::getSummaryForAllOutputs(SummaryType type,
cannam@233 366 AveragingMethod avg)
cannam@233 367 {
cannam@233 368 if (!m_reduced) {
cannam@233 369 accumulateFinalDurations();
cannam@233 370 segment();
cannam@233 371 reduce();
cannam@233 372 m_reduced = true;
cannam@233 373 }
cannam@233 374
cannam@233 375 FeatureSet fs;
cannam@233 376 for (OutputSummarySegmentMap::const_iterator i = m_summaries.begin();
cannam@233 377 i != m_summaries.end(); ++i) {
cannam@233 378 fs[i->first] = getSummaryForOutput(i->first, type, avg);
cannam@233 379 }
cannam@233 380 return fs;
cannam@233 381 }
cannam@233 382
cannam@233 383 void
cannam@233 384 PluginSummarisingAdapter::Impl::accumulate(const FeatureSet &fs,
cannam@233 385 RealTime timestamp,
cannam@233 386 bool final)
cannam@233 387 {
cannam@233 388 for (FeatureSet::const_iterator i = fs.begin(); i != fs.end(); ++i) {
cannam@233 389 for (FeatureList::const_iterator j = i->second.begin();
cannam@233 390 j != i->second.end(); ++j) {
cannam@233 391 if (j->hasTimestamp) {
cannam@233 392 accumulate(i->first, *j, j->timestamp, final);
cannam@233 393 } else {
cannam@233 394 //!!! is this correct?
cannam@233 395 accumulate(i->first, *j, timestamp, final);
cannam@233 396 }
cannam@233 397 }
cannam@233 398 }
cannam@233 399 }
cannam@233 400
cannam@233 401 std::string
cannam@233 402 PluginSummarisingAdapter::Impl::getSummaryLabel(SummaryType type,
cannam@233 403 AveragingMethod avg)
cannam@233 404 {
cannam@233 405 std::string label;
cannam@233 406 std::string avglabel;
cannam@233 407
cannam@233 408 if (avg == SampleAverage) avglabel = ", sample average";
cannam@233 409 else avglabel = ", continuous-time average";
cannam@233 410
cannam@233 411 switch (type) {
cannam@233 412 case Minimum: label = "(minimum value)"; break;
cannam@233 413 case Maximum: label = "(maximum value)"; break;
cannam@233 414 case Mean: label = "(mean value" + avglabel + ")"; break;
cannam@233 415 case Median: label = "(median value" + avglabel + ")"; break;
cannam@233 416 case Mode: label = "(modal value" + avglabel + ")"; break;
cannam@233 417 case Sum: label = "(sum)"; break;
cannam@233 418 case Variance: label = "(variance" + avglabel + ")"; break;
cannam@233 419 case StandardDeviation: label = "(standard deviation" + avglabel + ")"; break;
cannam@233 420 case Count: label = "(count)"; break;
cannam@233 421 case UnknownSummaryType: label = "(unknown summary)"; break;
cannam@233 422 }
cannam@233 423
cannam@233 424 return label;
cannam@233 425 }
cannam@233 426
cannam@233 427 void
cannam@233 428 PluginSummarisingAdapter::Impl::accumulate(int output,
cannam@233 429 const Feature &f,
cannam@233 430 RealTime timestamp,
cannam@233 431 bool final)
cannam@233 432 {
cannam@248 433 // What should happen if a feature's duration spans a segment
cannam@248 434 // boundary? I think we probably want to chop it, and pretend
cannam@248 435 // that it appears in both. A very long feature (e.g. key, if the
cannam@248 436 // whole audio is in a single key) might span many or all
cannam@248 437 // segments, and we want that to be reflected in the results
cannam@248 438 // (e.g. it is the modal key in all of those segments, not just
cannam@248 439 // the first). This is actually quite complicated to do.
cannam@233 440
cannam@248 441 // If features spanning a boundary should be chopped, then we need
cannam@248 442 // to have per-segment accumulators (and the feature value goes
cannam@248 443 // into both -- with a separate phase to split the accumulator up
cannam@248 444 // into segments).
cannam@233 445
cannam@233 446 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 447 std::cerr << "output " << output << ": timestamp " << timestamp << ", prev timestamp " << m_prevTimestamps[output] << ", final " << final << std::endl;
cannam@233 448 #endif
cannam@233 449
cannam@233 450 // At each process step, accumulate() is called once for each
cannam@233 451 // feature on each output within that process's returned feature
cannam@233 452 // list, and with the timestamp passed in being that of the start
cannam@233 453 // of the process block.
cannam@233 454
cannam@233 455 // At the end (in getRemainingFeatures), accumulate() is called
cannam@233 456 // once for each feature on each output within the feature list
cannam@233 457 // returned by getRemainingFeatures, and with the timestamp being
cannam@233 458 // the same as the last process block and final set to true.
cannam@233 459
cannam@233 460 // (What if getRemainingFeatures doesn't return any features? We
cannam@233 461 // still need to ensure that the final duration is written. Need
cannam@233 462 // a separate function to close the durations.)
cannam@233 463
cannam@233 464 // At each call, we pull out the value for the feature and stuff
cannam@233 465 // it into the accumulator's appropriate values array; and we
cannam@233 466 // calculate the duration for the _previous_ feature, or pull it
cannam@233 467 // from the prevDurations array if the previous feature had a
cannam@233 468 // duration in its structure, and stuff that into the
cannam@233 469 // accumulator's appropriate durations array.
cannam@233 470
cannam@233 471 if (m_prevDurations.find(output) != m_prevDurations.end()) {
cannam@233 472
cannam@233 473 // Not the first time accumulate has been called for this
cannam@233 474 // output -- there has been a previous feature
cannam@233 475
cannam@233 476 RealTime prevDuration;
cannam@233 477
cannam@233 478 // Note that m_prevDurations[output] only contains the
cannam@233 479 // duration field that was contained in the previous feature.
cannam@233 480 // If it didn't have an explicit duration,
cannam@233 481 // m_prevDurations[output] should be INVALID_DURATION and we
cannam@233 482 // will have to calculate the duration from the previous and
cannam@233 483 // current timestamps.
cannam@233 484
cannam@233 485 if (m_prevDurations[output] != INVALID_DURATION) {
cannam@233 486 prevDuration = m_prevDurations[output];
cannam@233 487 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 488 std::cerr << "Previous duration from previous feature: " << prevDuration << std::endl;
cannam@233 489 #endif
cannam@233 490 } else {
cannam@233 491 prevDuration = timestamp - m_prevTimestamps[output];
cannam@233 492 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 493 std::cerr << "Previous duration from diff: " << timestamp << " - "
cannam@233 494 << m_prevTimestamps[output] << std::endl;
cannam@233 495 #endif
cannam@233 496 }
cannam@233 497
cannam@233 498 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 499 std::cerr << "output " << output << ": ";
cannam@233 500 std::cerr << "Pushing previous duration as " << prevDuration << std::endl;
cannam@233 501 #endif
cannam@233 502
cannam@233 503 m_accumulators[output].results
cannam@233 504 [m_accumulators[output].results.size() - 1]
cannam@233 505 .duration = prevDuration;
cannam@233 506 }
cannam@233 507
cannam@233 508 if (f.hasDuration) m_prevDurations[output] = f.duration;
cannam@233 509 else m_prevDurations[output] = INVALID_DURATION;
cannam@233 510
cannam@233 511 m_prevTimestamps[output] = timestamp;
cannam@233 512
cannam@233 513 if (f.hasDuration) {
cannam@233 514 RealTime et = timestamp;
cannam@233 515 et = et + f.duration;
cannam@233 516 if (et > m_endTime) m_endTime = et;
cannam@233 517 }
cannam@233 518
cannam@233 519 Result result;
cannam@233 520 result.time = timestamp;
cannam@233 521 result.duration = INVALID_DURATION;
cannam@233 522
cannam@233 523 if (f.values.size() > m_accumulators[output].bins) {
cannam@233 524 m_accumulators[output].bins = f.values.size();
cannam@233 525 }
cannam@233 526
cannam@233 527 for (int i = 0; i < int(f.values.size()); ++i) {
cannam@233 528 result.values.push_back(f.values[i]);
cannam@233 529 }
cannam@233 530
cannam@233 531 m_accumulators[output].results.push_back(result);
cannam@233 532 }
cannam@233 533
cannam@233 534 void
cannam@233 535 PluginSummarisingAdapter::Impl::accumulateFinalDurations()
cannam@233 536 {
cannam@233 537 for (OutputTimestampMap::iterator i = m_prevTimestamps.begin();
cannam@233 538 i != m_prevTimestamps.end(); ++i) {
cannam@233 539
cannam@233 540 int output = i->first;
cannam@233 541
cannam@233 542 int acount = m_accumulators[output].results.size();
cannam@233 543
cannam@233 544 if (acount == 0) continue;
cannam@233 545
cannam@233 546 RealTime prevTimestamp = i->second;
cannam@233 547
cannam@233 548 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 549 std::cerr << "output " << output << ": ";
cannam@233 550 #endif
cannam@233 551
cannam@233 552 if (m_prevDurations.find(output) != m_prevDurations.end() &&
cannam@233 553 m_prevDurations[output] != INVALID_DURATION) {
cannam@233 554
cannam@233 555 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 556 std::cerr << "Pushing final duration from feature as " << m_prevDurations[output] << std::endl;
cannam@233 557 #endif
cannam@233 558
cannam@233 559 m_accumulators[output].results[acount - 1].duration =
cannam@233 560 m_prevDurations[output];
cannam@233 561
cannam@233 562 } else {
cannam@233 563
cannam@233 564 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 565 std::cerr << "Pushing final duration from diff as " << m_endTime << " - " << m_prevTimestamps[output] << std::endl;
cannam@233 566 #endif
cannam@233 567
cannam@233 568 m_accumulators[output].results[acount - 1].duration =
cannam@233 569 m_endTime - m_prevTimestamps[output];
cannam@233 570 }
cannam@233 571
cannam@233 572 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 573 std::cerr << "so duration for result no " << acount-1 << " is "
cannam@233 574 << m_accumulators[output].results[acount-1].duration
cannam@233 575 << std::endl;
cannam@233 576 #endif
cannam@233 577 }
cannam@233 578 }
cannam@233 579
cannam@233 580 void
cannam@233 581 PluginSummarisingAdapter::Impl::findSegmentBounds(RealTime t,
cannam@233 582 RealTime &start,
cannam@233 583 RealTime &end)
cannam@233 584 {
cannam@233 585 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER_SEGMENT
cannam@233 586 std::cerr << "findSegmentBounds: t = " << t << std::endl;
cannam@233 587 #endif
cannam@233 588
cannam@233 589 SegmentBoundaries::const_iterator i = std::upper_bound
cannam@233 590 (m_boundaries.begin(), m_boundaries.end(), t);
cannam@233 591
cannam@233 592 start = RealTime::zeroTime;
cannam@233 593 end = m_endTime;
cannam@233 594
cannam@233 595 if (i != m_boundaries.end()) {
cannam@233 596 end = *i;
cannam@233 597 }
cannam@233 598
cannam@233 599 if (i != m_boundaries.begin()) {
cannam@233 600 start = *--i;
cannam@233 601 }
cannam@233 602
cannam@233 603 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER_SEGMENT
cannam@233 604 std::cerr << "findSegmentBounds: " << t << " is in segment " << start << " -> " << end << std::endl;
cannam@233 605 #endif
cannam@233 606 }
cannam@233 607
cannam@233 608 void
cannam@233 609 PluginSummarisingAdapter::Impl::segment()
cannam@233 610 {
cannam@233 611 SegmentBoundaries::iterator boundaryitr = m_boundaries.begin();
cannam@233 612 RealTime segmentStart = RealTime::zeroTime;
cannam@233 613
cannam@233 614 for (OutputAccumulatorMap::iterator i = m_accumulators.begin();
cannam@233 615 i != m_accumulators.end(); ++i) {
cannam@233 616
cannam@233 617 int output = i->first;
cannam@233 618 OutputAccumulator &source = i->second;
cannam@233 619
cannam@233 620 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER_SEGMENT
cannam@233 621 std::cerr << "segment: total results for output " << output << " = "
cannam@233 622 << source.results.size() << std::endl;
cannam@233 623 #endif
cannam@233 624
cannam@248 625 // This is basically nonsense if the results have no values
cannam@248 626 // (i.e. their times and counts are the only things of
cannam@248 627 // interest)... but perhaps it's the user's problem if they
cannam@248 628 // ask for segmentation (or any summary at all) in that case
cannam@233 629
cannam@233 630 for (int n = 0; n < source.results.size(); ++n) {
cannam@233 631
cannam@233 632 // This result spans source.results[n].time to
cannam@233 633 // source.results[n].time + source.results[n].duration.
cannam@233 634 // We need to dispose it into segments appropriately
cannam@233 635
cannam@233 636 RealTime resultStart = source.results[n].time;
cannam@233 637 RealTime resultEnd = resultStart + source.results[n].duration;
cannam@233 638
cannam@233 639 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER_SEGMENT
cannam@233 640 std::cerr << "output: " << output << ", result start = " << resultStart << ", end = " << resultEnd << std::endl;
cannam@233 641 #endif
cannam@233 642
cannam@233 643 RealTime segmentStart = RealTime::zeroTime;
cannam@233 644 RealTime segmentEnd = resultEnd - RealTime(1, 0);
cannam@233 645
cannam@233 646 while (segmentEnd < resultEnd) {
cannam@233 647
cannam@233 648 findSegmentBounds(resultStart, segmentStart, segmentEnd);
cannam@233 649
cannam@233 650 RealTime chunkStart = resultStart;
cannam@233 651 if (chunkStart < segmentStart) chunkStart = segmentStart;
cannam@233 652
cannam@233 653 RealTime chunkEnd = resultEnd;
cannam@233 654 if (chunkEnd > segmentEnd) chunkEnd = segmentEnd;
cannam@233 655
cannam@233 656 m_segmentedAccumulators[output][segmentStart].bins = source.bins;
cannam@233 657
cannam@233 658 Result chunk;
cannam@233 659 chunk.time = chunkStart;
cannam@233 660 chunk.duration = chunkEnd - chunkStart;
cannam@233 661 chunk.values = source.results[n].values;
cannam@233 662
cannam@233 663 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER_SEGMENT
cannam@233 664 std::cerr << "chunk for segment " << segmentStart << ": from " << chunk.time << ", duration " << chunk.duration << std::endl;
cannam@233 665 #endif
cannam@233 666
cannam@233 667 m_segmentedAccumulators[output][segmentStart].results
cannam@233 668 .push_back(chunk);
cannam@233 669
cannam@233 670 resultStart = chunkEnd;
cannam@233 671 }
cannam@233 672 }
cannam@233 673 }
cannam@233 674 }
cannam@233 675
cannam@233 676 struct ValueDurationFloatPair
cannam@233 677 {
cannam@233 678 float value;
cannam@233 679 float duration;
cannam@233 680
cannam@233 681 ValueDurationFloatPair() : value(0), duration(0) { }
cannam@233 682 ValueDurationFloatPair(float v, float d) : value(v), duration(d) { }
cannam@233 683 ValueDurationFloatPair &operator=(const ValueDurationFloatPair &p) {
cannam@233 684 value = p.value;
cannam@233 685 duration = p.duration;
cannam@233 686 return *this;
cannam@233 687 }
cannam@233 688 bool operator<(const ValueDurationFloatPair &p) const {
cannam@233 689 return value < p.value;
cannam@233 690 }
cannam@233 691 };
cannam@233 692
cannam@233 693 static double toSec(const RealTime &r)
cannam@233 694 {
cannam@233 695 return r.sec + double(r.nsec) / 1000000000.0;
cannam@233 696 }
cannam@233 697
cannam@233 698 void
cannam@233 699 PluginSummarisingAdapter::Impl::reduce()
cannam@233 700 {
cannam@233 701 for (OutputSegmentAccumulatorMap::iterator i =
cannam@233 702 m_segmentedAccumulators.begin();
cannam@233 703 i != m_segmentedAccumulators.end(); ++i) {
cannam@233 704
cannam@233 705 int output = i->first;
cannam@233 706 SegmentAccumulatorMap &segments = i->second;
cannam@233 707
cannam@233 708 for (SegmentAccumulatorMap::iterator j = segments.begin();
cannam@233 709 j != segments.end(); ++j) {
cannam@233 710
cannam@233 711 RealTime segmentStart = j->first;
cannam@233 712 OutputAccumulator &accumulator = j->second;
cannam@233 713
cannam@233 714 int sz = accumulator.results.size();
cannam@233 715
cannam@233 716 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 717 std::cerr << "reduce: segment starting at " << segmentStart
cannam@233 718 << " on output " << output << " has " << sz << " result(s)" << std::endl;
cannam@233 719 #endif
cannam@233 720
cannam@233 721 double totalDuration = 0.0;
cannam@233 722 //!!! is this right?
cannam@233 723 if (sz > 0) {
cannam@233 724 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 725 std::cerr << "last time = " << accumulator.results[sz-1].time
cannam@233 726 << ", duration = " << accumulator.results[sz-1].duration
cannam@233 727 << " (step = " << m_stepSize << ", block = " << m_blockSize << ")"
cannam@233 728 << std::endl;
cannam@233 729 #endif
cannam@233 730 totalDuration = toSec((accumulator.results[sz-1].time +
cannam@233 731 accumulator.results[sz-1].duration) -
cannam@233 732 segmentStart);
cannam@233 733 }
cannam@233 734
cannam@233 735 for (int bin = 0; bin < accumulator.bins; ++bin) {
cannam@233 736
cannam@233 737 // work on all values over time for a single bin
cannam@233 738
cannam@233 739 OutputBinSummary summary;
cannam@233 740
cannam@233 741 summary.count = sz;
cannam@233 742
cannam@233 743 summary.minimum = 0.f;
cannam@233 744 summary.maximum = 0.f;
cannam@233 745
cannam@233 746 summary.median = 0.f;
cannam@233 747 summary.mode = 0.f;
cannam@233 748 summary.sum = 0.f;
cannam@233 749 summary.variance = 0.f;
cannam@233 750
cannam@233 751 summary.median_c = 0.f;
cannam@233 752 summary.mode_c = 0.f;
cannam@233 753 summary.mean_c = 0.f;
cannam@233 754 summary.variance_c = 0.f;
cannam@233 755
cannam@233 756 if (sz == 0) continue;
cannam@233 757
cannam@233 758 std::vector<ValueDurationFloatPair> valvec;
cannam@233 759
cannam@233 760 for (int k = 0; k < sz; ++k) {
cannam@233 761 while (accumulator.results[k].values.size() <
cannam@233 762 accumulator.bins) {
cannam@233 763 accumulator.results[k].values.push_back(0.f);
cannam@233 764 }
cannam@233 765 }
cannam@233 766
cannam@233 767 for (int k = 0; k < sz; ++k) {
cannam@233 768 float value = accumulator.results[k].values[bin];
cannam@233 769 valvec.push_back(ValueDurationFloatPair
cannam@233 770 (value,
cannam@233 771 toSec(accumulator.results[k].duration)));
cannam@233 772 }
cannam@233 773
cannam@233 774 std::sort(valvec.begin(), valvec.end());
cannam@233 775
cannam@233 776 summary.minimum = valvec[0].value;
cannam@233 777 summary.maximum = valvec[sz-1].value;
cannam@233 778
cannam@233 779 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 780 std::cerr << "total duration = " << totalDuration << std::endl;
cannam@233 781 #endif
cannam@233 782
cannam@233 783 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 784 /*
cannam@233 785 std::cerr << "value vector for medians:" << std::endl;
cannam@233 786 for (int k = 0; k < sz; ++k) {
cannam@233 787 std::cerr << "(" << valvec[k].value << "," << valvec[k].duration << ") ";
cannam@233 788 }
cannam@233 789 std::cerr << std::endl;
cannam@233 790 */
cannam@233 791 #endif
cannam@233 792
cannam@233 793 if (sz % 2 == 1) {
cannam@233 794 summary.median = valvec[sz/2].value;
cannam@233 795 } else {
cannam@233 796 summary.median = (valvec[sz/2].value + valvec[sz/2 + 1].value) / 2;
cannam@233 797 }
cannam@233 798
cannam@233 799 double duracc = 0.0;
cannam@233 800 summary.median_c = valvec[sz-1].value;
cannam@233 801
cannam@233 802 for (int k = 0; k < sz; ++k) {
cannam@233 803 duracc += valvec[k].duration;
cannam@233 804 if (duracc > totalDuration/2) {
cannam@233 805 summary.median_c = valvec[k].value;
cannam@233 806 break;
cannam@233 807 }
cannam@233 808 }
cannam@233 809
cannam@233 810 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 811 std::cerr << "median_c = " << summary.median_c << std::endl;
cannam@233 812 std::cerr << "median = " << summary.median << std::endl;
cannam@233 813 #endif
cannam@233 814
cannam@233 815 std::map<float, int> distribution;
cannam@233 816
cannam@233 817 for (int k = 0; k < sz; ++k) {
cannam@233 818 summary.sum += accumulator.results[k].values[bin];
cannam@233 819 distribution[accumulator.results[k].values[bin]] += 1;
cannam@233 820 }
cannam@233 821
cannam@233 822 int md = 0;
cannam@233 823
cannam@233 824 for (std::map<float, int>::iterator di = distribution.begin();
cannam@233 825 di != distribution.end(); ++di) {
cannam@233 826 if (di->second > md) {
cannam@233 827 md = di->second;
cannam@233 828 summary.mode = di->first;
cannam@233 829 }
cannam@233 830 }
cannam@233 831
cannam@233 832 distribution.clear();
cannam@233 833
cannam@233 834 std::map<float, double> distribution_c;
cannam@233 835
cannam@233 836 for (int k = 0; k < sz; ++k) {
cannam@233 837 distribution_c[accumulator.results[k].values[bin]]
cannam@233 838 += toSec(accumulator.results[k].duration);
cannam@233 839 }
cannam@233 840
cannam@233 841 double mrd = 0.0;
cannam@233 842
cannam@233 843 for (std::map<float, double>::iterator di = distribution_c.begin();
cannam@233 844 di != distribution_c.end(); ++di) {
cannam@233 845 if (di->second > mrd) {
cannam@233 846 mrd = di->second;
cannam@233 847 summary.mode_c = di->first;
cannam@233 848 }
cannam@233 849 }
cannam@233 850
cannam@233 851 distribution_c.clear();
cannam@233 852
cannam@233 853 if (totalDuration > 0.0) {
cannam@233 854
cannam@233 855 double sum_c = 0.0;
cannam@233 856
cannam@233 857 for (int k = 0; k < sz; ++k) {
cannam@233 858 double value = accumulator.results[k].values[bin]
cannam@233 859 * toSec(accumulator.results[k].duration);
cannam@233 860 sum_c += value;
cannam@233 861 }
cannam@233 862
cannam@233 863 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 864 std::cerr << "mean_c = " << sum_c << " / " << totalDuration << " = "
cannam@233 865 << sum_c / totalDuration << " (sz = " << sz << ")" << std::endl;
cannam@233 866 #endif
cannam@233 867
cannam@233 868 summary.mean_c = sum_c / totalDuration;
cannam@233 869
cannam@233 870 for (int k = 0; k < sz; ++k) {
cannam@233 871 double value = accumulator.results[k].values[bin];
cannam@233 872 // * toSec(accumulator.results[k].duration);
cannam@233 873 summary.variance_c +=
cannam@233 874 (value - summary.mean_c) * (value - summary.mean_c)
cannam@233 875 * toSec(accumulator.results[k].duration);
cannam@233 876 }
cannam@233 877
cannam@233 878 // summary.variance_c /= summary.count;
cannam@233 879 summary.variance_c /= totalDuration;
cannam@233 880 }
cannam@233 881
cannam@233 882 double mean = summary.sum / summary.count;
cannam@233 883
cannam@233 884 #ifdef DEBUG_PLUGIN_SUMMARISING_ADAPTER
cannam@233 885 std::cerr << "mean = " << summary.sum << " / " << summary.count << " = "
cannam@233 886 << summary.sum / summary.count << std::endl;
cannam@233 887 #endif
cannam@233 888
cannam@233 889 for (int k = 0; k < sz; ++k) {
cannam@233 890 float value = accumulator.results[k].values[bin];
cannam@233 891 summary.variance += (value - mean) * (value - mean);
cannam@233 892 }
cannam@233 893 summary.variance /= summary.count;
cannam@233 894
cannam@233 895 m_summaries[output][segmentStart][bin] = summary;
cannam@233 896 }
cannam@233 897 }
cannam@233 898 }
cannam@233 899
cannam@233 900 m_segmentedAccumulators.clear();
cannam@233 901 m_accumulators.clear();
cannam@233 902 }
cannam@233 903
cannam@233 904
cannam@233 905 }
cannam@233 906
cannam@233 907 }
cannam@233 908