diff framework/SVFileReader.cpp @ 72:4aa40182321f

* Merge from transforms branch -- switch over to using Transform object properly
author Chris Cannam
date Fri, 07 Dec 2007 16:47:31 +0000
parents 215b8b1b0308
children 2da0999dac1d
line wrap: on
line diff
--- a/framework/SVFileReader.cpp	Fri Nov 30 17:31:09 2007 +0000
+++ b/framework/SVFileReader.cpp	Fri Dec 07 16:47:31 2007 +0000
@@ -32,6 +32,8 @@
 #include "data/model/TextModel.h"
 #include "data/model/ImageModel.h"
 
+#include "plugin/transform/TransformFactory.h"
+
 #include "view/Pane.h"
 
 #include "Document.h"
@@ -53,6 +55,7 @@
     m_currentDerivedModel(0),
     m_currentDerivedModelId(-1),
     m_currentPlayParameters(0),
+    m_currentTransformSource(0),
     m_datasetSeparator(" "),
     m_inRow(false),
     m_inLayer(false),
@@ -136,6 +139,11 @@
     // row
     // view
     // window
+    // plugin
+    // transform
+    // selections
+    // selection
+    // measurement
 
     if (name == "sv") {
 
@@ -212,6 +220,14 @@
 
         ok = readMeasurement(attributes);
 
+    } else if (name == "transform") {
+        
+        ok = readTransform(attributes);
+
+    } else if (name == "parameter") {
+
+        ok = readParameter(attributes);
+
     } else {
         std::cerr << "WARNING: SV-XML: Unexpected element \""
                   << name.toLocal8Bit().data() << "\"" << std::endl;
@@ -286,24 +302,25 @@
                           << " as target, not regenerating" << std::endl;
             } else {
                 m_currentDerivedModel = m_models[m_currentDerivedModelId] =
-                    m_document->addDerivedModel(m_currentTransformer,
-                                                m_currentTransformerSource,
-                                                m_currentTransformerContext,
-                                                m_currentTransformerConfiguration);
+                    m_document->addDerivedModel
+                    (m_currentTransform,
+                     ModelTransformer::Input(m_currentTransformSource,
+                                             m_currentTransformChannel));
             }
         } else {
-            m_document->addDerivedModel(m_currentTransformer,
-                                        m_currentTransformerSource,
-                                        m_currentTransformerContext,
-                                        m_currentDerivedModel,
-                                        m_currentTransformerConfiguration);
+            m_document->addDerivedModel
+                (m_currentTransform,
+                 ModelTransformer::Input(m_currentTransformSource,
+                                         m_currentTransformChannel),
+                 m_currentDerivedModel);
         }
 
         m_addedModels.insert(m_currentDerivedModel);
         m_currentDerivedModel = 0;
         m_currentDerivedModelId = -1;
-        m_currentTransformer = "";
-        m_currentTransformerConfiguration = "";
+        m_currentTransformSource = 0;
+        m_currentTransform = Transform();
+        m_currentTransformChannel = -1;
 
     } else if (name == "row") {
 	m_inRow = false;
@@ -993,8 +1010,6 @@
 	return false;
     }
 
