changeset 1491:1dc64d3d323c import-audio-data

Support progress reporter for data file loads
author Chris Cannam
date Fri, 29 Jun 2018 13:44:53 +0100
parents 9ab8e5ec4731
children 6989d6215640
files base/ProgressReporter.h data/fileio/CSVFileReader.cpp data/fileio/CSVFileReader.h data/fileio/DataFileReaderFactory.cpp data/fileio/DataFileReaderFactory.h data/fileio/MIDIFileReader.cpp data/fileio/MIDIFileReader.h
diffstat 7 files changed, 92 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/base/ProgressReporter.h	Thu Jun 28 15:52:30 2018 +0100
+++ b/base/ProgressReporter.h	Fri Jun 29 13:44:53 2018 +0100
@@ -13,8 +13,8 @@
     COPYING included with this distribution for more information.
 */
 
-#ifndef _PROGRESS_REPORTER_H_
-#define _PROGRESS_REPORTER_H_
+#ifndef SV_PROGRESS_REPORTER_H
+#define SV_PROGRESS_REPORTER_H
 
 #include <QObject>
 #include <QString>
--- a/data/fileio/CSVFileReader.cpp	Thu Jun 28 15:52:30 2018 +0100
+++ b/data/fileio/CSVFileReader.cpp	Fri Jun 29 13:44:53 2018 +0100
@@ -18,6 +18,7 @@
 #include "model/Model.h"
 #include "base/RealTime.h"
 #include "base/StringBits.h"
+#include "base/ProgressReporter.h"
 #include "model/SparseOneDimensionalModel.h"
 #include "model/SparseTimeValueModel.h"
 #include "model/EditableDenseThreeDimensionalModel.h"
@@ -40,12 +41,17 @@
 using namespace std;
 
 CSVFileReader::CSVFileReader(QString path, CSVFormat format,
-                             sv_samplerate_t mainModelSampleRate) :
+                             sv_samplerate_t mainModelSampleRate,
+                             ProgressReporter *reporter) :
     m_format(format),
     m_device(0),
     m_ownDevice(true),
     m_warnings(0),
-    m_mainModelSampleRate(mainModelSampleRate)
+    m_mainModelSampleRate(mainModelSampleRate),
+    m_fileSize(0),
+    m_readCount(0),
+    m_progress(0),
+    m_reporter(reporter)
 {
     QFile *file = new QFile(path);
     bool good = false;
@@ -61,19 +67,27 @@
     if (good) {
         m_device = file;
         m_filename = QFileInfo(path).fileName();
+        m_fileSize = file->size();
+        if (m_reporter) m_reporter->setDefinite(true);
     } else {
         delete file;
     }
 }
 
 CSVFileReader::CSVFileReader(QIODevice *device, CSVFormat format,
-                             sv_samplerate_t mainModelSampleRate) :
+                             sv_samplerate_t mainModelSampleRate,
+                             ProgressReporter *reporter) :
     m_format(format),
     m_device(device),
     m_ownDevice(false),
     m_warnings(0),
-    m_mainModelSampleRate(mainModelSampleRate)
+    m_mainModelSampleRate(mainModelSampleRate),
+    m_fileSize(0),
+    m_readCount(0),
+    m_progress(0),
+    m_reporter(reporter)
 {
+    if (m_reporter) m_reporter->setDefinite(false);
 }
 
 CSVFileReader::~CSVFileReader()
@@ -230,6 +244,25 @@
 
         QString chunk = in.readLine();
         QStringList lines = chunk.split('\r', QString::SkipEmptyParts);
+
+        m_readCount += chunk.size() + 1;
+
+        if (m_reporter) {
+            if (m_reporter->wasCancelled()) {
+                break;
+            }
+            int progress;
+            if (m_fileSize > 0) {
+                progress = int((double(m_readCount) / double(m_fileSize))
+                               * 100.0);
+            } else {
+                progress = int(m_readCount / 10000);
+            }
+            if (progress != m_progress) {
+                m_reporter->setProgress(progress);
+                m_progress = progress;
+            }
+        }
         
         for (int li = 0; li < lines.size(); ++li) {
 
--- a/data/fileio/CSVFileReader.h	Thu Jun 28 15:52:30 2018 +0100
+++ b/data/fileio/CSVFileReader.h	Fri Jun 29 13:44:53 2018 +0100
@@ -27,6 +27,7 @@
 #include <QIODevice>
 
 class QFile;
+class ProgressReporter;
 
 class CSVFileReader : public DataFileReader
 {
@@ -36,7 +37,8 @@
      * path, with the given format.
      */
     CSVFileReader(QString path, CSVFormat format,
-                  sv_samplerate_t mainModelSampleRate);
+                  sv_samplerate_t mainModelSampleRate,
+                  ProgressReporter *reporter = 0);
 
     /**
      * Construct a CSVFileReader to read from the given
@@ -45,7 +47,8 @@
      * the CSVFileReader.
      */
     CSVFileReader(QIODevice *device, CSVFormat format,
-                  sv_samplerate_t mainModelSampleRate);
+                  sv_samplerate_t mainModelSampleRate,
+                  ProgressReporter *reporter = 0);
 
     virtual ~CSVFileReader();
 
