File indexing completed on 2024-04-28 04:52:21

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 "lib/external/kiss_fft/tools/kiss_fftr.h"
0013 #include "ui_audiospectrum_ui.h"
0014 
0015 // Enables debugging
0016 //#define DEBUG_AUDIOSPEC
0017 
0018 // Show overmodulation
0019 #define DETECT_OVERMODULATION
0020 
0021 #include <QHash>
0022 #include <QVector>
0023 
0024 class AudioSpectrum_UI;
0025 
0026 /**
0027  *  @brief Displays a spectral power distribution of audio samples.
0028  *  The frequency distribution is calculated by means of a Fast Fourier Transformation.
0029  *  For more information see Wikipedia:FFT and the code comments.
0030  *
0031  *  @todo Currently only supports one channel. Add support for multiple channels.
0032 */
0033 class AudioSpectrum : public AbstractAudioScopeWidget
0034 {
0035     Q_OBJECT
0036 
0037 public:
0038     explicit AudioSpectrum(QWidget *parent = nullptr);
0039     ~AudioSpectrum() override;
0040 
0041     // Implemented virtual methods
0042     QString widgetName() const override;
0043 
0044 protected:
0045     ///// Implemented methods /////
0046     QRect scopeRect() override;
0047     QImage renderHUD(uint accelerationFactor) override;
0048     QImage renderAudioScope(uint accelerationFactor, const audioShortVector &audioFrame, const int freq, const int num_channels, const int num_samples,
0049                             const int newData) override;
0050     QImage renderBackground(uint accelerationFactor) override;
0051     void readConfig() override;
0052     void writeConfig();
0053 
0054     void handleMouseDrag(const QPoint &movement, const RescaleDirection rescaleDirection, const Qt::KeyboardModifiers rescaleModifiers) override;
0055 
0056 private:
0057     Ui::AudioSpectrum_UI *m_ui;
0058 
0059     QAction *m_aResetHz;
0060     QAction *m_aTrackMouse;
0061     QAction *m_aShowMax;
0062 
0063     FFTTools m_fftTools;
0064     QVector<float> m_lastFFT;
0065     QSemaphore m_lastFFTLock;
0066 
0067     QVector<float> m_peaks;
0068     QVector<float> m_peakMap;
0069 
0070     /** Contains the plot only; m_scopeRect contains text and widgets as well */
0071     QRect m_innerScopeRect;
0072 
0073     /** Lower bound for the dB value to display */
0074     int m_dBmin{-70};
0075     /** Upper bound (max: 0) */
0076     int m_dBmax{0};
0077 
0078     /** Maximum frequency (limited by the sampling rate if determined automatically).
0079         Stored for the painters. */
0080     int m_freqMax{0};
0081     /** The user has chosen a custom frequency. */
0082     bool m_customFreq{false};
0083 
0084     float m_colorizeFactor{0};
0085 
0086 #ifdef DEBUG_AUDIOSPEC
0087     long m_timeTotal;
0088     long m_showTotal;
0089 #endif
0090 
0091 private Q_SLOTS:
0092     void slotResetMaxFreq();
0093 };