changeset 357:b92513201610

* better progress reporting in FileSource * fix set-to-default for audio dials with mappers
author Chris Cannam
date Fri, 04 Jan 2008 17:08:10 +0000
parents ca3b91119482
children 9203b82a8c53
files data/data.pro data/fileio/CodedAudioFileReader.h data/fileio/FileSource.cpp data/fileio/FileSource.h data/fileio/MP3FileReader.cpp data/fileio/OggVorbisFileReader.cpp data/fileio/ProgressPrinter.cpp data/fileio/ProgressPrinter.h data/fileio/QuickTimeFileReader.cpp plugin/LADSPAPluginFactory.cpp
diffstat 10 files changed, 193 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/data/data.pro	Mon Dec 17 12:32:28 2007 +0000
+++ b/data/data.pro	Fri Jan 04 17:08:10 2008 +0000
@@ -38,6 +38,7 @@
            fileio/MP3FileReader.h \
            fileio/OggVorbisFileReader.h \
            fileio/PlaylistFileReader.h \
+           fileio/ProgressPrinter.h \
            fileio/QuickTimeFileReader.h \
            fileio/ResamplingWavFileReader.h \
            fileio/WavFileReader.h \
@@ -85,6 +86,7 @@
            fileio/MP3FileReader.cpp \
            fileio/OggVorbisFileReader.cpp \
            fileio/PlaylistFileReader.cpp \
+           fileio/ProgressPrinter.cpp \
            fileio/QuickTimeFileReader.cpp \
            fileio/ResamplingWavFileReader.cpp \
            fileio/WavFileReader.cpp \
--- a/data/fileio/CodedAudioFileReader.h	Mon Dec 17 12:32:28 2007 +0000
+++ b/data/fileio/CodedAudioFileReader.h	Fri Jan 04 17:08:10 2008 +0000
@@ -27,6 +27,8 @@
 
 class CodedAudioFileReader : public AudioFileReader
 {
+    Q_OBJECT
+
 public:
     virtual ~CodedAudioFileReader();
 
@@ -40,6 +42,9 @@
 
     virtual size_t getNativeRate() const { return m_fileRate; }
 
+signals:
+    void progress(int);
+
 protected:
     CodedAudioFileReader(CacheMode cacheMode, size_t targetRate);
 
--- a/data/fileio/FileSource.cpp	Mon Dec 17 12:32:28 2007 +0000
+++ b/data/fileio/FileSource.cpp	Fri Jan 04 17:08:10 2008 +0000
@@ -14,6 +14,8 @@
 */
 
 #include "FileSource.h"
+#include "ProgressPrinter.h"
+
 #include "base/TempDirectory.h"
 #include "base/Exceptions.h"
 
@@ -44,7 +46,7 @@
 QMutex
 FileSource::m_mapMutex;
 
-FileSource::FileSource(QString fileOrUrl, bool showProgress) :
+FileSource::FileSource(QString fileOrUrl, ShowProgressType progressType) :
     m_url(fileOrUrl),
     m_ftp(0),
     m_http(0),
@@ -54,6 +56,8 @@
     m_remote(isRemote(fileOrUrl)),
     m_done(false),
     m_leaveLocalFile(false),
+    m_progressType(progressType),
+    m_progressPrinter(0),
     m_progressDialog(0),
     m_progressShowTimer(this),
     m_refCounted(false)
@@ -68,7 +72,7 @@
         return;
     }
 
-    init(showProgress);
+    init();
 
     if (isRemote() &&
         (fileOrUrl.contains('%') ||
@@ -99,7 +103,7 @@
             m_ok = false;
             m_done = false;
             m_lastStatus = 0;
-            init(showProgress);
+            init();
         }
     }
 
@@ -109,7 +113,7 @@
     }
 }
 
-FileSource::FileSource(QUrl url, bool showProgress) :
+FileSource::FileSource(QUrl url, ShowProgressType progressType) :
     m_url(url),
     m_ftp(0),
     m_http(0),
@@ -119,6 +123,8 @@
     m_remote(isRemote(url.toString())),
     m_done(false),
     m_leaveLocalFile(false),
+    m_progressType(progressType),
+    m_progressPrinter(0),
     m_progressDialog(0),
     m_progressShowTimer(this),
     m_refCounted(false)
@@ -133,7 +139,7 @@
         return;
     }
 
-    init(showProgress);
+    init();
 }
 
 FileSource::FileSource(const FileSource &rf) :
@@ -147,6 +153,8 @@
     m_remote(rf.m_remote),
     m_done(false),
     m_leaveLocalFile(false),
+    m_progressType(rf.m_progressType),
+    m_progressPrinter(0),
     m_progressDialog(0),
     m_progressShowTimer(0),
     m_refCounted(false)
