diff layer/SpectrogramLayer.cpp @ 1211:7a19738b9762 3.0-integration

Recreate FFT model when something changes, not the next time it's requested (avoids extra work when only retrieving the model)
author Chris Cannam
date Thu, 05 Jan 2017 11:10:57 +0000
parents f7bb22999d2e
children a1ee3108d1d3
line wrap: on
line diff
--- a/layer/SpectrogramLayer.cpp	Wed Jan 04 16:03:12 2017 +0000
+++ b/layer/SpectrogramLayer.cpp	Thu Jan 05 11:10:57 2017 +0000
@@ -129,7 +129,9 @@
 SpectrogramLayer::~SpectrogramLayer()
 {
     invalidateRenderers();
-    invalidateFFTModel();
+
+    delete m_fftModel;
+    delete m_peakCache;
 }
 
 pair<ColourScaleType, double>
@@ -193,7 +195,8 @@
     if (model == m_model) return;
 
     m_model = model;
-    invalidateFFTModel();
+
+    recreateFFTModel();
 
     if (!m_model || !m_model->isOK()) return;
 
@@ -673,7 +676,7 @@
 
     invalidateRenderers();
     m_channel = ch;
-    invalidateFFTModel();
+    recreateFFTModel();
 
     emit layerParametersChanged();
 }
@@ -717,7 +720,7 @@
     
     m_windowSize = ws;
     
-    invalidateFFTModel();
+    recreateFFTModel();
 
     emit layerParametersChanged();
 }
@@ -737,7 +740,7 @@
     
     m_windowHopLevel = v;
     
-    invalidateFFTModel();
+    recreateFFTModel();
 
     emit layerParametersChanged();
 
@@ -759,7 +762,7 @@
     
     m_windowType = w;
 
-    invalidateFFTModel();
+    recreateFFTModel();
 
     emit layerParametersChanged();
 }
@@ -1325,34 +1328,34 @@
     return rv;
 }
 	
-FFTModel *
-SpectrogramLayer::getFFTModel() const
+void
+SpectrogramLayer::recreateFFTModel()
 {
-    if (!m_model) return 0;
-
-    int fftSize = getFFTSize();
-
-    //!!! it is now surely slower to do this on every getFFTModel()
-    //!!! request than it would be to recreate the model immediately
-    //!!! when something changes instead of just invalidating it
-    
-    if (m_fftModel &&
-        m_fftModel->getHeight() == fftSize / 2 + 1 &&
-        m_fftModel->getWindowIncrement() == getWindowIncrement()) {
-        return m_fftModel;
+#ifdef DEBUG_SPECTROGRAM
+    cerr << "SpectrogramLayer::recreateFFTModel called" << endl;
+#endif
+
+    if (!m_model || !m_model->isOK()) {
+        emit sliceableModelReplaced(m_fftModel, 0);
+        delete m_fftModel;
+        delete m_peakCache;
+        m_fftModel = 0;
+        m_peakCache = 0;
+        return;
     }
-    
-    delete m_peakCache;
-    m_peakCache = 0;
-
-    delete m_fftModel;
+
+    FFTModel *oldModel = m_fftModel;
+
     m_fftModel = new FFTModel(m_model,
                               m_channel,
                               m_windowType,
                               m_windowSize,
                               getWindowIncrement(),
-                              fftSize);
-
+                              getFFTSize());
+
+    delete m_peakCache;
+    m_peakCache = 0;
+    
     if (!m_fftModel->isOK()) {
         QMessageBox::critical
             (0, tr("FFT cache failed"),
@@ -1360,25 +1363,14 @@
                 "There may be insufficient memory or disc space to continue."));
         delete m_fftModel;
         m_fftModel = 0;
-        return 0;
+        return;
     }
-
-    ((SpectrogramLayer *)this)->sliceableModelReplaced(0, m_fftModel);
-
-    return m_fftModel;
-}
-
-Dense3DModelPeakCache *
-SpectrogramLayer::getPeakCache() const
-{
-    //!!! see comment in getFFTModel
     
-    if (!m_peakCache) {
-        FFTModel *f = getFFTModel();
-        if (!f) return 0;
-        m_peakCache = new Dense3DModelPeakCache(f, m_peakCacheDivisor);
-    }
-    return m_peakCache;
+    m_peakCache = new Dense3DModelPeakCache(m_fftModel, m_peakCacheDivisor);
+
+    emit sliceableModelReplaced(oldModel, m_fftModel);
+
+    delete oldModel;
 }
 
 const Model *
@@ -1388,22 +1380,6 @@
 }
 
 void
-SpectrogramLayer::invalidateFFTModel()
-{
-#ifdef DEBUG_SPECTROGRAM
-    cerr << "SpectrogramLayer::invalidateFFTModel called" << endl;
-#endif
-
-    emit sliceableModelReplaced(m_fftModel, 0);
-
-    delete m_fftModel;
-    delete m_peakCache;
-
-    m_fftModel = 0;
-    m_peakCache = 0;
-}
-
-void
 SpectrogramLayer::invalidateMagnitudes()
 {
 #ifdef DEBUG_SPECTROGRAM