annotate vamp-sdk/hostext/PluginSummarisingAdapter.cpp @ 191:d1bdcd4a226f

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