changeset 176:570794f6f6a7

* Some fixes to updating of writable wave file models
author Chris Cannam
date Tue, 03 Oct 2006 15:01:50 +0000
parents b0f4555b625e
children aff66ec5aea4
files data/fileio/AudioFileReader.h data/fileio/WavFileReader.cpp data/fileio/WavFileReader.h data/model/WaveFileModel.cpp data/model/WritableWaveFileModel.cpp plugin/LADSPAPluginInstance.cpp
diffstat 6 files changed, 89 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/AudioFileReader.h	Tue Oct 03 14:17:37 2006 +0000
+++ b/data/fileio/AudioFileReader.h	Tue Oct 03 15:01:50 2006 +0000
@@ -42,6 +42,8 @@
     virtual void getInterleavedFrames(size_t start, size_t count,
 				      SampleBlock &frames) const = 0;
 
+    virtual bool isUpdating() const { return false; }
+
 signals:
     void frameCountChanged();
     
--- a/data/fileio/WavFileReader.cpp	Tue Oct 03 14:17:37 2006 +0000
+++ b/data/fileio/WavFileReader.cpp	Tue Oct 03 15:01:50 2006 +0000
@@ -19,13 +19,14 @@
 
 #include <QMutexLocker>
 
-WavFileReader::WavFileReader(QString path) :
+WavFileReader::WavFileReader(QString path, bool fileUpdating) :
     m_file(0),
     m_path(path),
     m_buffer(0),
     m_bufsiz(0),
     m_lastStart(0),
-    m_lastCount(0)
+    m_lastCount(0),
+    m_updating(fileUpdating)
 {
     m_frameCount = 0;
     m_channelCount = 0;
@@ -80,7 +81,16 @@
 
     std::cerr << "WavFileReader::updateFrameCount: now " << m_fileInfo.frames << std::endl;
 
-    if (m_fileInfo.frames != prevCount) emit frameCountChanged();
+    m_frameCount = m_fileInfo.frames;
+
+    if (m_frameCount != prevCount) emit frameCountChanged();
+}
+
+void
+WavFileReader::updateDone()
+{
+    updateFrameCount();
+    m_updating = false;
 }
 
 void
--- a/data/fileio/WavFileReader.h	Tue Oct 03 14:17:37 2006 +0000
+++ b/data/fileio/WavFileReader.h	Tue Oct 03 15:01:50 2006 +0000
@@ -26,7 +26,7 @@
 class WavFileReader : public AudioFileReader
 {
 public:
-    WavFileReader(QString path);
+    WavFileReader(QString path, bool fileUpdating = false);
     virtual ~WavFileReader();
 
     virtual QString getError() const { return m_error; }
@@ -40,7 +40,10 @@
     
     static void getSupportedExtensions(std::set<QString> &extensions);
 
+    bool isUpdating() const { return m_updating; }
+
     void updateFrameCount();
+    void updateDone();
 
 protected:
     SF_INFO m_fileInfo;
@@ -54,6 +57,8 @@
     mutable size_t m_bufsiz;
     mutable size_t m_lastStart;
     mutable size_t m_lastCount;
+
+    bool m_updating;
 };
 
 #endif
--- a/data/model/WaveFileModel.cpp	Tue Oct 03 14:17:37 2006 +0000
+++ b/data/model/WaveFileModel.cpp	Tue Oct 03 15:01:50 2006 +0000
@@ -60,7 +60,10 @@
     connect(m_reader, SIGNAL(frameCountChanged()),
             this, SLOT(frameCountChanged()));
     setObjectName(QFileInfo(path).fileName());
-    if (isOK()) fillCache();
+    if (isOK()) {
+        std::cerr << "OK; filling cache" << std::endl;
+        fillCache();
+    }
 }
 
 WaveFileModel::~WaveFileModel()
@@ -431,6 +434,8 @@
 WaveFileModel::RangeCacheFillThread::frameCountChanged()
 {
     m_frameCount = m_model.getFrameCount();
+    std::cerr << "WaveFileModel::RangeCacheFillThread::frameCountChanged: now "
+              << m_frameCount << std::endl;
 }
 
 void
@@ -452,54 +457,67 @@
     Range *range = new Range[2 * channels];
     size_t count[2];
     count[0] = count[1] = 0;
-    
-    while (frame < m_frameCount) {
+
+    bool first = true;
+    bool updating = m_model.m_reader->isUpdating();
+
+    while (first || updating) {
+
+        updating = m_model.m_reader->isUpdating();
 
         std::cerr << "WaveFileModel::fill: frame = " << frame << ", count = " << m_frameCount << std::endl;
 
-	m_model.m_reader->getInterleavedFrames(frame, readBlockSize, block);
+        while (frame < m_frameCount) {
 
-        for (size_t i = 0; i < readBlockSize; ++i) {
+            if (updating && (frame + readBlockSize > m_frameCount)) break;
+
+            m_model.m_reader->getInterleavedFrames(frame, readBlockSize, block);
+
+            for (size_t i = 0; i < readBlockSize; ++i) {
 		
-	    for (size_t ch = 0; ch < size_t(channels); ++ch) {
+                for (size_t ch = 0; ch < size_t(channels); ++ch) {
 
-                size_t index = channels * i + ch;
-		if (index >= block.size()) continue;
-                float sample = block[index];
+                    size_t index = channels * i + ch;
+                    if (index >= block.size()) continue;
+                    float sample = block[index];
+                    
+                    for (size_t ct = 0; ct < 2; ++ct) {
+                        
+                        size_t rangeIndex = ch * 2 + ct;
+                        
+                        if (sample > range[rangeIndex].max || count[ct] == 0) {
+                            range[rangeIndex].max = sample;
+                        }
+                        if (sample < range[rangeIndex].min || count[ct] == 0) {
+                            range[rangeIndex].min = sample;
+                        }
+                        range[rangeIndex].absmean += fabsf(sample);
+                    }
+                }
                 
+                QMutexLocker locker(&m_model.m_mutex);
                 for (size_t ct = 0; ct < 2; ++ct) {
+                    if (++count[ct] == cacheBlockSize[ct]) {
+                        for (size_t ch = 0; ch < size_t(channels); ++ch) {
+                            size_t rangeIndex = ch * 2 + ct;
+                            range[rangeIndex].absmean /= count[ct];
+                            m_model.m_cache[ct].push_back(range[rangeIndex]);
+                            range[rangeIndex] = Range();
+                        }
+                        count[ct] = 0;
+                    }
+                }
                 
-                    size_t rangeIndex = ch * 2 + ct;
-                    
-                    if (sample > range[rangeIndex].max || count[ct] == 0) {
-                        range[rangeIndex].max = sample;
-                    }
-                    if (sample < range[rangeIndex].min || count[ct] == 0) {
-                        range[rangeIndex].min = sample;
-                    }
-                    range[rangeIndex].absmean += fabsf(sample);
-                }
-	    }
-            
-	    QMutexLocker locker(&m_model.m_mutex);
-            for (size_t ct = 0; ct < 2; ++ct) {
-                if (++count[ct] == cacheBlockSize[ct]) {
-                    for (size_t ch = 0; ch < size_t(channels); ++ch) {
-                        size_t rangeIndex = ch * 2 + ct;
-                        range[rangeIndex].absmean /= count[ct];
-                        m_model.m_cache[ct].push_back(range[rangeIndex]);
-                        range[rangeIndex] = Range();
-                    }
-                    count[ct] = 0;
-                }
+                ++frame;
             }
             
-            ++frame;
+            if (m_model.m_exiting) break;
+            
+            m_fillExtent = frame;
         }
 
-	if (m_model.m_exiting) break;
-
-	m_fillExtent = frame;
+        first = false;
+        if (updating) sleep(1);
     }
 
     QMutexLocker locker(&m_model.m_mutex);
--- a/data/model/WritableWaveFileModel.cpp	Tue Oct 03 14:17:37 2006 +0000
+++ b/data/model/WritableWaveFileModel.cpp	Tue Oct 03 15:01:50 2006 +0000
@@ -77,7 +77,7 @@
 
     if (!m_model) {
 
-        m_reader = new WavFileReader(m_writer->getPath());
+        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;
@@ -108,7 +108,8 @@
 void
 WritableWaveFileModel::sync()
 {
-    if (m_reader) m_reader->updateFrameCount();
+    //!!! use setCompletion instead
+    if (m_reader) m_reader->updateDone();
 }    
 
 bool
--- a/plugin/LADSPAPluginInstance.cpp	Tue Oct 03 14:17:37 2006 +0000
+++ b/plugin/LADSPAPluginInstance.cpp	Tue Oct 03 15:01:50 2006 +0000
@@ -52,8 +52,17 @@
 {
     init(idealChannelCount);
 
-    m_inputBuffers  = new sample_t*[m_instanceCount * m_audioPortsIn.size()];
-    m_outputBuffers = new sample_t*[m_instanceCount * m_audioPortsOut.size()];
+    if (m_audioPortsIn.size() == 0) {
+        m_inputBuffers = 0;
+    } else {
+        m_inputBuffers  = new sample_t*[m_instanceCount * m_audioPortsIn.size()];
+    }
+
+    if (m_audioPortsOut.size() == 0) {
+        m_outputBuffers = 0;
+    } else {
+        m_outputBuffers = new sample_t*[m_instanceCount * m_audioPortsOut.size()];
+    }
 
     for (size_t i = 0; i < m_instanceCount * m_audioPortsIn.size(); ++i) {
 	m_inputBuffers[i] = new sample_t[blockSize];