changeset 9:10715d2b3069

* Make it possible for a rule to have more than one action; update test file
author cannam
date Mon, 27 Nov 2006 13:34:22 +0000
parents c1669af82d6e
children e7fc307b0b6d
files host/Processor.cpp host/Rule.cpp host/Rule.h host/SimpleXMLRuleLoader.cpp host/host.cpp test.xml
diffstat 6 files changed, 57 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/host/Processor.cpp	Mon Nov 27 11:32:40 2006 +0000
+++ b/host/Processor.cpp	Mon Nov 27 13:34:22 2006 +0000
@@ -263,7 +263,7 @@
                                      (fabsf(currentState.value - value) >
                                       0.000001));
                                 Vamp::RealTime gap = rt - currentState.laststamp;
-                                std::cerr << "gap = " << gap << std::endl;
+//                                std::cerr << "gap = " << gap << std::endl;
                                 m_pluginStates[pluginIndex][output] =
                                     OutputState(true, changed, value, rt, gap);
                             }
@@ -488,6 +488,7 @@
                 break;
                 
             case Condition::GapLessThan:
+                std::cout << "GapLessThan: gap is " << state.gap << ", argument is " << argument << std::endl;
                 passed = (state.gap < Vamp::RealTime::fromSeconds(argument));
                 break;
             }
@@ -496,10 +497,13 @@
         }
 
         if (passed) {
-            Action *action = rule->getAction();
-            if (action) {
-                std::cerr << "FIRING RULE: " << action->getName().toStdString() << "!" << std::endl;
-                action->fire();
+            for (Rule::ActionList::const_iterator ai = rule->getActions().begin();
+                 ai != rule->getActions().end(); ++ai) {
+                Action *action = *ai;
+                if (action) {
+//                    std::cerr << "FIRING RULE: " << action->getName().toStdString() << "!" << std::endl;
+                    action->fire();
+                }
             }
         }
     }
@@ -510,7 +514,7 @@
 void
 printFeatures(int plugno, int frame, int sr, int output, Vamp::Plugin::FeatureSet &features)
 {
-//    return;//!!!
+    return;//!!!
 
     if (output > 0) return;//!!!
 
--- a/host/Rule.cpp	Mon Nov 27 11:32:40 2006 +0000
+++ b/host/Rule.cpp	Mon Nov 27 13:34:22 2006 +0000
@@ -4,7 +4,9 @@
 
 Rule::~Rule()
 {
-    delete m_action;
+    while (!m_actions.empty()) {
+        delete *m_actions.begin();
+    }
 }
 
 void
@@ -14,9 +16,8 @@
 }
 
 void
-Rule::setAction(Action *action)
+Rule::addAction(Action *action)
 {
-    delete m_action;
-    m_action = action;
+    m_actions.push_back(action);
 }
 
--- a/host/Rule.h	Mon Nov 27 11:32:40 2006 +0000
+++ b/host/Rule.h	Mon Nov 27 13:34:22 2006 +0000
@@ -34,18 +34,6 @@
     Type getType() const { return m_type; }
     float getArgument() const { return m_argument; }
 
-/*
-    bool operator<(const Condition &c) const {
-        if (m_pluginIndex < c.m_pluginIndex) return true;
-        else if (m_pluginIndex > c.m_pluginIndex) return false;
-        if (m_outputNumber < c.m_outputNumber) return true;
-        else if (m_outputNumber > c.m_outputNumber) return false;
-        if (int(m_type) < int(c.m_type)) return true;
-        else if (int(m_type) > int(c.m_type)) return true;
-        return m_argument < c.m_argument;
-    }
-*/
-
 protected:
     int m_pluginIndex;
     int m_outputNumber;
@@ -56,20 +44,21 @@
 class Rule
 {
 public:
-    Rule() : m_action(0) { }
+    Rule() { }
     virtual ~Rule();
 
     void addCondition(Condition condition);
-    void setAction(Action *action); // I take ownership of action
+    void addAction(Action *action); // I take ownership of action
 
     typedef std::vector<Condition> ConditionList;
     const ConditionList &getConditions() const { return m_conditions; }
 
-    Action *getAction() { return m_action; }
+    typedef std::vector<Action *> ActionList;
+    const ActionList &getActions() const { return m_actions; }
 
 protected:
-    std::vector<Condition> m_conditions;
-    Action *m_action;
+    ConditionList m_conditions;
+    ActionList m_actions;
 };
 
 #endif