@@ -197,7 +205,7 @@
 }
 
 void
-FileSource::init(bool showProgress)
+FileSource::init()
 {
     if (!isRemote()) {
 #ifdef DEBUG_FILE_SOURCE
@@ -262,6 +270,7 @@
 
     if (scheme == "http") {
         initHttp();
+        std::cerr << "FileSource: initHttp succeeded" << std::endl;
     } else if (scheme == "ftp") {
         initFtp();
     } else {
@@ -295,14 +304,28 @@
         m_refCountMap[m_url]++;
         m_refCounted = true;
 
-        if (showProgress) {
-            m_progressDialog = new QProgressDialog(tr("Downloading %1...").arg(m_url.toString()), tr("Cancel"), 0, 100);
+        switch (m_progressType) {
+
+        case ProgressNone: break;
+
+        case ProgressDialog:
+            m_progressDialog = new QProgressDialog
+                (tr("Downloading %1...").arg(m_url.toString()),
+                 tr("Cancel"), 0, 100);
             m_progressDialog->hide();
             connect(&m_progressShowTimer, SIGNAL(timeout()),
                     this, SLOT(showProgressDialog()));
-            connect(m_progressDialog, SIGNAL(canceled()), this, SLOT(cancelled()));
+            connect(m_progressDialog, SIGNAL(canceled()),
+                    this, SLOT(cancelled()));
             m_progressShowTimer.setSingleShot(true);
             m_progressShowTimer.start(2000);
+            break;
+
+        case ProgressToConsole:
+            m_progressPrinter = new ProgressPrinter(tr("Downloading..."));
+            connect(this, SIGNAL(progress(int)),
+                    m_progressPrinter, SLOT(progress(int)));
+            break;
         }
     }
 }
@@ -420,6 +443,8 @@
     }
     delete m_progressDialog;
     m_progressDialog = 0;
+    delete m_progressPrinter;
+    m_progressPrinter = 0;
     delete m_localFile; // does not actually delete the file
     m_localFile = 0;
 }
@@ -469,6 +494,7 @@
 FileSource::waitForData()
 {
     while (m_ok && !m_done) {
+//        std::cerr << "FileSource::waitForData: calling QApplication::processEvents" << std::endl;
         QApplication::processEvents();
     }
 }
@@ -822,24 +848,3 @@
     return false;
 }
 
-FileSourceProgressPrinter::FileSourceProgressPrinter() :
-    m_lastProgress(0)
-{
-}
-
-FileSourceProgressPrinter::~FileSourceProgressPrinter()
-{
-    if (m_lastProgress > 0 && m_lastProgress != 100) {
-        std::cerr << "\r\n";
-    }
-}
-
-void
-FileSourceProgressPrinter::progress(int progress)
-{
-    if (progress == m_lastProgress) return;
-    if (progress == 100) std::cerr << "\r\n";
-    else std::cerr << "\r" << progress << "%";
-    m_lastProgress = progress;
-}
-
--- a/data/fileio/FileSource.h	Mon Dec 17 12:32:28 2007 +0000
+++ b/data/fileio/FileSource.h	Fri Jan 04 17:08:10 2008 +0000
@@ -28,6 +28,7 @@
 class QFile;
 class QProgressDialog;
 class QHttpResponseHeader;
+class ProgressPrinter;
 
 /**
  * FileSource is a class used to refer to the contents of a file that
@@ -62,19 +63,36 @@
 
 public:
 
+    enum ShowProgressType {
+        ProgressNone,
+        ProgressDialog,
+        ProgressToConsole
+    };
+
     /**
      * Construct a FileSource using the given local file path or URL.
-     * The URL may be raw or encoded.  If showProgress is true, a
-     * progress dialog will be shown for any network transfers.
+     * The URL may be raw or encoded.
+     *
+     * If progressType is ProgressDialog, a progress dialog will be
+     * shown for any network transfers; if it is ProgressToConsole, a
+     * progress indication will be sent to the console.
+     * Note that the progress() signal will also be emitted regularly
+     * during retrieval, even if progressType is ProgressNone.
      */
-    FileSource(QString fileOrUrl, bool showProgress = false);
+    FileSource(QString fileOrUrl,
+               ShowProgressType progressType = ProgressNone);
 
     /**
-     * Construct a FileSource using the given remote URL.  If
-     * showProgress is true, a progress dialog will be shown for any
-     * network transfers.
+     * Construct a FileSource using the given remote URL.
+     *
+     * If progressType is ProgressDialog, a progress dialog will be
+     * shown for any network transfers; if it is ProgressToConsole, a
+     * progress indication will be sent to the console.
+     * Note that the progress() signal also will be emitted regularly
+     * during retrieval, even if progressType is ProgressNone.
      */
