changeset 546:95391b480e17

* Make use of peak cache in spectrogram
author Chris Cannam
date Wed, 04 Feb 2009 15:03:42 +0000
parents c603d9439b37
children 806e3c72b5df
files base/ResizeableBitset.h data/fileio/MatrixFile.cpp data/model/Dense3DModelPeakCache.cpp data/model/Dense3DModelPeakCache.h
diffstat 4 files changed, 45 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/base/ResizeableBitset.h	Wed Feb 04 13:33:50 2009 +0000
+++ b/base/ResizeableBitset.h	Wed Feb 04 15:03:42 2009 +0000
@@ -24,7 +24,7 @@
 public:
     ResizeableBitset() : m_bits(0) {
     }
-    ResizeableBitset(size_t size) : m_bits(new std::vector<uint8_t>) {
+    ResizeableBitset(size_t size) : m_bits(new std::vector<uint8_t>), m_size(size) {
         m_bits->assign((size >> 3) + 1, 0);
     }
     ResizeableBitset(const ResizeableBitset &b) {
@@ -46,6 +46,7 @@
             m_bits = new std::vector<uint8_t>;
         }
         m_bits->assign((bits >> 3) + 1, 0);
+        m_size = bits;
     }
     
     bool get(size_t column) const {
@@ -63,9 +64,14 @@
     void copy(size_t source, size_t dest) {
         get(source) ? set(dest) : reset(dest);
     }
+
+    size_t size() const {
+        return m_size;
+    }
     
 private:
     std::vector<uint8_t> *m_bits;
+    size_t m_size;
 };
 
 
--- a/data/fileio/MatrixFile.cpp	Wed Feb 04 13:33:50 2009 +0000
+++ b/data/fileio/MatrixFile.cpp	Wed Feb 04 15:03:42 2009 +0000
@@ -156,6 +156,7 @@
         if (::close(m_fd) < 0) {
             ::perror("MatrixFile::~MatrixFile: close failed");
         }
+        openCount --;
     }
 
     QMutexLocker locker(&m_createMutex);
@@ -176,7 +177,6 @@
         totalStorage -= (m_headerSize + (m_width * m_height * m_cellSize) + m_width);
     }
     totalCount --;
-    openCount --;
 
 #ifdef DEBUG_MATRIX_FILE
     std::cerr << "MatrixFile[" << m_fd << "]::~MatrixFile: " << std::endl;
--- a/data/model/Dense3DModelPeakCache.cpp	Wed Feb 04 13:33:50 2009 +0000
+++ b/data/model/Dense3DModelPeakCache.cpp	Wed Feb 04 15:03:42 2009 +0000
@@ -20,6 +20,8 @@
     m_source(source),
     m_resolution(columnsPerPeak)
 {
+    m_coverage.resize(1); // otherwise it is simply invalid
+
     m_cache = new EditableDenseThreeDimensionalModel
         (source->getSampleRate(),
          getResolution(),
@@ -29,7 +31,7 @@
 
     connect(source, SIGNAL(modelChanged()),
             this, SLOT(sourceModelChanged()));
-    connect(source, SIGNAL(modelAboutToBeDeleted()),
+    connect(source, SIGNAL(aboutToBeDeleted()),
             this, SLOT(sourceModelAboutToBeDeleted()));
 
 }
@@ -72,7 +74,14 @@
 Dense3DModelPeakCache::sourceModelChanged()
 {
     if (!m_source) return;
-    m_coverage.resize(getWidth());
+    if (m_coverage.size() > 0) {
+        // The last peak may have come from an incomplete read, which
+        // may since have been filled, so reset it
+        m_coverage.reset(m_coverage.size()-1);
+    }
+    if (getWidth() > m_coverage.size()) {
+        m_coverage.resize(getWidth()); // clears all bits, which is OK with us
+    }
 }
 
 void
@@ -84,12 +93,14 @@
 bool
 Dense3DModelPeakCache::haveColumn(size_t column) const
 {
-    return m_coverage.get(column);
+    return column < m_coverage.size() && m_coverage.get(column);
 }
 
 void
 Dense3DModelPeakCache::fillColumn(size_t column) const
 {
+    if (column >= m_coverage.size()) m_coverage.resize(column + 1);
+
     Column peak;
     for (int i = 0; i < m_resolution; ++i) {
         Column here = m_source->getColumn(column * m_resolution + i);
@@ -101,6 +112,7 @@
             }
         }
     }
+
     m_cache->setColumn(column, peak);
     m_coverage.set(column);
 }
--- a/data/model/Dense3DModelPeakCache.h	Wed Feb 04 13:33:50 2009 +0000
+++ b/data/model/Dense3DModelPeakCache.h	Wed Feb 04 15:03:42 2009 +0000
@@ -22,11 +22,33 @@
 
 class Dense3DModelPeakCache : public DenseThreeDimensionalModel
 {
+    Q_OBJECT
+
 public:
     Dense3DModelPeakCache(DenseThreeDimensionalModel *source,
                           size_t columnsPerPeak);
     ~Dense3DModelPeakCache();
 
+    virtual bool isOK() const {
+        return m_source && m_source->isOK(); 
+    }
+
+    virtual size_t getSampleRate() const {
+        return m_source->getSampleRate();
+    }
+
+    virtual size_t getStartFrame() const {
+        return m_source->getStartFrame();
+    }
+
+    virtual size_t getEndFrame() const {
+        return m_source->getEndFrame();
+    }
+
+    virtual Model *clone() const {
+        return new Dense3DModelPeakCache(m_source, m_resolution);
+    }
+    
     virtual size_t getResolution() const {
         return m_source->getResolution() * m_resolution;
     }