annotate document/SVFileReader.cpp @ 151:152619d379e0

* Add a measurement mode tool -- no functionality yet
author Chris Cannam
date Wed, 13 Jun 2007 12:00:01 +0000
parents 006c90387f40
children f6ce5febc07f
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 Sonic Visualiser
Chris@0 5 An audio file viewer and annotation editor.
Chris@0 6 Centre for Digital Music, Queen Mary, University of London.
Chris@77 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@0 8
Chris@0 9 This program is free software; you can redistribute it and/or
Chris@0 10 modify it under the terms of the GNU General Public License as
Chris@0 11 published by the Free Software Foundation; either version 2 of the
Chris@0 12 License, or (at your option) any later version. See the file
Chris@0 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "SVFileReader.h"
Chris@0 17
Chris@1 18 #include "layer/Layer.h"
Chris@1 19 #include "view/View.h"
Chris@0 20 #include "base/PlayParameters.h"
Chris@0 21 #include "base/PlayParameterRepository.h"
Chris@0 22
Chris@1 23 #include "data/fileio/AudioFileReaderFactory.h"
Chris@87 24 #include "data/fileio/FileFinder.h"
Chris@87 25 #include "data/fileio/RemoteFile.h"
Chris@0 26
Chris@1 27 #include "data/model/WaveFileModel.h"
Chris@3 28 #include "data/model/EditableDenseThreeDimensionalModel.h"
Chris@1 29 #include "data/model/SparseOneDimensionalModel.h"
Chris@1 30 #include "data/model/SparseTimeValueModel.h"
Chris@1 31 #include "data/model/NoteModel.h"
Chris@1 32 #include "data/model/TextModel.h"
Chris@0 33
Chris@1 34 #include "view/Pane.h"
Chris@0 35
Chris@1 36 #include "Document.h"
Chris@0 37
Chris@0 38 #include <QString>
Chris@0 39 #include <QMessageBox>
Chris@0 40 #include <QFileDialog>
Chris@0 41
Chris@0 42 #include <iostream>
Chris@0 43
Chris@0 44 SVFileReader::SVFileReader(Document *document,
Chris@87 45 SVFileReaderPaneCallback &callback,
Chris@87 46 QString location) :
Chris@0 47 m_document(document),
Chris@0 48 m_paneCallback(callback),
Chris@87 49 m_location(location),
Chris@0 50 m_currentPane(0),
Chris@0 51 m_currentDataset(0),
Chris@0 52 m_currentDerivedModel(0),
Chris@55 53 m_currentDerivedModelId(-1),
Chris@0 54 m_currentPlayParameters(0),
Chris@0 55 m_datasetSeparator(" "),
Chris@0 56 m_inRow(false),
Chris@0 57 m_rowNumber(0),
Chris@0 58 m_ok(false)
Chris@0 59 {
Chris@0 60 }
Chris@0 61
Chris@0 62 void
Chris@0 63 SVFileReader::parse(const QString &xmlData)
Chris@0 64 {
Chris@0 65 QXmlInputSource inputSource;
Chris@0 66 inputSource.setData(xmlData);
Chris@0 67 parse(inputSource);
Chris@0 68 }
Chris@0 69
Chris@0 70 void
Chris@0 71 SVFileReader::parse(QXmlInputSource &inputSource)
Chris@0 72 {
Chris@0 73 QXmlSimpleReader reader;
Chris@0 74 reader.setContentHandler(this);
Chris@0 75 reader.setErrorHandler(this);
Chris@0 76 m_ok = reader.parse(inputSource);
Chris@0 77 }
Chris@0 78
Chris@0 79 bool
Chris@0 80 SVFileReader::isOK()
Chris@0 81 {
Chris@0 82 return m_ok;
Chris@0 83 }
Chris@0 84
Chris@0 85 SVFileReader::~SVFileReader()
Chris@0 86 {
Chris@0 87 if (!m_awaitingDatasets.empty()) {
Chris@0 88 std::cerr << "WARNING: SV-XML: File ended with "
Chris@0 89 << m_awaitingDatasets.size() << " unfilled model dataset(s)"
Chris@0 90 << std::endl;
Chris@0 91 }
Chris@0 92
Chris@0 93 std::set<Model *> unaddedModels;
Chris@0 94
Chris@0 95 for (std::map<int, Model *>::iterator i = m_models.begin();
Chris@0 96 i != m_models.end(); ++i) {
Chris@0 97 if (m_addedModels.find(i->second) == m_addedModels.end()) {
Chris@0 98 unaddedModels.insert(i->second);
Chris@0 99 }
Chris@0 100 }
Chris@0 101
Chris@0 102 if (!unaddedModels.empty()) {
Chris@0 103 std::cerr << "WARNING: SV-XML: File contained "
Chris@0 104 << unaddedModels.size() << " unused models"
Chris@0 105 << std::endl;
Chris@0 106 while (!unaddedModels.empty()) {
Chris@0 107 delete *unaddedModels.begin();
Chris@0 108 unaddedModels.erase(unaddedModels.begin());
Chris@0 109 }
Chris@0 110 }
Chris@0 111 }
Chris@0 112
Chris@0 113 bool
Chris@0 114 SVFileReader::startElement(const QString &, const QString &,
Chris@0 115 const QString &qName,
Chris@0 116 const QXmlAttributes &attributes)
Chris@0 117 {
Chris@0 118 QString name = qName.toLower();
Chris@0 119
Chris@0 120 bool ok = false;
Chris@0 121
Chris@0 122 // Valid element names:
Chris@0 123 //
Chris@0 124 // sv
Chris@0 125 // data
Chris@0 126 // dataset
Chris@0 127 // display
Chris@0 128 // derivation
Chris@0 129 // playparameters
Chris@0 130 // layer
Chris@0 131 // model
Chris@0 132 // point
Chris@0 133 // row
Chris@0 134 // view
Chris@0 135 // window
Chris@0 136
Chris@0 137 if (name == "sv") {
Chris@0 138
Chris@0 139 // nothing needed
Chris@0 140 ok = true;
Chris@0 141
Chris@0 142 } else if (name == "data") {
Chris@0 143
Chris@0 144 // nothing needed
Chris@0 145 m_inData = true;
Chris@0 146 ok = true;
Chris@0 147
Chris@0 148 } else if (name == "display") {
Chris@0 149
Chris@0 150 // nothing needed
Chris@0 151 ok = true;
Chris@0 152
Chris@0 153 } else if (name == "window") {
Chris@0 154
Chris@0 155 ok = readWindow(attributes);
Chris@0 156
Chris@0 157 } else if (name == "model") {
Chris@0 158
Chris@0 159 ok = readModel(attributes);
Chris@0 160
Chris@0 161 } else if (name == "dataset") {
Chris@0 162
Chris@0 163 ok = readDatasetStart(attributes);
Chris@0 164
Chris@0 165 } else if (name == "bin") {
Chris@0 166
Chris@0 167 ok = addBinToDataset(attributes);
Chris@0 168
Chris@0 169 } else if (name == "point") {
Chris@0 170
Chris@0 171 ok = addPointToDataset(attributes);
Chris@0 172
Chris@0 173 } else if (name == "row") {
Chris@0 174
Chris@0 175 ok = addRowToDataset(attributes);
Chris@0 176
Chris@0 177 } else if (name == "layer") {
Chris@0 178
Chris@0 179 addUnaddedModels(); // all models must be specified before first layer
Chris@0 180 ok = readLayer(attributes);
Chris@0 181
Chris@0 182 } else if (name == "view") {
Chris@0 183
Chris@0 184 m_inView = true;
Chris@0 185 ok = readView(attributes);
Chris@0 186
Chris@0 187 } else if (name == "derivation") {
Chris@0 188
Chris@0 189 ok = readDerivation(attributes);
Chris@0 190
Chris@0 191 } else if (name == "playparameters") {
Chris@0 192
Chris@0 193 ok = readPlayParameters(attributes);
Chris@0 194
Chris@0 195 } else if (name == "plugin") {
Chris@0 196
Chris@0 197 ok = readPlugin(attributes);
Chris@0 198
Chris@0 199 } else if (name == "selections") {
Chris@0 200
Chris@0 201 m_inSelections = true;
Chris@0 202 ok = true;
Chris@0 203
Chris@0 204 } else if (name == "selection") {
Chris@0 205
Chris@0 206 ok = readSelection(attributes);
Chris@0 207 }
Chris@0 208
Chris@0 209 if (!ok) {
Chris@0 210 std::cerr << "WARNING: SV-XML: Failed to completely process element \""
Chris@0 211 << name.toLocal8Bit().data() << "\"" << std::endl;
Chris@0 212 }
Chris@0 213
Chris@0 214 return true;
Chris@0 215 }
Chris@0 216
Chris@0 217 bool
Chris@0 218 SVFileReader::characters(const QString &text)
Chris@0 219 {
Chris@0 220 bool ok = false;
Chris@0 221
Chris@0 222 if (m_inRow) {
Chris@0 223 ok = readRowData(text);
Chris@0 224 if (!ok) {
Chris@0 225 std::cerr << "WARNING: SV-XML: Failed to read row data content for row " << m_rowNumber << std::endl;
Chris@0 226 }
Chris@0 227 }
Chris@0 228
Chris@0 229 return true;
Chris@0 230 }
Chris@0 231
Chris@0 232 bool
Chris@0 233 SVFileReader::endElement(const QString &, const QString &,
Chris@0 234 const QString &qName)
Chris@0 235 {
Chris@0 236 QString name = qName.toLower();
Chris@0 237
Chris@0 238 if (name == "dataset") {
Chris@0 239
Chris@0 240 if (m_currentDataset) {
Chris@0 241
Chris@0 242 bool foundInAwaiting = false;
Chris@0 243
Chris@0 244 for (std::map<int, int>::iterator i = m_awaitingDatasets.begin();
Chris@0 245 i != m_awaitingDatasets.end(); ++i) {
Chris@0 246 if (m_models[i->second] == m_currentDataset) {
Chris@0 247 m_awaitingDatasets.erase(i);
Chris@0 248 foundInAwaiting = true;
Chris@0 249 break;
Chris@0 250 }
Chris@0 251 }
Chris@0 252
Chris@0 253 if (!foundInAwaiting) {
Chris@0 254 std::cerr << "WARNING: SV-XML: Dataset precedes model, or no model uses dataset" << std::endl;
Chris@0 255 }
Chris@0 256 }
Chris@0 257
Chris@0 258 m_currentDataset = 0;
Chris@0 259
Chris@0 260 } else if (name == "data") {
Chris@0 261
Chris@0 262 addUnaddedModels();
Chris@0 263 m_inData = false;
Chris@0 264
Chris@0 265 } else if (name == "derivation") {
Chris@55 266
Chris@55 267 if (!m_currentDerivedModel) {
Chris@55 268 if (m_currentDerivedModel < 0) {
Chris@55 269 std::cerr << "WARNING: SV-XML: Bad derivation output model id "
Chris@55 270 << m_currentDerivedModelId << std::endl;
Chris@55 271 } else if (m_models[m_currentDerivedModelId]) {
Chris@55 272 std::cerr << "WARNING: SV-XML: Derivation has existing model "
Chris@55 273 << m_currentDerivedModelId
Chris@55 274 << " as target, not regenerating" << std::endl;
Chris@55 275 } else {
Chris@55 276 m_currentDerivedModel = m_models[m_currentDerivedModelId] =
Chris@55 277 m_document->addDerivedModel(m_currentTransform,
Chris@55 278 m_currentTransformSource,
Chris@55 279 m_currentTransformContext,
Chris@55 280 m_currentTransformConfiguration);
Chris@55 281 }
Chris@55 282 } else {
Chris@0 283 m_document->addDerivedModel(m_currentTransform,
Chris@55 284 m_currentTransformSource,
Chris@30 285 m_currentTransformContext,
Chris@0 286 m_currentDerivedModel,
Chris@0 287 m_currentTransformConfiguration);
Chris@0 288 }
Chris@0 289
Chris@55 290 m_addedModels.insert(m_currentDerivedModel);
Chris@55 291 m_currentDerivedModel = 0;
Chris@55 292 m_currentDerivedModelId = -1;
Chris@55 293 m_currentTransform = "";
Chris@55 294 m_currentTransformConfiguration = "";
Chris@55 295
Chris@0 296 } else if (name == "row") {
Chris@0 297 m_inRow = false;
Chris@0 298 } else if (name == "view") {
Chris@0 299 m_inView = false;
Chris@0 300 } else if (name == "selections") {
Chris@0 301 m_inSelections = false;
Chris@0 302 } else if (name == "playparameters") {
Chris@0 303 m_currentPlayParameters = 0;
Chris@0 304 }
Chris@0 305
Chris@0 306 return true;
Chris@0 307 }
Chris@0 308
Chris@0 309 bool
Chris@0 310 SVFileReader::error(const QXmlParseException &exception)
Chris@0 311 {
Chris@0 312 m_errorString =
Chris@0 313 QString("ERROR: SV-XML: %1 at line %2, column %3")
Chris@0 314 .arg(exception.message())
Chris@0 315 .arg(exception.lineNumber())
Chris@0 316 .arg(exception.columnNumber());
Chris@0 317 std::cerr << m_errorString.toLocal8Bit().data() << std::endl;
Chris@0 318 return QXmlDefaultHandler::error(exception);
Chris@0 319 }
Chris@0 320
Chris@0 321 bool
Chris@0 322 SVFileReader::fatalError(const QXmlParseException &exception)
Chris@0 323 {
Chris@0 324 m_errorString =
Chris@0 325 QString("FATAL ERROR: SV-XML: %1 at line %2, column %3")
Chris@0 326 .arg(exception.message())
Chris@0 327 .arg(exception.lineNumber())
Chris@0 328 .arg(exception.columnNumber());
Chris@0 329 std::cerr << m_errorString.toLocal8Bit().data() << std::endl;
Chris@0 330 return QXmlDefaultHandler::fatalError(exception);
Chris@0 331 }
Chris@0 332
Chris@0 333
Chris@0 334 #define READ_MANDATORY(TYPE, NAME, CONVERSION) \
Chris@0 335 TYPE NAME = attributes.value(#NAME).trimmed().CONVERSION(&ok); \
Chris@0 336 if (!ok) { \
Chris@0 337 std::cerr << "WARNING: SV-XML: Missing or invalid mandatory " #TYPE " attribute \"" #NAME "\"" << std::endl; \
Chris@0 338 return false; \
Chris@0 339 }
Chris@0 340
Chris@0 341 bool
Chris@0 342 SVFileReader::readWindow(const QXmlAttributes &attributes)
Chris@0 343 {
Chris@0 344 bool ok = false;
Chris@0 345
Chris@0 346 READ_MANDATORY(int, width, toInt);
Chris@0 347 READ_MANDATORY(int, height, toInt);
Chris@0 348
Chris@0 349 m_paneCallback.setWindowSize(width, height);
Chris@0 350 return true;
Chris@0 351 }
Chris@0 352
Chris@0 353 void
Chris@0 354 SVFileReader::addUnaddedModels()
Chris@0 355 {
Chris@0 356 std::set<Model *> unaddedModels;
Chris@0 357
Chris@0 358 for (std::map<int, Model *>::iterator i = m_models.begin();
Chris@0 359 i != m_models.end(); ++i) {
Chris@0 360 if (m_addedModels.find(i->second) == m_addedModels.end()) {
Chris@0 361 unaddedModels.insert(i->second);
Chris@0 362 }
Chris@0 363 }
Chris@0 364
Chris@0 365 for (std::set<Model *>::iterator i = unaddedModels.begin();
Chris@0 366 i != unaddedModels.end(); ++i) {
Chris@0 367 m_document->addImportedModel(*i);
Chris@0 368 m_addedModels.insert(*i);
Chris@0 369 }
Chris@0 370 }
Chris@0 371
Chris@0 372 bool
Chris@0 373 SVFileReader::readModel(const QXmlAttributes &attributes)
Chris@0 374 {
Chris@0 375 bool ok = false;
Chris@0 376
Chris@0 377 READ_MANDATORY(int, id, toInt);
Chris@0 378
Chris@0 379 if (m_models.find(id) != m_models.end()) {
Chris@0 380 std::cerr << "WARNING: SV-XML: Ignoring duplicate model id " << id
Chris@0 381 << std::endl;
Chris@0 382 return false;
Chris@0 383 }
Chris@0 384
Chris@0 385 QString name = attributes.value("name");
Chris@0 386
Chris@0 387 READ_MANDATORY(int, sampleRate, toInt);
Chris@0 388
Chris@0 389 QString type = attributes.value("type").trimmed();
Chris@0 390 bool mainModel = (attributes.value("mainModel").trimmed() == "true");
Chris@55 391
Chris@0 392 if (type == "wavefile") {
Chris@0 393
Chris@87 394 WaveFileModel *model = 0;
Chris@88 395 FileFinder *ff = FileFinder::getInstance();
Chris@88 396 QString originalPath = attributes.value("file");
Chris@88 397 QString path = ff->find(FileFinder::AudioFile,
Chris@88 398 originalPath, m_location);
Chris@87 399 QUrl url(path);
Chris@87 400
Chris@87 401 if (RemoteFile::canHandleScheme(url)) {
Chris@87 402
Chris@87 403 RemoteFile rf(url);
Chris@87 404 rf.wait();
Chris@87 405
Chris@87 406 if (rf.isOK()) {
Chris@88 407
Chris@88 408 model = new WaveFileModel(rf.getLocalFilename(), path);
Chris@87 409 if (!model->isOK()) {
Chris@87 410 delete model;
Chris@87 411 model = 0;
Chris@87 412 //!!! and delete local file?
Chris@87 413 }
Chris@87 414 }
Chris@87 415 } else {
Chris@87 416
Chris@87 417 model = new WaveFileModel(path);
Chris@87 418 if (!model->isOK()) {
Chris@87 419 delete model;
Chris@87 420 model = 0;
Chris@87 421 }
Chris@87 422 }
Chris@87 423
Chris@87 424 if (!model) return false;
Chris@87 425
Chris@0 426 m_models[id] = model;
Chris@0 427 if (mainModel) {
Chris@0 428 m_document->setMainModel(model);
Chris@0 429 m_addedModels.insert(model);
Chris@0 430 }
Chris@0 431 // Derived models will be added when their derivation
Chris@0 432 // is found.
Chris@0 433
Chris@0 434 return true;
Chris@0 435
Chris@0 436 } else if (type == "dense") {
Chris@0 437
Chris@0 438 READ_MANDATORY(int, dimensions, toInt);
Chris@0 439
Chris@55 440 // Currently the only dense model we support here is the dense
Chris@55 441 // 3d model. Dense time-value models are always file-backed
Chris@55 442 // waveform data, at this point, and they come in as wavefile
Chris@55 443 // models.
Chris@0 444
Chris@0 445 if (dimensions == 3) {
Chris@0 446
Chris@0 447 READ_MANDATORY(int, windowSize, toInt);
Chris@0 448 READ_MANDATORY(int, yBinCount, toInt);
Chris@0 449
Chris@3 450 EditableDenseThreeDimensionalModel *model =
Chris@3 451 new EditableDenseThreeDimensionalModel
Chris@3 452 (sampleRate, windowSize, yBinCount);
Chris@0 453
Chris@0 454 float minimum = attributes.value("minimum").trimmed().toFloat(&ok);
Chris@0 455 if (ok) model->setMinimumLevel(minimum);
Chris@0 456
Chris@0 457 float maximum = attributes.value("maximum").trimmed().toFloat(&ok);
Chris@0 458 if (ok) model->setMaximumLevel(maximum);
Chris@0 459
Chris@0 460 int dataset = attributes.value("dataset").trimmed().toInt(&ok);
Chris@0 461 if (ok) m_awaitingDatasets[dataset] = id;
Chris@0 462
Chris@0 463 m_models[id] = model;
Chris@0 464 return true;
Chris@0 465
Chris@0 466 } else {
Chris@0 467
Chris@0 468 std::cerr << "WARNING: SV-XML: Unexpected dense model dimension ("
Chris@0 469 << dimensions << ")" << std::endl;
Chris@0 470 }
Chris@0 471 } else if (type == "sparse") {
Chris@0 472
Chris@0 473 READ_MANDATORY(int, dimensions, toInt);
Chris@0 474
Chris@0 475 if (dimensions == 1) {
Chris@0 476
Chris@0 477 READ_MANDATORY(int, resolution, toInt);
Chris@0 478
Chris@0 479 SparseOneDimensionalModel *model = new SparseOneDimensionalModel
Chris@0 480 (sampleRate, resolution);
Chris@0 481 m_models[id] = model;
Chris@0 482
Chris@0 483 int dataset = attributes.value("dataset").trimmed().toInt(&ok);
Chris@0 484 if (ok) m_awaitingDatasets[dataset] = id;
Chris@0 485
Chris@0 486 return true;
Chris@0 487
Chris@0 488 } else if (dimensions == 2 || dimensions == 3) {
Chris@0 489
Chris@0 490 READ_MANDATORY(int, resolution, toInt);
Chris@0 491
Chris@0 492 float minimum = attributes.value("minimum").trimmed().toFloat(&ok);
Chris@0 493 float maximum = attributes.value("maximum").trimmed().toFloat(&ok);
Chris@0 494 float valueQuantization =
Chris@0 495 attributes.value("valueQuantization").trimmed().toFloat(&ok);
Chris@0 496
Chris@0 497 bool notifyOnAdd = (attributes.value("notifyOnAdd") == "true");
Chris@0 498
Chris@0 499 QString units = attributes.value("units");
Chris@0 500
Chris@0 501 if (dimensions == 2) {
Chris@0 502 if (attributes.value("subtype") == "text") {
Chris@0 503 TextModel *model = new TextModel
Chris@0 504 (sampleRate, resolution, notifyOnAdd);
Chris@0 505 m_models[id] = model;
Chris@0 506 } else {
Chris@0 507 SparseTimeValueModel *model = new SparseTimeValueModel
Chris@0 508 (sampleRate, resolution, minimum, maximum, notifyOnAdd);
Chris@0 509 model->setScaleUnits(units);
Chris@0 510 m_models[id] = model;
Chris@0 511 }
Chris@0 512 } else {
Chris@0 513 NoteModel *model = new NoteModel
Chris@0 514 (sampleRate, resolution, minimum, maximum, notifyOnAdd);
Chris@0 515 model->setValueQuantization(valueQuantization);
Chris@0 516 model->setScaleUnits(units);
Chris@0 517 m_models[id] = model;
Chris@0 518 }
Chris@0 519
Chris@0 520 int dataset = attributes.value("dataset").trimmed().toInt(&ok);
Chris@0 521 if (ok) m_awaitingDatasets[dataset] = id;
Chris@0 522
Chris@0 523 return true;
Chris@0 524
Chris@0 525 } else {
Chris@0 526
Chris@0 527 std::cerr << "WARNING: SV-XML: Unexpected sparse model dimension ("
Chris@0 528 << dimensions << ")" << std::endl;
Chris@0 529 }
Chris@0 530 } else {
Chris@0 531
Chris@0 532 std::cerr << "WARNING: SV-XML: Unexpected model type \""
Chris@88 533 << type.toLocal8Bit().data() << "\" for model id " << id << std::endl;
Chris@0 534 }
Chris@0 535
Chris@0 536 return false;
Chris@0 537 }
Chris@0 538
Chris@0 539 bool
Chris@0 540 SVFileReader::readView(const QXmlAttributes &attributes)
Chris@0 541 {
Chris@0 542 QString type = attributes.value("type");
Chris@0 543 m_currentPane = 0;
Chris@0 544
Chris@0 545 if (type != "pane") {
Chris@0 546 std::cerr << "WARNING: SV-XML: Unexpected view type \""
Chris@0 547 << type.toLocal8Bit().data() << "\"" << std::endl;
Chris@0 548 return false;
Chris@0 549 }
Chris@0 550
Chris@0 551 m_currentPane = m_paneCallback.addPane();
Chris@0 552
Chris@0 553 if (!m_currentPane) {
Chris@0 554 std::cerr << "WARNING: SV-XML: Internal error: Failed to add pane!"
Chris@0 555 << std::endl;
Chris@0 556 return false;
Chris@0 557 }
Chris@0 558
Chris@0 559 bool ok = false;
Chris@0 560
Chris@0 561 View *view = m_currentPane;
Chris@0 562
Chris@0 563 // The view properties first
Chris@0 564
Chris@0 565 READ_MANDATORY(size_t, centre, toUInt);
Chris@0 566 READ_MANDATORY(size_t, zoom, toUInt);
Chris@0 567 READ_MANDATORY(int, followPan, toInt);
Chris@0 568 READ_MANDATORY(int, followZoom, toInt);
Chris@0 569 QString tracking = attributes.value("tracking");
Chris@0 570
Chris@0 571 // Specify the follow modes before we set the actual values
Chris@0 572 view->setFollowGlobalPan(followPan);
Chris@0 573 view->setFollowGlobalZoom(followZoom);
Chris@109 574 view->setPlaybackFollow(tracking == "scroll" ? PlaybackScrollContinuous :
Chris@109 575 tracking == "page" ? PlaybackScrollPage
Chris@109 576 : PlaybackIgnore);
Chris@0 577
Chris@0 578 // Then set these values
Chris@0 579 view->setCentreFrame(centre);
Chris@0 580 view->setZoomLevel(zoom);
Chris@0 581
Chris@0 582 // And pane properties
Chris@0 583 READ_MANDATORY(int, centreLineVisible, toInt);
Chris@0 584 m_currentPane->setCentreLineVisible(centreLineVisible);
Chris@0 585
Chris@0 586 int height = attributes.value("height").toInt(&ok);
Chris@0 587 if (ok) {
Chris@0 588 m_currentPane->resize(m_currentPane->width(), height);
Chris@0 589 }
Chris@0 590
Chris@0 591 return true;
Chris@0 592 }
Chris@0 593
Chris@0 594 bool
Chris@0 595 SVFileReader::readLayer(const QXmlAttributes &attributes)
Chris@0 596 {
Chris@0 597 QString type = attributes.value("type");
Chris@0 598
Chris@0 599 int id;
Chris@0 600 bool ok = false;
Chris@0 601 id = attributes.value("id").trimmed().toInt(&ok);
Chris@0 602
Chris@0 603 if (!ok) {
Chris@0 604 std::cerr << "WARNING: SV-XML: No layer id for layer of type \""
Chris@0 605 << type.toLocal8Bit().data()
Chris@0 606 << "\"" << std::endl;
Chris@0 607 return false;
Chris@0 608 }
Chris@0 609
Chris@0 610 Layer *layer = 0;
Chris@0 611 bool isNewLayer = false;
Chris@0 612
Chris@0 613 // Layers are expected to be defined in layer elements in the data
Chris@0 614 // section, and referred to in layer elements in the view
Chris@0 615 // sections. So if we're in the data section, we expect this
Chris@0 616 // layer not to exist already; if we're in the view section, we
Chris@0 617 // expect it to exist.
Chris@0 618
Chris@0 619 if (m_inData) {
Chris@0 620
Chris@0 621 if (m_layers.find(id) != m_layers.end()) {
Chris@0 622 std::cerr << "WARNING: SV-XML: Ignoring duplicate layer id " << id
Chris@0 623 << " in data section" << std::endl;
Chris@0 624 return false;
Chris@0 625 }
Chris@0 626
Chris@0 627 layer = m_layers[id] = m_document->createLayer
Chris@0 628 (LayerFactory::getInstance()->getLayerTypeForName(type));
Chris@0 629
Chris@0 630 if (layer) {
Chris@0 631 m_layers[id] = layer;
Chris@0 632 isNewLayer = true;
Chris@0 633 }
Chris@0 634
Chris@0 635 } else {
Chris@0 636
Chris@0 637 if (!m_currentPane) {
Chris@0 638 std::cerr << "WARNING: SV-XML: No current pane for layer " << id
Chris@0 639 << " in view section" << std::endl;
Chris@0 640 return false;
Chris@0 641 }
Chris@0 642
Chris@0 643 if (m_layers.find(id) != m_layers.end()) {
Chris@0 644
Chris@0 645 layer = m_layers[id];
Chris@0 646
Chris@0 647 } else {
Chris@0 648 std::cerr << "WARNING: SV-XML: Layer id " << id
Chris@0 649 << " in view section has not been defined -- defining it here"
Chris@0 650 << std::endl;
Chris@0 651
Chris@0 652 layer = m_document->createLayer
Chris@0 653 (LayerFactory::getInstance()->getLayerTypeForName(type));
Chris@0 654
Chris@0 655 if (layer) {
Chris@0 656 m_layers[id] = layer;
Chris@0 657 isNewLayer = true;
Chris@0 658 }
Chris@0 659 }
Chris@0 660 }
Chris@0 661
Chris@0 662 if (!layer) {
Chris@0 663 std::cerr << "WARNING: SV-XML: Failed to add layer of type \""
Chris@0 664 << type.toLocal8Bit().data()
Chris@0 665 << "\"" << std::endl;
Chris@0 666 return false;
Chris@0 667 }
Chris@0 668
Chris@0 669 if (isNewLayer) {
Chris@0 670
Chris@0 671 QString name = attributes.value("name");
Chris@0 672 layer->setObjectName(name);
Chris@0 673
Chris@0 674 int modelId;
Chris@0 675 bool modelOk = false;
Chris@0 676 modelId = attributes.value("model").trimmed().toInt(&modelOk);
Chris@0 677
Chris@0 678 if (modelOk) {
Chris@0 679 if (m_models.find(modelId) != m_models.end()) {
Chris@0 680 Model *model = m_models[modelId];
Chris@0 681 m_document->setModel(layer, model);
Chris@0 682 } else {
Chris@0 683 std::cerr << "WARNING: SV-XML: Unknown model id " << modelId
Chris@0 684 << " in layer definition" << std::endl;
Chris@0 685 }
Chris@0 686 }
Chris@0 687
Chris@0 688 layer->setProperties(attributes);
Chris@0 689 }
Chris@0 690
Chris@0 691 if (!m_inData && m_currentPane) {
Chris@84 692
Chris@84 693 QString visible = attributes.value("visible");
Chris@84 694 bool dormant = (visible == "false");
Chris@84 695
Chris@84 696 // We need to do this both before and after adding the layer
Chris@84 697 // to the view -- we need it to be dormant if appropriate
Chris@84 698 // before it's actually added to the view so that any property
Chris@84 699 // box gets the right state when it's added, but the add layer
Chris@84 700 // command sets dormant to false because it assumes it may be
Chris@84 701 // restoring a previously dormant layer, so we need to set it
Chris@84 702 // again afterwards too. Hm
Chris@84 703 layer->setLayerDormant(m_currentPane, dormant);
Chris@84 704
Chris@0 705 m_document->addLayerToView(m_currentPane, layer);
Chris@84 706
Chris@84 707 layer->setLayerDormant(m_currentPane, dormant);
Chris@0 708 }
Chris@0 709
Chris@0 710 return true;
Chris@0 711 }
Chris@0 712
Chris@0 713 bool
Chris@0 714 SVFileReader::readDatasetStart(const QXmlAttributes &attributes)
Chris@0 715 {
Chris@0 716 bool ok = false;
Chris@0 717
Chris@0 718 READ_MANDATORY(int, id, toInt);
Chris@0 719 READ_MANDATORY(int, dimensions, toInt);
Chris@0 720
Chris@0 721 if (m_awaitingDatasets.find(id) == m_awaitingDatasets.end()) {
Chris@0 722 std::cerr << "WARNING: SV-XML: Unwanted dataset " << id << std::endl;
Chris@0 723 return false;
Chris@0 724 }
Chris@0 725
Chris@0 726 int modelId = m_awaitingDatasets[id];
Chris@0 727
Chris@0 728 Model *model = 0;
Chris@0 729 if (m_models.find(modelId) != m_models.end()) {
Chris@0 730 model = m_models[modelId];
Chris@0 731 } else {
Chris@0 732 std::cerr << "WARNING: SV-XML: Internal error: Unknown model " << modelId
Chris@0 733 << " expecting dataset " << id << std::endl;
Chris@0 734 return false;
Chris@0 735 }
Chris@0 736
Chris@0 737 bool good = false;
Chris@0 738
Chris@0 739 switch (dimensions) {
Chris@0 740 case 1:
Chris@0 741 if (dynamic_cast<SparseOneDimensionalModel *>(model)) good = true;
Chris@0 742 break;
Chris@0 743
Chris@0 744 case 2:
Chris@0 745 if (dynamic_cast<SparseTimeValueModel *>(model)) good = true;
Chris@0 746 else if (dynamic_cast<TextModel *>(model)) good = true;
Chris@0 747 break;
Chris@0 748
Chris@0 749 case 3:
Chris@0 750 if (dynamic_cast<NoteModel *>(model)) good = true;
Chris@3 751 else if (dynamic_cast<EditableDenseThreeDimensionalModel *>(model)) {
Chris@0 752 m_datasetSeparator = attributes.value("separator");
Chris@0 753 good = true;
Chris@0 754 }
Chris@0 755 break;
Chris@0 756 }
Chris@0 757
Chris@0 758 if (!good) {
Chris@0 759 std::cerr << "WARNING: SV-XML: Model id " << modelId << " has wrong number of dimensions for " << dimensions << "-D dataset " << id << std::endl;
Chris@0 760 m_currentDataset = 0;
Chris@0 761 return false;
Chris@0 762 }
Chris@0 763
Chris@0 764 m_currentDataset = model;
Chris@0 765 return true;
Chris@0 766 }
Chris@0 767
Chris@0 768 bool
Chris@0 769 SVFileReader::addPointToDataset(const QXmlAttributes &attributes)
Chris@0 770 {
Chris@0 771 bool ok = false;
Chris@0 772
Chris@0 773 READ_MANDATORY(int, frame, toInt);
Chris@0 774
Chris@0 775 SparseOneDimensionalModel *sodm = dynamic_cast<SparseOneDimensionalModel *>
Chris@0 776 (m_currentDataset);
Chris@0 777
Chris@0 778 if (sodm) {
Chris@0 779 QString label = attributes.value("label");
Chris@0 780 sodm->addPoint(SparseOneDimensionalModel::Point(frame, label));
Chris@0 781 return true;
Chris@0 782 }
Chris@0 783
Chris@0 784 SparseTimeValueModel *stvm = dynamic_cast<SparseTimeValueModel *>
Chris@0 785 (m_currentDataset);
Chris@0 786
Chris@0 787 if (stvm) {
Chris@0 788 float value = 0.0;
Chris@0 789 value = attributes.value("value").trimmed().toFloat(&ok);
Chris@0 790 QString label = attributes.value("label");
Chris@0 791 stvm->addPoint(SparseTimeValueModel::Point(frame, value, label));
Chris@0 792 return ok;
Chris@0 793 }
Chris@0 794
Chris@0 795 NoteModel *nm = dynamic_cast<NoteModel *>(m_currentDataset);
Chris@0 796
Chris@0 797 if (nm) {
Chris@0 798 float value = 0.0;
Chris@0 799 value = attributes.value("value").trimmed().toFloat(&ok);
Chris@137 800 size_t duration = 0;
Chris@137 801 duration = attributes.value("duration").trimmed().toUInt(&ok);
Chris@0 802 QString label = attributes.value("label");
Chris@0 803 nm->addPoint(NoteModel::Point(frame, value, duration, label));
Chris@0 804 return ok;
Chris@0 805 }
Chris@0 806
Chris@0 807 TextModel *tm = dynamic_cast<TextModel *>(m_currentDataset);
Chris@0 808
Chris@0 809 if (tm) {
Chris@0 810 float height = 0.0;
Chris@0 811 height = attributes.value("height").trimmed().toFloat(&ok);
Chris@0 812 QString label = attributes.value("label");
Chris@0 813 tm->addPoint(TextModel::Point(frame, height, label));
Chris@0 814 return ok;
Chris@0 815 }
Chris@0 816
Chris@0 817 std::cerr << "WARNING: SV-XML: Point element found in non-point dataset" << std::endl;
Chris@0 818
Chris@0 819 return false;
Chris@0 820 }
Chris@0 821
Chris@0 822 bool
Chris@0 823 SVFileReader::addBinToDataset(const QXmlAttributes &attributes)
Chris@0 824 {
Chris@3 825 EditableDenseThreeDimensionalModel *dtdm =
Chris@3 826 dynamic_cast<EditableDenseThreeDimensionalModel *>
Chris@0 827 (m_currentDataset);
Chris@0 828
Chris@0 829 if (dtdm) {
Chris@0 830
Chris@0 831 bool ok = false;
Chris@0 832 int n = attributes.value("number").trimmed().toInt(&ok);
Chris@0 833 if (!ok) {
Chris@0 834 std::cerr << "WARNING: SV-XML: Missing or invalid bin number"
Chris@0 835 << std::endl;
Chris@0 836 return false;
Chris@0 837 }
Chris@0 838
Chris@0 839 QString name = attributes.value("name");
Chris@0 840
Chris@0 841 dtdm->setBinName(n, name);
Chris@0 842 return true;
Chris@0 843 }
Chris@0 844
Chris@0 845 std::cerr << "WARNING: SV-XML: Bin definition found in incompatible dataset" << std::endl;
Chris@0 846
Chris@0 847 return false;
Chris@0 848 }
Chris@0 849
Chris@0 850
Chris@0 851 bool
Chris@0 852 SVFileReader::addRowToDataset(const QXmlAttributes &attributes)
Chris@0 853 {
Chris@0 854 m_inRow = false;
Chris@0 855
Chris@0 856 bool ok = false;
Chris@0 857 m_rowNumber = attributes.value("n").trimmed().toInt(&ok);
Chris@0 858 if (!ok) {
Chris@0 859 std::cerr << "WARNING: SV-XML: Missing or invalid row number"
Chris@0 860 << std::endl;
Chris@0 861 return false;
Chris@0 862 }
Chris@0 863
Chris@0 864 m_inRow = true;
Chris@0 865
Chris@0 866 // std::cerr << "SV-XML: In row " << m_rowNumber << std::endl;
Chris@0 867
Chris@0 868 return true;
Chris@0 869 }
Chris@0 870
Chris@0 871 bool
Chris@0 872 SVFileReader::readRowData(const QString &text)
Chris@0 873 {
Chris@3 874 EditableDenseThreeDimensionalModel *dtdm =
Chris@3 875 dynamic_cast<EditableDenseThreeDimensionalModel *>
Chris@0 876 (m_currentDataset);
Chris@0 877
Chris@0 878 bool warned = false;
Chris@0 879
Chris@0 880 if (dtdm) {
Chris@0 881 QStringList data = text.split(m_datasetSeparator);
Chris@0 882
Chris@51 883 DenseThreeDimensionalModel::Column values;
Chris@0 884
Chris@0 885 for (QStringList::iterator i = data.begin(); i != data.end(); ++i) {
Chris@0 886
Chris@51 887 if (values.size() == dtdm->getHeight()) {
Chris@0 888 if (!warned) {
Chris@0 889 std::cerr << "WARNING: SV-XML: Too many y-bins in 3-D dataset row "
Chris@0 890 << m_rowNumber << std::endl;
Chris@0 891 warned = true;
Chris@0 892 }
Chris@0 893 }
Chris@0 894
Chris@0 895 bool ok;
Chris@0 896 float value = i->toFloat(&ok);
Chris@0 897 if (!ok) {
Chris@0 898 std::cerr << "WARNING: SV-XML: Bad floating-point value "
Chris@0 899 << i->toLocal8Bit().data()
Chris@0 900 << " in row data" << std::endl;
Chris@0 901 } else {
Chris@0 902 values.push_back(value);
Chris@0 903 }
Chris@0 904 }
Chris@0 905
Chris@51 906 dtdm->setColumn(m_rowNumber, values);
Chris@0 907 return true;
Chris@0 908 }
Chris@0 909
Chris@0 910 std::cerr << "WARNING: SV-XML: Row data found in non-row dataset" << std::endl;
Chris@0 911
Chris@0 912 return false;
Chris@0 913 }
Chris@0 914
Chris@0 915 bool
Chris@0 916 SVFileReader::readDerivation(const QXmlAttributes &attributes)
Chris@0 917 {
Chris@0 918 int modelId = 0;
Chris@0 919 bool modelOk = false;
Chris@0 920 modelId = attributes.value("model").trimmed().toInt(&modelOk);
Chris@0 921
Chris@0 922 if (!modelOk) {
Chris@0 923 std::cerr << "WARNING: SV-XML: No model id specified for derivation" << std::endl;
Chris@0 924 return false;
Chris@0 925 }
Chris@55 926
Chris@55 927 QString transform = attributes.value("transform");
Chris@55 928
Chris@55 929 if (m_models.find(modelId) != m_models.end()) {
Chris@55 930 m_currentDerivedModel = m_models[modelId];
Chris@55 931 } else {
Chris@55 932 // we'll regenerate the model when the derivation element ends
Chris@55 933 m_currentDerivedModel = 0;
Chris@55 934 }
Chris@0 935
Chris@55 936 m_currentDerivedModelId = modelId;
Chris@0 937
Chris@55 938 int sourceId = 0;
Chris@55 939 bool sourceOk = false;
Chris@55 940 sourceId = attributes.value("source").trimmed().toInt(&sourceOk);
Chris@0 941
Chris@55 942 if (sourceOk && m_models[sourceId]) {
Chris@55 943 m_currentTransformSource = m_models[sourceId];
Chris@55 944 } else {
Chris@55 945 m_currentTransformSource = m_document->getMainModel();
Chris@55 946 }
Chris@0 947
Chris@55 948 m_currentTransform = transform;
Chris@55 949 m_currentTransformConfiguration = "";
Chris@30 950
Chris@55 951 m_currentTransformContext = PluginTransform::ExecutionContext();
Chris@30 952
Chris@55 953 bool ok = false;
Chris@55 954 int channel = attributes.value("channel").trimmed().toInt(&ok);
Chris@55 955 if (ok) m_currentTransformContext.channel = channel;
Chris@30 956
Chris@55 957 int domain = attributes.value("domain").trimmed().toInt(&ok);
Chris@55 958 if (ok) m_currentTransformContext.domain = Vamp::Plugin::InputDomain(domain);
Chris@30 959
Chris@55 960 int stepSize = attributes.value("stepSize").trimmed().toInt(&ok);
Chris@55 961 if (ok) m_currentTransformContext.stepSize = stepSize;
Chris@30 962
Chris@55 963 int blockSize = attributes.value("blockSize").trimmed().toInt(&ok);
Chris@55 964 if (ok) m_currentTransformContext.blockSize = blockSize;
Chris@0 965
Chris@55 966 int windowType = attributes.value("windowType").trimmed().toInt(&ok);
Chris@55 967 if (ok) m_currentTransformContext.windowType = WindowType(windowType);
Chris@0 968
Chris@0 969 return true;
Chris@0 970 }
Chris@0 971
Chris@0 972 bool
Chris@0 973 SVFileReader::readPlayParameters(const QXmlAttributes &attributes)
Chris@0 974 {
Chris@0 975 m_currentPlayParameters = 0;
Chris@0 976
Chris@0 977 int modelId = 0;
Chris@0 978 bool modelOk = false;
Chris@0 979 modelId = attributes.value("model").trimmed().toInt(&modelOk);
Chris@0 980
Chris@0 981 if (!modelOk) {
Chris@0 982 std::cerr << "WARNING: SV-XML: No model id specified for play parameters" << std::endl;
Chris@0 983 return false;
Chris@0 984 }
Chris@0 985
Chris@0 986 if (m_models.find(modelId) != m_models.end()) {
Chris@0 987
Chris@0 988 bool ok = false;
Chris@0 989
Chris@0 990 PlayParameters *parameters = PlayParameterRepository::getInstance()->
Chris@0 991 getPlayParameters(m_models[modelId]);
Chris@0 992
Chris@0 993 if (!parameters) {
Chris@0 994 std::cerr << "WARNING: SV-XML: Play parameters for model "
Chris@0 995 << modelId
Chris@0 996 << " not found - has model been added to document?"
Chris@0 997 << std::endl;
Chris@0 998 return false;
Chris@0 999 }
Chris@0 1000
Chris@0 1001 bool muted = (attributes.value("mute").trimmed() == "true");
Chris@83 1002 parameters->setPlayMuted(muted);
Chris@0 1003
Chris@0 1004 float pan = attributes.value("pan").toFloat(&ok);
Chris@0 1005 if (ok) parameters->setPlayPan(pan);
Chris@0 1006
Chris@0 1007 float gain = attributes.value("gain").toFloat(&ok);
Chris@0 1008 if (ok) parameters->setPlayGain(gain);
Chris@0 1009
Chris@0 1010 QString pluginId = attributes.value("pluginId");
Chris@0 1011 if (pluginId != "") parameters->setPlayPluginId(pluginId);
Chris@0 1012
Chris@0 1013 m_currentPlayParameters = parameters;
Chris@0 1014
Chris@0 1015 // std::cerr << "Current play parameters for model: " << m_models[modelId] << ": " << m_currentPlayParameters << std::endl;
Chris@0 1016
Chris@0 1017 } else {
Chris@0 1018
Chris@0 1019 std::cerr << "WARNING: SV-XML: Unknown model " << modelId
Chris@0 1020 << " for play parameters" << std::endl;
Chris@0 1021 return false;
Chris@0 1022 }
Chris@0 1023
Chris@0 1024 return true;
Chris@0 1025 }
Chris@0 1026
Chris@0 1027 bool
Chris@0 1028 SVFileReader::readPlugin(const QXmlAttributes &attributes)
Chris@0 1029 {
Chris@55 1030 if (m_currentDerivedModelId < 0 && !m_currentPlayParameters) {
Chris@0 1031 std::cerr << "WARNING: SV-XML: Plugin found outside derivation or play parameters" << std::endl;
Chris@0 1032 return false;
Chris@0 1033 }
Chris@0 1034
Chris@0 1035 QString configurationXml = "<plugin";
Chris@0 1036
Chris@0 1037 for (int i = 0; i < attributes.length(); ++i) {
Chris@0 1038 configurationXml += QString(" %1=\"%2\"")
Chris@55 1039 .arg(attributes.qName(i))
Chris@55 1040 .arg(XmlExportable::encodeEntities(attributes.value(i)));
Chris@0 1041 }
Chris@0 1042
Chris@0 1043 configurationXml += "/>";
Chris@0 1044
Chris@0 1045 if (m_currentPlayParameters) {
Chris@0 1046 m_currentPlayParameters->setPlayPluginConfiguration(configurationXml);
Chris@0 1047 } else {
Chris@0 1048 m_currentTransformConfiguration += configurationXml;
Chris@0 1049 }
Chris@0 1050
Chris@0 1051 return true;
Chris@0 1052 }
Chris@0 1053
Chris@0 1054 bool
Chris@0 1055 SVFileReader::readSelection(const QXmlAttributes &attributes)
Chris@0 1056 {
Chris@0 1057 bool ok;
Chris@0 1058
Chris@0 1059 READ_MANDATORY(int, start, toInt);
Chris@0 1060 READ_MANDATORY(int, end, toInt);
Chris@0 1061
Chris@0 1062 m_paneCallback.addSelection(start, end);
Chris@0 1063
Chris@0 1064 return true;
Chris@0 1065 }
Chris@0 1066
Chris@137 1067 SVFileReaderPaneCallback::~SVFileReaderPaneCallback()
Chris@137 1068 {
Chris@137 1069 }
Chris@137 1070