diff runner/JAMSFeatureWriter.cpp @ 153:ad96fd5f9cd7 jams

Output some data!
author Chris Cannam
date Tue, 14 Oct 2014 14:03:34 +0100
parents db83ea0e102d
children 4b19d824a213
line wrap: on
line diff
--- a/runner/JAMSFeatureWriter.cpp	Tue Oct 14 12:35:19 2014 +0100
+++ b/runner/JAMSFeatureWriter.cpp	Tue Oct 14 14:03:34 2014 +0100
@@ -74,13 +74,19 @@
 JAMSFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata)
 {
     QString json
-	(" \"file_metadata\":\n"
+	("\n\"file_metadata\":\n"
 	 "  { \"artist\": \"%1\",\n"
 	 "    \"title\": \"%2\" },\n");
     m_metadata[trackId] = json.arg(metadata.maker).arg(metadata.title);
     cerr << "setTrackMetadata: metadata is: " << m_metadata[trackId] << endl;
 }
 
+static double
+realTime2Sec(const Vamp::RealTime &r)
+{
+    return r / Vamp::RealTime(1, 0);
+}
+
 void
 JAMSFeatureWriter::write(QString trackId,
 			 const Transform &transform,
@@ -106,17 +112,70 @@
         m_startedTargets.insert(targetKey);
     }
 
+    bool justBegun = false;
+    
     if (m_data.find(tt) == m_data.end()) {
 
 	identifyTask(transform);
 
         QString json("\"%1\": [ ");
         m_data[tt] = json.arg(getTaskKey(m_tasks[transformId]));
+        justBegun = true;
     }
 
+    QString d = m_data[tt];
+
     for (int i = 0; i < int(features.size()); ++i) {
+
+        if (i > 0 || !justBegun) {
+            d += ",\n";
+        } else {
+            d += "\n";
+        }
+        
+        d += "    { ";
 	
+        Plugin::Feature f(features[i]);
+
+        switch (m_tasks[transformId]) {
+
+        case ChordTask:
+        case SegmentTask:
+        case NoteTask:
+        case UnknownTask:
+            if (f.hasDuration) {
+                d += QString
+                    ("\"start\": { \"value\": %1 }, "
+                     "\"end\": { \"value\": %2 }")
+                    .arg(realTime2Sec(f.timestamp))
+                    .arg(realTime2Sec
+                         (f.timestamp +
+                          (f.hasDuration ? f.duration : Vamp::RealTime::zeroTime)));
+                break;
+            } else {
+                // don't break; fall through to simpler no-duration case
+            }
+            
+        case BeatTask:
+        case KeyTask:
+        case OnsetTask:
+            d += QString("\"time\": { \"value\": %1 }")
+                .arg(realTime2Sec(f.timestamp));
+            break;
+        }
+        
+        if (f.label != "") {
+            d += QString(", \"label\": { \"value\": \"%2\" }")
+                .arg(f.label.c_str());
+        } else if (f.values.size() > 0) {
+            d += QString(", \"label\": { \"value\": \"%2\" }")
+                .arg(f.values[0]);
+        }
+            
+        d += " }";
     }	
+
+    m_data[tt] = d;
 }
 
 void
@@ -142,7 +201,7 @@
         }
         startedStreams.insert(sptr);
         
-        *sptr << data << "]";
+        *sptr << data << "\n  ]";
     }
         
     for (FileStreamMap::const_iterator i = m_streams.begin();