changeset 1488:53fa8d57b728 import-audio-data

Add wave model as possible target for CSV import
author Chris Cannam
date Thu, 28 Jun 2018 14:49:46 +0100
parents 71202259002d
children 8d4f09552ba4
files data/fileio/CSVFileReader.cpp data/fileio/CSVFileReader.h data/fileio/CSVFormat.h
diffstat 3 files changed, 49 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/CSVFileReader.cpp	Mon Jun 25 14:12:27 2018 +0100
+++ b/data/fileio/CSVFileReader.cpp	Thu Jun 28 14:49:46 2018 +0100
@@ -23,6 +23,7 @@
 #include "model/EditableDenseThreeDimensionalModel.h"
 #include "model/RegionModel.h"
 #include "model/NoteModel.h"
+#include "model/WritableWaveFileModel.h"
 #include "DataFileReaderFactory.h"
 
 #include <QFile>
@@ -184,6 +185,7 @@
     RegionModel *model2a = 0;
     NoteModel *model2b = 0;
     EditableDenseThreeDimensionalModel *model3 = 0;
+    WritableWaveFileModel *modelW = 0;
     Model *model = 0;
 
     QTextStream in(m_device);
@@ -268,6 +270,12 @@
                          EditableDenseThreeDimensionalModel::NoCompression);
                     model = model3;
                     break;
+
+                case CSVFormat::WaveFileModel:
+                    modelW = new WritableWaveFileModel
+                        (sampleRate, valueColumns);
+                    model = modelW;
+                    break;
                 }
 
                 if (model) {
@@ -402,8 +410,42 @@
 //                          << frameNo << ", time " << RealTime::frame2RealTime(frameNo, sampleRate) << endl;
 
                 model3->setColumn(lineno, values);
+
+            } else if (modelType == CSVFormat::WaveFileModel) {
+
+                int channels = modelW->getChannelCount();
+                
+                float **samples =
+                    breakfastquay::allocate_and_zero_channels<float>
+                    (channels, 1);
+                
+                for (int i = 0; i < list.size() && i < channels; ++i) {
+
+                    if (m_format.getColumnPurpose(i) != CSVFormat::ColumnValue) {
+                        continue;
+                    }
+
+                    bool ok = false;
+                    float value = list[i].toFloat(&ok);
+                    
+                    samples[i][0] = value;
+                }
+
+                bool ok = modelW->addSamples(samples, 1);
+
+                breakfastquay::deallocate_channels(samples, channels);
+                
+                if (!ok) {
+                    if (warnings < warnLimit) {
+                        SVCERR << "WARNING: CSVFileReader::load: "
+                               << "Unable to add sample to wave-file model"
+                               << endl;
+                        SVCERR << line << endl;
+                        ++warnings;
+                    }
+                }
             }
-
+            
             ++lineno;
             if (timingType == CSVFormat::ImplicitTiming ||
                 list.size() == 0) {
--- a/data/fileio/CSVFileReader.h	Mon Jun 25 14:12:27 2018 +0100
+++ b/data/fileio/CSVFileReader.h	Thu Jun 28 14:49:46 2018 +0100
@@ -35,7 +35,8 @@
      * Construct a CSVFileReader to read the CSV file at the given
      * path, with the given format.
      */
-    CSVFileReader(QString path, CSVFormat format, sv_samplerate_t mainModelSampleRate);
+    CSVFileReader(QString path, CSVFormat format,
+                  sv_samplerate_t mainModelSampleRate);
 
     /**
      * Construct a CSVFileReader to read from the given
@@ -43,7 +44,8 @@
      * CSVFileReader will not close or delete it and it must outlive
      * the CSVFileReader.
      */
-    CSVFileReader(QIODevice *device, CSVFormat format, sv_samplerate_t mainModelSampleRate);
+    CSVFileReader(QIODevice *device, CSVFormat format,
+                  sv_samplerate_t mainModelSampleRate);
 
     virtual ~CSVFileReader();
 
--- a/data/fileio/CSVFormat.h	Mon Jun 25 14:12:27 2018 +0100
+++ b/data/fileio/CSVFormat.h	Thu Jun 28 14:49:46 2018 +0100
@@ -29,7 +29,8 @@
         TwoDimensionalModel,
         TwoDimensionalModelWithDuration,
         TwoDimensionalModelWithDurationAndPitch,
-        ThreeDimensionalModel
+        ThreeDimensionalModel,
+        WaveFileModel
     };
     
     enum TimingType {