-    QString transform = attributes.value("transform");
-
     if (haveModel(modelId)) {
         m_currentDerivedModel = m_models[modelId];
     } else {
@@ -1009,31 +1024,43 @@
     sourceId = attributes.value("source").trimmed().toInt(&sourceOk);
 
     if (sourceOk && haveModel(sourceId)) {
-        m_currentTransformerSource = m_models[sourceId];
+        m_currentTransformSource = m_models[sourceId];
     } else {
-        m_currentTransformerSource = m_document->getMainModel();
+        m_currentTransformSource = m_document->getMainModel();
     }
 
-    m_currentTransformer = transform;
-    m_currentTransformerConfiguration = "";
-
-    m_currentTransformerContext = PluginTransformer::ExecutionContext();
+    m_currentTransform = Transform();
 
     bool ok = false;
     int channel = attributes.value("channel").trimmed().toInt(&ok);
-    if (ok) m_currentTransformerContext.channel = channel;
+    if (ok) m_currentTransformChannel = channel;
+    else m_currentTransformChannel = -1;
 
-    int domain = attributes.value("domain").trimmed().toInt(&ok);
-    if (ok) m_currentTransformerContext.domain = Vamp::Plugin::InputDomain(domain);
+    QString type = attributes.value("type");
+
+    if (type == "transform") {
+        m_currentTransformIsNewStyle = true;
+        return true;
+    } else {
+        m_currentTransformIsNewStyle = false;
+        std::cerr << "NOTE: SV-XML: Reading old-style derivation element"
+                  << std::endl;
+    }
+
+    QString transformId = attributes.value("transform");
+
+    m_currentTransform.setIdentifier(transformId);
 
     int stepSize = attributes.value("stepSize").trimmed().toInt(&ok);
-    if (ok) m_currentTransformerContext.stepSize = stepSize;
+    if (ok) m_currentTransform.setStepSize(stepSize);
 
     int blockSize = attributes.value("blockSize").trimmed().toInt(&ok);
-    if (ok) m_currentTransformerContext.blockSize = blockSize;
+    if (ok) m_currentTransform.setBlockSize(blockSize);
 
     int windowType = attributes.value("windowType").trimmed().toInt(&ok);
-    if (ok) m_currentTransformerContext.windowType = WindowType(windowType);
+    if (ok) m_currentTransform.setWindowType(WindowType(windowType));
+
+    if (!m_currentTransformSource) return true;
 
     QString startFrameStr = attributes.value("startFrame");
     QString durationStr = attributes.value("duration");
@@ -1050,8 +1077,13 @@
         if (!ok) duration = 0;
     }
 
-    m_currentTransformerContext.startFrame = startFrame;
-    m_currentTransformerContext.duration = duration;
+    m_currentTransform.setStartTime
+        (RealTime::frame2RealTime
+         (startFrame, m_currentTransformSource->getSampleRate()));
+
+    m_currentTransform.setDuration
+        (RealTime::frame2RealTime
+         (duration, m_currentTransformSource->getSampleRate()));
 
     return true;
 }
@@ -1119,6 +1151,10 @@
         return false;
     }
 
+    if (!m_currentPlayParameters && m_currentTransformIsNewStyle) {
+        return true;
+    }
+
     QString configurationXml = "<plugin";
     
     for (int i = 0; i < attributes.length(); ++i) {
@@ -1132,13 +1168,48 @@
     if (m_currentPlayParameters) {
         m_currentPlayParameters->setPlayPluginConfiguration(configurationXml);
     } else {
-        m_currentTransformerConfiguration += configurationXml;
+        TransformFactory::getInstance()->
+            setParametersFromPluginConfigurationXml(m_currentTransform,
+                                                    configurationXml);
     }
 
     return true;
 }
 
 bool
+SVFileReader::readTransform(const QXmlAttributes &attributes)
+{
+    if (m_currentDerivedModelId < 0) {
+        std::cerr << "WARNING: SV-XML: Transform found outside derivation" << std::endl;
+        return false;
+    }
+
+    m_currentTransform.setFromXmlAttributes(attributes);
+    return true;
+}
+
+bool
+SVFileReader::readParameter(const QXmlAttributes &attributes)
+{
+    if (m_currentDerivedModelId < 0) {
+        std::cerr << "WARNING: SV-XML: Parameter found outside derivation" << std::endl;
+        return false;
+    }
+
+    QString name = attributes.value("name");
+    if (name == "") {
+        std::cerr << "WARNING: SV-XML: Ignoring nameless transform parameter"
+                  << std::endl;
+        return false;
+    }
+
+    float value = attributes.value("value").trimmed().toFloat();
+
+    m_currentTransform.setParameter(name, value);
+    return true;
+}
+
+bool
 SVFileReader::readSelection(const QXmlAttributes &attributes)
 {
     bool ok;