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@45: Chris@45: #include "data/fileio/AudioFileReaderFactory.h" Chris@45: #include "data/fileio/FileFinder.h" Chris@45: #include "data/fileio/FileSource.h" Chris@45: Chris@45: #include "data/model/WaveFileModel.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@45: #include "data/model/TextModel.h" Chris@45: #include "data/model/ImageModel.h" Chris@45: Chris@45: #include "view/Pane.h" Chris@45: 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@45: SVFileReaderPaneCallback &callback, Chris@45: QString location) : Chris@45: m_document(document), Chris@45: m_paneCallback(callback), Chris@45: m_location(location), Chris@45: m_currentPane(0), Chris@45: m_currentDataset(0), Chris@45: m_currentDerivedModel(0), Chris@45: m_currentDerivedModelId(-1), Chris@45: m_currentPlayParameters(0), Chris@45: m_datasetSeparator(" "), Chris@45: m_inRow(false), Chris@45: m_inLayer(false), Chris@45: m_inView(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@45: Chris@45: SVFileReader::~SVFileReader() Chris@45: { Chris@45: if (!m_awaitingDatasets.empty()) { Chris@45: std::cerr << "WARNING: SV-XML: File ended with " Chris@45: << m_awaitingDatasets.size() << " unfilled model dataset(s)" Chris@45: << std::endl; Chris@45: } Chris@45: Chris@45: std::set unaddedModels; Chris@45: Chris@45: for (std::map::iterator i = m_models.begin(); Chris@45: i != m_models.end(); ++i) { Chris@45: if (m_addedModels.find(i->second) == m_addedModels.end()) { Chris@45: unaddedModels.insert(i->second); Chris@45: } Chris@45: } Chris@45: Chris@45: if (!unaddedModels.empty()) { Chris@45: std::cerr << "WARNING: SV-XML: File contained " Chris@45: << unaddedModels.size() << " unused models" Chris@45: << std::endl; Chris@45: while (!unaddedModels.empty()) { Chris@45: delete *unaddedModels.begin(); Chris@45: unaddedModels.erase(unaddedModels.begin()); Chris@45: } Chris@45: } Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::startElement(const QString &, const QString &, Chris@45: const QString &qName, Chris@45: 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@45: Chris@45: if (name == "sv") { Chris@45: Chris@45: // nothing needed Chris@45: ok = true; Chris@45: Chris@45: } else if (name == "data") { Chris@45: Chris@45: // nothing needed Chris@45: m_inData = true; Chris@45: ok = true; Chris@45: Chris@45: } else if (name == "display") { Chris@45: Chris@45: // nothing needed Chris@45: ok = true; Chris@45: Chris@45: } else if (name == "window") { Chris@45: Chris@45: ok = readWindow(attributes); Chris@45: Chris@45: } else if (name == "model") { Chris@45: Chris@45: ok = readModel(attributes); Chris@45: Chris@45: } else if (name == "dataset") { Chris@45: Chris@45: ok = readDatasetStart(attributes); Chris@45: Chris@45: } else if (name == "bin") { Chris@45: Chris@45: ok = addBinToDataset(attributes); Chris@45: Chris@45: } else if (name == "point") { Chris@45: Chris@45: ok = addPointToDataset(attributes); Chris@45: Chris@45: } else if (name == "row") { Chris@45: Chris@45: 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@45: ok = readLayer(attributes); Chris@45: Chris@45: } else if (name == "view") { Chris@45: Chris@45: m_inView = true; Chris@45: ok = readView(attributes); Chris@45: Chris@45: } else if (name == "derivation") { Chris@45: Chris@45: 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@45: ok = readPlugin(attributes); Chris@45: Chris@45: } else if (name == "selections") { Chris@45: Chris@45: m_inSelections = true; Chris@45: ok = true; Chris@45: Chris@45: } else if (name == "selection") { Chris@45: Chris@45: ok = readSelection(attributes); Chris@45: Chris@45: } else if (name == "measurement") { Chris@45: Chris@45: ok = readMeasurement(attributes); Chris@45: Chris@45: } else { Chris@45: std::cerr << "WARNING: SV-XML: Unexpected element \"" Chris@45: << name.toLocal8Bit().data() << "\"" << std::endl; Chris@45: } Chris@45: Chris@45: if (!ok) { Chris@45: std::cerr << "WARNING: SV-XML: Failed to completely process element \"" Chris@45: << name.toLocal8Bit().data() << "\"" << std::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@45: ok = readRowData(text); Chris@45: if (!ok) { Chris@45: std::cerr << "WARNING: SV-XML: Failed to read row data content for row " << m_rowNumber << std::endl; Chris@45: } Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::endElement(const QString &, const QString &, Chris@45: const QString &qName) Chris@45: { Chris@45: QString name = qName.toLower(); Chris@45: Chris@45: if (name == "dataset") { Chris@45: Chris@45: if (m_currentDataset) { Chris@45: Chris@45: bool foundInAwaiting = false; Chris@45: Chris@45: for (std::map::iterator i = m_awaitingDatasets.begin(); Chris@45: i != m_awaitingDatasets.end(); ++i) { Chris@45: if (haveModel(i->second) && Chris@45: m_models[i->second] == m_currentDataset) { Chris@45: m_awaitingDatasets.erase(i); Chris@45: foundInAwaiting = true; Chris@45: break; Chris@45: } Chris@45: } Chris@45: Chris@45: if (!foundInAwaiting) { Chris@45: std::cerr << "WARNING: SV-XML: Dataset precedes model, or no model uses dataset" << std::endl; Chris@45: } Chris@45: } Chris@45: Chris@45: m_currentDataset = 0; Chris@45: Chris@45: } else if (name == "data") { Chris@45: Chris@45: addUnaddedModels(); Chris@45: m_inData = false; Chris@45: Chris@45: } else if (name == "derivation") { Chris@45: Chris@45: if (!m_currentDerivedModel) { Chris@45: if (m_currentDerivedModel < 0) { Chris@45: std::cerr << "WARNING: SV-XML: Bad derivation output model id " Chris@45: << m_currentDerivedModelId << std::endl; Chris@45: } else if (haveModel(m_currentDerivedModelId)) { Chris@45: std::cerr << "WARNING: SV-XML: Derivation has existing model " Chris@45: << m_currentDerivedModelId Chris@45: << " as target, not regenerating" << std::endl; Chris@45: } else { Chris@45: m_currentDerivedModel = m_models[m_currentDerivedModelId] = Chris@45: m_document->addDerivedModel(m_currentTransform, Chris@45: m_currentTransformSource, Chris@45: m_currentTransformContext, Chris@45: m_currentTransformConfiguration); Chris@45: } Chris@45: } else { Chris@45: m_document->addDerivedModel(m_currentTransform, Chris@45: m_currentTransformSource, Chris@45: m_currentTransformContext, Chris@45: m_currentDerivedModel, Chris@45: m_currentTransformConfiguration); Chris@45: } Chris@45: Chris@45: m_addedModels.insert(m_currentDerivedModel); Chris@45: m_currentDerivedModel = 0; Chris@45: m_currentDerivedModelId = -1; Chris@45: m_currentTransform = ""; Chris@45: m_currentTransformConfiguration = ""; Chris@45: Chris@45: } else if (name == "row") { Chris@45: m_inRow = false; Chris@45: } else if (name == "layer") { Chris@45: m_inLayer = false; Chris@45: } else if (name == "view") { Chris@45: m_inView = false; Chris@45: } else if (name == "selections") { Chris@45: m_inSelections = false; Chris@45: } else if (name == "playparameters") { Chris@45: m_currentPlayParameters = 0; 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@45: QString("ERROR: SV-XML: %1 at line %2, column %3") Chris@45: .arg(exception.message()) Chris@45: .arg(exception.lineNumber()) Chris@45: .arg(exception.columnNumber()); Chris@45: std::cerr << m_errorString.toLocal8Bit().data() << std::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@45: QString("FATAL ERROR: SV-XML: %1 at line %2, column %3") Chris@45: .arg(exception.message()) Chris@45: .arg(exception.lineNumber()) Chris@45: .arg(exception.columnNumber()); Chris@45: std::cerr << m_errorString.toLocal8Bit().data() << std::endl; Chris@45: return QXmlDefaultHandler::fatalError(exception); Chris@45: } Chris@45: Chris@45: Chris@45: #define READ_MANDATORY(TYPE, NAME, CONVERSION) \ Chris@45: TYPE NAME = attributes.value(#NAME).trimmed().CONVERSION(&ok); \ Chris@45: if (!ok) { \ Chris@45: std::cerr << "WARNING: SV-XML: Missing or invalid mandatory " #TYPE " attribute \"" #NAME "\"" << std::endl; \ Chris@45: return false; \ Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readWindow(const QXmlAttributes &attributes) Chris@45: { Chris@45: bool ok = false; Chris@45: Chris@45: READ_MANDATORY(int, width, toInt); Chris@45: READ_MANDATORY(int, height, toInt); Chris@45: Chris@45: m_paneCallback.setWindowSize(width, height); Chris@45: return true; Chris@45: } Chris@45: Chris@45: void Chris@45: SVFileReader::addUnaddedModels() Chris@45: { Chris@45: std::set unaddedModels; Chris@45: Chris@45: for (std::map::iterator i = m_models.begin(); Chris@45: i != m_models.end(); ++i) { Chris@45: if (m_addedModels.find(i->second) == m_addedModels.end()) { Chris@45: unaddedModels.insert(i->second); Chris@45: } Chris@45: } Chris@45: Chris@45: for (std::set::iterator i = unaddedModels.begin(); Chris@45: i != unaddedModels.end(); ++i) { Chris@45: m_document->addImportedModel(*i); Chris@45: m_addedModels.insert(*i); 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@45: std::cerr << "WARNING: SV-XML: Ignoring duplicate model id " << id Chris@45: << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: QString name = attributes.value("name"); Chris@45: Chris@45: std::cerr << "SVFileReader::readModel: model name \"" << name.toStdString() << "\"" << std::endl; Chris@45: Chris@45: READ_MANDATORY(int, sampleRate, toInt); Chris@45: Chris@45: QString type = attributes.value("type").trimmed(); Chris@45: bool mainModel = (attributes.value("mainModel").trimmed() == "true"); Chris@45: Chris@45: if (type == "wavefile") { Chris@45: Chris@45: WaveFileModel *model = 0; 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@45: FileSource file(path); Chris@45: file.waitForStatus(); Chris@45: Chris@45: if (!file.isOK()) { Chris@45: std::cerr << "SVFileReader::readModel: Failed to retrieve file \"" << path.toStdString() << "\" for wave file model: " << file.getErrorString().toStdString() << std::endl; Chris@45: } else if (!file.isAvailable()) { Chris@45: std::cerr << "SVFileReader::readModel: Failed to retrieve file \"" << path.toStdString() << "\" for wave file model: Source unavailable" << std::endl; Chris@45: } else { Chris@45: Chris@45: file.waitForData(); Chris@45: model = new WaveFileModel(file); Chris@45: if (!model->isOK()) { Chris@45: delete model; Chris@45: model = 0; Chris@45: } Chris@45: } Chris@45: Chris@45: if (!model) return false; Chris@45: Chris@45: model->setObjectName(name); Chris@45: m_models[id] = model; Chris@45: if (mainModel) { Chris@45: m_document->setMainModel(model); Chris@45: m_addedModels.insert(model); Chris@45: } Chris@45: // Derived models will be added when their derivation Chris@45: // is found. Chris@45: Chris@45: return true; Chris@45: Chris@45: } else if (type == "dense") { Chris@45: Chris@45: READ_MANDATORY(int, dimensions, toInt); Chris@45: Chris@45: // Currently the only dense model we support here is the dense Chris@45: // 3d model. Dense time-value models are always file-backed Chris@45: // waveform data, at this point, and they come in as wavefile Chris@45: // models. Chris@45: Chris@45: if (dimensions == 3) { Chris@45: Chris@45: READ_MANDATORY(int, windowSize, toInt); Chris@45: READ_MANDATORY(int, yBinCount, toInt); Chris@45: Chris@45: EditableDenseThreeDimensionalModel *model = Chris@45: new EditableDenseThreeDimensionalModel Chris@45: (sampleRate, windowSize, yBinCount); Chris@45: Chris@45: float minimum = attributes.value("minimum").trimmed().toFloat(&ok); Chris@45: if (ok) model->setMinimumLevel(minimum); Chris@45: Chris@45: float maximum = attributes.value("maximum").trimmed().toFloat(&ok); Chris@45: if (ok) model->setMaximumLevel(maximum); Chris@45: Chris@45: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@45: if (ok) m_awaitingDatasets[dataset] = id; Chris@45: Chris@45: model->setObjectName(name); Chris@45: m_models[id] = model; Chris@45: return true; Chris@45: Chris@45: } else { Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Unexpected dense model dimension (" Chris@45: << dimensions << ")" << std::endl; Chris@45: } Chris@45: } else if (type == "sparse") { Chris@45: Chris@45: READ_MANDATORY(int, dimensions, toInt); Chris@45: Chris@45: if (dimensions == 1) { Chris@45: Chris@45: READ_MANDATORY(int, resolution, toInt); Chris@45: Chris@45: if (attributes.value("subtype") == "image") { Chris@45: Chris@45: bool notifyOnAdd = (attributes.value("notifyOnAdd") == "true"); Chris@45: ImageModel *model = new ImageModel(sampleRate, resolution, Chris@45: notifyOnAdd); Chris@45: model->setObjectName(name); Chris@45: m_models[id] = model; Chris@45: Chris@45: } else { Chris@45: Chris@45: SparseOneDimensionalModel *model = new SparseOneDimensionalModel Chris@45: (sampleRate, resolution); Chris@45: model->setObjectName(name); Chris@45: m_models[id] = model; Chris@45: } Chris@45: Chris@45: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@45: if (ok) m_awaitingDatasets[dataset] = id; Chris@45: Chris@45: return true; Chris@45: Chris@45: } else if (dimensions == 2 || dimensions == 3) { Chris@45: Chris@45: READ_MANDATORY(int, resolution, toInt); Chris@45: Chris@45: bool haveMinMax = true; Chris@45: float minimum = attributes.value("minimum").trimmed().toFloat(&ok); Chris@45: if (!ok) haveMinMax = false; Chris@45: float maximum = attributes.value("maximum").trimmed().toFloat(&ok); Chris@45: if (!ok) haveMinMax = false; Chris@45: Chris@45: float valueQuantization = Chris@45: attributes.value("valueQuantization").trimmed().toFloat(&ok); Chris@45: Chris@45: bool notifyOnAdd = (attributes.value("notifyOnAdd") == "true"); Chris@45: Chris@45: QString units = attributes.value("units"); Chris@45: Chris@45: if (dimensions == 2) { Chris@45: if (attributes.value("subtype") == "text") { Chris@45: TextModel *model = new TextModel Chris@45: (sampleRate, resolution, notifyOnAdd); Chris@45: model->setObjectName(name); Chris@45: m_models[id] = model; Chris@45: } else { Chris@45: SparseTimeValueModel *model; Chris@45: if (haveMinMax) { Chris@45: model = new SparseTimeValueModel Chris@45: (sampleRate, resolution, minimum, maximum, notifyOnAdd); Chris@45: } else { Chris@45: model = new SparseTimeValueModel Chris@45: (sampleRate, resolution, notifyOnAdd); Chris@45: } Chris@45: model->setScaleUnits(units); Chris@45: model->setObjectName(name); Chris@45: m_models[id] = model; Chris@45: } Chris@45: } else { Chris@45: NoteModel *model; Chris@45: if (haveMinMax) { Chris@45: model = new NoteModel Chris@45: (sampleRate, resolution, minimum, maximum, notifyOnAdd); Chris@45: } else { Chris@45: model = new NoteModel Chris@45: (sampleRate, resolution, notifyOnAdd); Chris@45: } Chris@45: model->setValueQuantization(valueQuantization); Chris@45: model->setScaleUnits(units); Chris@45: model->setObjectName(name); Chris@45: m_models[id] = model; Chris@45: } Chris@45: Chris@45: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@45: if (ok) m_awaitingDatasets[dataset] = id; Chris@45: Chris@45: return true; Chris@45: Chris@45: } else { Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Unexpected sparse model dimension (" Chris@45: << dimensions << ")" << std::endl; Chris@45: } Chris@45: } else { Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Unexpected model type \"" Chris@45: << type.toLocal8Bit().data() << "\" for model id " << id << std::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@45: m_currentPane = 0; Chris@45: Chris@45: if (type != "pane") { Chris@45: std::cerr << "WARNING: SV-XML: Unexpected view type \"" Chris@45: << type.toLocal8Bit().data() << "\"" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: m_currentPane = m_paneCallback.addPane(); Chris@45: Chris@45: if (!m_currentPane) { Chris@45: std::cerr << "WARNING: SV-XML: Internal error: Failed to add pane!" Chris@45: << std::endl; Chris@45: 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@45: READ_MANDATORY(size_t, centre, toUInt); Chris@45: READ_MANDATORY(size_t, zoom, toUInt); Chris@45: READ_MANDATORY(int, followPan, toInt); Chris@45: READ_MANDATORY(int, followZoom, toInt); Chris@45: QString tracking = attributes.value("tracking"); Chris@45: 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@45: tracking == "page" ? PlaybackScrollPage Chris@45: : PlaybackIgnore); Chris@45: Chris@45: // Then set these values Chris@45: view->setCentreFrame(centre); Chris@45: view->setZoomLevel(zoom); 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@45: 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@45: std::cerr << "WARNING: SV-XML: No layer id for layer of type \"" Chris@45: << type.toLocal8Bit().data() Chris@45: << "\"" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: Layer *layer = 0; 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@45: if (m_layers.find(id) != m_layers.end()) { Chris@45: std::cerr << "WARNING: SV-XML: Ignoring duplicate layer id " << id Chris@45: << " in data section" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: layer = m_layers[id] = m_document->createLayer Chris@45: (LayerFactory::getInstance()->getLayerTypeForName(type)); Chris@45: Chris@45: if (layer) { Chris@45: m_layers[id] = layer; Chris@45: isNewLayer = true; Chris@45: } Chris@45: Chris@45: } else { Chris@45: Chris@45: if (!m_currentPane) { Chris@45: std::cerr << "WARNING: SV-XML: No current pane for layer " << id Chris@45: << " in view section" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: if (m_layers.find(id) != m_layers.end()) { Chris@45: Chris@45: layer = m_layers[id]; Chris@45: Chris@45: } else { Chris@45: std::cerr << "WARNING: SV-XML: Layer id " << id Chris@45: << " in view section has not been defined -- defining it here" Chris@45: << std::endl; Chris@45: Chris@45: layer = m_document->createLayer Chris@45: (LayerFactory::getInstance()->getLayerTypeForName(type)); Chris@45: Chris@45: if (layer) { Chris@45: m_layers[id] = layer; Chris@45: isNewLayer = true; Chris@45: } Chris@45: } Chris@45: } Chris@45: Chris@45: if (!layer) { Chris@45: std::cerr << "WARNING: SV-XML: Failed to add layer of type \"" Chris@45: << type.toLocal8Bit().data() Chris@45: << "\"" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: if (isNewLayer) { Chris@45: Chris@45: QString name = attributes.value("name"); Chris@45: layer->setObjectName(name); Chris@45: Chris@45: int modelId; Chris@45: bool modelOk = false; Chris@45: modelId = attributes.value("model").trimmed().toInt(&modelOk); Chris@45: Chris@45: if (modelOk) { Chris@45: if (haveModel(modelId)) { Chris@45: Model *model = m_models[modelId]; Chris@45: m_document->setModel(layer, model); Chris@45: } else { Chris@45: std::cerr << "WARNING: SV-XML: Unknown model id " << modelId Chris@45: << " in layer definition" << std::endl; Chris@45: } Chris@45: } Chris@45: Chris@45: layer->setProperties(attributes); Chris@45: } Chris@45: Chris@45: if (!m_inData && m_currentPane) { 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@45: 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@45: m_inLayer = true; 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@45: std::cerr << "WARNING: SV-XML: Unwanted dataset " << id << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: int modelId = m_awaitingDatasets[id]; Chris@45: Chris@45: Model *model = 0; Chris@45: if (haveModel(modelId)) { Chris@45: model = m_models[modelId]; Chris@45: } else { Chris@45: std::cerr << "WARNING: SV-XML: Internal error: Unknown model " << modelId Chris@45: << " expecting dataset " << id << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: bool good = false; Chris@45: Chris@45: switch (dimensions) { Chris@45: case 1: Chris@45: if (dynamic_cast(model)) good = true; Chris@45: else if (dynamic_cast(model)) good = true; Chris@45: break; Chris@45: Chris@45: case 2: Chris@45: if (dynamic_cast(model)) good = true; Chris@45: else if (dynamic_cast(model)) good = true; Chris@45: break; Chris@45: Chris@45: case 3: Chris@45: if (dynamic_cast(model)) good = true; Chris@45: else if (dynamic_cast(model)) { Chris@45: m_datasetSeparator = attributes.value("separator"); Chris@45: good = true; Chris@45: } Chris@45: break; Chris@45: } Chris@45: Chris@45: if (!good) { Chris@45: std::cerr << "WARNING: SV-XML: Model id " << modelId << " has wrong number of dimensions or inappropriate type for " << dimensions << "-D dataset " << id << std::endl; Chris@45: m_currentDataset = 0; Chris@45: return false; Chris@45: } Chris@45: Chris@45: m_currentDataset = model; 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@45: // std::cerr << "SVFileReader::addPointToDataset: frame = " << frame << std::endl; Chris@45: Chris@45: SparseOneDimensionalModel *sodm = dynamic_cast Chris@45: (m_currentDataset); Chris@45: Chris@45: if (sodm) { Chris@45: // std::cerr << "Current dataset is a sparse one dimensional model" << std::endl; Chris@45: QString label = attributes.value("label"); Chris@45: sodm->addPoint(SparseOneDimensionalModel::Point(frame, label)); Chris@45: return true; Chris@45: } Chris@45: Chris@45: SparseTimeValueModel *stvm = dynamic_cast Chris@45: (m_currentDataset); Chris@45: Chris@45: if (stvm) { Chris@45: // std::cerr << "Current dataset is a sparse time-value model" << std::endl; Chris@45: float value = 0.0; Chris@45: value = attributes.value("value").trimmed().toFloat(&ok); Chris@45: QString label = attributes.value("label"); Chris@45: stvm->addPoint(SparseTimeValueModel::Point(frame, value, label)); Chris@45: return ok; Chris@45: } Chris@45: Chris@45: NoteModel *nm = dynamic_cast(m_currentDataset); Chris@45: Chris@45: if (nm) { Chris@45: // std::cerr << "Current dataset is a note model" << std::endl; Chris@45: float value = 0.0; Chris@45: value = attributes.value("value").trimmed().toFloat(&ok); Chris@45: size_t duration = 0; Chris@45: duration = attributes.value("duration").trimmed().toUInt(&ok); Chris@45: QString label = attributes.value("label"); Chris@45: nm->addPoint(NoteModel::Point(frame, value, duration, label)); Chris@45: return ok; Chris@45: } Chris@45: Chris@45: TextModel *tm = dynamic_cast(m_currentDataset); Chris@45: Chris@45: if (tm) { Chris@45: // std::cerr << "Current dataset is a text model" << std::endl; Chris@45: float height = 0.0; Chris@45: height = attributes.value("height").trimmed().toFloat(&ok); Chris@45: QString label = attributes.value("label"); Chris@45: // std::cerr << "SVFileReader::addPointToDataset: TextModel: frame = " << frame << ", height = " << height << ", label = " << label.toStdString() << ", ok = " << ok << std::endl; Chris@45: tm->addPoint(TextModel::Point(frame, height, label)); Chris@45: return ok; Chris@45: } Chris@45: Chris@45: ImageModel *im = dynamic_cast(m_currentDataset); Chris@45: Chris@45: if (im) { Chris@45: // std::cerr << "Current dataset is an image model" << std::endl; Chris@45: QString image = attributes.value("image"); Chris@45: QString label = attributes.value("label"); Chris@45: // std::cerr << "SVFileReader::addPointToDataset: ImageModel: frame = " << frame << ", image = " << image.toStdString() << ", label = " << label.toStdString() << ", ok = " << ok << std::endl; Chris@45: im->addPoint(ImageModel::Point(frame, image, label)); Chris@45: return ok; Chris@45: } Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Point element found in non-point dataset" << std::endl; Chris@45: Chris@45: return false; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::addBinToDataset(const QXmlAttributes &attributes) Chris@45: { Chris@45: EditableDenseThreeDimensionalModel *dtdm = Chris@45: dynamic_cast Chris@45: (m_currentDataset); Chris@45: Chris@45: if (dtdm) { Chris@45: Chris@45: bool ok = false; Chris@45: int n = attributes.value("number").trimmed().toInt(&ok); Chris@45: if (!ok) { Chris@45: std::cerr << "WARNING: SV-XML: Missing or invalid bin number" Chris@45: << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: QString name = attributes.value("name"); Chris@45: Chris@45: dtdm->setBinName(n, name); Chris@45: return true; Chris@45: } Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Bin definition found in incompatible dataset" << std::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@45: std::cerr << "WARNING: SV-XML: Missing or invalid row number" Chris@45: << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: m_inRow = true; Chris@45: Chris@45: // std::cerr << "SV-XML: In row " << m_rowNumber << std::endl; Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readRowData(const QString &text) Chris@45: { Chris@45: EditableDenseThreeDimensionalModel *dtdm = Chris@45: dynamic_cast Chris@45: (m_currentDataset); Chris@45: Chris@45: bool warned = false; Chris@45: Chris@45: if (dtdm) { Chris@45: QStringList data = text.split(m_datasetSeparator); Chris@45: Chris@45: DenseThreeDimensionalModel::Column values; Chris@45: Chris@45: for (QStringList::iterator i = data.begin(); i != data.end(); ++i) { Chris@45: Chris@45: if (values.size() == dtdm->getHeight()) { Chris@45: if (!warned) { Chris@45: std::cerr << "WARNING: SV-XML: Too many y-bins in 3-D dataset row " Chris@45: << m_rowNumber << std::endl; Chris@45: warned = true; Chris@45: } Chris@45: } Chris@45: Chris@45: bool ok; Chris@45: float value = i->toFloat(&ok); Chris@45: if (!ok) { Chris@45: std::cerr << "WARNING: SV-XML: Bad floating-point value " Chris@45: << i->toLocal8Bit().data() Chris@45: << " in row data" << std::endl; Chris@45: } else { Chris@45: values.push_back(value); Chris@45: } Chris@45: } Chris@45: Chris@45: dtdm->setColumn(m_rowNumber, values); Chris@45: return true; Chris@45: } Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Row data found in non-row dataset" << std::endl; Chris@45: Chris@45: return false; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readDerivation(const QXmlAttributes &attributes) Chris@45: { Chris@45: int modelId = 0; Chris@45: bool modelOk = false; Chris@45: modelId = attributes.value("model").trimmed().toInt(&modelOk); Chris@45: Chris@45: if (!modelOk) { Chris@45: std::cerr << "WARNING: SV-XML: No model id specified for derivation" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: QString transform = attributes.value("transform"); Chris@45: Chris@45: if (haveModel(modelId)) { Chris@45: m_currentDerivedModel = m_models[modelId]; Chris@45: } else { Chris@45: // we'll regenerate the model when the derivation element ends Chris@45: m_currentDerivedModel = 0; Chris@45: } Chris@45: Chris@45: m_currentDerivedModelId = modelId; 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@45: m_currentTransformSource = m_models[sourceId]; Chris@45: } else { Chris@45: m_currentTransformSource = m_document->getMainModel(); Chris@45: } Chris@45: Chris@45: m_currentTransform = transform; Chris@45: m_currentTransformConfiguration = ""; Chris@45: Chris@45: m_currentTransformContext = PluginTransform::ExecutionContext(); Chris@45: Chris@45: bool ok = false; Chris@45: int channel = attributes.value("channel").trimmed().toInt(&ok); Chris@45: if (ok) m_currentTransformContext.channel = channel; Chris@45: Chris@45: int domain = attributes.value("domain").trimmed().toInt(&ok); Chris@45: if (ok) m_currentTransformContext.domain = Vamp::Plugin::InputDomain(domain); Chris@45: Chris@45: int stepSize = attributes.value("stepSize").trimmed().toInt(&ok); Chris@45: if (ok) m_currentTransformContext.stepSize = stepSize; Chris@45: Chris@45: int blockSize = attributes.value("blockSize").trimmed().toInt(&ok); Chris@45: if (ok) m_currentTransformContext.blockSize = blockSize; Chris@45: Chris@45: int windowType = attributes.value("windowType").trimmed().toInt(&ok); Chris@45: if (ok) m_currentTransformContext.windowType = WindowType(windowType); Chris@45: Chris@45: QString startFrameStr = attributes.value("startFrame"); Chris@45: QString durationStr = attributes.value("duration"); Chris@45: Chris@45: size_t startFrame = 0; Chris@45: size_t 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@45: m_currentTransformContext.startFrame = startFrame; Chris@45: m_currentTransformContext.duration = duration; Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readPlayParameters(const QXmlAttributes &attributes) Chris@45: { Chris@45: m_currentPlayParameters = 0; Chris@45: Chris@45: int modelId = 0; Chris@45: bool modelOk = false; Chris@45: modelId = attributes.value("model").trimmed().toInt(&modelOk); Chris@45: Chris@45: if (!modelOk) { Chris@45: std::cerr << "WARNING: SV-XML: No model id specified for play parameters" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: if (haveModel(modelId)) { Chris@45: Chris@45: bool ok = false; Chris@45: Chris@45: PlayParameters *parameters = PlayParameterRepository::getInstance()-> Chris@45: getPlayParameters(m_models[modelId]); Chris@45: Chris@45: if (!parameters) { Chris@45: std::cerr << "WARNING: SV-XML: Play parameters for model " Chris@45: << modelId Chris@45: << " not found - has model been added to document?" Chris@45: << std::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@45: QString pluginId = attributes.value("pluginId"); Chris@45: if (pluginId != "") parameters->setPlayPluginId(pluginId); Chris@45: Chris@45: m_currentPlayParameters = parameters; Chris@45: Chris@45: // std::cerr << "Current play parameters for model: " << m_models[modelId] << ": " << m_currentPlayParameters << std::endl; Chris@45: Chris@45: } else { Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Unknown model " << modelId Chris@45: << " for play parameters" << std::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@45: if (m_currentDerivedModelId < 0 && !m_currentPlayParameters) { Chris@45: std::cerr << "WARNING: SV-XML: Plugin found outside derivation or play parameters" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: QString configurationXml = "setPlayPluginConfiguration(configurationXml); Chris@45: } else { Chris@45: m_currentTransformConfiguration += configurationXml; Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: 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@45: std::cerr << "SVFileReader::readMeasurement: inLayer " Chris@45: << m_inLayer << ", layer " << m_currentLayer << std::endl; Chris@45: Chris@45: if (!m_inLayer) { Chris@45: std::cerr << "WARNING: SV-XML: Measurement found outside layer" << std::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: