changeset 187:89b05b679dc3

* Fixes to update logic in writable wave file model
author Chris Cannam
date Thu, 12 Oct 2006 16:10:49 +0000
parents 06ad01f3e553
children f86b74d1b143
files data/fileio/WavFileReader.cpp data/model/WaveFileModel.cpp data/model/WaveFileModel.h data/model/WritableWaveFileModel.cpp
diffstat 4 files changed, 61 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/WavFileReader.cpp	Thu Oct 12 14:56:28 2006 +0000
+++ b/data/fileio/WavFileReader.cpp	Thu Oct 12 16:10:49 2006 +0000
@@ -36,7 +36,7 @@
     m_fileInfo.frames = 0;
     m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo);
 
-    if (!m_file || m_fileInfo.channels <= 0) {
+    if (!m_file || (!fileUpdating && m_fileInfo.channels <= 0)) {
 	std::cerr << "WavFileReader::initialize: Failed to open file ("
 		  << sf_strerror(m_file) << ")" << std::endl;
 
@@ -50,9 +50,11 @@
 	return;
     }
 
-    m_frameCount = m_fileInfo.frames;
-    m_channelCount = m_fileInfo.channels;
-    m_sampleRate = m_fileInfo.samplerate;
+    if (m_fileInfo.channels > 0) {
+        m_frameCount = m_fileInfo.frames;
+        m_channelCount = m_fileInfo.channels;
+        m_sampleRate = m_fileInfo.samplerate;
+    }
 
     std::cerr << "WavFileReader: Frame count " << m_frameCount << ", channel count " << m_channelCount << ", sample rate " << m_sampleRate << std::endl;
 
@@ -83,6 +85,11 @@
 
     m_frameCount = m_fileInfo.frames;
 
+    if (m_channelCount == 0) {
+        m_channelCount = m_fileInfo.channels;
+        m_sampleRate = m_fileInfo.samplerate;
+    }
+
     if (m_frameCount != prevCount) emit frameCountChanged();
 }
 
--- a/data/model/WaveFileModel.cpp	Thu Oct 12 14:56:28 2006 +0000
+++ b/data/model/WaveFileModel.cpp	Thu Oct 12 16:10:49 2006 +0000
@@ -45,8 +45,6 @@
     m_exiting(false)
 {
     m_reader = AudioFileReaderFactory::createReader(path);
-    connect(m_reader, SIGNAL(frameCountChanged()),
-            this, SLOT(frameCountChanged()));
     setObjectName(QFileInfo(path).fileName());
     if (isOK()) fillCache();
 }
@@ -60,13 +58,8 @@
     m_exiting(false)
 {
     m_reader = reader;
-    connect(m_reader, SIGNAL(frameCountChanged()),
-            this, SLOT(frameCountChanged()));
     setObjectName(QFileInfo(path).fileName());
-    if (isOK()) {
-        std::cerr << "OK; filling cache" << std::endl;
-        fillCache();
-    }
+    fillCache();
 }
 
 WaveFileModel::~WaveFileModel()
@@ -389,23 +382,6 @@
 }   
 
 void
-WaveFileModel::frameCountChanged()
-{
-    m_mutex.lock();
-    if (m_updateTimer) {
-        std::cerr << "WaveFileModel::frameCountChanged: updating existing fill thread" << std::endl;
-        m_fillThread->frameCountChanged();
-        m_mutex.unlock();
-    } else {
-        std::cerr << "WaveFileModel::frameCountChanged: restarting [inefficient]" << std::endl;
-        m_cache[0].clear();
-        m_cache[1].clear();
-        m_mutex.unlock();
-        fillCache();
-    }
-}
-
-void
 WaveFileModel::fillTimerTimedOut()
 {
     if (m_fillThread) {
@@ -435,14 +411,6 @@
 }
 
 void
-WaveFileModel::RangeCacheFillThread::frameCountChanged()
-{
-    m_frameCount = m_model.getFrameCount();
-    std::cerr << "WaveFileModel::RangeCacheFillThread::frameCountChanged: now "
-              << m_frameCount << std::endl;
-}
-
-void
 WaveFileModel::RangeCacheFillThread::run()
 {
     size_t cacheBlockSize[2];
@@ -457,17 +425,26 @@
     if (!m_model.isOK()) return;
     
     size_t channels = m_model.getChannelCount();
+    bool updating = m_model.m_reader->isUpdating();
+
+    if (updating) {
+        while (channels == 0 && !m_model.m_exiting) {
+            std::cerr << "WaveFileModel::fill: Waiting for channels..." << std::endl;
+            sleep(1);
+            channels = m_model.getChannelCount();
+        }
+    }
 
     Range *range = new Range[2 * channels];
     size_t count[2];
     count[0] = count[1] = 0;
 
     bool first = true;
-    bool updating = m_model.m_reader->isUpdating();
 
     while (first || updating) {
 
         updating = m_model.m_reader->isUpdating();
+        m_frameCount = m_model.getFrameCount();
 
         std::cerr << "WaveFileModel::fill: frame = " << frame << ", count = " << m_frameCount << std::endl;
 
@@ -522,7 +499,9 @@
 
         first = false;
         if (m_model.m_exiting) break;
-        if (updating) sleep(1);
+        if (updating) {
+            sleep(1);
+        }
     }
 
     if (!m_model.m_exiting) {
--- a/data/model/WaveFileModel.h	Thu Oct 12 14:56:28 2006 +0000
+++ b/data/model/WaveFileModel.h	Thu Oct 12 16:10:49 2006 +0000
@@ -73,7 +73,6 @@
 
 protected slots:
     void fillTimerTimedOut();
-    void frameCountChanged();
     void cacheFilled();
     
 protected:
@@ -87,7 +86,6 @@
             m_frameCount(model.getFrameCount()) { }
     
 	size_t getFillExtent() const { return m_fillExtent; }
-        void frameCountChanged();
         virtual void run();
 
     protected:
--- a/data/model/WritableWaveFileModel.cpp	Thu Oct 12 14:56:28 2006 +0000
+++ b/data/model/WritableWaveFileModel.cpp	Thu Oct 12 16:10:49 2006 +0000
@@ -26,6 +26,8 @@
 #include <cassert>
 #include <iostream>
 
+//!!! This class needs completing.
+
 WritableWaveFileModel::WritableWaveFileModel(size_t sampleRate,
 					     size_t channels,
 					     QString path) :
@@ -54,6 +56,28 @@
         m_writer = 0;
         return;
     }
+
+    m_reader = new WavFileReader(m_writer->getPath(), true);
+    if (!m_reader->getError().isEmpty()) {
+        std::cerr << "WritableWaveFileModel: Error in creating wave file reader" << std::endl;
+        delete m_reader;
+        m_reader = 0;
+        return;
+    }
+    
+    m_model = new WaveFileModel(m_writer->getPath(), m_reader);
+    if (!m_model->isOK()) {
+        std::cerr << "WritableWaveFileModel: Error in creating wave file model" << std::endl;
+        delete m_model;
+        m_model = 0;
+        delete m_reader;
+        m_reader = 0;
+        return;
+    }
+
+    connect(m_model, SLOT(modelChanged()), this, SIGNAL(modelChanged()));
+    connect(m_model, SLOT(modelChanged(size_t, size_t)),
+            this, SIGNAL(modelChanged(size_t, size_t)));
 }
 
 WritableWaveFileModel::~WritableWaveFileModel()
@@ -75,29 +99,11 @@
 
     m_frameCount += count;
 
-    if (!m_model) {
+    static int updateCounter = 0;
 
-        m_reader = new WavFileReader(m_writer->getPath(), true);
-        if (!m_reader->getError().isEmpty()) {
-            std::cerr << "WritableWaveFileModel: Error in creating wave file reader" << std::endl;
-            delete m_reader;
-            m_reader = 0;
-            return false;
-        }
-
-        m_model = new WaveFileModel(m_writer->getPath(), m_reader);
-        if (!m_model->isOK()) {
-            std::cerr << "WritableWaveFileModel: Error in creating wave file model" << std::endl;
-            delete m_model;
-            m_model = 0;
-            delete m_reader;
-            m_reader = 0;
-            return false;
-        }
-    }
-    
-    static int updateCounter = 0;
-    if (++updateCounter == 100) {
+    if (m_reader && m_reader->getChannelCount() == 0) {
+        m_reader->updateFrameCount();
+    } else if (++updateCounter == 100) {
         if (m_reader) m_reader->updateFrameCount();
         updateCounter = 0;
     }
@@ -115,7 +121,7 @@
 bool
 WritableWaveFileModel::isOK() const
 {
-    bool ok = (m_model && m_model->isOK());
+    bool ok = (m_writer && m_writer->isOK());
     std::cerr << "WritableWaveFileModel::isOK(): ok = " << ok << std::endl;
     return ok;
 }
@@ -145,7 +151,7 @@
 WritableWaveFileModel::getValues(int channel, size_t start, size_t end,
                                  float *buffer) const
 {
-    if (!m_model) return 0;
+    if (!m_model || m_model->getChannelCount() == 0) return 0;
     return m_model->getValues(channel, start, end, buffer);
 }
 
@@ -153,7 +159,7 @@
 WritableWaveFileModel::getValues(int channel, size_t start, size_t end,
                                  double *buffer) const
 {
-    if (!m_model) return 0;
+    if (!m_model || m_model->getChannelCount() == 0) return 0;
 //    std::cerr << "WritableWaveFileModel::getValues(" << channel << ", "
 //              << start << ", " << end << "): calling model" << std::endl;
     return m_model->getValues(channel, start, end, buffer);
@@ -163,14 +169,14 @@
 WritableWaveFileModel::getRanges(size_t channel, size_t start, size_t end,
                                  size_t &blockSize) const
 {
-    if (!m_model) return RangeBlock();
+    if (!m_model || m_model->getChannelCount() == 0) return RangeBlock();
     return m_model->getRanges(channel, start, end, blockSize);
 }
 
 WritableWaveFileModel::Range
 WritableWaveFileModel::getRange(size_t channel, size_t start, size_t end) const
 {
-    if (!m_model) return Range();
+    if (!m_model || m_model->getChannelCount() == 0) return Range();
     return m_model->getRange(channel, start, end);
 }
 
@@ -179,6 +185,9 @@
                              QString indent,
                              QString extraAttributes) const
 {
+    //!!! need to indicate that some models are not saved but must be
+    //regenerated -- same goes for those very large dense 3d models
+
     assert(0); //!!!
 }