annotate vamp-sdk/hostext/PluginSummarisingAdapter.cpp @ 189:5ce2c3f79a45

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