@@ -62,6 +65,10 @@
     QString m_error;
     mutable int m_warnings;
     sv_samplerate_t m_mainModelSampleRate;
+    qint64 m_fileSize;
+    mutable qint64 m_readCount;
+    mutable int m_progress;
+    ProgressReporter *m_reporter;
 
     sv_frame_t convertTimeValue(QString, int lineno, sv_samplerate_t sampleRate,
                                 int windowSize) const;
--- a/data/fileio/DataFileReaderFactory.cpp	Thu Jun 28 15:52:30 2018 +0100
+++ b/data/fileio/DataFileReaderFactory.cpp	Fri Jun 29 13:44:53 2018 +0100
@@ -32,21 +32,28 @@
                                     bool csv,
                                     MIDIFileImportPreferenceAcquirer *acquirer,
                                     CSVFormat format,
-                                    sv_samplerate_t mainModelSampleRate)
+                                    sv_samplerate_t mainModelSampleRate,
+                                    ProgressReporter *reporter)
 {
     QString err;
 
     DataFileReader *reader = 0;
 
     if (!csv) {
-        reader = new MIDIFileReader(path, acquirer, mainModelSampleRate);
+        reader = new MIDIFileReader(path,
+                                    acquirer,
+                                    mainModelSampleRate,
+                                    reporter);
         if (reader->isOK()) return reader;
         if (reader->getError() != "") err = reader->getError();
         delete reader;
     }
 
     if (csv) {
-        reader = new CSVFileReader(path, format, mainModelSampleRate);
+        reader = new CSVFileReader(path,
+                                   format,
+                                   mainModelSampleRate,
+                                   reporter);
         if (reader->isOK()) return reader;
         if (reader->getError() != "") err = reader->getError();
         delete reader;
@@ -58,14 +65,15 @@
 DataFileReader *
 DataFileReaderFactory::createReader(QString path,
                                     MIDIFileImportPreferenceAcquirer *acquirer,
-                                    sv_samplerate_t mainModelSampleRate)
+                                    sv_samplerate_t mainModelSampleRate,
+                                    ProgressReporter *reporter)
 {
     DataFileReader *reader = createReader
-        (path, false, acquirer, CSVFormat(), mainModelSampleRate);
+        (path, false, acquirer, CSVFormat(), mainModelSampleRate, reporter);
     if (reader) return reader;
 
     reader = createReader
-        (path, true, acquirer, CSVFormat(path), mainModelSampleRate);
+        (path, true, acquirer, CSVFormat(path), mainModelSampleRate, reporter);
     if (reader) return reader;
 
     return 0;
@@ -74,11 +82,13 @@
 Model *
 DataFileReaderFactory::load(QString path,
                             MIDIFileImportPreferenceAcquirer *acquirer,
-                            sv_samplerate_t mainModelSampleRate)
+                            sv_samplerate_t mainModelSampleRate,
+                            ProgressReporter *reporter)
 {
     DataFileReader *reader = createReader(path,
                                           acquirer,
-                                          mainModelSampleRate);
+                                          mainModelSampleRate,
+                                          reporter);
     if (!reader) return NULL;
 
     try {
@@ -94,12 +104,14 @@
 Model *
 DataFileReaderFactory::loadNonCSV(QString path,
                                   MIDIFileImportPreferenceAcquirer *acquirer,
-                                  sv_samplerate_t mainModelSampleRate)
+                                  sv_samplerate_t mainModelSampleRate,
+                                  ProgressReporter *reporter)
 {
     DataFileReader *reader = createReader(path, false,
                                           acquirer,
                                           CSVFormat(),
-                                          mainModelSampleRate);
+                                          mainModelSampleRate,
+                                          reporter);
     if (!reader) return NULL;
 
     try {
@@ -114,10 +126,12 @@
 
 Model *
 DataFileReaderFactory::loadCSV(QString path, CSVFormat format,
-                               sv_samplerate_t mainModelSampleRate)
+                               sv_samplerate_t mainModelSampleRate,
+                               ProgressReporter *reporter)
 {
     DataFileReader *reader = createReader(path, true, 0, format,
-                                          mainModelSampleRate);
+                                          mainModelSampleRate,
+                                          reporter);
     if (!reader) return NULL;
 
     try {
--- a/data/fileio/DataFileReaderFactory.h	Thu Jun 28 15:52:30 2018 +0100
+++ b/data/fileio/DataFileReaderFactory.h	Fri Jun 29 13:44:53 2018 +0100
@@ -13,8 +13,8 @@
     COPYING included with this distribution for more information.
 */
 
-#ifndef _DATA_FILE_READER_FACTORY_H_
-#define _DATA_FILE_READER_FACTORY_H_
+#ifndef SV_DATA_FILE_READER_FACTORY_H
+#define SV_DATA_FILE_READER_FACTORY_H
 
 #include <QString>
 
@@ -23,6 +23,7 @@
 
 class DataFileReader;
 class Model;
+class ProgressReporter;
 
 class DataFileReaderFactory
 {
@@ -48,7 +49,8 @@
      */
     static DataFileReader *createReader(QString path,
                                         MIDIFileImportPreferenceAcquirer *,
-                                        sv_samplerate_t mainModelSampleRate);
+                                        sv_samplerate_t mainModelSampleRate,
+                                        ProgressReporter *reporter = 0);
 
     /**
      * Read the given path, if a suitable reader is available.
@@ -60,7 +62,8 @@
      */
     static Model *load(QString path,
                        MIDIFileImportPreferenceAcquirer *acquirer,
-                       sv_samplerate_t mainModelSampleRate);
+                       sv_samplerate_t mainModelSampleRate,
+                       ProgressReporter *reporter = 0);
 
     /**
      * Read the given path, if a suitable reader is available.
@@ -69,7 +72,8 @@
      */
     static Model *loadNonCSV(QString path,
                              MIDIFileImportPreferenceAcquirer *acquirer,
-                             sv_samplerate_t mainModelSampleRate);
+                             sv_samplerate_t mainModelSampleRate,
+                             ProgressReporter *reporter = 0);
 
     /**
      * Read the given path using the CSV reader with the given format.
@@ -77,13 +81,15 @@
      */
     static Model *loadCSV(QString path,
                           CSVFormat format,
-                          sv_samplerate_t mainModelSampleRate);
+                          sv_samplerate_t mainModelSampleRate,
+                          ProgressReporter *reporter = 0);
 
 protected:
     static DataFileReader *createReader(QString path, bool csv,
                                         MIDIFileImportPreferenceAcquirer *,
                                         CSVFormat format,
-                                        sv_samplerate_t mainModelSampleRate);
+                                        sv_samplerate_t mainModelSampleRate,
+                                        ProgressReporter *reporter = 0);
 };
 
 #endif
