changeset 724:cd1e8c731095 spectrogram-export

Permit exporting selected regions only
author Chris Cannam
date Wed, 08 Jan 2020 15:31:27 +0000
parents 2915cc467ebf
children 16f6737fa557
files framework/MainWindowBase.cpp framework/MainWindowBase.h
diffstat 2 files changed, 39 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/framework/MainWindowBase.cpp	Wed Jan 08 15:29:35 2020 +0000
+++ b/framework/MainWindowBase.cpp	Wed Jan 08 15:31:27 2020 +0000
@@ -62,6 +62,8 @@
 #include "rdf/RDFImporter.h"
 #include "rdf/RDFExporter.h"
 
+#include "transform/ModelTransformerFactory.h"
+
 #include "base/RecentFiles.h"
 
 #include "base/XmlExportable.h"
@@ -2804,8 +2806,14 @@
 
 bool
 MainWindowBase::exportLayerTo(Layer *layer, View *fromView,
+                              MultiSelection *selectionsToWrite,
                               QString path, QString &error)
 {
+    //!!! should we pull out the whole export logic into another
+    // class?  then we can more reasonably query it for things like
+    // "can we export this layer type to this file format? can we
+    // export selections, or only the whole layer?"
+    
     if (QFileInfo(path).suffix() == "") path += ".svl";
 
     QString suffix = QFileInfo(path).suffix().toLower();
@@ -2818,6 +2826,8 @@
 
     if (suffix == "xml" || suffix == "svl") {
 
+        //!!! +selection
+        
         QFile file(path);
         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
             error = tr("Failed to open file %1 for writing").arg(path);
@@ -2846,16 +2856,37 @@
 
         if (!nm) {
             error = tr("Can't export non-note layers to MIDI");
-        } else {
+        } else if (!selectionsToWrite) {
             MIDIFileWriter writer(path, nm.get(), nm->getSampleRate());
             writer.write();
             if (!writer.isOK()) {
                 error = writer.getError();
             }
+        } else {
+            NoteModel temporary(nm->getSampleRate(),
+                                nm->getResolution(),
+                                nm->getValueMinimum(),
+                                nm->getValueMaximum(),
+                                false);
+            temporary.setScaleUnits(nm->getScaleUnits());
+            for (const auto &s: selectionsToWrite->getSelections()) {
+                EventVector ev(nm->getEventsStartingWithin
+                               (s.getStartFrame(), s.getDuration()));
+                for (const auto &e: ev) {
+                    temporary.add(e);
+                }
+            }
+            MIDIFileWriter writer(path, &temporary, temporary.getSampleRate());
+            writer.write();
+            if (!writer.isOK()) {
+                error = writer.getError();
+            }
         }
 
     } else if (suffix == "ttl" || suffix == "n3") {
 
+        //!!! +selection
+
         if (!RDFExporter::canExportModel(model.get())) {
             error = tr("Sorry, cannot export this layer type to RDF (supported types are: region, note, text, time instants, time values)");
         } else {
@@ -2875,7 +2906,12 @@
             
         CSVFileWriter writer(path, model.get(), &dialog,
                              ((suffix == "csv") ? "," : "\t"));
-        writer.write();
+
+        if (selectionsToWrite) {
+            writer.writeSelection(*selectionsToWrite);
+        } else {
+            writer.write();
+        }
 
         if (!writer.isOK()) {
             error = writer.getError();
--- a/framework/MainWindowBase.h	Wed Jan 08 15:29:35 2020 +0000
+++ b/framework/MainWindowBase.h	Wed Jan 08 15:31:27 2020 +0000
@@ -166,6 +166,7 @@
     virtual bool saveSessionTemplate(QString path);
 
     virtual bool exportLayerTo(Layer *layer, View *fromView,
+                               MultiSelection *selectionsToWrite, // or null
                                QString toPath, QString &error);
 
     void cueOSCScript(QString filename);