]> git.sesse.net Git - kdenlive/commitdiff
Audio spectrum:
authorSimon A. Eugster <simon.eu@gmail.com>
Tue, 11 Jan 2011 08:02:48 +0000 (08:02 +0000)
committerSimon A. Eugster <simon.eu@gmail.com>
Tue, 11 Jan 2011 08:02:48 +0000 (08:02 +0000)
Fixed bug causing infinite loop when the widget was too small (negative size; loop until overflow).
http://kdenlive.org/mantis/view.php?id=1971

svn path=/trunk/kdenlive/; revision=5317

src/audioscopes/audiospectrum.cpp
src/audioscopes/spectrogram.cpp

index 2f4e1db8d1f19135ba85464db6b30f6f2184d73f..27f4c0a267f0fdddf881bbbfe9a0640e38bfddfa 100644 (file)
@@ -147,7 +147,10 @@ QImage AudioSpectrum::renderBackground(uint) { return QImage(); }
 QImage AudioSpectrum::renderAudioScope(uint, const QVector<int16_t> audioFrame, const int freq, const int num_channels,
                                        const int num_samples, const int)
 {
-    if (audioFrame.size() > 63) {
+    if (
+            audioFrame.size() > 63
+            && m_innerScopeRect.width() > 0 && m_innerScopeRect.height() > 0    // <= 0 if widget is too small (resized by user)
+    ) {
         if (!m_customFreq) {
             m_freqMax = freq / 2;
         }
@@ -273,137 +276,156 @@ QImage AudioSpectrum::renderHUD(uint)
 {
     QTime start = QTime::currentTime();
 
-    // Minimum distance between two lines
-    const uint minDistY = 30;
-    const uint minDistX = 40;
-    const uint textDistX = 10;
-    const uint textDistY = 25;
-    const uint topDist = m_innerScopeRect.top() - m_scopeRect.top();
-    const uint leftDist = m_innerScopeRect.left() - m_scopeRect.left();
-    const uint dbDiff = ceil((float)minDistY/m_innerScopeRect.height() * (m_dBmax-m_dBmin));
-    const int mouseX = m_mousePos.x() - m_innerScopeRect.left();
-    const int mouseY = m_mousePos.y() - m_innerScopeRect.top();
-
-    QImage hud(m_scopeRect.size(), QImage::Format_ARGB32);
-    hud.fill(qRgba(0,0,0,0));
-
-    QPainter davinci(&hud);
-    davinci.setPen(AbstractScopeWidget::penLight);
-
-    int y;
-    for (int db = -dbDiff; db > m_dBmin; db -= dbDiff) {
-        y = topDist + m_innerScopeRect.height() * ((float)db)/(m_dBmin - m_dBmax);
-        if (y-topDist > m_innerScopeRect.height()-minDistY+10) {
-            // Abort here, there is still a line left for min dB to paint which needs some room.
-            break;
-        }
-        davinci.drawLine(leftDist, y, leftDist + m_innerScopeRect.width()-1, y);
-        davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, y + 6, i18n("%1 dB", m_dBmax + db));
-    }
-    davinci.drawLine(leftDist, topDist, leftDist + m_innerScopeRect.width()-1, topDist);
-    davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, topDist+6, i18n("%1 dB", m_dBmax));
-    davinci.drawLine(leftDist, topDist+m_innerScopeRect.height()-1, leftDist + m_innerScopeRect.width()-1, topDist+m_innerScopeRect.height()-1);
-    davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, topDist+m_innerScopeRect.height()+6, i18n("%1 dB", m_dBmin));
-
-    const uint hzDiff = ceil( ((float)minDistX)/m_innerScopeRect.width() * m_freqMax / 1000 ) * 1000;
-    int x = 0;
-    const int rightBorder = leftDist + m_innerScopeRect.width()-1;
-    y = topDist + m_innerScopeRect.height() + textDistY;
-    for (int hz = 0; x <= rightBorder; hz += hzDiff) {
-        davinci.setPen(AbstractScopeWidget::penLighter);
-        x = leftDist + m_innerScopeRect.width() * ((float)hz)/m_freqMax;
-
-        if (x <= rightBorder) {
-            davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
-        }
-        if (hz < m_freqMax && x+textDistY < leftDist + m_innerScopeRect.width()) {
-            davinci.drawText(x-4, y, QVariant(hz/1000).toString());
-        } else {
-            x = leftDist + m_innerScopeRect.width();
-            davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
-            davinci.drawText(x-10, y, i18n("%1 kHz").arg((double)m_freqMax/1000, 0, 'f', 1));
+    if (m_innerScopeRect.height() > 0 && m_innerScopeRect.width() > 0) { // May be below 0 if widget is too small
+
+        // Minimum distance between two lines
+        const uint minDistY = 30;
+        const uint minDistX = 40;
+        const uint textDistX = 10;
+        const uint textDistY = 25;
+        const uint topDist = m_innerScopeRect.top() - m_scopeRect.top();
+        const uint leftDist = m_innerScopeRect.left() - m_scopeRect.left();
+        const uint dbDiff = ceil((float)minDistY/m_innerScopeRect.height() * (m_dBmax-m_dBmin));
+        const int mouseX = m_mousePos.x() - m_innerScopeRect.left();
+        const int mouseY = m_mousePos.y() - m_innerScopeRect.top();
+
+
+        QImage hud(m_scopeRect.size(), QImage::Format_ARGB32);
+        hud.fill(qRgba(0,0,0,0));
+
+        QPainter davinci(&hud);
+        davinci.setPen(AbstractScopeWidget::penLight);
+
+        int _dw = m_innerScopeRect.width();
+        int _dh = m_innerScopeRect.height();
+        int _dld = m_innerScopeRect.width() + leftDist + textDistX;
+        int _dtd = topDist+ m_innerScopeRect.height() + 6;
+        int _fw = AbstractScopeWidget::geometry().width();
+        int _fh = AbstractScopeWidget::geometry().height();
+
+        int y;
+        for (int db = -dbDiff; db > m_dBmin; db -= dbDiff) {
+            y = topDist + m_innerScopeRect.height() * ((float)db)/(m_dBmin - m_dBmax);
+            if (y-topDist > m_innerScopeRect.height()-minDistY+10) {
+                // Abort here, there is still a line left for min dB to paint which needs some room.
+                break;
+            }
+            davinci.drawLine(leftDist, y, leftDist + m_innerScopeRect.width()-1, y);
+            davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, y + 6, i18n("%1 dB", m_dBmax + db));
         }
+        davinci.drawLine(leftDist, topDist, leftDist + m_innerScopeRect.width()-1, topDist);
+        davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, topDist+6, i18n("%1 dB", m_dBmax));
+        davinci.drawLine(leftDist, topDist+m_innerScopeRect.height()-1, leftDist + m_innerScopeRect.width()-1, topDist+m_innerScopeRect.height()-1);
+        davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, topDist+m_innerScopeRect.height()+6, i18n("%1 dB", m_dBmin));
+
+        const uint hzDiff = ceil( ((float)minDistX)/m_innerScopeRect.width() * m_freqMax / 1000 ) * 1000;
+        int x = 0;
+        const int rightBorder = leftDist + m_innerScopeRect.width()-1;
+        y = topDist + m_innerScopeRect.height() + textDistY;
+        for (int hz = 0; x <= rightBorder; hz += hzDiff) {
+            davinci.setPen(AbstractScopeWidget::penLighter);
+            x = leftDist + m_innerScopeRect.width() * ((float)hz)/m_freqMax;
 
-        if (hz > 0) {
-            // Draw finer lines between the main lines
-            davinci.setPen(AbstractScopeWidget::penLightDots);
-            for (uint dHz = 3; dHz > 0; dHz--) {
-                x = leftDist + m_innerScopeRect.width() * ((float)hz - dHz * hzDiff/4.0f)/m_freqMax;
-                if (x > rightBorder) {
-                    break;
+            if (x <= rightBorder) {
+                davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
+            }
+            if (hz < m_freqMax && x+textDistY < leftDist + m_innerScopeRect.width()) {
+                davinci.drawText(x-4, y, QVariant(hz/1000).toString());
+            } else {
+                x = leftDist + m_innerScopeRect.width();
+                davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
+                davinci.drawText(x-10, y, i18n("%1 kHz").arg((double)m_freqMax/1000, 0, 'f', 1));
+            }
+
+            if (hz > 0) {
+                // Draw finer lines between the main lines
+                davinci.setPen(AbstractScopeWidget::penLightDots);
+                for (uint dHz = 3; dHz > 0; dHz--) {
+                    x = leftDist + m_innerScopeRect.width() * ((float)hz - dHz * hzDiff/4.0f)/m_freqMax;
+                    if (x > rightBorder) {
+                        break;
+                    }
+                    davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()-1);
                 }
-                davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()-1);
             }
         }
-    }
 
-    if (m_aTrackMouse->isChecked() && m_mouseWithinWidget && mouseX < m_innerScopeRect.width()-1) {
-        davinci.setPen(AbstractScopeWidget::penThin);
+        if (m_aTrackMouse->isChecked() && m_mouseWithinWidget && mouseX < m_innerScopeRect.width()-1) {
+            davinci.setPen(AbstractScopeWidget::penThin);
 
-        x = leftDist + mouseX;
+            x = leftDist + mouseX;
 
-        float db = 0;
-        float freq = ((float) mouseX)/(m_innerScopeRect.width()-1) * m_freqMax;
-        bool drawDb = false;
+            float db = 0;
+            float freq = ((float) mouseX)/(m_innerScopeRect.width()-1) * m_freqMax;
+            bool drawDb = false;
 
-        m_lastFFTLock.acquire();
-        // We need to test whether the mouse is inside the widget
-        // because the position could already have changed in the meantime (-> crash)
-        if (m_lastFFT.size() > 0 && mouseX >= 0 && mouseX < m_innerScopeRect.width()) {
-            uint right = ((float) m_freqMax)/(m_freq/2) * (m_lastFFT.size() - 1);
-            QVector<float> dbMap = FFTTools::interpolatePeakPreserving(m_lastFFT, m_innerScopeRect.width(), 0, right, -120);
-
-            db = dbMap[mouseX];
-            y = topDist + m_innerScopeRect.height()-1 - (dbMap[mouseX] - m_dBmin) / (m_dBmax-m_dBmin) * (m_innerScopeRect.height()-1);
-
-            if (y < (int)topDist + m_innerScopeRect.height()-1) {
-                drawDb = true;
-                davinci.drawLine(x, y, leftDist + m_innerScopeRect.width()-1, y);
+            m_lastFFTLock.acquire();
+            // We need to test whether the mouse is inside the widget
+            // because the position could already have changed in the meantime (-> crash)
+            if (m_lastFFT.size() > 0 && mouseX >= 0 && mouseX < m_innerScopeRect.width()) {
+                uint right = ((float) m_freqMax)/(m_freq/2) * (m_lastFFT.size() - 1);
+                QVector<float> dbMap = FFTTools::interpolatePeakPreserving(m_lastFFT, m_innerScopeRect.width(), 0, right, -120);
+
+                db = dbMap[mouseX];
+                y = topDist + m_innerScopeRect.height()-1 - (dbMap[mouseX] - m_dBmin) / (m_dBmax-m_dBmin) * (m_innerScopeRect.height()-1);
+
+                if (y < (int)topDist + m_innerScopeRect.height()-1) {
+                    drawDb = true;
+                    davinci.drawLine(x, y, leftDist + m_innerScopeRect.width()-1, y);
+                }
+            } else {
+                y = topDist + mouseY;
             }
-        } else {
-            y = topDist + mouseY;
-        }
-        m_lastFFTLock.release();
+            m_lastFFTLock.release();
 
-        if (y > (int)topDist + mouseY) {
-            y = topDist+ mouseY;
-        }
-        davinci.drawLine(x, y, x, topDist + m_innerScopeRect.height()-1);
-
-        if (drawDb) {
-            QPoint dist(20, -20);
-            QRect rect(
-                        leftDist + mouseX + dist.x(),
-                        topDist + mouseY + dist.y(),
-                        100,
-                        40
-                        );
-            if (rect.right() > (int)leftDist + m_innerScopeRect.width()-1) {
-                // Mirror the rectangle at the y axis to keep it inside the widget
-                rect = QRect(
-                            rect.topLeft() - QPoint(rect.width() + 2*dist.x(), 0),
-                            rect.size());
+            if (y > (int)topDist + mouseY) {
+                y = topDist+ mouseY;
             }
+            davinci.drawLine(x, y, x, topDist + m_innerScopeRect.height()-1);
+
+            if (drawDb) {
+                QPoint dist(20, -20);
+                QRect rect(
+                            leftDist + mouseX + dist.x(),
+                            topDist + mouseY + dist.y(),
+                            100,
+                            40
+                            );
+                if (rect.right() > (int)leftDist + m_innerScopeRect.width()-1) {
+                    // Mirror the rectangle at the y axis to keep it inside the widget
+                    rect = QRect(
+                                rect.topLeft() - QPoint(rect.width() + 2*dist.x(), 0),
+                                rect.size());
+                }
 
-            QRect textRect(
-                        rect.topLeft() + QPoint(12, 4),
-                        rect.size()
-                        );
+                QRect textRect(
+                            rect.topLeft() + QPoint(12, 4),
+                            rect.size()
+                            );
+
+                davinci.fillRect(rect, AbstractScopeWidget::penBackground.brush());
+                davinci.setPen(AbstractScopeWidget::penLighter);
+                davinci.drawRect(rect);
+                davinci.drawText(textRect, QString(
+                                     i18n("%1 dB", QString("%1").arg(db, 0, 'f', 2))
+                                     + "\n"
+                                     + i18n("%1 kHz", QString("%1").arg(freq/1000, 0, 'f', 2))));
+            }
 
-            davinci.fillRect(rect, AbstractScopeWidget::penBackground.brush());
-            davinci.setPen(AbstractScopeWidget::penLighter);
-            davinci.drawRect(rect);
-            davinci.drawText(textRect, QString(
-                                 i18n("%1 dB", QString("%1").arg(db, 0, 'f', 2))
-                                 + "\n"
-                                 + i18n("%1 kHz", QString("%1").arg(freq/1000, 0, 'f', 2))));
         }
 
-    }
+        emit signalHUDRenderingFinished(start.elapsed(), 1);
+        return hud;
 
+    } else {
+#ifdef DEBUG_AUDIOSPEC
+        qDebug() << "Widget is too small for painting inside. Size of inner scope rect is "
+                 << m_innerScopeRect.width() << "x" << m_innerScopeRect.height() <<".";
+#endif
+        emit signalHUDRenderingFinished(0, 1);
+        return QImage();
+    }
 
-    emit signalHUDRenderingFinished(start.elapsed(), 1);
-    return hud;
 }
 
 QRect AudioSpectrum::scopeRect()
index d7198f0a5f380bb1b51a59356514112d97cbafba..06a4c013bdd866ac10fced661b5727ce48a4763e 100644 (file)
@@ -160,148 +160,153 @@ QRect Spectrogram::scopeRect()
 
 QImage Spectrogram::renderHUD(uint)
 {
+    if (m_innerScopeRect.width() > 0 && m_innerScopeRect.height() > 0) {
+        QTime start = QTime::currentTime();
 
-    QTime start = QTime::currentTime();
+        int x, y;
+        const uint minDistY = 30; // Minimum distance between two lines
+        const uint minDistX = 40;
+        const uint textDistX = 10;
+        const uint textDistY = 25;
+        const uint topDist = m_innerScopeRect.top() - m_scopeRect.top();
+        const uint leftDist = m_innerScopeRect.left() - m_scopeRect.left();
+        const int mouseX = m_mousePos.x() - m_innerScopeRect.left();
+        const int mouseY = m_mousePos.y() - m_innerScopeRect.top();
+        bool hideText;
 
-    int x, y;
-    const uint minDistY = 30; // Minimum distance between two lines
-    const uint minDistX = 40;
-    const uint textDistX = 10;
-    const uint textDistY = 25;
-    const uint topDist = m_innerScopeRect.top() - m_scopeRect.top();
-    const uint leftDist = m_innerScopeRect.left() - m_scopeRect.left();
-    const int mouseX = m_mousePos.x() - m_innerScopeRect.left();
-    const int mouseY = m_mousePos.y() - m_innerScopeRect.top();
-    bool hideText;
+        QImage hud(m_scopeRect.size(), QImage::Format_ARGB32);
+        hud.fill(qRgba(0,0,0,0));
 
-    QImage hud(m_scopeRect.size(), QImage::Format_ARGB32);
-    hud.fill(qRgba(0,0,0,0));
+        QPainter davinci(&hud);
+        davinci.setPen(AbstractScopeWidget::penLight);
 
-    QPainter davinci(&hud);
-    davinci.setPen(AbstractScopeWidget::penLight);
 
+        // Frame display
+        if (m_aGrid->isChecked()) {
+            for (int frameNumber = 0; frameNumber < m_innerScopeRect.height(); frameNumber += minDistY) {
+                y = topDist + m_innerScopeRect.height()-1 - frameNumber;
+                hideText = m_aTrackMouse->isChecked() && m_mouseWithinWidget && abs(y - mouseY) < (int)textDistY && mouseY < m_innerScopeRect.height()
+                        && mouseX < m_innerScopeRect.width() && mouseX >= 0;
 
-    // Frame display
-    if (m_aGrid->isChecked()) {
-        for (int frameNumber = 0; frameNumber < m_innerScopeRect.height(); frameNumber += minDistY) {
-            y = topDist + m_innerScopeRect.height()-1 - frameNumber;
-            hideText = m_aTrackMouse->isChecked() && m_mouseWithinWidget && abs(y - mouseY) < (int)textDistY && mouseY < m_innerScopeRect.height()
-                    && mouseX < m_innerScopeRect.width() && mouseX >= 0;
-    
-            davinci.drawLine(leftDist, y, leftDist + m_innerScopeRect.width()-1, y);
-            if (!hideText) {
-                davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, y + 6, QVariant(frameNumber).toString());
+                davinci.drawLine(leftDist, y, leftDist + m_innerScopeRect.width()-1, y);
+                if (!hideText) {
+                    davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, y + 6, QVariant(frameNumber).toString());
+                }
             }
         }
-    }
-    // Draw a line through the mouse position with the correct Frame number
-    if (m_aTrackMouse->isChecked() && m_mouseWithinWidget && mouseY < m_innerScopeRect.height()
-            && mouseX < m_innerScopeRect.width() && mouseX >= 0) {
-        davinci.setPen(AbstractScopeWidget::penLighter);
-
-        x = leftDist + mouseX;
-        y = topDist + mouseY - 20;
-        if (y < 0) {
-            y = 0;
-        }
-        if (y > (int)topDist + m_innerScopeRect.height()-1 - 30) {
-            y = topDist + m_innerScopeRect.height()-1 - 30;
+        // Draw a line through the mouse position with the correct Frame number
+        if (m_aTrackMouse->isChecked() && m_mouseWithinWidget && mouseY < m_innerScopeRect.height()
+                && mouseX < m_innerScopeRect.width() && mouseX >= 0) {
+            davinci.setPen(AbstractScopeWidget::penLighter);
+
+            x = leftDist + mouseX;
+            y = topDist + mouseY - 20;
+            if (y < 0) {
+                y = 0;
+            }
+            if (y > (int)topDist + m_innerScopeRect.height()-1 - 30) {
+                y = topDist + m_innerScopeRect.height()-1 - 30;
+            }
+            davinci.drawLine(x, topDist + mouseY, leftDist + m_innerScopeRect.width()-1, topDist + mouseY);
+            davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX,
+                             y,
+                             m_scopeRect.right()-m_innerScopeRect.right()-textDistX,
+                             40,
+                             Qt::AlignLeft,
+                             i18n("Frame\n%1", m_innerScopeRect.height()-1-mouseY));
         }
-        davinci.drawLine(x, topDist + mouseY, leftDist + m_innerScopeRect.width()-1, topDist + mouseY);
-        davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX,
-                         y,
-                         m_scopeRect.right()-m_innerScopeRect.right()-textDistX,
-                         40,
-                         Qt::AlignLeft,
-                         i18n("Frame\n%1", m_innerScopeRect.height()-1-mouseY));
-    }
 
-    // Frequency grid
-    const uint hzDiff = ceil( ((float)minDistX)/m_innerScopeRect.width() * m_freqMax / 1000 ) * 1000;
-    const int rightBorder = leftDist + m_innerScopeRect.width()-1;
-    x = 0;
-    y = topDist + m_innerScopeRect.height() + textDistY;
-    if (m_aGrid->isChecked()) {
-        for (uint hz = 0; x <= rightBorder; hz += hzDiff) {
-            davinci.setPen(AbstractScopeWidget::penLight);
-            x = leftDist + (m_innerScopeRect.width()-1) * ((float)hz)/m_freqMax;
-
-            // Hide text if it would overlap with the text drawn at the mouse position
-            hideText = m_aTrackMouse->isChecked() && m_mouseWithinWidget && abs(x-(leftDist + mouseX + 20)) < (int) minDistX + 16
-                    && mouseX < m_innerScopeRect.width() && mouseX >= 0;
-
-            if (x <= rightBorder) {
-                davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
-            }
-            if (x+textDistY < leftDist + m_innerScopeRect.width()) {
-                // Only draw the text label if there is still enough room for the final one at the right.
-                if (!hideText) {
-                    davinci.drawText(x-4, y, QVariant(hz/1000).toString());
+        // Frequency grid
+        const uint hzDiff = ceil( ((float)minDistX)/m_innerScopeRect.width() * m_freqMax / 1000 ) * 1000;
+        const int rightBorder = leftDist + m_innerScopeRect.width()-1;
+        x = 0;
+        y = topDist + m_innerScopeRect.height() + textDistY;
+        if (m_aGrid->isChecked()) {
+            for (uint hz = 0; x <= rightBorder; hz += hzDiff) {
+                davinci.setPen(AbstractScopeWidget::penLight);
+                x = leftDist + (m_innerScopeRect.width()-1) * ((float)hz)/m_freqMax;
+
+                // Hide text if it would overlap with the text drawn at the mouse position
+                hideText = m_aTrackMouse->isChecked() && m_mouseWithinWidget && abs(x-(leftDist + mouseX + 20)) < (int) minDistX + 16
+                        && mouseX < m_innerScopeRect.width() && mouseX >= 0;
+
+                if (x <= rightBorder) {
+                    davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
+                }
+                if (x+textDistY < leftDist + m_innerScopeRect.width()) {
+                    // Only draw the text label if there is still enough room for the final one at the right.
+                    if (!hideText) {
+                        davinci.drawText(x-4, y, QVariant(hz/1000).toString());
+                    }
                 }
-            }
 
 
-            if (hz > 0) {
-                // Draw finer lines between the main lines
-                davinci.setPen(AbstractScopeWidget::penLightDots);
-                for (uint dHz = 3; dHz > 0; dHz--) {
-                    x = leftDist + m_innerScopeRect.width() * ((float)hz - dHz * hzDiff/4.0f)/m_freqMax;
-                    if (x > rightBorder) {
-                        break;
+                if (hz > 0) {
+                    // Draw finer lines between the main lines
+                    davinci.setPen(AbstractScopeWidget::penLightDots);
+                    for (uint dHz = 3; dHz > 0; dHz--) {
+                        x = leftDist + m_innerScopeRect.width() * ((float)hz - dHz * hzDiff/4.0f)/m_freqMax;
+                        if (x > rightBorder) {
+                            break;
+                        }
+                        davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()-1);
                     }
-                    davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()-1);
                 }
             }
+            // Draw the line at the very right (maximum frequency)
+            x = leftDist + m_innerScopeRect.width()-1;
+            hideText = m_aTrackMouse->isChecked() && m_mouseWithinWidget && abs(x-(leftDist + mouseX + 30)) < (int) minDistX
+                    && mouseX < m_innerScopeRect.width() && mouseX >= 0;
+            davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
+            if (!hideText) {
+                davinci.drawText(x-10, y, i18n("%1 kHz").arg((double)m_freqMax/1000, 0, 'f', 1));
+            }
         }
-        // Draw the line at the very right (maximum frequency)
-        x = leftDist + m_innerScopeRect.width()-1;
-        hideText = m_aTrackMouse->isChecked() && m_mouseWithinWidget && abs(x-(leftDist + mouseX + 30)) < (int) minDistX
-                && mouseX < m_innerScopeRect.width() && mouseX >= 0;
-        davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
-        if (!hideText) {
-            davinci.drawText(x-10, y, i18n("%1 kHz").arg((double)m_freqMax/1000, 0, 'f', 1));
-        }
-    }
 
-    // Draw a line through the mouse position with the correct frequency label
-    if (m_aTrackMouse->isChecked() && m_mouseWithinWidget && mouseX < m_innerScopeRect.width() && mouseX >= 0) {
-        davinci.setPen(AbstractScopeWidget::penThin);
-        x = leftDist + mouseX;
-        davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
-        davinci.drawText(x-10, y, i18n("%1 kHz")
-                         .arg((double)(m_mousePos.x()-m_innerScopeRect.left())/m_innerScopeRect.width() * m_freqMax/1000, 0, 'f', 2));
-    }
+        // Draw a line through the mouse position with the correct frequency label
+        if (m_aTrackMouse->isChecked() && m_mouseWithinWidget && mouseX < m_innerScopeRect.width() && mouseX >= 0) {
+            davinci.setPen(AbstractScopeWidget::penThin);
+            x = leftDist + mouseX;
+            davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
+            davinci.drawText(x-10, y, i18n("%1 kHz")
+                             .arg((double)(m_mousePos.x()-m_innerScopeRect.left())/m_innerScopeRect.width() * m_freqMax/1000, 0, 'f', 2));
+        }
 
-    // Draw the dB brightness scale
-    float val;
-    davinci.setPen(AbstractScopeWidget::penLighter);
-    for (y = topDist; y < (int)topDist + m_innerScopeRect.height(); y++) {
-        val = 1-((float)y-topDist)/(m_innerScopeRect.height()-1);
-        int col = qRgba(255, 255, 255, 255.0 * val);
-        for (x = leftDist-6; x >= (int)leftDist-13; x--) {
-            hud.setPixel(x, y, col);
+        // Draw the dB brightness scale
+        float val;
+        davinci.setPen(AbstractScopeWidget::penLighter);
+        for (y = topDist; y < (int)topDist + m_innerScopeRect.height(); y++) {
+            val = 1-((float)y-topDist)/(m_innerScopeRect.height()-1);
+            int col = qRgba(255, 255, 255, 255.0 * val);
+            for (x = leftDist-6; x >= (int)leftDist-13; x--) {
+                hud.setPixel(x, y, col);
+            }
         }
-    }
-    const int rectWidth = leftDist-m_scopeRect.left()-22;
-    const int rectHeight = 50;
-    davinci.setFont(QFont(QFont().defaultFamily(), 10));
-    davinci.drawText(m_scopeRect.left(), topDist, rectWidth, rectHeight, Qt::AlignRight, i18n("%1\ndB", m_dBmax));
-    davinci.drawText(m_scopeRect.left(), topDist + m_innerScopeRect.height()-20, rectWidth, rectHeight, Qt::AlignRight, i18n("%1\ndB", m_dBmin));
+        const int rectWidth = leftDist-m_scopeRect.left()-22;
+        const int rectHeight = 50;
+        davinci.setFont(QFont(QFont().defaultFamily(), 10));
+        davinci.drawText(m_scopeRect.left(), topDist, rectWidth, rectHeight, Qt::AlignRight, i18n("%1\ndB", m_dBmax));
+        davinci.drawText(m_scopeRect.left(), topDist + m_innerScopeRect.height()-20, rectWidth, rectHeight, Qt::AlignRight, i18n("%1\ndB", m_dBmin));
 
 
-    emit signalHUDRenderingFinished(start.elapsed(), 1);
-    return hud;
+        emit signalHUDRenderingFinished(start.elapsed(), 1);
+        return hud;
+    } else {
+        emit signalHUDRenderingFinished(0, 1);
+        return QImage();
+    }
 }
 QImage Spectrogram::renderAudioScope(uint, const QVector<int16_t> audioFrame, const int freq,
                                      const int num_channels, const int num_samples, const int newData) {
-    if (audioFrame.size() > 63) {
+    if (
+            audioFrame.size() > 63
+            && m_innerScopeRect.width() > 0 && m_innerScopeRect.height() > 0
+    ) {
         if (!m_customFreq) {
             m_freqMax = freq / 2;
         }
         bool newDataAvailable = newData > 0;
 
-        //TODO highlight data above certain limit (-6 dB)
-
 #ifdef DEBUG_SPECTROGRAM
         qDebug() << "New data for " << widgetName() << ": " << newDataAvailable << " (" << newData << " units)";
 #endif