diff data/model/WaveFileModel.cpp @ 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
line wrap: on
line diff
--- 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);