-    FileSource(QUrl url, bool showProgress = false);
+    FileSource(QUrl url,
+               ShowProgressType progressType = ProgressNone);
 
     FileSource(const FileSource &);
 
@@ -212,6 +230,8 @@
     bool m_remote;
     bool m_done;
     bool m_leaveLocalFile;
+    ShowProgressType m_progressType;
+    ProgressPrinter *m_progressPrinter;
     QProgressDialog *m_progressDialog;
     QTimer m_progressShowTimer;
 
@@ -222,7 +242,7 @@
     static QMutex m_mapMutex;
     bool m_refCounted;
 
-    void init(bool showProgress);
+    void init();
     void initHttp();
     void initFtp();
 
@@ -237,19 +257,4 @@
     static int m_count;
 };
 
-class FileSourceProgressPrinter : public QObject
-{
-    Q_OBJECT
-
-public:
-    FileSourceProgressPrinter();
-    virtual ~FileSourceProgressPrinter();
-    
-public slots:
-    void progress(int);
-
-protected:
-    int m_lastProgress;
-};
-
 #endif
--- a/data/fileio/MP3FileReader.cpp	Mon Dec 17 12:32:28 2007 +0000
+++ b/data/fileio/MP3FileReader.cpp	Fri Jan 04 17:08:10 2008 +0000
@@ -17,6 +17,8 @@
 #ifdef HAVE_MAD
 
 #include "MP3FileReader.h"
+#include "ProgressPrinter.h"
+
 #include "system/System.h"
 
 #include <sys/types.h>
@@ -111,6 +113,9 @@
                 (QObject::tr("Decoding %1...").arg(QFileInfo(m_path).fileName()),
                  QObject::tr("Stop"), 0, 100);
             m_progress->hide();
+        } else {
+            ProgressPrinter *pp = new ProgressPrinter(tr("Decoding..."), this);
+            connect(this, SIGNAL(progress(int)), pp, SLOT(progress(int)));
         }
 
         if (!decode(m_filebuffer, m_fileSize)) {
@@ -343,7 +348,7 @@
         initialiseDecodeCache();
 
         if (m_cacheMode == CacheInTemporaryFile) {
-            m_completion = 1;
+//            m_completion = 1;
             std::cerr << "MP3FileReader::accept: channel count " << m_channelCount << ", file rate " << m_fileRate << ", about to start serialised section" << std::endl;
             startSerialised("MP3FileReader::Decode");
         }
@@ -354,18 +359,21 @@
         double duration = double(m_fileSize * 8) / bitrate;
         double elapsed = double(m_frameCount) / m_sampleRate;
         double percent = ((elapsed * 100.0) / duration);
-        int progress = int(percent);
-        if (progress < 1) progress = 1;
-        if (progress > 99) progress = 99;
-        m_completion = progress;
-        if (m_progress) {
-            if (progress > m_progress->value()) {
-                m_progress->setValue(progress);
-                m_progress->show();
-                m_progress->raise();
-                qApp->processEvents();
-                if (m_progress->wasCanceled()) {
-                    m_cancelled = true;
+        int p = int(percent);
+        if (p < 1) p = 1;
+        if (p > 99) p = 99;
+        if (m_completion != p || (m_progress && !m_progress->isVisible())) {
+            m_completion = p;
+            emit progress(m_completion);
+            if (m_progress) {
+                if (m_completion > m_progress->value()) {
+                    m_progress->setValue(m_completion);
+                    m_progress->show();
+                    m_progress->raise();
+                    qApp->processEvents();
+                    if (m_progress->wasCanceled()) {
+                        m_cancelled = true;
+                    }
                 }
             }
         }
--- a/data/fileio/OggVorbisFileReader.cpp	Mon Dec 17 12:32:28 2007 +0000
+++ b/data/fileio/OggVorbisFileReader.cpp	Fri Jan 04 17:08:10 2008 +0000
@@ -17,6 +17,8 @@
 #ifdef HAVE_FISHSOUND
 
 #include "OggVorbisFileReader.h"
+#include "ProgressPrinter.h"
+
 #include "base/Profiler.h"
 #include "system/System.h"
 
@@ -75,6 +77,9 @@
                 (QObject::tr("Decoding %1...").arg(QFileInfo(m_path).fileName()),
                  QObject::tr("Stop"), 0, 100);
             m_progress->hide();
+        } else {
+            ProgressPrinter *pp = new ProgressPrinter(tr("Decoding..."), this);
+            connect(this, SIGNAL(progress(int)), pp, SLOT(progress(int)));
         }
 
         while (oggz_read(m_oggz, 1024) > 0);
@@ -145,14 +150,15 @@
 
     // The number of bytes read by this function is smaller than
     // the file size because of the packet headers
-    int progress = lrint(double(reader->m_bytesRead) * 114 /
-                         double(reader->m_fileSize));
-    if (progress > 99) progress = 99;
-    reader->m_completion = progress;
-    
+    int p = lrint(double(reader->m_bytesRead) * 114 /
+                  double(reader->m_fileSize));
+    if (p > 99) p = 99;
+    reader->m_completion = p;
+    reader->progress(p);
+
     if (reader->m_fileSize > 0 && reader->m_progress) {
-	if (progress > reader->m_progress->value()) {
-	    reader->m_progress->setValue(progress);
+	if (p > reader->m_progress->value()) {
+	    reader->m_progress->setValue(p);
 	    reader->m_progress->show();
 	    reader->m_progress->raise();
 	    qApp->processEvents();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/fileio/ProgressPrinter.cpp	Fri Jan 04 17:08:10 2008 +0000
@@ -0,0 +1,48 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    Sonic Visualiser
+    An audio file viewer and annotation editor.
+    Centre for Digital Music, Queen Mary, University of London.
+    This file copyright 2007 QMUL.
+    
+    This program is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License as
+    published by the Free Software Foundation; either version 2 of the
+    License, or (at your option) any later version.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#include "ProgressPrinter.h"
+
+#include <iostream>
+
+ProgressPrinter::ProgressPrinter(QString prefix, QObject *parent) :
+    QObject(parent),
+    m_prefix(prefix),
+    m_lastProgress(0)
+{
+}
+
+ProgressPrinter::~ProgressPrinter()
+{
+    if (m_lastProgress > 0 && m_lastProgress != 100) {
+        std::cerr << "\r\n";
+    }
+    std::cerr << "(progress printer dtor)" << std::endl;
+}
+
+void
+ProgressPrinter::progress(int progress)
+{
+    if (progress == m_lastProgress) return;
+    if (progress == 100) std::cerr << "\r\n";
+    else {
+        std::cerr << "\r"
+                  << m_prefix.toStdString() 
+                  << (m_prefix == "" ? "" : " ")
+                  << progress << "%";
+    }
+    m_lastProgress = progress;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/fileio/ProgressPrinter.h	Fri Jan 04 17:08:10 2008 +0000
@@ -0,0 +1,38 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    Sonic Visualiser
+    An audio file viewer and annotation editor.
+    Centre for Digital Music, Queen Mary, University of London.
+    This file copyright 2007 QMUL.
+    
+    This program is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License as
+    published by the Free Software Foundation; either version 2 of the
+    License, or (at your option) any later version.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#ifndef _PROGRESS_PRINTER_H_
+#define _PROGRESS_PRINTER_H_
+
+#include <QObject>
+#include <QString>
+
+class ProgressPrinter : public QObject
+{
+    Q_OBJECT
+
+public:
+    ProgressPrinter(QString prefix = "", QObject *parent = 0);
+    virtual ~ProgressPrinter();
+    
+public slots:
+    void progress(int);
+
+protected:
+    QString m_prefix;
+    int m_lastProgress;
+};
+
+#endif
--- a/data/fileio/QuickTimeFileReader.cpp	Mon Dec 17 12:32:28 2007 +0000
+++ b/data/fileio/QuickTimeFileReader.cpp	Fri Jan 04 17:08:10 2008 +0000
@@ -236,6 +236,8 @@
                 break;
             }
 
+            //!!! progress?
+
 //    std::cerr << "Read " << framesRead << " frames (block size " << m_d->blockSize << ")" << std::endl;
 
             // QuickTime buffers are interleaved unless specified otherwise
--- a/plugin/LADSPAPluginFactory.cpp	Mon Dec 17 12:32:28 2007 +0000
+++ b/plugin/LADSPAPluginFactory.cpp	Fri Jan 04 17:08:10 2008 +0000
@@ -151,7 +151,6 @@
 		
     if (LADSPA_IS_HINT_BOUNDED_BELOW(d)) {
 	float lb = descriptor->PortRangeHints[port].LowerBound;
-        std::cerr << "LADSPAPluginFactory::getPortMinimum: bounded below at " << lb << std::endl;
 	minimum = lb;
     } else if (LADSPA_IS_HINT_BOUNDED_ABOVE(d)) {
 	float ub = descriptor->PortRangeHints[port].UpperBound;
@@ -225,7 +224,7 @@
         else logmax = log10f(maximum);
     }
 
-    std::cerr << "LADSPAPluginFactory::getPortDefault: hint = " << d << std::endl;
+//    std::cerr << "LADSPAPluginFactory::getPortDefault: hint = " << d << std::endl;
 
     if (!LADSPA_IS_HINT_HAS_DEFAULT(d)) {