Chris@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: Sonic Visualiser Chris@0: An audio file viewer and annotation editor. Chris@0: Centre for Digital Music, Queen Mary, University of London. Chris@77: This file copyright 2006 Chris Cannam and QMUL. Chris@0: Chris@0: This program is free software; you can redistribute it and/or Chris@0: modify it under the terms of the GNU General Public License as Chris@0: published by the Free Software Foundation; either version 2 of the Chris@0: License, or (at your option) any later version. See the file Chris@0: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "SVFileReader.h" Chris@0: Chris@1: #include "layer/Layer.h" Chris@1: #include "view/View.h" Chris@0: #include "base/PlayParameters.h" Chris@0: #include "base/PlayParameterRepository.h" Chris@0: Chris@1: #include "data/fileio/AudioFileReaderFactory.h" Chris@87: #include "data/fileio/FileFinder.h" Chris@87: #include "data/fileio/RemoteFile.h" Chris@0: Chris@1: #include "data/model/WaveFileModel.h" Chris@3: #include "data/model/EditableDenseThreeDimensionalModel.h" Chris@1: #include "data/model/SparseOneDimensionalModel.h" Chris@1: #include "data/model/SparseTimeValueModel.h" Chris@1: #include "data/model/NoteModel.h" Chris@1: #include "data/model/TextModel.h" Chris@0: Chris@1: #include "view/Pane.h" Chris@0: Chris@1: #include "Document.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@55: /* Chris@55: Some notes about the SV XML format. We're very lazy with our XML: Chris@55: there's no schema or DTD, and we depend heavily on elements being Chris@55: in a particular order. Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: Chris@55: */ Chris@55: Chris@55: Chris@0: SVFileReader::SVFileReader(Document *document, Chris@87: SVFileReaderPaneCallback &callback, Chris@87: QString location) : Chris@0: m_document(document), Chris@0: m_paneCallback(callback), Chris@87: m_location(location), Chris@0: m_currentPane(0), Chris@0: m_currentDataset(0), Chris@0: m_currentDerivedModel(0), Chris@55: m_currentDerivedModelId(-1), Chris@0: m_currentPlayParameters(0), Chris@0: m_datasetSeparator(" "), Chris@0: m_inRow(false), Chris@0: m_rowNumber(0), Chris@0: m_ok(false) Chris@0: { Chris@0: } Chris@0: Chris@0: void Chris@0: SVFileReader::parse(const QString &xmlData) Chris@0: { Chris@0: QXmlInputSource inputSource; Chris@0: inputSource.setData(xmlData); Chris@0: parse(inputSource); Chris@0: } Chris@0: Chris@0: void Chris@0: SVFileReader::parse(QXmlInputSource &inputSource) Chris@0: { Chris@0: QXmlSimpleReader reader; Chris@0: reader.setContentHandler(this); Chris@0: reader.setErrorHandler(this); Chris@0: m_ok = reader.parse(inputSource); Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::isOK() Chris@0: { Chris@0: return m_ok; Chris@0: } Chris@0: Chris@0: SVFileReader::~SVFileReader() Chris@0: { Chris@0: if (!m_awaitingDatasets.empty()) { Chris@0: std::cerr << "WARNING: SV-XML: File ended with " Chris@0: << m_awaitingDatasets.size() << " unfilled model dataset(s)" Chris@0: << std::endl; Chris@0: } Chris@0: Chris@0: std::set unaddedModels; Chris@0: Chris@0: for (std::map::iterator i = m_models.begin(); Chris@0: i != m_models.end(); ++i) { Chris@0: if (m_addedModels.find(i->second) == m_addedModels.end()) { Chris@0: unaddedModels.insert(i->second); Chris@0: } Chris@0: } Chris@0: Chris@0: if (!unaddedModels.empty()) { Chris@0: std::cerr << "WARNING: SV-XML: File contained " Chris@0: << unaddedModels.size() << " unused models" Chris@0: << std::endl; Chris@0: while (!unaddedModels.empty()) { Chris@0: delete *unaddedModels.begin(); Chris@0: unaddedModels.erase(unaddedModels.begin()); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::startElement(const QString &, const QString &, Chris@0: const QString &qName, Chris@0: const QXmlAttributes &attributes) Chris@0: { Chris@0: QString name = qName.toLower(); Chris@0: Chris@0: bool ok = false; Chris@0: Chris@0: // Valid element names: Chris@0: // Chris@0: // sv Chris@0: // data Chris@0: // dataset Chris@0: // display Chris@0: // derivation Chris@0: // playparameters Chris@0: // layer Chris@0: // model Chris@0: // point Chris@0: // row Chris@0: // view Chris@0: // window Chris@0: Chris@0: if (name == "sv") { Chris@0: Chris@0: // nothing needed Chris@0: ok = true; Chris@0: Chris@0: } else if (name == "data") { Chris@0: Chris@0: // nothing needed Chris@0: m_inData = true; Chris@0: ok = true; Chris@0: Chris@0: } else if (name == "display") { Chris@0: Chris@0: // nothing needed Chris@0: ok = true; Chris@0: Chris@0: } else if (name == "window") { Chris@0: Chris@0: ok = readWindow(attributes); Chris@0: Chris@0: } else if (name == "model") { Chris@0: Chris@0: ok = readModel(attributes); Chris@0: Chris@0: } else if (name == "dataset") { Chris@0: Chris@0: ok = readDatasetStart(attributes); Chris@0: Chris@0: } else if (name == "bin") { Chris@0: Chris@0: ok = addBinToDataset(attributes); Chris@0: Chris@0: } else if (name == "point") { Chris@0: Chris@0: ok = addPointToDataset(attributes); Chris@0: Chris@0: } else if (name == "row") { Chris@0: Chris@0: ok = addRowToDataset(attributes); Chris@0: Chris@0: } else if (name == "layer") { Chris@0: Chris@0: addUnaddedModels(); // all models must be specified before first layer Chris@0: ok = readLayer(attributes); Chris@0: Chris@0: } else if (name == "view") { Chris@0: Chris@0: m_inView = true; Chris@0: ok = readView(attributes); Chris@0: Chris@0: } else if (name == "derivation") { Chris@0: Chris@0: ok = readDerivation(attributes); Chris@0: Chris@0: } else if (name == "playparameters") { Chris@0: Chris@0: ok = readPlayParameters(attributes); Chris@0: Chris@0: } else if (name == "plugin") { Chris@0: Chris@0: ok = readPlugin(attributes); Chris@0: Chris@0: } else if (name == "selections") { Chris@0: Chris@0: m_inSelections = true; Chris@0: ok = true; Chris@0: Chris@0: } else if (name == "selection") { Chris@0: Chris@0: ok = readSelection(attributes); Chris@0: } Chris@0: Chris@0: if (!ok) { Chris@0: std::cerr << "WARNING: SV-XML: Failed to completely process element \"" Chris@0: << name.toLocal8Bit().data() << "\"" << std::endl; Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::characters(const QString &text) Chris@0: { Chris@0: bool ok = false; Chris@0: Chris@0: if (m_inRow) { Chris@0: ok = readRowData(text); Chris@0: if (!ok) { Chris@0: std::cerr << "WARNING: SV-XML: Failed to read row data content for row " << m_rowNumber << std::endl; Chris@0: } Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::endElement(const QString &, const QString &, Chris@0: const QString &qName) Chris@0: { Chris@0: QString name = qName.toLower(); Chris@0: Chris@0: if (name == "dataset") { Chris@0: Chris@0: if (m_currentDataset) { Chris@0: Chris@0: bool foundInAwaiting = false; Chris@0: Chris@0: for (std::map::iterator i = m_awaitingDatasets.begin(); Chris@0: i != m_awaitingDatasets.end(); ++i) { Chris@0: if (m_models[i->second] == m_currentDataset) { Chris@0: m_awaitingDatasets.erase(i); Chris@0: foundInAwaiting = true; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if (!foundInAwaiting) { Chris@0: std::cerr << "WARNING: SV-XML: Dataset precedes model, or no model uses dataset" << std::endl; Chris@0: } Chris@0: } Chris@0: Chris@0: m_currentDataset = 0; Chris@0: Chris@0: } else if (name == "data") { Chris@0: Chris@0: addUnaddedModels(); Chris@0: m_inData = false; Chris@0: Chris@0: } else if (name == "derivation") { Chris@55: Chris@55: if (!m_currentDerivedModel) { Chris@55: if (m_currentDerivedModel < 0) { Chris@55: std::cerr << "WARNING: SV-XML: Bad derivation output model id " Chris@55: << m_currentDerivedModelId << std::endl; Chris@55: } else if (m_models[m_currentDerivedModelId]) { Chris@55: std::cerr << "WARNING: SV-XML: Derivation has existing model " Chris@55: << m_currentDerivedModelId Chris@55: << " as target, not regenerating" << std::endl; Chris@55: } else { Chris@55: m_currentDerivedModel = m_models[m_currentDerivedModelId] = Chris@55: m_document->addDerivedModel(m_currentTransform, Chris@55: m_currentTransformSource, Chris@55: m_currentTransformContext, Chris@55: m_currentTransformConfiguration); Chris@55: } Chris@55: } else { Chris@0: m_document->addDerivedModel(m_currentTransform, Chris@55: m_currentTransformSource, Chris@30: m_currentTransformContext, Chris@0: m_currentDerivedModel, Chris@0: m_currentTransformConfiguration); Chris@0: } Chris@0: Chris@55: m_addedModels.insert(m_currentDerivedModel); Chris@55: m_currentDerivedModel = 0; Chris@55: m_currentDerivedModelId = -1; Chris@55: m_currentTransform = ""; Chris@55: m_currentTransformConfiguration = ""; Chris@55: Chris@0: } else if (name == "row") { Chris@0: m_inRow = false; Chris@0: } else if (name == "view") { Chris@0: m_inView = false; Chris@0: } else if (name == "selections") { Chris@0: m_inSelections = false; Chris@0: } else if (name == "playparameters") { Chris@0: m_currentPlayParameters = 0; Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::error(const QXmlParseException &exception) Chris@0: { Chris@0: m_errorString = Chris@0: QString("ERROR: SV-XML: %1 at line %2, column %3") Chris@0: .arg(exception.message()) Chris@0: .arg(exception.lineNumber()) Chris@0: .arg(exception.columnNumber()); Chris@0: std::cerr << m_errorString.toLocal8Bit().data() << std::endl; Chris@0: return QXmlDefaultHandler::error(exception); Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::fatalError(const QXmlParseException &exception) Chris@0: { Chris@0: m_errorString = Chris@0: QString("FATAL ERROR: SV-XML: %1 at line %2, column %3") Chris@0: .arg(exception.message()) Chris@0: .arg(exception.lineNumber()) Chris@0: .arg(exception.columnNumber()); Chris@0: std::cerr << m_errorString.toLocal8Bit().data() << std::endl; Chris@0: return QXmlDefaultHandler::fatalError(exception); Chris@0: } Chris@0: Chris@0: Chris@0: #define READ_MANDATORY(TYPE, NAME, CONVERSION) \ Chris@0: TYPE NAME = attributes.value(#NAME).trimmed().CONVERSION(&ok); \ Chris@0: if (!ok) { \ Chris@0: std::cerr << "WARNING: SV-XML: Missing or invalid mandatory " #TYPE " attribute \"" #NAME "\"" << std::endl; \ Chris@0: return false; \ Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::readWindow(const QXmlAttributes &attributes) Chris@0: { Chris@0: bool ok = false; Chris@0: Chris@0: READ_MANDATORY(int, width, toInt); Chris@0: READ_MANDATORY(int, height, toInt); Chris@0: Chris@0: m_paneCallback.setWindowSize(width, height); Chris@0: return true; Chris@0: } Chris@0: Chris@0: void Chris@0: SVFileReader::addUnaddedModels() Chris@0: { Chris@0: std::set unaddedModels; Chris@0: Chris@0: for (std::map::iterator i = m_models.begin(); Chris@0: i != m_models.end(); ++i) { Chris@0: if (m_addedModels.find(i->second) == m_addedModels.end()) { Chris@0: unaddedModels.insert(i->second); Chris@0: } Chris@0: } Chris@0: Chris@0: for (std::set::iterator i = unaddedModels.begin(); Chris@0: i != unaddedModels.end(); ++i) { Chris@0: m_document->addImportedModel(*i); Chris@0: m_addedModels.insert(*i); Chris@0: } Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::readModel(const QXmlAttributes &attributes) Chris@0: { Chris@0: bool ok = false; Chris@0: Chris@0: READ_MANDATORY(int, id, toInt); Chris@0: Chris@0: if (m_models.find(id) != m_models.end()) { Chris@0: std::cerr << "WARNING: SV-XML: Ignoring duplicate model id " << id Chris@0: << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: QString name = attributes.value("name"); Chris@0: Chris@0: READ_MANDATORY(int, sampleRate, toInt); Chris@0: Chris@0: QString type = attributes.value("type").trimmed(); Chris@0: bool mainModel = (attributes.value("mainModel").trimmed() == "true"); Chris@55: Chris@0: if (type == "wavefile") { Chris@0: Chris@87: WaveFileModel *model = 0; Chris@88: FileFinder *ff = FileFinder::getInstance(); Chris@88: QString originalPath = attributes.value("file"); Chris@88: QString path = ff->find(FileFinder::AudioFile, Chris@88: originalPath, m_location); Chris@87: QUrl url(path); Chris@87: Chris@87: if (RemoteFile::canHandleScheme(url)) { Chris@87: Chris@87: RemoteFile rf(url); Chris@87: rf.wait(); Chris@87: Chris@87: if (rf.isOK()) { Chris@88: Chris@88: model = new WaveFileModel(rf.getLocalFilename(), path); Chris@87: if (!model->isOK()) { Chris@87: delete model; Chris@87: model = 0; Chris@87: //!!! and delete local file? Chris@87: } Chris@87: } Chris@87: } else { Chris@87: Chris@87: model = new WaveFileModel(path); Chris@87: if (!model->isOK()) { Chris@87: delete model; Chris@87: model = 0; Chris@87: } Chris@87: } Chris@87: Chris@87: if (!model) return false; Chris@87: Chris@0: m_models[id] = model; Chris@0: if (mainModel) { Chris@0: m_document->setMainModel(model); Chris@0: m_addedModels.insert(model); Chris@0: } Chris@0: // Derived models will be added when their derivation Chris@0: // is found. Chris@0: Chris@0: return true; Chris@0: Chris@0: } else if (type == "dense") { Chris@0: Chris@0: READ_MANDATORY(int, dimensions, toInt); Chris@0: Chris@55: // Currently the only dense model we support here is the dense Chris@55: // 3d model. Dense time-value models are always file-backed Chris@55: // waveform data, at this point, and they come in as wavefile Chris@55: // models. Chris@0: Chris@0: if (dimensions == 3) { Chris@0: Chris@0: READ_MANDATORY(int, windowSize, toInt); Chris@0: READ_MANDATORY(int, yBinCount, toInt); Chris@0: Chris@3: EditableDenseThreeDimensionalModel *model = Chris@3: new EditableDenseThreeDimensionalModel Chris@3: (sampleRate, windowSize, yBinCount); Chris@0: Chris@0: float minimum = attributes.value("minimum").trimmed().toFloat(&ok); Chris@0: if (ok) model->setMinimumLevel(minimum); Chris@0: Chris@0: float maximum = attributes.value("maximum").trimmed().toFloat(&ok); Chris@0: if (ok) model->setMaximumLevel(maximum); Chris@0: Chris@0: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@0: if (ok) m_awaitingDatasets[dataset] = id; Chris@0: Chris@0: m_models[id] = model; Chris@0: return true; Chris@0: Chris@0: } else { Chris@0: Chris@0: std::cerr << "WARNING: SV-XML: Unexpected dense model dimension (" Chris@0: << dimensions << ")" << std::endl; Chris@0: } Chris@0: } else if (type == "sparse") { Chris@0: Chris@0: READ_MANDATORY(int, dimensions, toInt); Chris@0: Chris@0: if (dimensions == 1) { Chris@0: Chris@0: READ_MANDATORY(int, resolution, toInt); Chris@0: Chris@0: SparseOneDimensionalModel *model = new SparseOneDimensionalModel Chris@0: (sampleRate, resolution); Chris@0: m_models[id] = model; Chris@0: Chris@0: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@0: if (ok) m_awaitingDatasets[dataset] = id; Chris@0: Chris@0: return true; Chris@0: Chris@0: } else if (dimensions == 2 || dimensions == 3) { Chris@0: Chris@0: READ_MANDATORY(int, resolution, toInt); Chris@0: Chris@0: float minimum = attributes.value("minimum").trimmed().toFloat(&ok); Chris@0: float maximum = attributes.value("maximum").trimmed().toFloat(&ok); Chris@0: float valueQuantization = Chris@0: attributes.value("valueQuantization").trimmed().toFloat(&ok); Chris@0: Chris@0: bool notifyOnAdd = (attributes.value("notifyOnAdd") == "true"); Chris@0: Chris@0: QString units = attributes.value("units"); Chris@0: Chris@0: if (dimensions == 2) { Chris@0: if (attributes.value("subtype") == "text") { Chris@0: TextModel *model = new TextModel Chris@0: (sampleRate, resolution, notifyOnAdd); Chris@0: m_models[id] = model; Chris@0: } else { Chris@0: SparseTimeValueModel *model = new SparseTimeValueModel Chris@0: (sampleRate, resolution, minimum, maximum, notifyOnAdd); Chris@0: model->setScaleUnits(units); Chris@0: m_models[id] = model; Chris@0: } Chris@0: } else { Chris@0: NoteModel *model = new NoteModel Chris@0: (sampleRate, resolution, minimum, maximum, notifyOnAdd); Chris@0: model->setValueQuantization(valueQuantization); Chris@0: model->setScaleUnits(units); Chris@0: m_models[id] = model; Chris@0: } Chris@0: Chris@0: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@0: if (ok) m_awaitingDatasets[dataset] = id; Chris@0: Chris@0: return true; Chris@0: Chris@0: } else { Chris@0: Chris@0: std::cerr << "WARNING: SV-XML: Unexpected sparse model dimension (" Chris@0: << dimensions << ")" << std::endl; Chris@0: } Chris@0: } else { Chris@0: Chris@0: std::cerr << "WARNING: SV-XML: Unexpected model type \"" Chris@88: << type.toLocal8Bit().data() << "\" for model id " << id << std::endl; Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::readView(const QXmlAttributes &attributes) Chris@0: { Chris@0: QString type = attributes.value("type"); Chris@0: m_currentPane = 0; Chris@0: Chris@0: if (type != "pane") { Chris@0: std::cerr << "WARNING: SV-XML: Unexpected view type \"" Chris@0: << type.toLocal8Bit().data() << "\"" << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: m_currentPane = m_paneCallback.addPane(); Chris@0: Chris@0: if (!m_currentPane) { Chris@0: std::cerr << "WARNING: SV-XML: Internal error: Failed to add pane!" Chris@0: << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: bool ok = false; Chris@0: Chris@0: View *view = m_currentPane; Chris@0: Chris@0: // The view properties first Chris@0: Chris@0: READ_MANDATORY(size_t, centre, toUInt); Chris@0: READ_MANDATORY(size_t, zoom, toUInt); Chris@0: READ_MANDATORY(int, followPan, toInt); Chris@0: READ_MANDATORY(int, followZoom, toInt); Chris@0: QString tracking = attributes.value("tracking"); Chris@0: Chris@0: // Specify the follow modes before we set the actual values Chris@0: view->setFollowGlobalPan(followPan); Chris@0: view->setFollowGlobalZoom(followZoom); Chris@109: view->setPlaybackFollow(tracking == "scroll" ? PlaybackScrollContinuous : Chris@109: tracking == "page" ? PlaybackScrollPage Chris@109: : PlaybackIgnore); Chris@0: Chris@0: // Then set these values Chris@0: view->setCentreFrame(centre); Chris@0: view->setZoomLevel(zoom); Chris@0: Chris@0: // And pane properties Chris@0: READ_MANDATORY(int, centreLineVisible, toInt); Chris@0: m_currentPane->setCentreLineVisible(centreLineVisible); Chris@0: Chris@0: int height = attributes.value("height").toInt(&ok); Chris@0: if (ok) { Chris@0: m_currentPane->resize(m_currentPane->width(), height); Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::readLayer(const QXmlAttributes &attributes) Chris@0: { Chris@0: QString type = attributes.value("type"); Chris@0: Chris@0: int id; Chris@0: bool ok = false; Chris@0: id = attributes.value("id").trimmed().toInt(&ok); Chris@0: Chris@0: if (!ok) { Chris@0: std::cerr << "WARNING: SV-XML: No layer id for layer of type \"" Chris@0: << type.toLocal8Bit().data() Chris@0: << "\"" << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: Layer *layer = 0; Chris@0: bool isNewLayer = false; Chris@0: Chris@0: // Layers are expected to be defined in layer elements in the data Chris@0: // section, and referred to in layer elements in the view Chris@0: // sections. So if we're in the data section, we expect this Chris@0: // layer not to exist already; if we're in the view section, we Chris@0: // expect it to exist. Chris@0: Chris@0: if (m_inData) { Chris@0: Chris@0: if (m_layers.find(id) != m_layers.end()) { Chris@0: std::cerr << "WARNING: SV-XML: Ignoring duplicate layer id " << id Chris@0: << " in data section" << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: layer = m_layers[id] = m_document->createLayer Chris@0: (LayerFactory::getInstance()->getLayerTypeForName(type)); Chris@0: Chris@0: if (layer) { Chris@0: m_layers[id] = layer; Chris@0: isNewLayer = true; Chris@0: } Chris@0: Chris@0: } else { Chris@0: Chris@0: if (!m_currentPane) { Chris@0: std::cerr << "WARNING: SV-XML: No current pane for layer " << id Chris@0: << " in view section" << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: if (m_layers.find(id) != m_layers.end()) { Chris@0: Chris@0: layer = m_layers[id]; Chris@0: Chris@0: } else { Chris@0: std::cerr << "WARNING: SV-XML: Layer id " << id Chris@0: << " in view section has not been defined -- defining it here" Chris@0: << std::endl; Chris@0: Chris@0: layer = m_document->createLayer Chris@0: (LayerFactory::getInstance()->getLayerTypeForName(type)); Chris@0: Chris@0: if (layer) { Chris@0: m_layers[id] = layer; Chris@0: isNewLayer = true; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if (!layer) { Chris@0: std::cerr << "WARNING: SV-XML: Failed to add layer of type \"" Chris@0: << type.toLocal8Bit().data() Chris@0: << "\"" << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: if (isNewLayer) { Chris@0: Chris@0: QString name = attributes.value("name"); Chris@0: layer->setObjectName(name); Chris@0: Chris@0: int modelId; Chris@0: bool modelOk = false; Chris@0: modelId = attributes.value("model").trimmed().toInt(&modelOk); Chris@0: Chris@0: if (modelOk) { Chris@0: if (m_models.find(modelId) != m_models.end()) { Chris@0: Model *model = m_models[modelId]; Chris@0: m_document->setModel(layer, model); Chris@0: } else { Chris@0: std::cerr << "WARNING: SV-XML: Unknown model id " << modelId Chris@0: << " in layer definition" << std::endl; Chris@0: } Chris@0: } Chris@0: Chris@0: layer->setProperties(attributes); Chris@0: } Chris@0: Chris@0: if (!m_inData && m_currentPane) { Chris@84: Chris@84: QString visible = attributes.value("visible"); Chris@84: bool dormant = (visible == "false"); Chris@84: Chris@84: // We need to do this both before and after adding the layer Chris@84: // to the view -- we need it to be dormant if appropriate Chris@84: // before it's actually added to the view so that any property Chris@84: // box gets the right state when it's added, but the add layer Chris@84: // command sets dormant to false because it assumes it may be Chris@84: // restoring a previously dormant layer, so we need to set it Chris@84: // again afterwards too. Hm Chris@84: layer->setLayerDormant(m_currentPane, dormant); Chris@84: Chris@0: m_document->addLayerToView(m_currentPane, layer); Chris@84: Chris@84: layer->setLayerDormant(m_currentPane, dormant); Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::readDatasetStart(const QXmlAttributes &attributes) Chris@0: { Chris@0: bool ok = false; Chris@0: Chris@0: READ_MANDATORY(int, id, toInt); Chris@0: READ_MANDATORY(int, dimensions, toInt); Chris@0: Chris@0: if (m_awaitingDatasets.find(id) == m_awaitingDatasets.end()) { Chris@0: std::cerr << "WARNING: SV-XML: Unwanted dataset " << id << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: int modelId = m_awaitingDatasets[id]; Chris@0: Chris@0: Model *model = 0; Chris@0: if (m_models.find(modelId) != m_models.end()) { Chris@0: model = m_models[modelId]; Chris@0: } else { Chris@0: std::cerr << "WARNING: SV-XML: Internal error: Unknown model " << modelId Chris@0: << " expecting dataset " << id << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: bool good = false; Chris@0: Chris@0: switch (dimensions) { Chris@0: case 1: Chris@0: if (dynamic_cast(model)) good = true; Chris@0: break; Chris@0: Chris@0: case 2: Chris@0: if (dynamic_cast(model)) good = true; Chris@0: else if (dynamic_cast(model)) good = true; Chris@0: break; Chris@0: Chris@0: case 3: Chris@0: if (dynamic_cast(model)) good = true; Chris@3: else if (dynamic_cast(model)) { Chris@0: m_datasetSeparator = attributes.value("separator"); Chris@0: good = true; Chris@0: } Chris@0: break; Chris@0: } Chris@0: Chris@0: if (!good) { Chris@0: std::cerr << "WARNING: SV-XML: Model id " << modelId << " has wrong number of dimensions for " << dimensions << "-D dataset " << id << std::endl; Chris@0: m_currentDataset = 0; Chris@0: return false; Chris@0: } Chris@0: Chris@0: m_currentDataset = model; Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::addPointToDataset(const QXmlAttributes &attributes) Chris@0: { Chris@0: bool ok = false; Chris@0: Chris@0: READ_MANDATORY(int, frame, toInt); Chris@0: Chris@0: SparseOneDimensionalModel *sodm = dynamic_cast Chris@0: (m_currentDataset); Chris@0: Chris@0: if (sodm) { Chris@0: QString label = attributes.value("label"); Chris@0: sodm->addPoint(SparseOneDimensionalModel::Point(frame, label)); Chris@0: return true; Chris@0: } Chris@0: Chris@0: SparseTimeValueModel *stvm = dynamic_cast Chris@0: (m_currentDataset); Chris@0: Chris@0: if (stvm) { Chris@0: float value = 0.0; Chris@0: value = attributes.value("value").trimmed().toFloat(&ok); Chris@0: QString label = attributes.value("label"); Chris@0: stvm->addPoint(SparseTimeValueModel::Point(frame, value, label)); Chris@0: return ok; Chris@0: } Chris@0: Chris@0: NoteModel *nm = dynamic_cast(m_currentDataset); Chris@0: Chris@0: if (nm) { Chris@0: float value = 0.0; Chris@0: value = attributes.value("value").trimmed().toFloat(&ok); Chris@0: float duration = 0.0; Chris@0: duration = attributes.value("duration").trimmed().toFloat(&ok); Chris@0: QString label = attributes.value("label"); Chris@0: nm->addPoint(NoteModel::Point(frame, value, duration, label)); Chris@0: return ok; Chris@0: } Chris@0: Chris@0: TextModel *tm = dynamic_cast(m_currentDataset); Chris@0: Chris@0: if (tm) { Chris@0: float height = 0.0; Chris@0: height = attributes.value("height").trimmed().toFloat(&ok); Chris@0: QString label = attributes.value("label"); Chris@0: tm->addPoint(TextModel::Point(frame, height, label)); Chris@0: return ok; Chris@0: } Chris@0: Chris@0: std::cerr << "WARNING: SV-XML: Point element found in non-point dataset" << std::endl; Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::addBinToDataset(const QXmlAttributes &attributes) Chris@0: { Chris@3: EditableDenseThreeDimensionalModel *dtdm = Chris@3: dynamic_cast Chris@0: (m_currentDataset); Chris@0: Chris@0: if (dtdm) { Chris@0: Chris@0: bool ok = false; Chris@0: int n = attributes.value("number").trimmed().toInt(&ok); Chris@0: if (!ok) { Chris@0: std::cerr << "WARNING: SV-XML: Missing or invalid bin number" Chris@0: << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: QString name = attributes.value("name"); Chris@0: Chris@0: dtdm->setBinName(n, name); Chris@0: return true; Chris@0: } Chris@0: Chris@0: std::cerr << "WARNING: SV-XML: Bin definition found in incompatible dataset" << std::endl; Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: Chris@0: bool Chris@0: SVFileReader::addRowToDataset(const QXmlAttributes &attributes) Chris@0: { Chris@0: m_inRow = false; Chris@0: Chris@0: bool ok = false; Chris@0: m_rowNumber = attributes.value("n").trimmed().toInt(&ok); Chris@0: if (!ok) { Chris@0: std::cerr << "WARNING: SV-XML: Missing or invalid row number" Chris@0: << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: m_inRow = true; Chris@0: Chris@0: // std::cerr << "SV-XML: In row " << m_rowNumber << std::endl; Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::readRowData(const QString &text) Chris@0: { Chris@3: EditableDenseThreeDimensionalModel *dtdm = Chris@3: dynamic_cast Chris@0: (m_currentDataset); Chris@0: Chris@0: bool warned = false; Chris@0: Chris@0: if (dtdm) { Chris@0: QStringList data = text.split(m_datasetSeparator); Chris@0: Chris@51: DenseThreeDimensionalModel::Column values; Chris@0: Chris@0: for (QStringList::iterator i = data.begin(); i != data.end(); ++i) { Chris@0: Chris@51: if (values.size() == dtdm->getHeight()) { Chris@0: if (!warned) { Chris@0: std::cerr << "WARNING: SV-XML: Too many y-bins in 3-D dataset row " Chris@0: << m_rowNumber << std::endl; Chris@0: warned = true; Chris@0: } Chris@0: } Chris@0: Chris@0: bool ok; Chris@0: float value = i->toFloat(&ok); Chris@0: if (!ok) { Chris@0: std::cerr << "WARNING: SV-XML: Bad floating-point value " Chris@0: << i->toLocal8Bit().data() Chris@0: << " in row data" << std::endl; Chris@0: } else { Chris@0: values.push_back(value); Chris@0: } Chris@0: } Chris@0: Chris@51: dtdm->setColumn(m_rowNumber, values); Chris@0: return true; Chris@0: } Chris@0: Chris@0: std::cerr << "WARNING: SV-XML: Row data found in non-row dataset" << std::endl; Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::readDerivation(const QXmlAttributes &attributes) Chris@0: { Chris@0: int modelId = 0; Chris@0: bool modelOk = false; Chris@0: modelId = attributes.value("model").trimmed().toInt(&modelOk); Chris@0: Chris@0: if (!modelOk) { Chris@0: std::cerr << "WARNING: SV-XML: No model id specified for derivation" << std::endl; Chris@0: return false; Chris@0: } Chris@55: Chris@55: QString transform = attributes.value("transform"); Chris@55: Chris@55: if (m_models.find(modelId) != m_models.end()) { Chris@55: m_currentDerivedModel = m_models[modelId]; Chris@55: } else { Chris@55: // we'll regenerate the model when the derivation element ends Chris@55: m_currentDerivedModel = 0; Chris@55: } Chris@0: Chris@55: m_currentDerivedModelId = modelId; Chris@0: Chris@55: int sourceId = 0; Chris@55: bool sourceOk = false; Chris@55: sourceId = attributes.value("source").trimmed().toInt(&sourceOk); Chris@0: Chris@55: if (sourceOk && m_models[sourceId]) { Chris@55: m_currentTransformSource = m_models[sourceId]; Chris@55: } else { Chris@55: m_currentTransformSource = m_document->getMainModel(); Chris@55: } Chris@0: Chris@55: m_currentTransform = transform; Chris@55: m_currentTransformConfiguration = ""; Chris@30: Chris@55: m_currentTransformContext = PluginTransform::ExecutionContext(); Chris@30: Chris@55: bool ok = false; Chris@55: int channel = attributes.value("channel").trimmed().toInt(&ok); Chris@55: if (ok) m_currentTransformContext.channel = channel; Chris@30: Chris@55: int domain = attributes.value("domain").trimmed().toInt(&ok); Chris@55: if (ok) m_currentTransformContext.domain = Vamp::Plugin::InputDomain(domain); Chris@30: Chris@55: int stepSize = attributes.value("stepSize").trimmed().toInt(&ok); Chris@55: if (ok) m_currentTransformContext.stepSize = stepSize; Chris@30: Chris@55: int blockSize = attributes.value("blockSize").trimmed().toInt(&ok); Chris@55: if (ok) m_currentTransformContext.blockSize = blockSize; Chris@0: Chris@55: int windowType = attributes.value("windowType").trimmed().toInt(&ok); Chris@55: if (ok) m_currentTransformContext.windowType = WindowType(windowType); Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::readPlayParameters(const QXmlAttributes &attributes) Chris@0: { Chris@0: m_currentPlayParameters = 0; Chris@0: Chris@0: int modelId = 0; Chris@0: bool modelOk = false; Chris@0: modelId = attributes.value("model").trimmed().toInt(&modelOk); Chris@0: Chris@0: if (!modelOk) { Chris@0: std::cerr << "WARNING: SV-XML: No model id specified for play parameters" << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: if (m_models.find(modelId) != m_models.end()) { Chris@0: Chris@0: bool ok = false; Chris@0: Chris@0: PlayParameters *parameters = PlayParameterRepository::getInstance()-> Chris@0: getPlayParameters(m_models[modelId]); Chris@0: Chris@0: if (!parameters) { Chris@0: std::cerr << "WARNING: SV-XML: Play parameters for model " Chris@0: << modelId Chris@0: << " not found - has model been added to document?" Chris@0: << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: bool muted = (attributes.value("mute").trimmed() == "true"); Chris@83: parameters->setPlayMuted(muted); Chris@0: Chris@0: float pan = attributes.value("pan").toFloat(&ok); Chris@0: if (ok) parameters->setPlayPan(pan); Chris@0: Chris@0: float gain = attributes.value("gain").toFloat(&ok); Chris@0: if (ok) parameters->setPlayGain(gain); Chris@0: Chris@0: QString pluginId = attributes.value("pluginId"); Chris@0: if (pluginId != "") parameters->setPlayPluginId(pluginId); Chris@0: Chris@0: m_currentPlayParameters = parameters; Chris@0: Chris@0: // std::cerr << "Current play parameters for model: " << m_models[modelId] << ": " << m_currentPlayParameters << std::endl; Chris@0: Chris@0: } else { Chris@0: Chris@0: std::cerr << "WARNING: SV-XML: Unknown model " << modelId Chris@0: << " for play parameters" << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::readPlugin(const QXmlAttributes &attributes) Chris@0: { Chris@55: if (m_currentDerivedModelId < 0 && !m_currentPlayParameters) { Chris@0: std::cerr << "WARNING: SV-XML: Plugin found outside derivation or play parameters" << std::endl; Chris@0: return false; Chris@0: } Chris@0: Chris@0: QString configurationXml = "setPlayPluginConfiguration(configurationXml); Chris@0: } else { Chris@0: m_currentTransformConfiguration += configurationXml; Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@0: SVFileReader::readSelection(const QXmlAttributes &attributes) Chris@0: { Chris@0: bool ok; Chris@0: Chris@0: READ_MANDATORY(int, start, toInt); Chris@0: READ_MANDATORY(int, end, toInt); Chris@0: Chris@0: m_paneCallback.addSelection(start, end); Chris@0: Chris@0: return true; Chris@0: } Chris@0: