File indexing completed on 2024-05-12 08:54:29

0001 /*
0002     SPDX-FileCopyrightText: 2010 Simon Andreas Eugster <simon.eu@gmail.com>
0003     This file is part of kdenlive. See www.kdenlive.org.
0004 
0005 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #pragma once
0009 
0010 #include "abstractaudioscopewidget.h"
0011 #include "lib/audio/fftTools.h"
0012 #include "ui_spectrogram_ui.h"
0013 
0014 class Spectrogram_UI;
0015 
0016 /** @class Spectrogram 
0017     @brief This Spectrogram shows the spectral power distribution of incoming audio samples
0018     over time. See https://en.wikipedia.org/wiki/Spectrogram.
0019 
0020     The Spectrogram makes use of two caches:
0021     * A cached image where only the most recent line needs to be appended instead of
0022       having to recalculate the whole image. A typical speedup factor is 10x.
0023     * A FFT cache storing a history of previous spectral power distributions (i.e.
0024       the Fourier-transformed audio signals). This is used if the user adjusts parameters
0025       like the maximum frequency to display or minimum/maximum signal strength in dB.
0026       All required information is preserved in the FFT history, which would not be the
0027       case for an image (consider re-sizing the widget to 100x100 px and then back to
0028       800x400 px -- lost is lost).
0029 */
0030 class Spectrogram : public AbstractAudioScopeWidget
0031 {
0032     Q_OBJECT
0033 
0034 public:
0035     explicit Spectrogram(QWidget *parent = nullptr);
0036     ~Spectrogram() override;
0037 
0038     QString widgetName() const override;
0039 
0040 protected:
0041     ///// Implemented methods /////
0042     QRect scopeRect() override;
0043     QImage renderHUD(uint accelerationFactor) override;
0044     QImage renderAudioScope(uint accelerationFactor, const audioShortVector &audioFrame, const int freq, const int num_channels, const int num_samples,
0045                             const int newData) override;
0046     QImage renderBackground(uint accelerationFactor) override;
0047     bool isHUDDependingOnInput() const override;
0048     bool isScopeDependingOnInput() const override;
0049     bool isBackgroundDependingOnInput() const override;
0050     void readConfig() override;
0051     void writeConfig();
0052     void handleMouseDrag(const QPoint &movement, const RescaleDirection rescaleDirection, const Qt::KeyboardModifiers rescaleModifiers) override;
0053     void resizeEvent(QResizeEvent *event) override;
0054 
0055 private:
0056     Ui::Spectrogram_UI *m_ui;
0057     FFTTools m_fftTools;
0058     QAction *m_aResetHz;
0059     QAction *m_aGrid;
0060     QAction *m_aTrackMouse;
0061     QAction *m_aHighlightPeaks;
0062 
0063     QList<QVector<float>> m_fftHistory;
0064     QImage m_fftHistoryImg;
0065 
0066     int m_dBmin{-70};
0067     int m_dBmax{0};
0068 
0069     int m_freqMax{0};
0070     bool m_customFreq{false};
0071 
0072     bool m_parameterChanged{false};
0073 
0074     QRect m_innerScopeRect;
0075     QRgb m_colorMap[256];
0076 
0077 private Q_SLOTS:
0078     void slotResetMaxFreq();
0079 };