]> git.sesse.net Git - kdenlive/blobdiff - src/audioscopes/spectrogram.cpp
Spectrogram: Peaks (larger or equal as maximum frequency) can be highlighted
[kdenlive] / src / audioscopes / spectrogram.cpp
index 28ea17623f29c8231e301c91eb2a2e6f27ab2112..d7198f0a5f380bb1b51a59356514112d97cbafba 100644 (file)
@@ -44,11 +44,17 @@ Spectrogram::Spectrogram(QWidget *parent) :
     m_aResetHz = new QAction(i18n("Reset maximum frequency to sampling rate"), this);
     m_aGrid = new QAction(i18n("Draw grid"), this);
     m_aGrid->setCheckable(true);
+    m_aTrackMouse = new QAction(i18n("Track mouse"), this);
+    m_aTrackMouse->setCheckable(true);
+    m_aHighlightPeaks = new QAction(i18n("Highlight peaks"), this);
+    m_aHighlightPeaks->setCheckable(true);
 
 
     m_menu->addSeparator();
     m_menu->addAction(m_aResetHz);
+    m_menu->addAction(m_aTrackMouse);
     m_menu->addAction(m_aGrid);
+    m_menu->addAction(m_aHighlightPeaks);
     m_menu->removeAction(m_aRealtime);
 
 
@@ -80,6 +86,7 @@ Spectrogram::~Spectrogram()
     writeConfig();
 
     delete m_aResetHz;
+    delete m_aTrackMouse;
     delete m_aGrid;
 }
 
@@ -92,7 +99,9 @@ void Spectrogram::readConfig()
 
     ui->windowSize->setCurrentIndex(scopeConfig.readEntry("windowSize", 0));
     ui->windowFunction->setCurrentIndex(scopeConfig.readEntry("windowFunction", 0));
+    m_aTrackMouse->setChecked(scopeConfig.readEntry("trackMouse", true));
     m_aGrid->setChecked(scopeConfig.readEntry("drawGrid", true));
+    m_aHighlightPeaks->setChecked(scopeConfig.readEntry("highlightPeaks", true));
     m_dBmax = scopeConfig.readEntry("dBmax", 0);
     m_dBmin = scopeConfig.readEntry("dBmin", -70);
     m_freqMax = scopeConfig.readEntry("freqMax", 0);
@@ -111,7 +120,9 @@ void Spectrogram::writeConfig()
 
     scopeConfig.writeEntry("windowSize", ui->windowSize->currentIndex());
     scopeConfig.writeEntry("windowFunction", ui->windowFunction->currentIndex());
+    scopeConfig.writeEntry("trackMouse", m_aTrackMouse->isChecked());
     scopeConfig.writeEntry("drawGrid", m_aGrid->isChecked());
+    scopeConfig.writeEntry("highlightPeaks", m_aHighlightPeaks->isChecked());
     scopeConfig.writeEntry("dBmax", m_dBmax);
     scopeConfig.writeEntry("dBmin", m_dBmin);
 
@@ -137,7 +148,7 @@ QRect Spectrogram::scopeRect()
     );
     m_innerScopeRect = QRect(
             QPoint(
-                    m_scopeRect.left()+56,                  // Left
+                    m_scopeRect.left()+66,                  // Left
                     m_scopeRect.top()+6                     // Top
             ), QPoint(
                     ui->verticalSpacer->geometry().right()-70,
@@ -174,7 +185,8 @@ QImage Spectrogram::renderHUD(uint)
     if (m_aGrid->isChecked()) {
         for (int frameNumber = 0; frameNumber < m_innerScopeRect.height(); frameNumber += minDistY) {
             y = topDist + m_innerScopeRect.height()-1 - frameNumber;
-            hideText = m_mouseWithinWidget && abs(y - mouseY) < (int)textDistY && mouseY < m_innerScopeRect.height() && mouseX < m_innerScopeRect.width();
+            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) {
@@ -183,7 +195,8 @@ QImage Spectrogram::renderHUD(uint)
         }
     }
     // Draw a line through the mouse position with the correct Frame number
-    if (m_mouseWithinWidget && mouseY < m_innerScopeRect.height() && mouseX < m_innerScopeRect.width()) {
+    if (m_aTrackMouse->isChecked() && m_mouseWithinWidget && mouseY < m_innerScopeRect.height()
+            && mouseX < m_innerScopeRect.width() && mouseX >= 0) {
         davinci.setPen(AbstractScopeWidget::penLighter);
 
         x = leftDist + mouseX;
@@ -214,7 +227,8 @@ QImage Spectrogram::renderHUD(uint)
             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_mouseWithinWidget && abs(x-(leftDist + mouseX + 20)) < (int) minDistX + 16 && mouseX < m_innerScopeRect.width();
+            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);
@@ -241,7 +255,8 @@ QImage Spectrogram::renderHUD(uint)
         }
         // Draw the line at the very right (maximum frequency)
         x = leftDist + m_innerScopeRect.width()-1;
-        hideText = m_mouseWithinWidget && abs(x-(leftDist + mouseX + 30)) < (int) minDistX && mouseX < m_innerScopeRect.width();
+        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));
@@ -249,7 +264,7 @@ QImage Spectrogram::renderHUD(uint)
     }
 
     // Draw a line through the mouse position with the correct frequency label
