annotate vamp-sdk/hostext/PluginSummarisingAdapter.cpp @ 186:8311695c13f9

* segmentation for results being summarised -- I need to come up with a good way to test this before going any further!
author cannam
date Thu, 11 Sep 2008 16:11:34 +0000
parents 701505ac170c
children ed8aa954e72f
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@184 41 #include <climits>
cannam@174 42
cannam@173 43 namespace Vamp {
cannam@173 44
cannam@173 45 namespace HostExt {
cannam@173 46
cannam@173 47 class PluginSummarisingAdapter::Impl
cannam@173 48 {
cannam@173 49 public:
cannam@173 50 Impl(Plugin *plugin, float inputSampleRate);
cannam@173 51 ~Impl();
cannam@173 52
cannam@173 53 FeatureSet process(const float *const *inputBuffers, RealTime timestamp);
cannam@173 54 FeatureSet getRemainingFeatures();
cannam@173 55
cannam@173 56 void setSummarySegmentBoundaries(const SegmentBoundaries &);
cannam@173 57
cannam@180 58 FeatureList getSummaryForOutput(int output,
cannam@180 59 SummaryType type,
cannam@180 60 AveragingMethod avg);
cannam@180 61
cannam@180 62 FeatureSet getSummaryForAllOutputs(SummaryType type,
cannam@180 63 AveragingMethod avg);
cannam@173 64
cannam@173 65 protected:
cannam@174 66 Plugin *m_plugin;
cannam@181 67 float m_inputSampleRate;
cannam@174 68
cannam@173 69 SegmentBoundaries m_boundaries;
cannam@185 70
cannam@174 71 typedef std::vector<float> ValueList;
cannam@185 72
cannam@185 73 struct Result { // smaller than Feature
cannam@185 74 RealTime time;
cannam@185 75 RealTime duration;
cannam@185 76 ValueList values; // bin number -> value
cannam@185 77 };
cannam@185 78
cannam@185 79 typedef std::vector<Result> ResultList;
cannam@185 80
cannam@174 81 struct OutputAccumulator {
cannam@185 82 int bins;
cannam@185 83 ResultList results;
cannam@185 84 OutputAccumulator() : bins(0) { }
cannam@174 85 };
cannam@174 86
cannam@174 87 typedef std::map<int, OutputAccumulator> OutputAccumulatorMap;
cannam@180 88 OutputAccumulatorMap m_accumulators; // output number -> accumulator
cannam@180 89
cannam@185 90 typedef std::map<RealTime, OutputAccumulator> SegmentAccumulatorMap;
cannam@185 91 typedef std::map<int, SegmentAccumulatorMap> OutputSegmentAccumulatorMap;
cannam@185 92 OutputSegmentAccumulatorMap m_segmentedAccumulators;
cannam@185 93
cannam@180 94 typedef std::map<int, RealTime> OutputTimestampMap;
cannam@180 95 OutputTimestampMap m_prevTimestamps; // output number -> timestamp
cannam@183 96 OutputTimestampMap m_prevDurations; // output number -> durations
cannam@174 97
cannam@174 98 struct OutputBinSummary {
cannam@180 99
cannam@180 100 int count;
cannam@180 101
cannam@180 102 // extents
cannam@174 103 float minimum;
cannam@174 104 float maximum;
cannam@180 105 float sum;
cannam@180 106
cannam@180 107 // sample-average results
cannam@174 108 float median;
cannam@174 109 float mode;
cannam@174 110 float variance;
cannam@180 111
cannam@180 112 // continuous-time average results
cannam@180 113 float median_c;
cannam@180 114 float mode_c;
cannam@180 115 float mean_c;
cannam@180 116 float variance_c;
cannam@174 117 };
cannam@174 118
cannam@174 119 typedef std::map<int, OutputBinSummary> OutputSummary;
cannam@174 120 typedef std::map<RealTime, OutputSummary> SummarySegmentMap;
cannam@174 121 typedef std::map<int, SummarySegmentMap> OutputSummarySegmentMap;
cannam@174 122
cannam@174 123 OutputSummarySegmentMap m_summaries;
cannam@174 124
cannam@183 125 bool m_reduced;
cannam@174 126 RealTime m_lastTimestamp;
cannam@174 127
cannam@180 128 void accumulate(const FeatureSet &fs, RealTime, bool final);
cannam@180 129 void accumulate(int output, const Feature &f, RealTime, bool final);
cannam@184 130 void accumulateFinalDurations();
cannam@186 131 void findSegmentBounds(RealTime t, RealTime &start, RealTime &end);
cannam@185 132 void segment();
cannam@174 133 void reduce();
cannam@173 134 };
cannam@184 135
cannam@184 136 static RealTime INVALID_DURATION(INT_MIN, INT_MIN);
cannam@173 137
cannam@173 138 PluginSummarisingAdapter::PluginSummarisingAdapter(Plugin *plugin) :
cannam@173 139 PluginWrapper(plugin)
cannam@173 140 {
cannam@173 141 m_impl = new Impl(plugin, m_inputSampleRate);
cannam@173 142 }
cannam@173 143
cannam@173 144 PluginSummarisingAdapter::~PluginSummarisingAdapter()
cannam@173 145 {
cannam@173 146 delete m_impl;
cannam@173 147 }
cannam@173 148
cannam@173 149 Plugin::FeatureSet
cannam@173 150 PluginSummarisingAdapter::process(const float *const *inputBuffers, RealTime timestamp)
cannam@173 151 {
cannam@173 152 return m_impl->process(inputBuffers, timestamp);
cannam@173 153 }
cannam@173 154
cannam@174 155 Plugin::FeatureSet
cannam@174 156 PluginSummarisingAdapter::getRemainingFeatures()
cannam@174 157 {
cannam@174 158 return m_impl->getRemainingFeatures();
cannam@174 159 }
cannam@174 160
cannam@175 161 Plugin::FeatureList
cannam@180 162 PluginSummarisingAdapter::getSummaryForOutput(int output,
cannam@180 163 SummaryType type,
cannam@180 164 AveragingMethod avg)
cannam@175 165 {
cannam@180 166 return m_impl->getSummaryForOutput(output, type, avg);
cannam@176 167 }
cannam@176 168
cannam@176 169 Plugin::FeatureSet
cannam@180 170 PluginSummarisingAdapter::getSummaryForAllOutputs(SummaryType type,
cannam@180 171 AveragingMethod avg)
cannam@176 172 {
cannam@180 173 return m_impl->getSummaryForAllOutputs(type, avg);
cannam@175 174 }
cannam@173 175
cannam@173 176 PluginSummarisingAdapter::Impl::Impl(Plugin *plugin, float inputSampleRate) :
cannam@181 177 m_plugin(plugin),
cannam@183 178 m_inputSampleRate(inputSampleRate),
cannam@183 179 m_reduced(false)
cannam@173 180 {
cannam@173 181 }
cannam@173 182
cannam@173 183 PluginSummarisingAdapter::Impl::~Impl()
cannam@173 184 {
cannam@173 185 }
cannam@173 186
cannam@174 187 Plugin::FeatureSet
cannam@174 188 PluginSummarisingAdapter::Impl::process(const float *const *inputBuffers, RealTime timestamp)
cannam@174 189 {
cannam@183 190 if (m_reduced) {
cannam@183 191 std::cerr << "WARNING: Cannot call PluginSummarisingAdapter::process() or getRemainingFeatures() after one of the getSummary methods" << std::endl;
cannam@183 192 }
cannam@174 193 FeatureSet fs = m_plugin->process(inputBuffers, timestamp);
cannam@180 194 accumulate(fs, timestamp, false);
cannam@185 195 //!!! should really be "timestamp plus step size"
cannam@174 196 m_lastTimestamp = timestamp;
cannam@174 197 return fs;
cannam@174 198 }
cannam@174 199
cannam@174 200 Plugin::FeatureSet
cannam@174 201 PluginSummarisingAdapter::Impl::getRemainingFeatures()
cannam@174 202 {
cannam@183 203 if (m_reduced) {
cannam@183 204 std::cerr << "WARNING: Cannot call PluginSummarisingAdapter::process() or getRemainingFeatures() after one of the getSummary methods" << std::endl;
cannam@183 205 }
cannam@174 206 FeatureSet fs = m_plugin->getRemainingFeatures();
cannam@180 207 accumulate(fs, m_lastTimestamp, true);
cannam@174 208 return fs;
cannam@174 209 }
cannam@174 210
cannam@175 211 Plugin::FeatureList
cannam@180 212 PluginSummarisingAdapter::Impl::getSummaryForOutput(int output,
cannam@180 213 SummaryType type,
cannam@180 214 AveragingMethod avg)
cannam@175 215 {
cannam@185 216 if (!m_reduced) {
cannam@185 217 segment();
cannam@185 218 reduce();
cannam@185 219 m_reduced = true;
cannam@185 220 }
cannam@183 221
cannam@180 222 bool continuous = (avg == ContinuousTimeAverage);
cannam@180 223
cannam@175 224 FeatureList fl;
cannam@175 225 for (SummarySegmentMap::const_iterator i = m_summaries[output].begin();
cannam@175 226 i != m_summaries[output].end(); ++i) {
cannam@177 227
cannam@175 228 Feature f;
cannam@175 229 f.hasTimestamp = true;
cannam@175 230 f.timestamp = i->first;
cannam@175 231 f.hasDuration = false;
cannam@177 232
cannam@175 233 for (OutputSummary::const_iterator j = i->second.begin();
cannam@175 234 j != i->second.end(); ++j) {
cannam@175 235
cannam@175 236 // these will be ordered by bin number, and no bin numbers
cannam@175 237 // will be missing except at the end (because of the way
cannam@175 238 // the accumulators were initially filled in accumulate())
cannam@175 239
cannam@175 240 const OutputBinSummary &summary = j->second;
cannam@175 241 float result = 0.f;
cannam@175 242
cannam@175 243 switch (type) {
cannam@175 244
cannam@175 245 case Minimum:
cannam@175 246 result = summary.minimum;
cannam@175 247 break;
cannam@175 248
cannam@175 249 case Maximum:
cannam@175 250 result = summary.maximum;
cannam@175 251 break;
cannam@175 252
cannam@175 253 case Mean:
cannam@180 254 if (continuous) {
cannam@180 255 result = summary.mean_c;
cannam@180 256 } else if (summary.count) {
cannam@175 257 result = summary.sum / summary.count;
cannam@175 258 }
cannam@175 259 break;
cannam@175 260
cannam@175 261 case Median:
cannam@180 262 if (continuous) result = summary.median_c;
cannam@180 263 else result = summary.median;
cannam@175 264 break;
cannam@175 265
cannam@175 266 case Mode:
cannam@180 267 if (continuous) result = summary.mode_c;
cannam@180 268 else result = summary.mode;
cannam@175 269 break;
cannam@175 270
cannam@175 271 case Sum:
cannam@175 272 result = summary.sum;
cannam@175 273 break;
cannam@175 274
cannam@175 275 case Variance:
cannam@180 276 if (continuous) result = summary.variance_c;
cannam@180 277 else result = summary.variance;
cannam@175 278 break;
cannam@175 279
cannam@175 280 case StandardDeviation:
cannam@180 281 if (continuous) result = sqrtf(summary.variance_c);
cannam@180 282 else result = sqrtf(summary.variance);
cannam@175 283 break;
cannam@175 284
cannam@175 285 case Count:
cannam@175 286 result = summary.count;
cannam@175 287 break;
cannam@180 288
cannam@180 289 default:
cannam@180 290 break;
cannam@175 291 }
cannam@177 292
cannam@177 293 f.values.push_back(result);
cannam@175 294 }
cannam@175 295
cannam@175 296 fl.push_back(f);
cannam@175 297 }
cannam@175 298 return fl;
cannam@175 299 }
cannam@175 300
cannam@176 301 Plugin::FeatureSet
cannam@180 302 PluginSummarisingAdapter::Impl::getSummaryForAllOutputs(SummaryType type,
cannam@180 303 AveragingMethod avg)
cannam@176 304 {
cannam@185 305 if (!m_reduced) {
cannam@185 306 segment();
cannam@185 307 reduce();
cannam@185 308 m_reduced = true;
cannam@185 309 }
cannam@183 310
cannam@176 311 FeatureSet fs;
cannam@176 312 for (OutputSummarySegmentMap::const_iterator i = m_summaries.begin();
cannam@176 313 i != m_summaries.end(); ++i) {
cannam@180 314 fs[i->first] = getSummaryForOutput(i->first, type, avg);
cannam@176 315 }
cannam@176 316 return fs;
cannam@176 317 }
cannam@176 318
cannam@174 319 void
cannam@174 320 PluginSummarisingAdapter::Impl::accumulate(const FeatureSet &fs,
cannam@180 321 RealTime timestamp,
cannam@180 322 bool final)
cannam@174 323 {
cannam@174 324 for (FeatureSet::const_iterator i = fs.begin(); i != fs.end(); ++i) {
cannam@174 325 for (FeatureList::const_iterator j = i->second.begin();
cannam@174 326 j != i->second.end(); ++j) {
cannam@182 327 if (j->hasTimestamp) {
cannam@182 328 accumulate(i->first, *j, j->timestamp, final);
cannam@182 329 } else {
cannam@182 330 //!!! is this correct?
cannam@182 331 accumulate(i->first, *j, timestamp, final);
cannam@182 332 }
cannam@174 333 }
cannam@174 334 }
cannam@174 335 }
cannam@174 336
cannam@174 337 void
cannam@174 338 PluginSummarisingAdapter::Impl::accumulate(int output,
cannam@174 339 const Feature &f,
cannam@180 340 RealTime timestamp,
cannam@180 341 bool final)
cannam@174 342 {
cannam@180 343 //!!! to do: use timestamp to determine which segment we're on
cannam@185 344
cannam@185 345 //!!! What should happen if a feature's duration spans a segment
cannam@185 346 // boundary? I think we probably want to chop it, and pretend that it
cannam@185 347 // appears in both -- don't we? do we? A very long feature (e.g. key,
cannam@185 348 // if the whole audio is in a single key) might span many or all
cannam@185 349 // segments, and we want that to be reflected in the results (e.g. it
cannam@185 350 // is the modal key in all of those segments, not just the first).
cannam@185 351 // That is actually quite complicated to do!
cannam@180 352
cannam@185 353 //!!! This affects how we record things. If features spanning a
cannam@185 354 // boundary should be chopped, then we need to have per-segment
cannam@185 355 // accumulators (and the feature value goes into both -- perhaps we
cannam@185 356 // need a separate phase to split the accumulator up into segments).
cannam@185 357 // If features spanning a boundary should be counted only in the first
cannam@185 358 // segment, with their full duration, then we should store them in a
cannam@185 359 // single accumulator and distribute into segments only on reduce.
cannam@180 360
cannam@184 361 std::cerr << "output " << output << ": timestamp " << timestamp << ", prev timestamp " << m_prevTimestamps[output] << ", final " << final << std::endl;
cannam@182 362
cannam@184 363 // At each process step, accumulate() is called once for each
cannam@184 364 // feature on each output within that process's returned feature
cannam@184 365 // list, and with the timestamp passed in being that of the start
cannam@184 366 // of the process block.
cannam@182 367
cannam@184 368 // At the end (in getRemainingFeatures), accumulate() is called
cannam@184 369 // once for each feature on each output within the feature list
cannam@184 370 // returned by getRemainingFeatures, and with the timestamp being
cannam@184 371 // the same as the last process block and final set to true.
cannam@184 372
cannam@184 373 // (What if getRemainingFeatures doesn't return any features? We
cannam@184 374 // still need to ensure that the final duration is written. Need
cannam@184 375 // a separate function to close the durations.)
cannam@184 376
cannam@184 377 // At each call, we pull out the value for the feature and stuff
cannam@184 378 // it into the accumulator's appropriate values array; and we
cannam@184 379 // calculate the duration for the _previous_ feature, or pull it
cannam@184 380 // from the prevDurations array if the previous feature had a
cannam@184 381 // duration in its structure, and stuff that into the
cannam@184 382 // accumulator's appropriate durations array.
cannam@184 383
cannam@184 384 if (m_prevDurations.find(output) != m_prevDurations.end()) {
cannam@184 385
cannam@184 386 // Not the first time accumulate has been called for this
cannam@184 387 // output -- there has been a previous feature
cannam@184 388
cannam@184 389 RealTime prevDuration;
cannam@184 390
cannam@184 391 // Note that m_prevDurations[output] only contains the
cannam@184 392 // duration field that was contained in the previous feature.
cannam@184 393 // If it didn't have an explicit duration,
cannam@184 394 // m_prevDurations[output] should be INVALID_DURATION and we
cannam@184 395 // will have to calculate the duration from the previous and
cannam@184 396 // current timestamps.
cannam@184 397
cannam@184 398 if (m_prevDurations[output] != INVALID_DURATION) {
cannam@184 399 prevDuration = m_prevDurations[output];
cannam@184 400 std::cerr << "Previous duration from previous feature: " << prevDuration << std::endl;
cannam@184 401 } else {
cannam@184 402 prevDuration = timestamp - m_prevTimestamps[output];
cannam@184 403 std::cerr << "Previous duration from diff: " << timestamp << " - "
cannam@184 404 << m_prevTimestamps[output] << std::endl;
cannam@180 405 }
cannam@184 406
cannam@184 407 std::cerr << "output " << output << ": ";
cannam@184 408
cannam@184 409 std::cerr << "Pushing previous duration as " << prevDuration << std::endl;
cannam@185 410
cannam@185 411 m_accumulators[output].results
cannam@185 412 [m_accumulators[output].results.size() - 1]
cannam@185 413 .duration = prevDuration;
cannam@180 414 }
cannam@180 415
cannam@184 416 if (f.hasDuration) m_prevDurations[output] = f.duration;
cannam@184 417 else m_prevDurations[output] = INVALID_DURATION;
cannam@184 418
cannam@180 419 m_prevTimestamps[output] = timestamp;
cannam@185 420
cannam@185 421 //!!! should really be "timestamp plus duration" or "timestamp plus output resolution"
cannam@184 422 if (timestamp > m_lastTimestamp) m_lastTimestamp = timestamp;
cannam@180 423
cannam@185 424 Result result;
cannam@185 425 result.time = timestamp;
cannam@185 426 result.duration = INVALID_DURATION;
cannam@185 427
cannam@185 428 if (f.values.size() > m_accumulators[output].bins) {
cannam@185 429 m_accumulators[output].bins = f.values.size();
cannam@185 430 }
cannam@185 431
cannam@174 432 for (int i = 0; i < int(f.values.size()); ++i) {
cannam@185 433 result.values.push_back(f.values[i]);
cannam@174 434 }
cannam@185 435
cannam@185 436 m_accumulators[output].results.push_back(result);
cannam@184 437 }
cannam@180 438
cannam@184 439 void
cannam@184 440 PluginSummarisingAdapter::Impl::accumulateFinalDurations()
cannam@184 441 {
cannam@184 442 for (OutputTimestampMap::iterator i = m_prevTimestamps.begin();
cannam@184 443 i != m_prevTimestamps.end(); ++i) {
cannam@184 444
cannam@184 445 int output = i->first;
cannam@185 446
cannam@185 447 int acount = m_accumulators[output].results.size();
cannam@185 448
cannam@185 449 if (acount == 0) continue;
cannam@185 450
cannam@184 451 RealTime prevTimestamp = i->second;
cannam@184 452
cannam@184 453 std::cerr << "output " << output << ": ";
cannam@184 454
cannam@184 455 if (m_prevDurations.find(output) != m_prevDurations.end() &&
cannam@184 456 m_prevDurations[output] != INVALID_DURATION) {
cannam@184 457
cannam@184 458 std::cerr << "Pushing final duration from feature as " << m_prevDurations[output] << std::endl;
cannam@184 459
cannam@185 460 m_accumulators[output].results[acount - 1].duration =
cannam@185 461 m_prevDurations[output];
cannam@184 462
cannam@184 463 } else {
cannam@184 464
cannam@184 465 std::cerr << "Pushing final duration from diff as " << m_lastTimestamp << " - " << m_prevTimestamps[output] << std::endl;
cannam@184 466
cannam@185 467 m_accumulators[output].results[acount - 1].duration =
cannam@185 468 m_lastTimestamp - m_prevTimestamps[output];
cannam@184 469 }
cannam@180 470 }
cannam@174 471 }
cannam@174 472
cannam@185 473 void
cannam@186 474 PluginSummarisingAdapter::Impl::findSegmentBounds(RealTime t,
cannam@186 475 RealTime &start,
cannam@186 476 RealTime &end)
cannam@186 477 {
cannam@186 478 std::cerr << "findSegmentBounds: t = " << t << std::endl;
cannam@186 479
cannam@186 480 SegmentBoundaries::const_iterator i = std::lower_bound
cannam@186 481 (m_boundaries.begin(), m_boundaries.end(), t);
cannam@186 482
cannam@186 483 start = RealTime::zeroTime;
cannam@186 484 end = m_lastTimestamp;
cannam@186 485
cannam@186 486 if (i != m_boundaries.end()) {
cannam@186 487
cannam@186 488 start = *i;
cannam@186 489
cannam@186 490 if (++i != m_boundaries.end()) {
cannam@186 491 end = *i;
cannam@186 492 }
cannam@186 493 }
cannam@186 494
cannam@186 495 std::cerr << "findSegmentBounds: " << t << " is in segment " << start << " -> " << end << std::endl;
cannam@186 496 }
cannam@186 497
cannam@186 498 void
cannam@185 499 PluginSummarisingAdapter::Impl::segment()
cannam@185 500 {
cannam@185 501 SegmentBoundaries::iterator boundaryitr = m_boundaries.begin();
cannam@185 502 RealTime segmentStart = RealTime::zeroTime;
cannam@186 503
cannam@185 504 for (OutputAccumulatorMap::iterator i = m_accumulators.begin();
cannam@185 505 i != m_accumulators.end(); ++i) {
cannam@185 506
cannam@185 507 int output = i->first;
cannam@185 508 OutputAccumulator &source = i->second;
cannam@185 509
cannam@186 510 for (int n = 0; n < source.results.size(); ++n) {
cannam@186 511
cannam@186 512 // This result spans source.results[n].time to
cannam@186 513 // source.results[n].time + source.results[n].duration.
cannam@186 514 // We need to dispose it into segments appropriately
cannam@186 515
cannam@186 516 RealTime resultStart = source.results[n].time;
cannam@186 517 RealTime resultEnd = resultStart + source.results[n].duration;
cannam@186 518
cannam@186 519 RealTime segmentStart = RealTime::zeroTime;
cannam@186 520 RealTime segmentEnd = resultEnd - RealTime(1, 0);
cannam@186 521
cannam@186 522 while (segmentEnd < resultEnd) {
cannam@186 523
cannam@186 524 findSegmentBounds(resultStart, segmentStart, segmentEnd);
cannam@186 525
cannam@186 526 RealTime chunkStart = resultStart;
cannam@186 527 if (chunkStart < segmentStart) chunkStart = segmentStart;
cannam@186 528
cannam@186 529 RealTime chunkEnd = resultEnd;
cannam@186 530 if (chunkEnd > segmentEnd) chunkEnd = segmentEnd;
cannam@186 531
cannam@186 532 m_segmentedAccumulators[output][segmentStart].bins = source.bins;
cannam@186 533
cannam@186 534 Result chunk;
cannam@186 535 chunk.time = chunkStart;
cannam@186 536 chunk.duration = chunkEnd - chunkStart;
cannam@186 537 chunk.values = source.results[n].values;
cannam@186 538
cannam@186 539 std::cerr << "chunk for segment " << segmentStart << ": from " << chunk.time << ", duration " << chunk.duration << std::endl;
cannam@186 540
cannam@186 541 m_segmentedAccumulators[output][segmentStart].results
cannam@186 542 .push_back(chunk);
cannam@186 543
cannam@186 544 resultStart = chunkEnd;
cannam@186 545 }
cannam@186 546 }
cannam@186 547 }
cannam@186 548
cannam@185 549
cannam@185 550
cannam@185 551 /*
cannam@185 552 if (boundaryitr == m_boundaries.end()) {
cannam@185 553 m_segmentedAccumulators[output][segmentStart] = source;
cannam@185 554 source.clear();
cannam@185 555 continue;
cannam@185 556 }
cannam@185 557 */
cannam@185 558
cannam@185 559
cannam@185 560
cannam@185 561
cannam@185 562 }
cannam@185 563
cannam@181 564 struct ValueDurationFloatPair
cannam@181 565 {
cannam@181 566 float value;
cannam@181 567 float duration;
cannam@181 568
cannam@181 569 ValueDurationFloatPair() : value(0), duration(0) { }
cannam@181 570 ValueDurationFloatPair(float v, float d) : value(v), duration(d) { }
cannam@181 571 ValueDurationFloatPair &operator=(const ValueDurationFloatPair &p) {
cannam@181 572 value = p.value;
cannam@181 573 duration = p.duration;
cannam@181 574 return *this;
cannam@181 575 }
cannam@181 576 bool operator<(const ValueDurationFloatPair &p) const {
cannam@181 577 return value < p.value;
cannam@181 578 }
cannam@181 579 };
cannam@181 580
cannam@181 581 static double toSec(const RealTime &r)
cannam@181 582 {
cannam@181 583 return r.sec + double(r.nsec) / 1000000000.0;
cannam@181 584 }
cannam@181 585
cannam@174 586 void
cannam@174 587 PluginSummarisingAdapter::Impl::reduce()
cannam@174 588 {
cannam@184 589 accumulateFinalDurations();
cannam@184 590
cannam@174 591 RealTime segmentStart = RealTime::zeroTime; //!!!
cannam@174 592
cannam@174 593 for (OutputAccumulatorMap::iterator i = m_accumulators.begin();
cannam@174 594 i != m_accumulators.end(); ++i) {
cannam@174 595
cannam@174 596 int output = i->first;
cannam@174 597 OutputAccumulator &accumulator = i->second;
cannam@174 598
cannam@185 599 int sz = accumulator.results.size();
cannam@185 600
cannam@182 601 double totalDuration = 0.0;
cannam@185 602 //!!! is this right?
cannam@185 603 if (sz > 0) {
cannam@185 604 totalDuration = toSec(accumulator.results[sz-1].time +
cannam@185 605 accumulator.results[sz-1].duration);
cannam@180 606 }
cannam@180 607
cannam@185 608 for (int bin = 0; bin < accumulator.bins; ++bin) {
cannam@174 609
cannam@180 610 // work on all values over time for a single bin
cannam@180 611
cannam@174 612 OutputBinSummary summary;
cannam@180 613
cannam@185 614 summary.count = sz;
cannam@180 615
cannam@174 616 summary.minimum = 0.f;
cannam@174 617 summary.maximum = 0.f;
cannam@180 618
cannam@174 619 summary.median = 0.f;
cannam@174 620 summary.mode = 0.f;
cannam@174 621 summary.sum = 0.f;
cannam@174 622 summary.variance = 0.f;
cannam@180 623
cannam@180 624 summary.median_c = 0.f;
cannam@180 625 summary.mode_c = 0.f;
cannam@180 626 summary.mean_c = 0.f;
cannam@180 627 summary.variance_c = 0.f;
cannam@180 628
cannam@185 629 if (sz == 0) continue;
cannam@180 630
cannam@181 631 std::vector<ValueDurationFloatPair> valvec;
cannam@181 632
cannam@181 633 for (int k = 0; k < sz; ++k) {
cannam@185 634 while (accumulator.results[k].values.size() <
cannam@185 635 accumulator.bins) {
cannam@185 636 accumulator.results[k].values.push_back(0.f);
cannam@185 637 }
cannam@185 638 }
cannam@185 639
cannam@185 640 for (int k = 0; k < sz; ++k) {
cannam@185 641 float value = accumulator.results[k].values[bin];
cannam@185 642 valvec.push_back(ValueDurationFloatPair
cannam@185 643 (value,
cannam@185 644 toSec(accumulator.results[k].duration)));
cannam@181 645 }
cannam@181 646
cannam@181 647 std::sort(valvec.begin(), valvec.end());
cannam@181 648
cannam@181 649 summary.minimum = valvec[0].value;
cannam@181 650 summary.maximum = valvec[sz-1].value;
cannam@174 651
cannam@174 652 if (sz % 2 == 1) {
cannam@181 653 summary.median = valvec[sz/2].value;
cannam@174 654 } else {
cannam@181 655 summary.median = (valvec[sz/2].value + valvec[sz/2 + 1].value) / 2;
cannam@174 656 }
cannam@181 657
cannam@181 658 double duracc = 0.0;
cannam@181 659 summary.median_c = valvec[sz-1].value;
cannam@174 660
cannam@181 661 for (int k = 0; k < sz; ++k) {
cannam@181 662 duracc += valvec[k].duration;
cannam@181 663 if (duracc > totalDuration/2) {
cannam@181 664 summary.median_c = valvec[k].value;
cannam@181 665 break;
cannam@181 666 }
cannam@181 667 }
cannam@185 668
cannam@185 669 std::cerr << "median_c = " << summary.median_c << std::endl;
cannam@185 670 std::cerr << "median = " << summary.median << std::endl;
cannam@181 671
cannam@174 672 std::map<float, int> distribution;
cannam@174 673
cannam@174 674 for (int k = 0; k < sz; ++k) {
cannam@185 675 summary.sum += accumulator.results[k].values[bin];
cannam@185 676 distribution[accumulator.results[k].values[bin]] += 1;
cannam@174 677 }
cannam@174 678
cannam@174 679 int md = 0;
cannam@174 680
cannam@174 681 for (std::map<float, int>::iterator di = distribution.begin();
cannam@174 682 di != distribution.end(); ++di) {
cannam@174 683 if (di->second > md) {
cannam@174 684 md = di->second;
cannam@174 685 summary.mode = di->first;
cannam@174 686 }
cannam@174 687 }
cannam@174 688
cannam@174 689 distribution.clear();
cannam@174 690
cannam@181 691 std::map<float, double> distribution_c;
cannam@180 692
cannam@180 693 for (int k = 0; k < sz; ++k) {
cannam@185 694 distribution_c[accumulator.results[k].values[bin]]
cannam@185 695 += toSec(accumulator.results[k].duration);
cannam@180 696 }
cannam@180 697
cannam@181 698 double mrd = 0.0;
cannam@180 699
cannam@181 700 for (std::map<float, double>::iterator di = distribution_c.begin();
cannam@180 701 di != distribution_c.end(); ++di) {
cannam@180 702 if (di->second > mrd) {
cannam@180 703 mrd = di->second;
cannam@180 704 summary.mode_c = di->first;
cannam@180 705 }
cannam@180 706 }
cannam@180 707
cannam@180 708 distribution_c.clear();
cannam@180 709
cannam@181 710 if (totalDuration > 0.0) {
cannam@181 711
cannam@181 712 double sum_c = 0.0;
cannam@181 713
cannam@181 714 for (int k = 0; k < sz; ++k) {
cannam@185 715 double value = accumulator.results[k].values[bin]
cannam@185 716 * toSec(accumulator.results[k].duration);
cannam@181 717 sum_c += value;
cannam@181 718 }
cannam@182 719
cannam@182 720 std::cerr << "mean_c = " << sum_c << " / " << totalDuration << " = "
cannam@184 721 << sum_c / totalDuration << " (sz = " << sz << ")" << std::endl;
cannam@181 722
cannam@181 723 summary.mean_c = sum_c / totalDuration;
cannam@181 724
cannam@181 725 for (int k = 0; k < sz; ++k) {
cannam@185 726 double value = accumulator.results[k].values[bin]
cannam@185 727 * toSec(accumulator.results[k].duration);
cannam@181 728 summary.variance_c +=
cannam@181 729 (value - summary.mean_c) * (value - summary.mean_c);
cannam@181 730 }
cannam@181 731
cannam@181 732 summary.variance_c /= summary.count;
cannam@181 733 }
cannam@181 734
cannam@174 735 float mean = summary.sum / summary.count;
cannam@174 736
cannam@182 737 std::cerr << "mean = " << summary.sum << " / " << summary.count << " = "
cannam@182 738 << summary.sum / summary.count << std::endl;
cannam@182 739
cannam@174 740 for (int k = 0; k < sz; ++k) {
cannam@185 741 float value = accumulator.results[k].values[bin];
cannam@185 742 summary.variance += (value - mean) * (value - mean);
cannam@174 743 }
cannam@174 744 summary.variance /= summary.count;
cannam@174 745
cannam@174 746 m_summaries[output][segmentStart][bin] = summary;
cannam@174 747 }
cannam@174 748 }
cannam@175 749
cannam@175 750 m_accumulators.clear();
cannam@174 751 }
cannam@174 752
cannam@174 753
cannam@174 754 }
cannam@174 755
cannam@174 756 }
cannam@174 757