annotate vamp-sdk/hostext/PluginSummarisingAdapter.cpp @ 187:ed8aa954e72f

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