changeset 499:b71116d3c180

* Much more sensible pane layouts, etc., when loading RDF; also offer choice of adding to current session or starting new session if RDF references audio (needs some refinement)
author Chris Cannam
date Fri, 28 Nov 2008 15:45:20 +0000
parents fdf5930b7ccc
children 83eae5239db6
files data/fileio/MP3FileReader.cpp rdf/RDFImporter.cpp rdf/RDFImporter.h
diffstat 3 files changed, 88 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/MP3FileReader.cpp	Fri Nov 28 13:47:11 2008 +0000
+++ b/data/fileio/MP3FileReader.cpp	Fri Nov 28 15:45:20 2008 +0000
@@ -1,4 +1,3 @@
-
 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
 
 /*
@@ -139,6 +138,10 @@
         
         std::cerr << "MP3FileReader ctor: exiting with file rate = " << m_fileRate << std::endl;
     }
+
+    if (m_error != "") {
+        std::cerr << "MP3FileReader::MP3FileReader(\"" << m_path.toStdString() << "\"): ERROR: " << m_error.toStdString() << std::endl;
+    }
 }
 
 MP3FileReader::~MP3FileReader()
--- a/rdf/RDFImporter.cpp	Fri Nov 28 13:47:11 2008 +0000
+++ b/rdf/RDFImporter.cpp	Fri Nov 28 15:45:20 2008 +0000
@@ -31,6 +31,9 @@
 #include "data/model/EditableDenseThreeDimensionalModel.h"
 #include "data/model/NoteModel.h"
 #include "data/model/RegionModel.h"
+#include "data/model/WaveFileModel.h"
+
+#include "data/fileio/FileSource.h"
 
 using std::cerr;
 using std::endl;
@@ -46,16 +49,15 @@
     bool isOK();
     QString getErrorString() const;
 
-    QString getAudioAvailableUrl() const { return m_audioAvailableAt; }
-
     std::vector<Model *> getDataModels(ProgressReporter *);
 
 protected:
     QString m_uristring;
     QString m_errorString;
-    QString m_audioAvailableAt;
+    std::map<QString, Model *> m_audioModelMap;
     int m_sampleRate;
 
+    void getDataModelsAudio(std::vector<Model *> &, ProgressReporter *);
     void getDataModelsSparse(std::vector<Model *> &, ProgressReporter *);
     void getDataModelsDense(std::vector<Model *> &, ProgressReporter *);
 
@@ -65,7 +67,6 @@
                                    int &sampleRate, int &windowLength,
                                    int &hopSize, int &width, int &height);
 
-
     void fillModel(Model *, long, long, bool, std::vector<float> &, QString);
 };
 
@@ -104,12 +105,6 @@
     return m_d->getErrorString();
 }
 
-QString
-RDFImporter::getAudioAvailableUrl() const
-{
-    return m_d->getAudioAvailableUrl();
-}
-
 std::vector<Model *>
 RDFImporter::getDataModels(ProgressReporter *r)
 {
@@ -120,19 +115,6 @@
     m_uristring(uri),
     m_sampleRate(sampleRate)
 {
-    SimpleSPARQLQuery::Value value =
-        SimpleSPARQLQuery::singleResultQuery
-        (SimpleSPARQLQuery::QueryFromSingleSource,
-         QString
-         (" PREFIX mo: <http://purl.org/ontology/mo/> "
-          " SELECT ?url FROM <%1> "
-          " WHERE { ?signal a mo:Signal ; mo:available_as ?url } "
-             ).arg(m_uristring),
-         "url");
-
-    if (value.type == SimpleSPARQLQuery::URIValue) {
-        m_audioAvailableAt = value.value;
-    }
 }
 
 RDFImporterImpl::~RDFImporterImpl()
@@ -157,6 +139,8 @@
 {
     std::vector<Model *> models;
 
+    getDataModelsAudio(models, reporter);
+
     if (m_sampleRate == 0) {
         std::cerr << "RDFImporter::getDataModels: invalid sample rate" << std::endl;
         return models;
@@ -176,9 +160,56 @@
 }
 
 void
+RDFImporterImpl::getDataModelsAudio(std::vector<Model *> &models,
+                                    ProgressReporter *reporter)
+{
+    SimpleSPARQLQuery query = SimpleSPARQLQuery
+        (SimpleSPARQLQuery::QueryFromSingleSource,
+         QString
+         (
+             " PREFIX mo: <http://purl.org/ontology/mo/> "
+             " SELECT ?signal ?source FROM <%1> "
+             " WHERE { ?signal a mo:Signal ; mo:available_as ?source } "
+             )
+         .arg(m_uristring));
+
+    SimpleSPARQLQuery::ResultList results = query.execute();
+
+    for (int i = 0; i < results.size(); ++i) {
+
+        QString signal = results[i]["signal"].value;
+        QString source = results[i]["source"].value;
+
+        FileSource fs(source, reporter);
+        if (fs.isAvailable()) {
+            if (reporter) {
+                reporter->setMessage(RDFImporter::tr("Importing audio referenced in RDF..."));
+            }
+            fs.waitForData();
+            WaveFileModel *newModel = new WaveFileModel(fs, m_sampleRate);
+            if (newModel->isOK()) {
+                std::cerr << "Successfully created wave file model from source at \"" << source.toStdString() << "\"" << std::endl;
+                models.push_back(newModel);
+                m_audioModelMap[signal] = newModel;
+                if (m_sampleRate == 0) {
+                    m_sampleRate = newModel->getSampleRate();
+                }
+            } else {
+                std::cerr << "Failed to create wave file model from source at \"" << source.toStdString() << "\"" << std::endl;
+                delete newModel;
+            }
+        }
+    }
+}
+
+void
 RDFImporterImpl::getDataModelsDense(std::vector<Model *> &models,
                                     ProgressReporter *reporter)
 {
+    if (reporter) {
+        reporter->setMessage(RDFImporter::tr("Importing dense signal data from RDF..."));
+    }
+
     SimpleSPARQLQuery query = SimpleSPARQLQuery
         (SimpleSPARQLQuery::QueryFromSingleSource,
          QString
@@ -433,6 +464,10 @@
 RDFImporterImpl::getDataModelsSparse(std::vector<Model *> &models,
                                      ProgressReporter *reporter)
 {
+    if (reporter) {
+        reporter->setMessage(RDFImporter::tr("Importing event data from RDF..."));
+    }
+
     SimpleSPARQLQuery::QueryType s = SimpleSPARQLQuery::QueryFromSingleSource;
 
     // Our query is intended to retrieve every thing that has a time,
@@ -475,12 +510,11 @@
 
     QString queryString = prefixes + QString(
 
-        " SELECT ?signal_source ?timed_thing ?event_type ?value"
+        " SELECT ?signal ?timed_thing ?event_type ?value"
         " FROM <%1>"
 
         " WHERE {"
 
-        "   ?signal mo:available_as ?signal_source ."
         "   ?signal a mo:Signal ."
 
         "   ?signal mo:time ?interval ."
@@ -543,10 +577,7 @@
         return;
     }        
 
-
-
     /*
-
       This function is now only used for sparse data (for dense data
       we would be in getDataModelsDense instead).
 
@@ -566,8 +597,6 @@
       start, for each of the source+type keys, and then push each
       feature into the relevant model depending on what we find out
       about it.  Then return only non-empty models.
-
-      
     */
 
     // Map from signal source to event type to dimensionality to
