annotate data/model/EditableDenseThreeDimensionalModel.cpp @ 607:a67651386253

...
author Chris Cannam
date Thu, 10 Sep 2009 14:31:49 +0000
parents 1415e35881f6
children dd97f7b3d120
rev   line source
Chris@152 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@152 2
Chris@152 3 /*
Chris@152 4 Sonic Visualiser
Chris@152 5 An audio file viewer and annotation editor.
Chris@152 6 Centre for Digital Music, Queen Mary, University of London.
Chris@202 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@152 8
Chris@152 9 This program is free software; you can redistribute it and/or
Chris@152 10 modify it under the terms of the GNU General Public License as
Chris@152 11 published by the Free Software Foundation; either version 2 of the
Chris@152 12 License, or (at your option) any later version. See the file
Chris@152 13 COPYING included with this distribution for more information.
Chris@152 14 */
Chris@152 15
Chris@152 16 #include "EditableDenseThreeDimensionalModel.h"
Chris@152 17
Chris@478 18 #include "base/LogRange.h"
Chris@478 19
Chris@152 20 #include <QTextStream>
Chris@387 21 #include <QStringList>
Chris@536 22 #include <QReadLocker>
Chris@536 23 #include <QWriteLocker>
Chris@387 24
Chris@181 25 #include <iostream>
Chris@181 26
Chris@256 27 #include <cmath>
Chris@534 28 #include <cassert>
Chris@256 29
Chris@607 30 #include "system/System.h"
Chris@607 31
Chris@152 32 EditableDenseThreeDimensionalModel::EditableDenseThreeDimensionalModel(size_t sampleRate,
Chris@152 33 size_t resolution,
Chris@152 34 size_t yBinCount,
Chris@535 35 CompressionType compression,
Chris@152 36 bool notifyOnAdd) :
Chris@152 37 m_sampleRate(sampleRate),
Chris@152 38 m_resolution(resolution),
Chris@152 39 m_yBinCount(yBinCount),
Chris@535 40 m_compression(compression),
Chris@152 41 m_minimum(0.0),
Chris@152 42 m_maximum(0.0),
Chris@256 43 m_haveExtents(false),
Chris@152 44 m_notifyOnAdd(notifyOnAdd),
Chris@152 45 m_sinceLastNotifyMin(-1),
Chris@152 46 m_sinceLastNotifyMax(-1),
Chris@152 47 m_completion(100)
Chris@152 48 {
Chris@152 49 }
Chris@152 50
Chris@152 51 bool
Chris@152 52 EditableDenseThreeDimensionalModel::isOK() const
Chris@152 53 {
Chris@152 54 return true;
Chris@152 55 }
Chris@152 56
Chris@152 57 size_t
Chris@152 58 EditableDenseThreeDimensionalModel::getSampleRate() const
Chris@152 59 {
Chris@152 60 return m_sampleRate;
Chris@152 61 }
Chris@152 62
Chris@152 63 size_t
Chris@152 64 EditableDenseThreeDimensionalModel::getStartFrame() const
Chris@152 65 {
Chris@152 66 return 0;
Chris@152 67 }
Chris@152 68
Chris@152 69 size_t
Chris@152 70 EditableDenseThreeDimensionalModel::getEndFrame() const
Chris@152 71 {
Chris@152 72 return m_resolution * m_data.size() + (m_resolution - 1);
Chris@152 73 }
Chris@152 74
Chris@152 75 Model *
Chris@152 76 EditableDenseThreeDimensionalModel::clone() const
Chris@152 77 {
Chris@536 78 QReadLocker locker(&m_lock);
Chris@534 79
Chris@152 80 EditableDenseThreeDimensionalModel *model =
Chris@152 81 new EditableDenseThreeDimensionalModel
Chris@535 82 (m_sampleRate, m_resolution, m_yBinCount, m_compression);
Chris@152 83
Chris@152 84 model->m_minimum = m_minimum;
Chris@152 85 model->m_maximum = m_maximum;
Chris@256 86 model->m_haveExtents = m_haveExtents;
Chris@152 87
Chris@152 88 for (size_t i = 0; i < m_data.size(); ++i) {
Chris@533 89 model->setColumn(i, m_data.at(i));
Chris@152 90 }
Chris@152 91
Chris@152 92 return model;
Chris@152 93 }
Chris@152 94
Chris@152 95 size_t
Chris@152 96 EditableDenseThreeDimensionalModel::getResolution() const
Chris@152 97 {
Chris@152 98 return m_resolution;
Chris@152 99 }
Chris@152 100
Chris@152 101 void
Chris@152 102 EditableDenseThreeDimensionalModel::setResolution(size_t sz)
Chris@152 103 {
Chris@152 104 m_resolution = sz;
Chris@152 105 }
Chris@152 106
Chris@152 107 size_t
Chris@182 108 EditableDenseThreeDimensionalModel::getWidth() const
Chris@182 109 {
Chris@182 110 return m_data.size();
Chris@182 111 }
Chris@182 112
Chris@182 113 size_t
Chris@182 114 EditableDenseThreeDimensionalModel::getHeight() const
Chris@152 115 {
Chris@152 116 return m_yBinCount;
Chris@152 117 }
Chris@152 118
Chris@152 119 void
Chris@182 120 EditableDenseThreeDimensionalModel::setHeight(size_t sz)
Chris@152 121 {
Chris@152 122 m_yBinCount = sz;
Chris@152 123 }
Chris@152 124
Chris@152 125 float
Chris@152 126 EditableDenseThreeDimensionalModel::getMinimumLevel() const
Chris@152 127 {
Chris@152 128 return m_minimum;
Chris@152 129 }
Chris@152 130
Chris@152 131 void
Chris@152 132 EditableDenseThreeDimensionalModel::setMinimumLevel(float level)
Chris@152 133 {
Chris@152 134 m_minimum = level;
Chris@152 135 }
Chris@152 136
Chris@152 137 float
Chris@152 138 EditableDenseThreeDimensionalModel::getMaximumLevel() const
Chris@152 139 {
Chris@152 140 return m_maximum;
Chris@152 141 }
Chris@152 142
Chris@152 143 void
Chris@152 144 EditableDenseThreeDimensionalModel::setMaximumLevel(float level)
Chris@152 145 {
Chris@152 146 m_maximum = level;
Chris@152 147 }
Chris@152 148
Chris@533 149 EditableDenseThreeDimensionalModel::Column
Chris@533 150 EditableDenseThreeDimensionalModel::getColumn(size_t index) const
Chris@152 151 {
Chris@536 152 QReadLocker locker(&m_lock);
Chris@534 153 if (index >= m_data.size()) return Column();
Chris@534 154 return expandAndRetrieve(index);
Chris@152 155 }
Chris@152 156
Chris@152 157 float
Chris@182 158 EditableDenseThreeDimensionalModel::getValueAt(size_t index, size_t n) const
Chris@152 159 {
Chris@534 160 Column c = getColumn(index);
Chris@535 161 if (n < c.size()) return c.at(n);
Chris@534 162 return m_minimum;
Chris@534 163 }
Chris@152 164
Chris@535 165 //static int given = 0, stored = 0;
Chris@534 166
Chris@534 167 void
Chris@534 168 EditableDenseThreeDimensionalModel::truncateAndStore(size_t index,
Chris@534 169 const Column &values)
Chris@534 170 {
Chris@534 171 assert(index < m_data.size());
Chris@534 172
Chris@534 173 //std::cout << "truncateAndStore(" << index << ", " << values.size() << ")" << std::endl;
Chris@534 174
Chris@535 175 // The default case is to store the entire column at m_data[index]
Chris@535 176 // and place 0 at m_trunc[index] to indicate that it has not been
Chris@535 177 // truncated. We only do clever stuff if one of the clever-stuff
Chris@535 178 // tests works out.
Chris@535 179
Chris@534 180 m_trunc[index] = 0;
Chris@535 181 if (index == 0 ||
Chris@535 182 m_compression == NoCompression ||
Chris@535 183 values.size() != m_yBinCount) {
Chris@535 184 // given += values.size();
Chris@535 185 // stored += values.size();
Chris@534 186 m_data[index] = values;
Chris@534 187 return;
Chris@152 188 }
Chris@152 189
Chris@535 190 // Maximum distance between a column and the one we refer to as
Chris@535 191 // the source of its truncated values. Limited by having to fit
Chris@535 192 // in a signed char, but in any case small values are usually
Chris@535 193 // better
Chris@535 194 static int maxdist = 6;
Chris@534 195
Chris@535 196 bool known = false; // do we know whether to truncate at top or bottom?
Chris@535 197 bool top = false; // if we do know, will we truncate at top?
Chris@534 198
Chris@535 199 // If the previous column is not truncated, then it is the only
Chris@535 200 // candidate for comparison. If it is truncated, then the column
Chris@535 201 // that it refers to is the only candidate. Either way, we only
Chris@535 202 // have one possible column to compare against here, and we are
Chris@535 203 // being careful to ensure it is not a truncated one (to avoid
Chris@535 204 // doing more work recursively when uncompressing).
Chris@534 205 int tdist = 1;
Chris@534 206 int ptrunc = m_trunc[index-1];
Chris@534 207 if (ptrunc < 0) {
Chris@534 208 top = false;
Chris@534 209 known = true;
Chris@534 210 tdist = -ptrunc + 1;
Chris@534 211 } else if (ptrunc > 0) {
Chris@534 212 top = true;
Chris@534 213 known = true;
Chris@534 214 tdist = ptrunc + 1;
Chris@534 215 }
Chris@534 216
Chris@534 217 Column p = expandAndRetrieve(index - tdist);
Chris@534 218 int h = m_yBinCount;
Chris@534 219
Chris@534 220 if (p.size() == h && tdist <= maxdist) {
Chris@534 221
Chris@534 222 int bcount = 0, tcount = 0;
Chris@534 223 if (!known || !top) {
Chris@535 224 // count how many identical values there are at the bottom
Chris@534 225 for (int i = 0; i < h; ++i) {
Chris@534 226 if (values.at(i) == p.at(i)) ++bcount;
Chris@534 227 else break;
Chris@534 228 }
Chris@534 229 }
Chris@534 230 if (!known || top) {
Chris@535 231 // count how many identical values there are at the top
Chris@534 232 for (int i = h; i > 0; --i) {
Chris@534 233 if (values.at(i-1) == p.at(i-1)) ++tcount;
Chris@534 234 else break;
Chris@534 235 }
Chris@534 236 }
Chris@534 237 if (!known) top = (tcount > bcount);
Chris@534 238
Chris@535 239 int limit = h / 4; // don't bother unless we have at least this many
Chris@534 240 if ((top ? tcount : bcount) > limit) {
Chris@534 241
Chris@534 242 if (!top) {
Chris@535 243 // create a new column with h - bcount values from bcount up
Chris@534 244 Column tcol(h - bcount);
Chris@535 245 // given += values.size();
Chris@535 246 // stored += h - bcount;
Chris@534 247 for (int i = bcount; i < h; ++i) {
Chris@534 248 tcol[i - bcount] = values.at(i);
Chris@534 249 }
Chris@534 250 m_data[index] = tcol;
Chris@534 251 m_trunc[index] = -tdist;
Chris@534 252 return;
Chris@534 253 } else {
Chris@535 254 // create a new column with h - tcount values from 0 up
Chris@534 255 Column tcol(h - tcount);
Chris@535 256 // given += values.size();
Chris@535 257 // stored += h - tcount;
Chris@534 258 for (int i = 0; i < h - tcount; ++i) {
Chris@534 259 tcol[i] = values.at(i);
Chris@534 260 }
Chris@534 261 m_data[index] = tcol;
Chris@534 262 m_trunc[index] = tdist;
Chris@534 263 return;
Chris@534 264 }
Chris@534 265 }
Chris@534 266 }
Chris@534 267
Chris@535 268 // given += values.size();
Chris@535 269 // stored += values.size();
Chris@534 270 // std::cout << "given: " << given << ", stored: " << stored << " ("
Chris@534 271 // << ((float(stored) / float(given)) * 100.f) << "%)" << std::endl;
Chris@534 272
Chris@535 273 // default case if nothing wacky worked out
Chris@534 274 m_data[index] = values;
Chris@534 275 return;
Chris@534 276 }
Chris@534 277
Chris@534 278 EditableDenseThreeDimensionalModel::Column
Chris@534 279 EditableDenseThreeDimensionalModel::expandAndRetrieve(size_t index) const
Chris@534 280 {
Chris@535 281 // See comment above m_trunc declaration in header
Chris@535 282
Chris@534 283 assert(index < m_data.size());
Chris@534 284 Column c = m_data.at(index);
Chris@534 285 if (index == 0) {
Chris@534 286 return c;
Chris@534 287 }
Chris@534 288 int trunc = (int)m_trunc[index];
Chris@534 289 if (trunc == 0) {
Chris@534 290 return c;
Chris@534 291 }
Chris@534 292 bool top = true;
Chris@534 293 int tdist = trunc;
Chris@534 294 if (trunc < 0) { top = false; tdist = -trunc; }
Chris@534 295 Column p = expandAndRetrieve(index - tdist);
Chris@537 296 int psize = p.size(), csize = c.size();
Chris@537 297 if (psize != m_yBinCount) {
Chris@534 298 std::cerr << "WARNING: EditableDenseThreeDimensionalModel::expandAndRetrieve: Trying to expand from incorrectly sized column" << std::endl;
Chris@534 299 }
Chris@534 300 if (top) {
Chris@537 301 for (int i = csize; i < psize; ++i) {
Chris@534 302 c.push_back(p.at(i));
Chris@534 303 }
Chris@534 304 } else {
Chris@593 305 // push_front is very slow on QVector -- but not enough to
Chris@593 306 // make it desirable to choose a different container, since
Chris@593 307 // QVector has all the other advantages for us. easier to
Chris@593 308 // write the whole array out to a new vector
Chris@593 309 Column cc(psize);
Chris@593 310 for (int i = 0; i < psize - csize; ++i) {
Chris@593 311 cc[i] = p.at(i);
Chris@534 312 }
Chris@593 313 for (int i = 0; i < csize; ++i) {
Chris@593 314 cc[i + (psize - csize)] = c.at(i);
Chris@593 315 }
Chris@593 316 return cc;
Chris@534 317 }
Chris@534 318 return c;
Chris@152 319 }
Chris@152 320
Chris@152 321 void
Chris@182 322 EditableDenseThreeDimensionalModel::setColumn(size_t index,
Chris@182 323 const Column &values)
Chris@152 324 {
Chris@536 325 QWriteLocker locker(&m_lock);
Chris@152 326
Chris@182 327 while (index >= m_data.size()) {
Chris@182 328 m_data.push_back(Column());
Chris@534 329 m_trunc.push_back(0);
Chris@152 330 }
Chris@152 331
Chris@152 332 bool allChange = false;
Chris@152 333
Chris@534 334 // if (values.size() > m_yBinCount) m_yBinCount = values.size();
Chris@439 335
Chris@152 336 for (size_t i = 0; i < values.size(); ++i) {
Chris@256 337 float value = values[i];
Chris@606 338 if (ISNAN(value) || ISINF(value)) {
Chris@256 339 continue;
Chris@256 340 }
Chris@256 341 if (!m_haveExtents || value < m_minimum) {
Chris@256 342 m_minimum = value;
Chris@152 343 allChange = true;
Chris@152 344 }
Chris@256 345 if (!m_haveExtents || value > m_maximum) {
Chris@256 346 m_maximum = value;
Chris@152 347 allChange = true;
Chris@152 348 }
Chris@256 349 m_haveExtents = true;
Chris@152 350 }
Chris@152 351
Chris@534 352 truncateAndStore(index, values);
Chris@534 353
Chris@593 354 // assert(values == expandAndRetrieve(index));
Chris@152 355
Chris@182 356 long windowStart = index;
Chris@182 357 windowStart *= m_resolution;
Chris@182 358
Chris@152 359 if (m_notifyOnAdd) {
Chris@152 360 if (allChange) {
Chris@152 361 emit modelChanged();
Chris@152 362 } else {
Chris@152 363 emit modelChanged(windowStart, windowStart + m_resolution);
Chris@152 364 }
Chris@152 365 } else {
Chris@152 366 if (allChange) {
Chris@152 367 m_sinceLastNotifyMin = -1;
Chris@152 368 m_sinceLastNotifyMax = -1;
Chris@152 369 emit modelChanged();
Chris@152 370 } else {
Chris@152 371 if (m_sinceLastNotifyMin == -1 ||
Chris@152 372 windowStart < m_sinceLastNotifyMin) {
Chris@152 373 m_sinceLastNotifyMin = windowStart;
Chris@152 374 }
Chris@152 375 if (m_sinceLastNotifyMax == -1 ||
Chris@152 376 windowStart > m_sinceLastNotifyMax) {
Chris@152 377 m_sinceLastNotifyMax = windowStart;
Chris@152 378 }
Chris@152 379 }
Chris@152 380 }
Chris@152 381 }
Chris@152 382
Chris@152 383 QString
Chris@152 384 EditableDenseThreeDimensionalModel::getBinName(size_t n) const
Chris@152 385 {
Chris@152 386 if (m_binNames.size() > n) return m_binNames[n];
Chris@152 387 else return "";
Chris@152 388 }
Chris@152 389
Chris@152 390 void
Chris@152 391 EditableDenseThreeDimensionalModel::setBinName(size_t n, QString name)
Chris@152 392 {
Chris@152 393 while (m_binNames.size() <= n) m_binNames.push_back("");
Chris@152 394 m_binNames[n] = name;
Chris@152 395 emit modelChanged();
Chris@152 396 }
Chris@152 397
Chris@152 398 void
Chris@152 399 EditableDenseThreeDimensionalModel::setBinNames(std::vector<QString> names)
Chris@152 400 {
Chris@152 401 m_binNames = names;
Chris@152 402 emit modelChanged();
Chris@152 403 }
Chris@152 404
Chris@478 405 bool
Chris@478 406 EditableDenseThreeDimensionalModel::shouldUseLogValueScale() const
Chris@478 407 {
Chris@536 408 QReadLocker locker(&m_lock);
Chris@534 409
Chris@533 410 QVector<float> sample;
Chris@533 411 QVector<int> n;
Chris@478 412
Chris@478 413 for (int i = 0; i < 10; ++i) {
Chris@478 414 size_t index = i * 10;
Chris@478 415 if (index < m_data.size()) {
Chris@533 416 const Column &c = m_data.at(index);
Chris@478 417 while (c.size() > sample.size()) {
Chris@478 418 sample.push_back(0.f);
Chris@478 419 n.push_back(0);
Chris@478 420 }
Chris@478 421 for (int j = 0; j < c.size(); ++j) {
Chris@533 422 sample[j] += c.at(j);
Chris@478 423 ++n[j];
Chris@478 424 }
Chris@478 425 }
Chris@478 426 }
Chris@478 427
Chris@478 428 if (sample.empty()) return false;
Chris@478 429 for (int j = 0; j < sample.size(); ++j) {
Chris@478 430 if (n[j]) sample[j] /= n[j];
Chris@478 431 }
Chris@478 432
Chris@533 433 return LogRange::useLogScale(sample.toStdVector());
Chris@478 434 }
Chris@478 435
Chris@152 436 void
Chris@333 437 EditableDenseThreeDimensionalModel::setCompletion(int completion, bool update)
Chris@152 438 {
Chris@152 439 if (m_completion != completion) {
Chris@152 440 m_completion = completion;
Chris@152 441
Chris@152 442 if (completion == 100) {
Chris@152 443
Chris@152 444 m_notifyOnAdd = true; // henceforth
Chris@152 445 emit modelChanged();
Chris@152 446
Chris@152 447 } else if (!m_notifyOnAdd) {
Chris@152 448
Chris@333 449 if (update &&
Chris@333 450 m_sinceLastNotifyMin >= 0 &&
Chris@152 451 m_sinceLastNotifyMax >= 0) {
Chris@152 452 emit modelChanged(m_sinceLastNotifyMin,
Chris@152 453 m_sinceLastNotifyMax + m_resolution);
Chris@152 454 m_sinceLastNotifyMin = m_sinceLastNotifyMax = -1;
Chris@152 455 } else {
Chris@152 456 emit completionChanged();
Chris@152 457 }
Chris@152 458 } else {
Chris@152 459 emit completionChanged();
Chris@152 460 }
Chris@152 461 }
Chris@152 462 }
Chris@152 463
Chris@318 464 QString
Chris@318 465 EditableDenseThreeDimensionalModel::toDelimitedDataString(QString delimiter) const
Chris@318 466 {
Chris@536 467 QReadLocker locker(&m_lock);
Chris@318 468 QString s;
Chris@318 469 for (size_t i = 0; i < m_data.size(); ++i) {
Chris@318 470 QStringList list;
Chris@533 471 for (size_t j = 0; j < m_data.at(i).size(); ++j) {
Chris@533 472 list << QString("%1").arg(m_data.at(i).at(j));
Chris@318 473 }
Chris@318 474 s += list.join(delimiter) + "\n";
Chris@318 475 }
Chris@318 476 return s;
Chris@318 477 }
Chris@318 478
Chris@152 479 void
Chris@152 480 EditableDenseThreeDimensionalModel::toXml(QTextStream &out,
Chris@314 481 QString indent,
Chris@314 482 QString extraAttributes) const
Chris@152 483 {
Chris@536 484 QReadLocker locker(&m_lock);
Chris@534 485
Chris@152 486 // For historical reasons we read and write "resolution" as "windowSize"
Chris@152 487
Chris@318 488 std::cerr << "EditableDenseThreeDimensionalModel::toXml" << std::endl;
Chris@318 489
Chris@314 490 Model::toXml
Chris@314 491 (out, indent,
Chris@314 492 QString("type=\"dense\" dimensions=\"3\" windowSize=\"%1\" yBinCount=\"%2\" minimum=\"%3\" maximum=\"%4\" dataset=\"%5\" %6")
Chris@152 493 .arg(m_resolution)
Chris@152 494 .arg(m_yBinCount)
Chris@152 495 .arg(m_minimum)
Chris@152 496 .arg(m_maximum)
Chris@152 497 .arg(getObjectExportId(&m_data))
Chris@152 498 .arg(extraAttributes));
Chris@152 499
Chris@152 500 out << indent;
Chris@152 501 out << QString("<dataset id=\"%1\" dimensions=\"3\" separator=\" \">\n")
Chris@152 502 .arg(getObjectExportId(&m_data));
Chris@152 503
Chris@152 504 for (size_t i = 0; i < m_binNames.size(); ++i) {
Chris@152 505 if (m_binNames[i] != "") {
Chris@152 506 out << indent + " ";
Chris@152 507 out << QString("<bin number=\"%1\" name=\"%2\"/>\n")
Chris@152 508 .arg(i).arg(m_binNames[i]);
Chris@152 509 }
Chris@152 510 }
Chris@152 511
Chris@152 512 for (size_t i = 0; i < m_data.size(); ++i) {
Chris@152 513 out << indent + " ";
Chris@152 514 out << QString("<row n=\"%1\">").arg(i);
Chris@533 515 for (size_t j = 0; j < m_data.at(i).size(); ++j) {
Chris@152 516 if (j > 0) out << " ";
Chris@533 517 out << m_data.at(i).at(j);
Chris@152 518 }
Chris@152 519 out << QString("</row>\n");
Chris@318 520 out.flush();
Chris@152 521 }
Chris@152 522
Chris@152 523 out << indent + "</dataset>\n";
Chris@152 524 }
Chris@152 525
Chris@152 526