-    if (m_mouseWithinWidget && mouseX < m_innerScopeRect.width()) {
+    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);
@@ -285,6 +300,8 @@ QImage Spectrogram::renderAudioScope(uint, const QVector<int16_t> audioFrame, co
         }
         bool newDataAvailable = newData > 0;
 
+        //TODO highlight data above certain limit (-6 dB)
+
 #ifdef DEBUG_SPECTROGRAM
         qDebug() << "New data for " << widgetName() << ": " << newDataAvailable << " (" << newData << " units)";
 #endif
@@ -332,16 +349,11 @@ QImage Spectrogram::renderAudioScope(uint, const QVector<int16_t> audioFrame, co
         QImage spectrum(m_scopeRect.size(), QImage::Format_ARGB32);
         spectrum.fill(qRgba(0,0,0,0));
         QPainter davinci(&spectrum);
-        const uint w = m_innerScopeRect.width();
         const uint h = m_innerScopeRect.height();
         const uint leftDist = m_innerScopeRect.left() - m_scopeRect.left();
         const uint topDist = m_innerScopeRect.top() - m_scopeRect.top();
-        float f;
-        float x;
-        float x_prev = 0;
         float val;
         uint windowSize;
-        uint xi;
         uint y;
         bool completeRedraw = true;
 
@@ -361,58 +373,34 @@ QImage Spectrogram::renderAudioScope(uint, const QVector<int16_t> audioFrame, co
         y = 0;
         if (newData || m_parameterChanged) {
             m_parameterChanged = false;
+            bool peak = false;
 
+            QVector<float> dbMap;
+            uint right;
             for (QList<QVector<float> >::iterator it = m_fftHistory.begin(); it != m_fftHistory.end(); it++) {
 
                 windowSize = (*it).size();
 
-                for (uint i = 0; i < w; i++) {
-
-                    // i:   Pixel coordinate
-                    // f:   Target frequency
-                    // x:   Frequency array index (float!) corresponding to the pixel
-                    // xi:  floor(x)
-                    // val: dB value at position x (Range: [-inf,0])
+                // Interpolate the frequency data to match the pixel coordinates
+                right = ((float) m_freqMax)/(m_freq/2) * (windowSize - 1);
+                dbMap = FFTTools::interpolatePeakPreserving((*it), m_innerScopeRect.width(), 0, right, -180);
 
-                    f = i/((float) w-1.0) * m_freqMax;
-                    x = 2*f/freq * (windowSize - 1);
-                    xi = (int) floor(x);
-
-                    if (x >= windowSize) {
-                        break;
-                    }
-
-                    // Use linear interpolation in order to get smoother display
-                    if (i == 0 || xi == windowSize-1) {
-                        // ... except if we are at the left or right border of the display or the spectrum
-                        val = (*it)[xi];
-                    } else {
+                for (int i = 0; i < dbMap.size(); i++) {
+                    val = dbMap[i];
+                    peak = val > m_dBmax;
 
-                        if ((*it)[xi] > (*it)[xi+1]
-                            && x_prev < xi) {
-                            // This is a hack to preserve peaks.
-                            // Consider f = {0, 100, 0}
-                            //          x = {0.5,  1.5}
-                            // Then x is 50 both times, and the 100 peak is lost.
-                            // Get it back here for the first x after the peak.
-                            val = (*it)[xi];
-                        } else {
-                            val =   (xi+1 - x) * (*it)[xi]
-                                  + (x - xi)   * (*it)[xi+1];
-                        }
-                    }
-
-                    // Normalize to [0 1], 1 corresponding to 0 dB and 0 to dbMin dB
+                    // Normalize dB value to [0 1], 1 corresponding to dbMax dB and 0 to dbMin dB
                     val = (val-m_dBmax)/(m_dBmax-m_dBmin) + 1;
                     if (val < 0) {
                         val = 0;
                     } else if (val > 1) {
                         val = 1;
                     }
-
-                    spectrum.setPixel(leftDist + i, topDist + h-1 - y, qRgba(255, 255, 255, val * 255));
-
-                    x_prev = x;
+                    if (!peak || !m_aHighlightPeaks->isChecked()) {
+                        spectrum.setPixel(leftDist + i, topDist + h-1 - y, qRgba(255, 255, 255, val * 255));
+                    } else {
+                        spectrum.setPixel(leftDist + i, topDist + h-1 - y, AbstractScopeWidget::colHighlightDark.rgba());
+                    }
                 }
 
                 y++;