--- a/host/SimpleXMLRuleLoader.cpp	Mon Nov 27 11:32:40 2006 +0000
+++ b/host/SimpleXMLRuleLoader.cpp	Mon Nov 27 13:34:22 2006 +0000
@@ -163,13 +163,13 @@
         if (type == "image") {
             QString image = atts.value("image").toLower();
             Action *action = new ImageAction(image);
-            m_rule->setAction(action);
+            m_rule->addAction(action);
             std::cerr << "INFO: SimpleXMLRuleLoader: Added image action" << std::endl;
         } else if (type == "process") {
             QString process = atts.value("process").toLower();
             QStringList args = atts.value("arguments").split(',');
             Action *action = new ExternalProcessAction(process, args);
-            m_rule->setAction(action);
+            m_rule->addAction(action);
             std::cerr << "INFO: SimpleXMLRuleLoader: Added external process action" << std::endl;
         } else {
             std::cerr << "WARNING: SimpleXMLRuleLoader: Unknown action type \""
--- a/host/host.cpp	Mon Nov 27 11:32:40 2006 +0000
+++ b/host/host.cpp	Mon Nov 27 13:34:22 2006 +0000
@@ -12,13 +12,14 @@
 
 #include "Processor.h"
 #include "SimpleXMLRuleLoader.h"
-#include "ImageWindow.h"
 
 #include <cmath>
 #include <cassert>
 
 #include <QApplication>
 #include <QFont>
+#include <QString>
+#include <QStringList>
 #include <QPushButton>
 
 using std::cout;
@@ -27,13 +28,26 @@
 using std::string;
 using std::vector;
 
-static bool loadPlugin(Processor &processor, QString id);
-
 
 int main(int argc, char **argv)
 {
     QApplication application(argc, argv);
 
+    QStringList args = application.arguments();
+
+    QString fileName;
+    for (QStringList::iterator i = args.begin(); i != args.end(); ++i) {
+        if (i == args.begin()) continue;
+        if (i->startsWith('-')) continue;
+        fileName = *i;
+        break;
+    }
+
+    if (fileName == "") {
+        std::cerr << "Usage: " << args.begin()->toStdString() << " <ruleset.xml>" << std::endl;
+        return 2;
+    }
+
     QFont fn = application.font();
     fn.setPointSize(fn.pointSize() + 3);
     application.setFont(fn);
@@ -52,17 +66,11 @@
         return 1;
     }
 
-//    ImageWindow window;
-//    window.show();
+    Processor processor(target);
 
-    Processor processor(target);
-/*
-    QObject::connect(&processor, SIGNAL(showImage(QString)),
-                     &window, SLOT(showImage(QString)));
-*/
     SimpleXMLRuleLoader loader;
-    if (!loader.loadFile(processor, "test.xml")) {
-        std::cerr << "ERROR: Failed to load test XML file" << std::endl;
+    if (!loader.loadFile(processor, fileName)) {
+        std::cerr << "ERROR: Failed to load XML ruleset file \"" << fileName.toStdString() << "\"" << std::endl;
         return 1;
     }
 
@@ -72,14 +80,3 @@
     application.exec();
 }
 
-bool loadPlugin(Processor &processor, QString id)
-{
-    int n = processor.addPlugin(id);
-    if (n) {
-        std::cerr << "Plugin \"" << id.toStdString() << "\" successfully loaded (index " << n << ")" << std::endl;
-        return true;
-    } else {
-        std::cerr << "Failed to load plugin \"" << id.toStdString() << "\"" << std::endl;
-        return false;
-    }
-}
--- a/test.xml	Mon Nov 27 11:32:40 2006 +0000
+++ b/test.xml	Mon Nov 27 13:34:22 2006 +0000
@@ -1,21 +1,29 @@
 <vlh>
 <plugins>
   <plugin index="1" id="vamp:vamp-aubio:aubiotempo"/>
-<!--  <plugin index="2" id="vamp:vamp-example-plugins:zerocrossing"/> -->
+  <plugin index="2" id="vamp:vamp-example-plugins:spectralcentroid"/>
 </plugins>
 <rules>
 <outputRule>
+  <condition pluginIndex="1" condition="GapGreaterThan" argument="0.51"/>
+  <condition pluginIndex="2" outputNumber="1" condition="GreaterThan" argument="500"/>
+  <action type="image" image="bright.jpg"/>
+  <action type="process" process="/bin/echo" arguments="Fast tempo / high spectral centroid"/>
+</outputRule>
+<outputRule>
   <condition pluginIndex="1" condition="GapLessThan" argument="0.49"/>
-  <action type="image" image="trees-green.jpg"/>
+  <condition pluginIndex="2" outputNumber="1" condition="LessThan" argument="500"/>
+  <action type="image" image="grey.jpg"/>
+  <action type="process" process="/bin/echo" arguments="Fast tempo / low spectral centroid"/>
 </outputRule>
 <outputRule>
   <condition pluginIndex="1" condition="GapGreaterThan" argument="0.51"/>
-  <action type="image" image="trees-mono.jpg"/>
+  <action type="image" image="gloomy.jpg"/>
+  <action type="process" process="/bin/echo" arguments="Slow tempo"/>
 </outputRule>
 <outputRule>
-  <condition pluginIndex="1" condition="GapGreaterThan" argument="0.51"/>
-  <action type="process" process="/bin/echo" arguments="blah"/>
+  <condition pluginIndex="1" condition="Present"/>
+  <action type="process" process="/bin/echo" arguments="Beat!"/>
 </outputRule>
-<!-- <outputRule pluginIndex="1" outputNumber="0" condition="Present" action="image" image="ping"/> -->
 </rules>
 </vlh>