--- a/data/fileio/MIDIFileReader.cpp	Thu Jun 28 15:52:30 2018 +0100
+++ b/data/fileio/MIDIFileReader.cpp	Fri Jun 29 13:44:53 2018 +0100
@@ -58,7 +58,8 @@
 
 MIDIFileReader::MIDIFileReader(QString path,
                                MIDIFileImportPreferenceAcquirer *acquirer,
-                               sv_samplerate_t mainModelSampleRate) :
+                               sv_samplerate_t mainModelSampleRate,
+                               ProgressReporter *) : // we don't actually report progress
     m_smpte(false),
     m_timingDivision(0),
     m_fps(0),
--- a/data/fileio/MIDIFileReader.h	Thu Jun 28 15:52:30 2018 +0100
+++ b/data/fileio/MIDIFileReader.h	Fri Jun 29 13:44:53 2018 +0100
@@ -31,6 +31,7 @@
 #include <QObject>
 
 class MIDIEvent;
+class ProgressReporter;
 
 typedef unsigned char MIDIByte;
 
@@ -61,7 +62,8 @@
 public:
     MIDIFileReader(QString path,
                    MIDIFileImportPreferenceAcquirer *pref, // may be null
-                   sv_samplerate_t mainModelSampleRate);
+                   sv_samplerate_t mainModelSampleRate,
+                   ProgressReporter *reporter = 0);
     virtual ~MIDIFileReader();
 
     virtual bool isOK() const;