@@ -577,7 +606,11 @@
 
     for (int i = 0; i < results.size(); ++i) {
 
-        QString source = results[i]["signal_source"].value;
+        if (i % 4 == 0) {
+            if (reporter) reporter->setProgress(i/4);
+        }
+
+        QString source = results[i]["signal"].value;
         QString type = results[i]["event_type"].value;
         QString thinguri = results[i]["timed_thing"].value;
         
@@ -688,6 +721,11 @@
                 }
             }
 
+            if (m_audioModelMap.find(source) != m_audioModelMap.end()) {
+                std::cerr << "source model for " << model << " is " << m_audioModelMap[source] << std::endl;
+                model->setSourceModel(m_audioModelMap[source]);
+            }
+
             QString titleQuery = QString
                 (
                     " PREFIX dc: <http://purl.org/dc/elements/1.1/> "
@@ -805,6 +843,18 @@
     bool haveAudio = false;
     bool haveAnnotations = false;
 
+    // This query is not expected to return any values, but if it
+    // executes successfully (leaving no error in the error string)
+    // then we know we have RDF
+    SimpleSPARQLQuery q(SimpleSPARQLQuery::QueryFromSingleSource,
+                        QString(" SELECT ?x FROM <%1> WHERE { ?x <y> <z> } ")
+                        .arg(url));
+    
+    SimpleSPARQLQuery::ResultList r = q.execute();
+    if (!q.isOK()) {
+        return NotRDF;
+    }
+
     SimpleSPARQLQuery::Value value =
         SimpleSPARQLQuery::singleResultQuery
         (SimpleSPARQLQuery::QueryFromSingleSource,
@@ -860,7 +910,7 @@
         if (haveAnnotations) {
             return Annotations;
         } else {
-            return OtherDocument;
+            return OtherRDFDocument;
         }
     }
 
--- a/rdf/RDFImporter.h	Fri Nov 28 13:47:11 2008 +0000
+++ b/rdf/RDFImporter.h	Fri Nov 28 15:45:20 2008 +0000
@@ -45,15 +45,14 @@
     bool isOK();
     QString getErrorString() const;
 
-    QString getAudioAvailableUrl() const;
-
     std::vector<Model *> getDataModels(ProgressReporter *reporter);
 
     enum RDFDocumentType {
         AudioRefAndAnnotations,
         Annotations,
         AudioRef,
-        OtherDocument
+        OtherRDFDocument,
+        NotRDF
     };
 
     static RDFDocumentType identifyDocumentType(QString url);