Chris@45: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@45: Chris@45: /* Chris@45: Sonic Visualiser Chris@45: An audio file viewer and annotation editor. Chris@45: Centre for Digital Music, Queen Mary, University of London. Chris@45: This file copyright 2006 Chris Cannam and QMUL. Chris@45: Chris@45: This program is free software; you can redistribute it and/or Chris@45: modify it under the terms of the GNU General Public License as Chris@45: published by the Free Software Foundation; either version 2 of the Chris@45: License, or (at your option) any later version. See the file Chris@45: COPYING included with this distribution for more information. Chris@45: */ Chris@45: Chris@45: #include "SVFileReader.h" Chris@45: Chris@45: #include "layer/Layer.h" Chris@45: #include "view/View.h" Chris@45: #include "base/PlayParameters.h" Chris@45: #include "base/PlayParameterRepository.h" Chris@116: #include "base/Preferences.h" Chris@45: Chris@45: #include "data/fileio/AudioFileReaderFactory.h" Chris@45: #include "data/fileio/FileSource.h" Chris@45: Chris@170: #include "data/fileio/FileFinder.h" Chris@109: Chris@479: #include "data/model/ReadOnlyWaveFileModel.h" Chris@45: #include "data/model/EditableDenseThreeDimensionalModel.h" Chris@45: #include "data/model/SparseOneDimensionalModel.h" Chris@45: #include "data/model/SparseTimeValueModel.h" Chris@45: #include "data/model/NoteModel.h" Chris@137: #include "data/model/RegionModel.h" Chris@45: #include "data/model/TextModel.h" Chris@45: #include "data/model/ImageModel.h" Chris@111: #include "data/model/AlignmentModel.h" Chris@629: #include "data/model/AggregateWaveModel.h" Chris@45: Chris@106: #include "transform/TransformFactory.h" Chris@72: Chris@45: #include "view/Pane.h" Chris@45: Chris@109: #include "widgets/ProgressDialog.h" Chris@109: Chris@45: #include "Document.h" Chris@45: Chris@45: #include Chris@45: #include Chris@45: #include Chris@45: Chris@45: #include Chris@45: Chris@45: SVFileReader::SVFileReader(Document *document, Chris@595: SVFileReaderPaneCallback &callback, Chris@45: QString location) : Chris@45: m_document(document), Chris@45: m_paneCallback(callback), Chris@45: m_location(location), Chris@636: m_currentPane(nullptr), Chris@685: m_currentDataset(XmlExportable::NO_ID), Chris@636: m_currentLayer(nullptr), Chris@685: m_pendingDerivedModel(XmlExportable::NO_ID), Chris@410: m_currentTransformChannel(0), Chris@410: m_currentTransformIsNewStyle(true), Chris@45: m_datasetSeparator(" "), Chris@45: m_inRow(false), Chris@45: m_inLayer(false), Chris@45: m_inView(false), Chris@410: m_inData(false), Chris@410: m_inSelections(false), Chris@45: m_rowNumber(0), Chris@45: m_ok(false) Chris@45: { Chris@45: } Chris@45: Chris@45: void Chris@45: SVFileReader::parse(const QString &xmlData) Chris@45: { Chris@45: QXmlInputSource inputSource; Chris@45: inputSource.setData(xmlData); Chris@45: parse(inputSource); Chris@45: } Chris@45: Chris@45: void Chris@45: SVFileReader::parse(QXmlInputSource &inputSource) Chris@45: { Chris@45: QXmlSimpleReader reader; Chris@45: reader.setContentHandler(this); Chris@45: reader.setErrorHandler(this); Chris@45: m_ok = reader.parse(inputSource); Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::isOK() Chris@45: { Chris@45: return m_ok; Chris@45: } Chris@595: Chris@45: SVFileReader::~SVFileReader() Chris@45: { Chris@45: if (!m_awaitingDatasets.empty()) { Chris@601: SVCERR << "WARNING: SV-XML: File ended with " Chris@595: << m_awaitingDatasets.size() << " unfilled model dataset(s)" Chris@595: << endl; Chris@45: } Chris@45: Chris@685: std::set unaddedModels; Chris@45: Chris@685: for (auto i: m_models) { Chris@685: if (m_addedModels.find(i.second) == m_addedModels.end()) { Chris@685: unaddedModels.insert(i.second); Chris@595: } Chris@45: } Chris@45: Chris@45: if (!unaddedModels.empty()) { Chris@601: SVCERR << "WARNING: SV-XML: File contained " Chris@685: << unaddedModels.size() << " unused models" Chris@685: << endl; Chris@685: for (auto m: unaddedModels) { Chris@685: ModelById::release(m); Chris@595: } Chris@685: } Chris@685: Chris@685: if (!m_paths.empty()) { Chris@685: SVCERR << "WARNING: SV-XML: File contained " Chris@685: << m_paths.size() << " unused paths" Chris@685: << endl; Chris@685: for (auto p: m_paths) { Chris@685: delete p.second; Chris@685: } Chris@685: } Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::startElement(const QString &, const QString &, Chris@595: const QString &qName, Chris@595: const QXmlAttributes &attributes) Chris@45: { Chris@45: QString name = qName.toLower(); Chris@45: Chris@45: bool ok = false; Chris@45: Chris@45: // Valid element names: Chris@45: // Chris@45: // sv Chris@45: // data Chris@45: // dataset Chris@45: // display Chris@45: // derivation Chris@45: // playparameters Chris@45: // layer Chris@45: // model Chris@45: // point Chris@45: // row Chris@45: // view Chris@45: // window Chris@72: // plugin Chris@72: // transform Chris@72: // selections Chris@72: // selection Chris@72: // measurement Chris@45: Chris@45: if (name == "sv") { Chris@45: Chris@595: // nothing needed Chris@595: ok = true; Chris@45: Chris@45: } else if (name == "data") { Chris@45: Chris@595: // nothing needed Chris@595: m_inData = true; Chris@595: ok = true; Chris@45: Chris@45: } else if (name == "display") { Chris@45: Chris@595: // nothing needed Chris@595: ok = true; Chris@45: Chris@45: } else if (name == "window") { Chris@45: Chris@595: ok = readWindow(attributes); Chris@45: Chris@45: } else if (name == "model") { Chris@45: Chris@595: ok = readModel(attributes); Chris@45: Chris@45: } else if (name == "dataset") { Chris@595: Chris@595: ok = readDatasetStart(attributes); Chris@45: Chris@45: } else if (name == "bin") { Chris@595: Chris@595: ok = addBinToDataset(attributes); Chris@45: Chris@45: } else if (name == "point") { Chris@595: Chris@595: ok = addPointToDataset(attributes); Chris@45: Chris@45: } else if (name == "row") { Chris@45: Chris@595: ok = addRowToDataset(attributes); Chris@45: Chris@45: } else if (name == "layer") { Chris@45: Chris@45: addUnaddedModels(); // all models must be specified before first layer Chris@595: ok = readLayer(attributes); Chris@45: Chris@45: } else if (name == "view") { Chris@45: Chris@595: m_inView = true; Chris@595: ok = readView(attributes); Chris@45: Chris@45: } else if (name == "derivation") { Chris@45: Chris@629: makeAggregateModels(); // must be done before derivations that use them Chris@595: ok = readDerivation(attributes); Chris@45: Chris@45: } else if (name == "playparameters") { Chris@45: Chris@45: ok = readPlayParameters(attributes); Chris@45: Chris@45: } else if (name == "plugin") { Chris@45: Chris@595: ok = readPlugin(attributes); Chris@45: Chris@45: } else if (name == "selections") { Chris@45: Chris@595: m_inSelections = true; Chris@595: ok = true; Chris@45: Chris@45: } else if (name == "selection") { Chris@45: Chris@595: ok = readSelection(attributes); Chris@45: Chris@45: } else if (name == "measurement") { Chris@45: Chris@45: ok = readMeasurement(attributes); Chris@45: Chris@72: } else if (name == "transform") { Chris@72: Chris@72: ok = readTransform(attributes); Chris@72: Chris@72: } else if (name == "parameter") { Chris@72: Chris@72: ok = readParameter(attributes); Chris@72: Chris@45: } else { Chris@601: SVCERR << "WARNING: SV-XML: Unexpected element \"" Chris@294: << name << "\"" << endl; Chris@45: } Chris@45: Chris@45: if (!ok) { Chris@601: SVCERR << "WARNING: SV-XML: Failed to completely process element \"" Chris@595: << name << "\"" << endl; Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::characters(const QString &text) Chris@45: { Chris@45: bool ok = false; Chris@45: Chris@45: if (m_inRow) { Chris@595: ok = readRowData(text); Chris@595: if (!ok) { Chris@601: SVCERR << "WARNING: SV-XML: Failed to read row data content for row " << m_rowNumber << endl; Chris@595: } Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::endElement(const QString &, const QString &, Chris@595: const QString &qName) Chris@45: { Chris@45: QString name = qName.toLower(); Chris@45: Chris@45: if (name == "dataset") { Chris@45: Chris@685: if (m_currentDataset != XmlExportable::NO_ID) { Chris@595: Chris@595: bool foundInAwaiting = false; Chris@45: Chris@685: for (auto i: m_awaitingDatasets) { Chris@685: if (i.second == m_currentDataset) { Chris@685: m_awaitingDatasets.erase(i.first); Chris@595: foundInAwaiting = true; Chris@595: break; Chris@595: } Chris@595: } Chris@45: Chris@595: if (!foundInAwaiting) { Chris@601: SVCERR << "WARNING: SV-XML: Dataset precedes model, or no model uses dataset" << endl; Chris@595: } Chris@595: } Chris@45: Chris@685: m_currentDataset = XmlExportable::NO_ID; Chris@45: Chris@45: } else if (name == "data") { Chris@45: Chris@45: addUnaddedModels(); Chris@595: m_inData = false; Chris@45: Chris@45: } else if (name == "derivation") { Chris@45: Chris@685: if (m_currentDerivedModel.isNone()) { Chris@685: if (m_pendingDerivedModel == XmlExportable::NO_ID) { Chris@685: SVCERR << "WARNING: SV-XML: No valid output model id " Chris@685: << "for derivation" << endl; Chris@685: } else if (haveModel(m_pendingDerivedModel)) { Chris@601: SVCERR << "WARNING: SV-XML: Derivation has existing model " Chris@685: << m_pendingDerivedModel Chris@685: << " as target, not regenerating" << endl; Chris@45: } else { Chris@78: QString message; Chris@685: m_currentDerivedModel = m_models[m_pendingDerivedModel] = Chris@72: m_document->addDerivedModel Chris@72: (m_currentTransform, Chris@72: ModelTransformer::Input(m_currentTransformSource, Chris@78: m_currentTransformChannel), Chris@78: message); Chris@685: if (m_currentDerivedModel.isNone()) { Chris@79: emit modelRegenerationFailed(tr("(derived model in SV-XML)"), Chris@79: m_currentTransform.getIdentifier(), Chris@79: message); Chris@79: } else if (message != "") { Chris@79: emit modelRegenerationWarning(tr("(derived model in SV-XML)"), Chris@79: m_currentTransform.getIdentifier(), Chris@79: message); Chris@79: } Chris@45: } Chris@45: } else { Chris@329: m_document->addAlreadyDerivedModel Chris@72: (m_currentTransform, Chris@72: ModelTransformer::Input(m_currentTransformSource, Chris@72: m_currentTransformChannel), Chris@72: m_currentDerivedModel); Chris@45: } Chris@45: Chris@45: m_addedModels.insert(m_currentDerivedModel); Chris@685: m_currentDerivedModel = {}; Chris@685: m_pendingDerivedModel = XmlExportable::NO_ID; Chris@685: m_currentTransformSource = {}; Chris@72: m_currentTransform = Transform(); Chris@72: m_currentTransformChannel = -1; Chris@45: Chris@45: } else if (name == "row") { Chris@595: m_inRow = false; Chris@45: } else if (name == "layer") { Chris@45: m_inLayer = false; Chris@45: } else if (name == "view") { Chris@595: m_inView = false; Chris@45: } else if (name == "selections") { Chris@595: m_inSelections = false; Chris@45: } else if (name == "playparameters") { Chris@686: m_currentPlayParameters = {}; Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::error(const QXmlParseException &exception) Chris@45: { Chris@45: m_errorString = Chris@595: QString("ERROR: SV-XML: %1 at line %2, column %3") Chris@595: .arg(exception.message()) Chris@595: .arg(exception.lineNumber()) Chris@595: .arg(exception.columnNumber()); Chris@601: SVCERR << m_errorString << endl; Chris@45: return QXmlDefaultHandler::error(exception); Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::fatalError(const QXmlParseException &exception) Chris@45: { Chris@45: m_errorString = Chris@595: QString("FATAL ERROR: SV-XML: %1 at line %2, column %3") Chris@595: .arg(exception.message()) Chris@595: .arg(exception.lineNumber()) Chris@595: .arg(exception.columnNumber()); Chris@601: SVCERR << m_errorString << endl; Chris@45: return QXmlDefaultHandler::fatalError(exception); Chris@45: } Chris@45: Chris@45: Chris@595: #define READ_MANDATORY(TYPE, NAME, CONVERSION) \ Chris@45: TYPE NAME = attributes.value(#NAME).trimmed().CONVERSION(&ok); \ Chris@45: if (!ok) { \ Chris@601: SVCERR << "WARNING: SV-XML: Missing or invalid mandatory " #TYPE " attribute \"" #NAME "\"" << endl; \ Chris@595: return false; \ Chris@45: } Chris@45: Chris@45: bool Chris@576: SVFileReader::readWindow(const QXmlAttributes &) Chris@45: { Chris@576: // The window element contains window dimensions, which we used to Chris@576: // read and size the window accordingly. This was a Bad Idea [tm] Chris@576: // and we now do nothing instead. See #1769 Loading window Chris@576: // dimensions from session file is a really bad idea Chris@45: return true; Chris@45: } Chris@45: Chris@45: void Chris@629: SVFileReader::makeAggregateModels() Chris@629: { Chris@685: std::map stillPending; Chris@629: Chris@629: for (auto p: m_pendingAggregates) { Chris@629: Chris@629: int id = p.first; Chris@629: const PendingAggregateRec &rec = p.second; Chris@629: bool skip = false; Chris@629: Chris@629: AggregateWaveModel::ChannelSpecList specs; Chris@685: for (ExportId componentId: rec.components) { Chris@629: bool found = false; Chris@629: if (m_models.find(componentId) != m_models.end()) { Chris@685: ModelId modelId = m_models[componentId]; Chris@685: auto rs = ModelById::getAs Chris@685: (modelId); Chris@629: if (rs) { Chris@629: specs.push_back(AggregateWaveModel::ModelChannelSpec Chris@685: (modelId, -1)); Chris@629: found = true; Chris@685: } else { Chris@685: SVDEBUG << "SVFileReader::makeAggregateModels: " Chris@685: << "Component model id " << componentId Chris@685: << "in aggregate model id " << id Chris@685: << "does not appear to be convertible to " Chris@685: << "RangeSummarisableTimeValueModel" Chris@685: << endl; Chris@629: } Chris@629: } Chris@629: if (!found) { Chris@685: SVDEBUG << "SVFileReader::makeAggregateModels: " Chris@629: << "Unknown component model id " Chris@629: << componentId << " in aggregate model id " << id Chris@629: << ", hoping we won't be needing it just yet" Chris@629: << endl; Chris@629: skip = true; Chris@629: } Chris@629: } Chris@629: Chris@629: if (skip) { Chris@629: stillPending[id] = rec; Chris@629: } else { Chris@685: auto model = std::make_shared(specs); Chris@629: model->setObjectName(rec.name); Chris@685: m_models[id] = ModelById::add(model); Chris@629: Chris@685: SVDEBUG << "SVFileReader::makeAggregateModels: created aggregate " Chris@685: << "model id " << id << " with " << specs.size() Chris@685: << " components" << endl; Chris@629: } Chris@629: } Chris@629: Chris@629: m_pendingAggregates = stillPending; Chris@629: } Chris@629: Chris@629: void Chris@45: SVFileReader::addUnaddedModels() Chris@45: { Chris@629: makeAggregateModels(); Chris@662: Chris@685: for (auto i: m_models) { Chris@662: Chris@685: ModelId modelId = i.second; Chris@685: Chris@685: if (m_addedModels.find(modelId) != m_addedModels.end()) { Chris@662: // already added this one Chris@662: continue; Chris@45: } Chris@685: Chris@693: m_document->addNonDerivedModel(modelId); Chris@662: Chris@693: // make a note of all models that have been added to the Chris@693: // document, so they don't get released by our own destructor Chris@685: m_addedModels.insert(modelId); Chris@45: } Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readModel(const QXmlAttributes &attributes) Chris@45: { Chris@45: bool ok = false; Chris@45: Chris@45: READ_MANDATORY(int, id, toInt); Chris@45: Chris@45: if (haveModel(id)) { Chris@601: SVCERR << "WARNING: SV-XML: Ignoring duplicate model id " << id Chris@595: << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: QString name = attributes.value("name"); Chris@45: Chris@233: SVDEBUG << "SVFileReader::readModel: model name \"" << name << "\"" << endl; Chris@45: Chris@436: READ_MANDATORY(double, sampleRate, toDouble); Chris@45: Chris@45: QString type = attributes.value("type").trimmed(); Chris@362: bool isMainModel = (attributes.value("mainModel").trimmed() == "true"); Chris@45: Chris@45: if (type == "wavefile") { Chris@595: Chris@636: WaveFileModel *model = nullptr; Chris@45: FileFinder *ff = FileFinder::getInstance(); Chris@45: QString originalPath = attributes.value("file"); Chris@45: QString path = ff->find(FileFinder::AudioFile, Chris@45: originalPath, m_location); Chris@45: Chris@248: SVDEBUG << "Wave file originalPath = " << originalPath << ", path = " Chris@248: << path << endl; Chris@248: Chris@109: ProgressDialog dialog(tr("Opening file or URL..."), true, 2000); Chris@109: FileSource file(path, &dialog); Chris@45: file.waitForStatus(); Chris@45: Chris@45: if (!file.isOK()) { Chris@601: SVCERR << "SVFileReader::readModel: Failed to retrieve file \"" << path << "\" for wave file model: " << file.getErrorString() << endl; Chris@45: } else if (!file.isAvailable()) { Chris@601: SVCERR << "SVFileReader::readModel: Failed to retrieve file \"" << path << "\" for wave file model: Source unavailable" << endl; Chris@45: } else { Chris@45: Chris@45: file.waitForData(); Chris@116: Chris@436: sv_samplerate_t rate = sampleRate; Chris@116: Chris@360: if (Preferences::getInstance()->getFixedSampleRate() != 0) { Chris@360: rate = Preferences::getInstance()->getFixedSampleRate(); Chris@362: } else if (rate == 0 && Chris@362: !isMainModel && Chris@360: Preferences::getInstance()->getResampleOnLoad()) { Chris@685: auto mm = ModelById::getAs Chris@685: (m_document->getMainModel()); Chris@116: if (mm) rate = mm->getSampleRate(); Chris@116: } Chris@116: Chris@479: model = new ReadOnlyWaveFileModel(file, rate); Chris@45: if (!model->isOK()) { Chris@45: delete model; Chris@636: model = nullptr; Chris@45: } Chris@45: } Chris@45: Chris@601: if (!model) { Chris@601: m_document->setIncomplete(true); Chris@601: return false; Chris@601: } Chris@45: Chris@45: model->setObjectName(name); Chris@685: Chris@687: ModelId modelId = ModelById::add(std::shared_ptr(model)); Chris@685: m_models[id] = modelId; Chris@685: Chris@595: if (isMainModel) { Chris@685: m_document->setMainModel(modelId); Chris@685: m_addedModels.insert(modelId); Chris@595: } Chris@595: // Derived models will be added when their derivation Chris@595: // is found. Chris@45: Chris@595: return true; Chris@45: Chris@629: } else if (type == "aggregatewave") { Chris@629: Chris@629: QString components = attributes.value("components"); Chris@629: QStringList componentIdStrings = components.split(","); Chris@629: std::vector componentIds; Chris@685: for (auto cidStr: componentIdStrings) { Chris@629: bool ok = false; Chris@685: int cid = cidStr.toInt(&ok); Chris@629: if (!ok) { Chris@685: SVCERR << "SVFileReader::readModel: Failed to convert component model id from part \"" << cidStr << "\" in \"" << components << "\"" << endl; Chris@629: } else { Chris@685: componentIds.push_back(cid); Chris@629: } Chris@629: } Chris@629: PendingAggregateRec rec { name, sampleRate, componentIds }; Chris@629: m_pendingAggregates[id] = rec; Chris@629: Chris@629: // The aggregate model will be constructed from its pending Chris@629: // record in makeAggregateModels; it can't happen here because Chris@629: // the component models might not all have been observed yet Chris@629: // (an unfortunate accident of the way the file is written) Chris@629: Chris@629: return true; Chris@629: Chris@45: } else if (type == "dense") { Chris@595: Chris@595: READ_MANDATORY(int, dimensions, toInt); Chris@595: Chris@595: // Currently the only dense model we support here is the dense Chris@595: // 3d model. Dense time-value models are always file-backed Chris@595: // waveform data, at this point, and they come in as wavefile Chris@595: // models. Chris@595: Chris@595: if (dimensions == 3) { Chris@595: Chris@595: READ_MANDATORY(int, windowSize, toInt); Chris@595: READ_MANDATORY(int, yBinCount, toInt); Chris@595: Chris@685: auto model = std::make_shared Chris@707: (sampleRate, windowSize, yBinCount); Chris@685: Chris@685: model->setObjectName(name); Chris@685: m_models[id] = ModelById::add(model); Chris@595: Chris@595: float minimum = attributes.value("minimum").trimmed().toFloat(&ok); Chris@595: if (ok) model->setMinimumLevel(minimum); Chris@595: Chris@595: float maximum = attributes.value("maximum").trimmed().toFloat(&ok); Chris@595: if (ok) model->setMaximumLevel(maximum); Chris@45: Chris@595: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@595: if (ok) m_awaitingDatasets[dataset] = id; Chris@45: Chris@181: int startFrame = attributes.value("startFrame").trimmed().toInt(&ok); Chris@181: if (ok) model->setStartFrame(startFrame); Chris@181: Chris@595: return true; Chris@45: Chris@595: } else { Chris@45: Chris@601: SVCERR << "WARNING: SV-XML: Unexpected dense model dimension (" Chris@595: << dimensions << ")" << endl; Chris@595: } Chris@45: } else if (type == "sparse") { Chris@45: Chris@595: READ_MANDATORY(int, dimensions, toInt); Chris@595: Chris@595: if (dimensions == 1) { Chris@595: Chris@595: READ_MANDATORY(int, resolution, toInt); Chris@685: Chris@45: if (attributes.value("subtype") == "image") { Chris@45: Chris@45: bool notifyOnAdd = (attributes.value("notifyOnAdd") == "true"); Chris@685: auto model = std::make_shared Chris@685: (sampleRate, resolution, notifyOnAdd); Chris@45: model->setObjectName(name); Chris@685: m_models[id] = ModelById::add(model); Chris@45: Chris@45: } else { Chris@45: Chris@685: auto model = std::make_shared Chris@45: (sampleRate, resolution); Chris@45: model->setObjectName(name); Chris@685: m_models[id] = ModelById::add(model); Chris@45: } Chris@45: Chris@595: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@595: if (ok) m_awaitingDatasets[dataset] = id; Chris@45: Chris@595: return true; Chris@45: Chris@595: } else if (dimensions == 2 || dimensions == 3) { Chris@595: Chris@595: READ_MANDATORY(int, resolution, toInt); Chris@45: Chris@45: bool haveMinMax = true; Chris@595: float minimum = attributes.value("minimum").trimmed().toFloat(&ok); Chris@45: if (!ok) haveMinMax = false; Chris@595: float maximum = attributes.value("maximum").trimmed().toFloat(&ok); Chris@45: if (!ok) haveMinMax = false; Chris@45: Chris@595: float valueQuantization = Chris@595: attributes.value("valueQuantization").trimmed().toFloat(&ok); Chris@45: Chris@595: bool notifyOnAdd = (attributes.value("notifyOnAdd") == "true"); Chris@45: Chris@45: QString units = attributes.value("units"); Chris@45: Chris@595: if (dimensions == 2) { Chris@595: if (attributes.value("subtype") == "text") { Chris@685: auto model = std::make_shared Chris@595: (sampleRate, resolution, notifyOnAdd); Chris@45: model->setObjectName(name); Chris@685: m_models[id] = ModelById::add(model); Chris@111: } else if (attributes.value("subtype") == "path") { Chris@685: // Paths are no longer actually models Chris@685: Path *path = new Path(sampleRate, resolution); Chris@685: m_paths[id] = path; Chris@595: } else { Chris@685: std::shared_ptr model; Chris@45: if (haveMinMax) { Chris@685: model = std::make_shared Chris@685: (sampleRate, resolution, minimum, maximum, Chris@685: notifyOnAdd); Chris@45: } else { Chris@685: model = std::make_shared Chris@45: (sampleRate, resolution, notifyOnAdd); Chris@45: } Chris@45: model->setScaleUnits(units); Chris@45: model->setObjectName(name); Chris@685: m_models[id] = ModelById::add(model); Chris@595: } Chris@595: } else { Chris@137: if (attributes.value("subtype") == "region") { Chris@685: std::shared_ptr model; Chris@137: if (haveMinMax) { Chris@685: model = std::make_shared Chris@685: (sampleRate, resolution, minimum, maximum, Chris@685: notifyOnAdd); Chris@137: } else { Chris@685: model = std::make_shared Chris@137: (sampleRate, resolution, notifyOnAdd); Chris@137: } Chris@137: model->setValueQuantization(valueQuantization); Chris@137: model->setScaleUnits(units); Chris@137: model->setObjectName(name); Chris@685: m_models[id] = ModelById::add(model); Chris@343: } else if (attributes.value("subtype") == "flexinote") { Chris@685: std::shared_ptr model; Chris@343: if (haveMinMax) { Chris@685: model = std::make_shared Chris@646: (sampleRate, resolution, minimum, maximum, Chris@646: notifyOnAdd, Chris@646: NoteModel::FLEXI_NOTE); Chris@343: } else { Chris@685: model = std::make_shared Chris@646: (sampleRate, resolution, notifyOnAdd, Chris@646: NoteModel::FLEXI_NOTE); Chris@343: } Chris@343: model->setValueQuantization(valueQuantization); Chris@343: model->setScaleUnits(units); Chris@343: model->setObjectName(name); Chris@685: m_models[id] = ModelById::add(model); Chris@45: } else { Chris@137: // note models written out by SV 1.3 and earlier Chris@137: // have no subtype, so we can't test that Chris@685: std::shared_ptr model; Chris@137: if (haveMinMax) { Chris@685: model = std::make_shared Chris@137: (sampleRate, resolution, minimum, maximum, notifyOnAdd); Chris@137: } else { Chris@685: model = std::make_shared Chris@137: (sampleRate, resolution, notifyOnAdd); Chris@137: } Chris@137: model->setValueQuantization(valueQuantization); Chris@137: model->setScaleUnits(units); Chris@137: model->setObjectName(name); Chris@685: m_models[id] = ModelById::add(model); Chris@45: } Chris@137: } Chris@45: Chris@595: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@595: if (ok) m_awaitingDatasets[dataset] = id; Chris@45: Chris@595: return true; Chris@45: Chris@595: } else { Chris@45: Chris@601: SVCERR << "WARNING: SV-XML: Unexpected sparse model dimension (" Chris@595: << dimensions << ")" << endl; Chris@595: } Chris@111: Chris@111: } else if (type == "alignment") { Chris@111: Chris@111: READ_MANDATORY(int, reference, toInt); Chris@111: READ_MANDATORY(int, aligned, toInt); Chris@111: READ_MANDATORY(int, path, toInt); Chris@111: Chris@685: ModelId refModel, alignedModel; Chris@685: Path *pathPtr = nullptr; Chris@111: Chris@111: if (m_models.find(reference) != m_models.end()) { Chris@111: refModel = m_models[reference]; Chris@111: } else { Chris@601: SVCERR << "WARNING: SV-XML: Unknown reference model id " Chris@111: << reference << " in alignment model id " << id Chris@293: << endl; Chris@111: } Chris@111: Chris@111: if (m_models.find(aligned) != m_models.end()) { Chris@111: alignedModel = m_models[aligned]; Chris@111: } else { Chris@601: SVCERR << "WARNING: SV-XML: Unknown aligned model id " Chris@111: << aligned << " in alignment model id " << id Chris@293: << endl; Chris@111: } Chris@111: Chris@685: if (m_paths.find(path) != m_paths.end()) { Chris@685: pathPtr = m_paths[path]; Chris@111: } else { Chris@685: SVCERR << "WARNING: SV-XML: Unknown path id " Chris@111: << path << " in alignment model id " << id Chris@293: << endl; Chris@111: } Chris@111: Chris@685: if (!refModel.isNone() && !alignedModel.isNone() && pathPtr) { Chris@685: auto model = std::make_shared Chris@685: (refModel, alignedModel, ModelId()); Chris@685: model->setPath(*pathPtr); Chris@685: model->setObjectName(name); Chris@685: m_models[id] = ModelById::add(model); Chris@685: if (auto am = ModelById::get(alignedModel)) { Chris@685: am->setAlignment(m_models[id]); Chris@111: } Chris@111: return true; Chris@111: } Chris@685: Chris@685: if (pathPtr) { Chris@685: delete pathPtr; Chris@685: m_paths.erase(path); Chris@685: } Chris@111: Chris@45: } else { Chris@45: Chris@590: SVCERR << "WARNING: SV-XML: Unexpected model type \"" Chris@590: << type << "\" for model id " << id << endl; Chris@45: } Chris@45: Chris@45: return false; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readView(const QXmlAttributes &attributes) Chris@45: { Chris@45: QString type = attributes.value("type"); Chris@636: m_currentPane = nullptr; Chris@45: Chris@45: if (type != "pane") { Chris@601: SVCERR << "WARNING: SV-XML: Unexpected view type \"" Chris@595: << type << "\"" << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: m_currentPane = m_paneCallback.addPane(); Chris@45: Chris@630: SVDEBUG << "SVFileReader::addPane: pane is " << m_currentPane << endl; Chris@342: Chris@45: if (!m_currentPane) { Chris@601: SVCERR << "WARNING: SV-XML: Internal error: Failed to add pane!" Chris@595: << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: bool ok = false; Chris@45: Chris@45: View *view = m_currentPane; Chris@45: Chris@45: // The view properties first Chris@45: Chris@366: READ_MANDATORY(int, centre, toInt); Chris@366: READ_MANDATORY(int, zoom, toInt); Chris@45: READ_MANDATORY(int, followPan, toInt); Chris@45: READ_MANDATORY(int, followZoom, toInt); Chris@45: QString tracking = attributes.value("tracking"); Chris@45: Chris@624: ZoomLevel zoomLevel; Chris@624: int deepZoom = attributes.value("deepZoom").trimmed().toInt(&ok); Chris@624: if (ok && zoom == 1 && deepZoom > 1) { Chris@624: zoomLevel = { ZoomLevel::PixelsPerFrame, deepZoom }; Chris@624: } else { Chris@624: zoomLevel = { ZoomLevel::FramesPerPixel, zoom }; Chris@624: } Chris@624: Chris@45: // Specify the follow modes before we set the actual values Chris@45: view->setFollowGlobalPan(followPan); Chris@45: view->setFollowGlobalZoom(followZoom); Chris@45: view->setPlaybackFollow(tracking == "scroll" ? PlaybackScrollContinuous : Chris@595: tracking == "page" ? PlaybackScrollPageWithCentre : Chris@595: tracking == "daw" ? PlaybackScrollPage Chris@595: : PlaybackIgnore); Chris@45: Chris@45: // Then set these values Chris@45: view->setCentreFrame(centre); Chris@624: view->setZoomLevel(zoomLevel); Chris@45: Chris@45: // And pane properties Chris@45: READ_MANDATORY(int, centreLineVisible, toInt); Chris@45: m_currentPane->setCentreLineVisible(centreLineVisible); Chris@45: Chris@45: int height = attributes.value("height").toInt(&ok); Chris@45: if (ok) { Chris@595: m_currentPane->resize(m_currentPane->width(), height); Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readLayer(const QXmlAttributes &attributes) Chris@45: { Chris@45: QString type = attributes.value("type"); Chris@45: Chris@45: int id; Chris@45: bool ok = false; Chris@45: id = attributes.value("id").trimmed().toInt(&ok); Chris@45: Chris@45: if (!ok) { Chris@601: SVCERR << "WARNING: SV-XML: No layer id for layer of type \"" Chris@595: << type Chris@595: << "\"" << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@636: Layer *layer = nullptr; Chris@45: bool isNewLayer = false; Chris@45: Chris@45: // Layers are expected to be defined in layer elements in the data Chris@45: // section, and referred to in layer elements in the view Chris@45: // sections. So if we're in the data section, we expect this Chris@45: // layer not to exist already; if we're in the view section, we Chris@45: // expect it to exist. Chris@45: Chris@45: if (m_inData) { Chris@45: Chris@595: if (m_layers.find(id) != m_layers.end()) { Chris@601: SVCERR << "WARNING: SV-XML: Ignoring duplicate layer id " << id Chris@595: << " in data section" << endl; Chris@595: return false; Chris@595: } Chris@45: Chris@595: layer = m_layers[id] = m_document->createLayer Chris@595: (LayerFactory::getInstance()->getLayerTypeForName(type)); Chris@45: Chris@595: if (layer) { Chris@595: m_layers[id] = layer; Chris@595: isNewLayer = true; Chris@595: } Chris@45: Chris@45: } else { Chris@45: Chris@595: if (!m_currentPane) { Chris@601: SVCERR << "WARNING: SV-XML: No current pane for layer " << id Chris@595: << " in view section" << endl; Chris@595: return false; Chris@595: } Chris@45: Chris@595: if (m_layers.find(id) != m_layers.end()) { Chris@595: Chris@595: layer = m_layers[id]; Chris@595: Chris@595: } else { Chris@601: SVCERR << "WARNING: SV-XML: Layer id " << id Chris@595: << " in view section has not been defined -- defining it here" Chris@595: << endl; Chris@45: Chris@595: layer = m_document->createLayer Chris@595: (LayerFactory::getInstance()->getLayerTypeForName(type)); Chris@45: Chris@595: if (layer) { Chris@595: m_layers[id] = layer; Chris@595: isNewLayer = true; Chris@595: } Chris@595: } Chris@45: } Chris@595: Chris@45: if (!layer) { Chris@601: SVCERR << "WARNING: SV-XML: Failed to add layer of type \"" Chris@595: << type Chris@595: << "\"" << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: if (isNewLayer) { Chris@45: Chris@595: QString name = attributes.value("name"); Chris@595: layer->setObjectName(name); Chris@45: Chris@89: QString presentationName = attributes.value("presentationName"); Chris@89: layer->setPresentationName(presentationName); Chris@89: Chris@595: int modelId; Chris@595: bool modelOk = false; Chris@595: modelId = attributes.value("model").trimmed().toInt(&modelOk); Chris@45: Chris@595: if (modelOk) { Chris@595: if (haveModel(modelId)) { Chris@685: m_document->setModel(layer, m_models[modelId]); Chris@595: } else { Chris@601: SVCERR << "WARNING: SV-XML: Unknown model id " << modelId Chris@685: << " in layer definition" << endl; Chris@451: if (!layer->canExistWithoutModel()) { Chris@451: // Don't add a layer with an unknown model id Chris@451: // unless it explicitly supports this state Chris@451: m_document->deleteLayer(layer); Chris@636: m_layers[id] = layer = nullptr; Chris@451: return false; Chris@451: } Chris@451: } Chris@595: } Chris@45: Chris@447: if (layer) layer->setProperties(attributes); Chris@45: } Chris@45: Chris@447: if (!m_inData && m_currentPane && layer) { Chris@45: Chris@45: QString visible = attributes.value("visible"); Chris@45: bool dormant = (visible == "false"); Chris@45: Chris@45: // We need to do this both before and after adding the layer Chris@45: // to the view -- we need it to be dormant if appropriate Chris@45: // before it's actually added to the view so that any property Chris@45: // box gets the right state when it's added, but the add layer Chris@45: // command sets dormant to false because it assumes it may be Chris@45: // restoring a previously dormant layer, so we need to set it Chris@45: // again afterwards too. Hm Chris@45: layer->setLayerDormant(m_currentPane, dormant); Chris@45: Chris@595: m_document->addLayerToView(m_currentPane, layer); Chris@45: Chris@45: layer->setLayerDormant(m_currentPane, dormant); Chris@45: } Chris@45: Chris@45: m_currentLayer = layer; Chris@636: m_inLayer = (layer != nullptr); Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readDatasetStart(const QXmlAttributes &attributes) Chris@45: { Chris@45: bool ok = false; Chris@45: Chris@45: READ_MANDATORY(int, id, toInt); Chris@45: READ_MANDATORY(int, dimensions, toInt); Chris@45: Chris@45: if (m_awaitingDatasets.find(id) == m_awaitingDatasets.end()) { Chris@601: SVCERR << "WARNING: SV-XML: Unwanted dataset " << id << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@685: int awaitingId = m_awaitingDatasets[id]; Chris@685: Chris@685: ModelId modelId; Chris@685: Path *path = nullptr; Chris@45: Chris@685: if (haveModel(awaitingId)) { Chris@685: modelId = m_models[awaitingId]; Chris@685: } else if (m_paths.find(awaitingId) != m_paths.end()) { Chris@685: path = m_paths[awaitingId]; Chris@45: } else { Chris@685: SVCERR << "WARNING: SV-XML: Internal error: Unknown model or path " Chris@685: << modelId << " awaiting dataset " << id << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: bool good = false; Chris@45: Chris@45: switch (dimensions) { Chris@45: case 1: Chris@685: good = Chris@685: (ModelById::isa(modelId) || Chris@685: ModelById::isa(modelId)); Chris@595: break; Chris@45: Chris@45: case 2: Chris@685: good = Chris@685: (ModelById::isa(modelId) || Chris@685: ModelById::isa(modelId) || Chris@685: path); Chris@595: break; Chris@45: Chris@45: case 3: Chris@685: if (ModelById::isa(modelId)) { Chris@685: good = true; Chris@595: m_datasetSeparator = attributes.value("separator"); Chris@685: } else { Chris@685: good = Chris@685: (ModelById::isa(modelId) || Chris@685: ModelById::isa(modelId)); Chris@595: } Chris@595: break; Chris@45: } Chris@45: Chris@45: if (!good) { Chris@601: SVCERR << "WARNING: SV-XML: Model id " << modelId << " has wrong number of dimensions or inappropriate type for " << dimensions << "-D dataset " << id << endl; Chris@685: m_currentDataset = XmlExportable::NO_ID; Chris@595: return false; Chris@45: } Chris@45: Chris@685: m_currentDataset = awaitingId; Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::addPointToDataset(const QXmlAttributes &attributes) Chris@45: { Chris@45: bool ok = false; Chris@45: Chris@45: READ_MANDATORY(int, frame, toInt); Chris@45: Chris@685: if (m_paths.find(m_currentDataset) != m_paths.end()) { Chris@685: Path *path = m_paths[m_currentDataset]; Chris@685: int mapframe = attributes.value("mapframe").trimmed().toInt(&ok); Chris@685: path->add(PathPoint(frame, mapframe)); Chris@685: return ok; Chris@685: } Chris@45: Chris@685: if (!haveModel(m_currentDataset)) { Chris@685: SVCERR << "WARNING: SV-XML: Point element found in non-point dataset" Chris@685: << endl; Chris@685: return false; Chris@685: } Chris@685: Chris@685: ModelId modelId = m_models[m_currentDataset]; Chris@45: Chris@685: if (auto sodm = ModelById::getAs(modelId)) { Chris@595: QString label = attributes.value("label"); Chris@651: sodm->add(Event(frame, label)); Chris@595: return true; Chris@45: } Chris@45: Chris@685: if (auto stvm = ModelById::getAs(modelId)) { Chris@595: float value = 0.0; Chris@595: value = attributes.value("value").trimmed().toFloat(&ok); Chris@595: QString label = attributes.value("label"); Chris@649: stvm->add(Event(frame, value, label)); Chris@595: return ok; Chris@45: } Chris@595: Chris@685: if (auto nm = ModelById::getAs(modelId)) { Chris@595: float value = 0.0; Chris@595: value = attributes.value("value").trimmed().toFloat(&ok); Chris@595: int duration = 0; Chris@595: duration = attributes.value("duration").trimmed().toInt(&ok); Chris@595: QString label = attributes.value("label"); Chris@61: float level = attributes.value("level").trimmed().toFloat(&ok); Chris@61: if (!ok) { // level is optional Chris@61: level = 1.f; Chris@61: ok = true; Chris@61: } Chris@646: nm->add(Event(frame, value, duration, level, label)); Chris@595: return ok; Chris@343: } Chris@343: Chris@685: if (auto rm = ModelById::getAs(modelId)) { Chris@595: float value = 0.0; Chris@595: value = attributes.value("value").trimmed().toFloat(&ok); Chris@595: int duration = 0; Chris@595: duration = attributes.value("duration").trimmed().toInt(&ok); Chris@595: QString label = attributes.value("label"); Chris@648: rm->add(Event(frame, value, duration, label)); Chris@595: return ok; Chris@137: } Chris@137: Chris@685: if (auto tm = ModelById::getAs(modelId)) { Chris@595: float height = 0.0; Chris@595: height = attributes.value("height").trimmed().toFloat(&ok); Chris@595: QString label = attributes.value("label"); Chris@652: tm->add(Event(frame, height, label)); Chris@595: return ok; Chris@45: } Chris@45: Chris@685: if (auto im = ModelById::getAs(modelId)) { Chris@595: QString image = attributes.value("image"); Chris@595: QString label = attributes.value("label"); Chris@653: im->add(Event(frame).withURI(image).withLabel(label)); Chris@595: return ok; Chris@45: } Chris@45: Chris@685: SVCERR << "WARNING: SV-XML: Point element found in non-point dataset" Chris@685: << endl; Chris@45: Chris@45: return false; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::addBinToDataset(const QXmlAttributes &attributes) Chris@45: { Chris@685: if (!haveModel(m_currentDataset)) { Chris@685: SVCERR << "WARNING: SV-XML: Bin definition found in incompatible dataset" Chris@685: << endl; Chris@685: return false; Chris@685: } Chris@685: Chris@685: ModelId modelId = m_models[m_currentDataset]; Chris@45: Chris@685: if (auto dtdm = ModelById::getAs Chris@685: (modelId)) { Chris@45: Chris@595: bool ok = false; Chris@595: int n = attributes.value("number").trimmed().toInt(&ok); Chris@595: if (!ok) { Chris@601: SVCERR << "WARNING: SV-XML: Missing or invalid bin number" Chris@595: << endl; Chris@595: return false; Chris@595: } Chris@45: Chris@595: QString name = attributes.value("name"); Chris@595: dtdm->setBinName(n, name); Chris@595: return true; Chris@45: } Chris@45: Chris@685: SVCERR << "WARNING: SV-XML: Bin definition found in incompatible dataset" Chris@685: << endl; Chris@45: Chris@45: return false; Chris@45: } Chris@45: Chris@45: Chris@45: bool Chris@45: SVFileReader::addRowToDataset(const QXmlAttributes &attributes) Chris@45: { Chris@45: m_inRow = false; Chris@45: Chris@45: bool ok = false; Chris@45: m_rowNumber = attributes.value("n").trimmed().toInt(&ok); Chris@45: if (!ok) { Chris@601: SVCERR << "WARNING: SV-XML: Missing or invalid row number" Chris@595: << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: m_inRow = true; Chris@45: Chris@601: // SVCERR << "SV-XML: In row " << m_rowNumber << endl; Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readRowData(const QString &text) Chris@45: { Chris@685: if (!haveModel(m_currentDataset)) { Chris@685: SVCERR << "WARNING: SV-XML: Row data found in non-row dataset" << endl; Chris@685: return false; Chris@685: } Chris@685: Chris@685: ModelId modelId = m_models[m_currentDataset]; Chris@45: bool warned = false; Chris@45: Chris@685: if (auto dtdm = ModelById::getAs Chris@685: (modelId)) { Chris@685: Chris@595: QStringList data = text.split(m_datasetSeparator); Chris@45: Chris@595: DenseThreeDimensionalModel::Column values; Chris@45: Chris@595: for (QStringList::iterator i = data.begin(); i != data.end(); ++i) { Chris@45: Chris@595: if (int(values.size()) == dtdm->getHeight()) { Chris@595: if (!warned) { Chris@601: SVCERR << "WARNING: SV-XML: Too many y-bins in 3-D dataset row " Chris@595: << m_rowNumber << endl; Chris@595: warned = true; Chris@595: } Chris@595: } Chris@45: Chris@595: bool ok; Chris@595: float value = i->toFloat(&ok); Chris@595: if (!ok) { Chris@601: SVCERR << "WARNING: SV-XML: Bad floating-point value " Chris@595: << i->toLocal8Bit().data() Chris@595: << " in row data" << endl; Chris@595: } else { Chris@595: values.push_back(value); Chris@595: } Chris@595: } Chris@45: Chris@595: dtdm->setColumn(m_rowNumber, values); Chris@595: return true; Chris@45: } Chris@45: Chris@601: SVCERR << "WARNING: SV-XML: Row data found in non-row dataset" << endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readDerivation(const QXmlAttributes &attributes) Chris@45: { Chris@685: int modelExportId = 0; Chris@45: bool modelOk = false; Chris@685: modelExportId = attributes.value("model").trimmed().toInt(&modelOk); Chris@45: Chris@45: if (!modelOk) { Chris@601: SVCERR << "WARNING: SV-XML: No model id specified for derivation" << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@685: if (haveModel(modelExportId)) { Chris@685: m_currentDerivedModel = m_models[modelExportId]; Chris@45: } else { Chris@45: // we'll regenerate the model when the derivation element ends Chris@685: m_currentDerivedModel = {}; Chris@45: } Chris@45: Chris@685: m_pendingDerivedModel = modelExportId; Chris@45: Chris@45: int sourceId = 0; Chris@45: bool sourceOk = false; Chris@45: sourceId = attributes.value("source").trimmed().toInt(&sourceOk); Chris@45: Chris@45: if (sourceOk && haveModel(sourceId)) { Chris@72: m_currentTransformSource = m_models[sourceId]; Chris@45: } else { Chris@585: SVDEBUG << "NOTE: SV-XML: Can't find a model with id " << sourceId Chris@585: << " for derivation source, falling back to main model" << endl; Chris@72: m_currentTransformSource = m_document->getMainModel(); Chris@45: } Chris@45: Chris@72: m_currentTransform = Transform(); Chris@45: Chris@45: bool ok = false; Chris@45: int channel = attributes.value("channel").trimmed().toInt(&ok); Chris@72: if (ok) m_currentTransformChannel = channel; Chris@72: else m_currentTransformChannel = -1; Chris@45: Chris@72: QString type = attributes.value("type"); Chris@72: Chris@72: if (type == "transform") { Chris@72: m_currentTransformIsNewStyle = true; Chris@72: return true; Chris@72: } else { Chris@72: m_currentTransformIsNewStyle = false; Chris@233: SVDEBUG << "NOTE: SV-XML: Reading old-style derivation element" Chris@229: << endl; Chris@72: } Chris@72: Chris@72: QString transformId = attributes.value("transform"); Chris@72: Chris@72: m_currentTransform.setIdentifier(transformId); Chris@45: Chris@45: int stepSize = attributes.value("stepSize").trimmed().toInt(&ok); Chris@72: if (ok) m_currentTransform.setStepSize(stepSize); Chris@45: Chris@45: int blockSize = attributes.value("blockSize").trimmed().toInt(&ok); Chris@72: if (ok) m_currentTransform.setBlockSize(blockSize); Chris@45: Chris@45: int windowType = attributes.value("windowType").trimmed().toInt(&ok); Chris@72: if (ok) m_currentTransform.setWindowType(WindowType(windowType)); Chris@72: Chris@685: auto currentTransformSourceModel = ModelById::get(m_currentTransformSource); Chris@685: if (!currentTransformSourceModel) return true; Chris@685: Chris@685: sv_samplerate_t sampleRate = currentTransformSourceModel->getSampleRate(); Chris@45: Chris@45: QString startFrameStr = attributes.value("startFrame"); Chris@45: QString durationStr = attributes.value("duration"); Chris@45: Chris@366: int startFrame = 0; Chris@366: int duration = 0; Chris@45: Chris@45: if (startFrameStr != "") { Chris@45: startFrame = startFrameStr.trimmed().toInt(&ok); Chris@45: if (!ok) startFrame = 0; Chris@45: } Chris@45: if (durationStr != "") { Chris@45: duration = durationStr.trimmed().toInt(&ok); Chris@45: if (!ok) duration = 0; Chris@45: } Chris@45: Chris@72: m_currentTransform.setStartTime Chris@685: (RealTime::frame2RealTime(startFrame, sampleRate)); Chris@72: Chris@72: m_currentTransform.setDuration Chris@685: (RealTime::frame2RealTime(duration, sampleRate)); Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readPlayParameters(const QXmlAttributes &attributes) Chris@45: { Chris@686: m_currentPlayParameters = {}; Chris@45: Chris@685: int modelExportId = 0; Chris@45: bool modelOk = false; Chris@685: modelExportId = attributes.value("model").trimmed().toInt(&modelOk); Chris@45: Chris@45: if (!modelOk) { Chris@601: SVCERR << "WARNING: SV-XML: No model id specified for play parameters" << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@685: if (haveModel(modelExportId)) { Chris@45: Chris@45: bool ok = false; Chris@45: Chris@686: auto parameters = PlayParameterRepository::getInstance()-> Chris@685: getPlayParameters(m_models[modelExportId].untyped); Chris@45: Chris@45: if (!parameters) { Chris@601: SVCERR << "WARNING: SV-XML: Play parameters for model " Chris@685: << modelExportId Chris@45: << " not found - has model been added to document?" Chris@293: << endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: bool muted = (attributes.value("mute").trimmed() == "true"); Chris@45: parameters->setPlayMuted(muted); Chris@45: Chris@45: float pan = attributes.value("pan").toFloat(&ok); Chris@45: if (ok) parameters->setPlayPan(pan); Chris@45: Chris@45: float gain = attributes.value("gain").toFloat(&ok); Chris@45: if (ok) parameters->setPlayGain(gain); Chris@45: Chris@309: QString clipId = attributes.value("clipId"); Chris@309: if (clipId != "") parameters->setPlayClipId(clipId); Chris@45: Chris@45: m_currentPlayParameters = parameters; Chris@45: Chris@685: // SVCERR << "Current play parameters for model: " << m_models[modelExportId] << ": " << m_currentPlayParameters << endl; Chris@45: Chris@45: } else { Chris@45: Chris@685: SVCERR << "WARNING: SV-XML: Unknown model " << modelExportId Chris@685: << " for play parameters" << endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readPlugin(const QXmlAttributes &attributes) Chris@45: { Chris@685: if (m_pendingDerivedModel != XmlExportable::NO_ID) { Chris@308: return readPluginForTransform(attributes); Chris@308: } else if (m_currentPlayParameters) { Chris@308: return readPluginForPlayback(attributes); Chris@308: } else { Chris@601: SVCERR << "WARNING: SV-XML: Plugin found outside derivation or play parameters" << endl; Chris@45: return false; Chris@45: } Chris@308: } Chris@45: Chris@308: bool Chris@308: SVFileReader::readPluginForTransform(const QXmlAttributes &attributes) Chris@308: { Chris@308: if (m_currentTransformIsNewStyle) { Chris@308: // Not needed, we have the transform element instead Chris@72: return true; Chris@72: } Chris@72: Chris@45: QString configurationXml = " Chris@308: setParametersFromPluginConfigurationXml(m_currentTransform, Chris@308: configurationXml); Chris@308: return true; Chris@308: } Chris@308: Chris@308: bool Chris@308: SVFileReader::readPluginForPlayback(const QXmlAttributes &attributes) Chris@308: { Chris@308: // Obsolete but supported for compatibility Chris@308: Chris@308: QString ident = attributes.value("identifier"); Chris@308: if (ident == "sample_player") { Chris@309: QString clipId = attributes.value("program"); Chris@309: if (clipId != "") m_currentPlayParameters->setPlayClipId(clipId); Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@72: SVFileReader::readTransform(const QXmlAttributes &attributes) Chris@72: { Chris@685: if (m_pendingDerivedModel == XmlExportable::NO_ID) { Chris@601: SVCERR << "WARNING: SV-XML: Transform found outside derivation" << endl; Chris@72: return false; Chris@72: } Chris@72: Chris@82: m_currentTransform = Transform(); Chris@72: m_currentTransform.setFromXmlAttributes(attributes); Chris@72: return true; Chris@72: } Chris@72: Chris@72: bool Chris@72: SVFileReader::readParameter(const QXmlAttributes &attributes) Chris@72: { Chris@685: if (m_pendingDerivedModel == XmlExportable::NO_ID) { Chris@601: SVCERR << "WARNING: SV-XML: Parameter found outside derivation" << endl; Chris@72: return false; Chris@72: } Chris@72: Chris@72: QString name = attributes.value("name"); Chris@72: if (name == "") { Chris@601: SVCERR << "WARNING: SV-XML: Ignoring nameless transform parameter" Chris@293: << endl; Chris@72: return false; Chris@72: } Chris@72: Chris@72: float value = attributes.value("value").trimmed().toFloat(); Chris@72: Chris@72: m_currentTransform.setParameter(name, value); Chris@72: return true; Chris@72: } Chris@72: Chris@72: bool Chris@45: SVFileReader::readSelection(const QXmlAttributes &attributes) Chris@45: { Chris@45: bool ok; Chris@45: Chris@45: READ_MANDATORY(int, start, toInt); Chris@45: READ_MANDATORY(int, end, toInt); Chris@45: Chris@45: m_paneCallback.addSelection(start, end); Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readMeasurement(const QXmlAttributes &attributes) Chris@45: { Chris@233: SVDEBUG << "SVFileReader::readMeasurement: inLayer " Chris@229: << m_inLayer << ", layer " << m_currentLayer << endl; Chris@45: Chris@45: if (!m_inLayer) { Chris@601: SVCERR << "WARNING: SV-XML: Measurement found outside layer" << endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: m_currentLayer->addMeasurementRect(attributes); Chris@45: return true; Chris@45: } Chris@45: Chris@45: SVFileReaderPaneCallback::~SVFileReaderPaneCallback() Chris@45: { Chris@45: } Chris@45: Chris@140: Chris@140: class SVFileIdentifier : public QXmlDefaultHandler Chris@140: { Chris@140: public: Chris@140: SVFileIdentifier() : Chris@140: m_inSv(false), Chris@140: m_inData(false), Chris@140: m_type(SVFileReader::UnknownFileType) Chris@140: { } Chris@633: ~SVFileIdentifier() override { } Chris@140: Chris@140: void parse(QXmlInputSource &source) { Chris@140: QXmlSimpleReader reader; Chris@140: reader.setContentHandler(this); Chris@140: reader.setErrorHandler(this); Chris@140: reader.parse(source); Chris@140: } Chris@140: Chris@140: SVFileReader::FileType getType() const { return m_type; } Chris@140: Chris@633: bool startElement(const QString &, Chris@595: const QString &, Chris@595: const QString &qName, Chris@633: const QXmlAttributes& atts) override Chris@140: { Chris@140: QString name = qName.toLower(); Chris@140: Chris@140: // SV session files have an sv element containing a data Chris@140: // element containing a model element with mainModel="true". Chris@140: Chris@140: // If the sv element is present but the rest does not satisfy, Chris@140: // then it's (probably) an SV layer file. Chris@140: Chris@140: // Otherwise, it's of unknown type. Chris@140: Chris@140: if (name == "sv") { Chris@140: m_inSv = true; Chris@140: if (m_type == SVFileReader::UnknownFileType) { Chris@140: m_type = SVFileReader::SVLayerFile; Chris@140: } Chris@140: return true; Chris@140: } else if (name == "data") { Chris@140: if (!m_inSv) return true; Chris@140: m_inData = true; Chris@140: } else if (name == "model") { Chris@140: if (!m_inData) return true; Chris@140: if (atts.value("mainModel").trimmed() == "true") { Chris@140: if (m_type == SVFileReader::SVLayerFile) { Chris@140: m_type = SVFileReader::SVSessionFile; Chris@140: return false; // done Chris@140: } Chris@140: } Chris@140: } Chris@140: return true; Chris@140: } Chris@140: Chris@633: bool endElement(const QString &, Chris@595: const QString &, Chris@633: const QString &qName) override Chris@140: { Chris@140: QString name = qName.toLower(); Chris@140: Chris@140: if (name == "sv") { Chris@140: if (m_inSv) { Chris@140: m_inSv = false; Chris@140: return false; // done Chris@140: } Chris@140: } else if (name == "data") { Chris@140: if (m_inData) { Chris@140: m_inData = false; Chris@140: return false; // also done, nothing after the first Chris@140: // data element is of use here Chris@140: } Chris@140: } Chris@140: return true; Chris@140: } Chris@140: Chris@140: private: Chris@140: bool m_inSv; Chris@140: bool m_inData; Chris@140: SVFileReader::FileType m_type; Chris@140: }; Chris@140: Chris@140: Chris@140: SVFileReader::FileType Chris@140: SVFileReader::identifyXmlFile(QString path) Chris@140: { Chris@140: QFile file(path); Chris@140: SVFileIdentifier identifier; Chris@140: QXmlInputSource source(&file); Chris@140: identifier.parse(source); Chris@140: return identifier.getType(); Chris@140: } Chris@140: Chris@140: Chris@140: