diff main/MainWindow.cpp @ 631:2484381b53a1

Add "Export Audio Data" to export audio waveform data into CSV or similar
author Chris Cannam
date Wed, 09 Oct 2013 14:56:48 +0100
parents c0a20cd1a9ff
children f45af8d8091e 6f06094daba0
line wrap: on
line diff
--- a/main/MainWindow.cpp	Tue Oct 01 11:18:54 2013 +0100
+++ b/main/MainWindow.cpp	Wed Oct 09 14:56:48 2013 +0100
@@ -525,6 +525,12 @@
     connect(this, SIGNAL(canExportAudio(bool)), action, SLOT(setEnabled(bool)));
     menu->addAction(action);
 
+    action = new QAction(tr("&Export Audio Data..."), this);
+    action->setStatusTip(tr("Export audio from selection into a data file"));
+    connect(action, SIGNAL(triggered()), this, SLOT(exportAudioData()));
+    connect(this, SIGNAL(canExportAudio(bool)), action, SLOT(setEnabled(bool)));
+    menu->addAction(action);
+
     menu->addSeparator();
 
     action = new QAction(tr("Import Annotation &Layer..."), this);
@@ -2338,6 +2344,18 @@
 void
 MainWindow::exportAudio()
 {
+    exportAudio(false);
+}
+
+void
+MainWindow::exportAudioData()
+{
+    exportAudio(true);
+}
+
+void
+MainWindow::exportAudio(bool asData)
+{
     if (!getMainModel()) return;
 
     RangeSummarisableTimeValueModel *model = getMainModel();
@@ -2399,7 +2417,12 @@
         }
     }
 
-    QString path = getSaveFileName(FileFinder::AudioFile);
+    QString path;
+    if (asData) {
+        path = getSaveFileName(FileFinder::CSVFile);
+    } else {
+        path = getSaveFileName(FileFinder::AudioFile);
+    }
     if (path == "") return;
 
     bool ok = false;
@@ -2430,25 +2453,33 @@
 
     } else if (selections.size() > 1) {
 
-	QStringList items;
-	items << tr("Export the selected regions into a single audio file")
-	      << tr("Export the selected regions into separate files")
-	      << tr("Export the whole audio file");
-
-	QString item = ListInputDialog::getItem
-	    (this, tr("Select region to export"),
-	     tr("Multiple regions of the original audio file are selected.\nWhat do you want to export?"),
-	     items, 0, &ok);
+        bool multiple = false;
+
+        if (!asData) { // Multi-file export not supported for data
+
+            QStringList items;
+            items << tr("Export the selected regions into a single file")
+                  << tr("Export the selected regions into separate files")
+                  << tr("Export the whole file");
+
+            QString item = ListInputDialog::getItem
+                (this, tr("Select region to export"),
+                 tr("Multiple regions of the original audio file are selected.\nWhat do you want to export?"),
+                 items, 0, &ok);
 	    
-	if (!ok || item.isEmpty()) return;
-
-	if (item == items[0]) {
-
+            if (!ok || item.isEmpty()) return;
+            
+            if (item == items[0]) {
+                selectionToWrite = &ms;
+            } else if (item == items[1]) {
+                multiple = true;
+            }
+
+        } else { // asData
             selectionToWrite = &ms;
-
-        } else if (item == items[1]) {
-
-            multiple = true;
+        }
+
+        if (multiple) { // Can only happen when asData false
 
 	    int n = 1;
 	    QString base = path;
@@ -2484,13 +2515,26 @@
     }
 
     if (!multiple) {
-        WavFileWriter writer(path,
-                             model->getSampleRate(),
-                             model->getChannelCount(),
-                             WavFileWriter::WriteToTemporary);
-        writer.writeModel(model, selectionToWrite);
-	ok = writer.isOK();
-	error = writer.getError();
+        if (asData) {
+            CSVFileWriter writer(path, model,
+                                 ((QFileInfo(path).suffix() == "csv") ?
+                                  "," : "\t"));
+            if (selectionToWrite) {
+                writer.writeSelection(selectionToWrite);
+            } else {
+                writer.write();
+            }
+            ok = writer.isOK();
+            error = writer.getError();
+        } else {
+            WavFileWriter writer(path,
+                                 model->getSampleRate(),
+                                 model->getChannelCount(),
+                                 WavFileWriter::WriteToTemporary);
+            writer.writeModel(model, selectionToWrite);
+            ok = writer.isOK();
+            error = writer.getError();
+        }
     }
 
     if (ok) {