-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add first working version of a spectrogram visualization
addresses #25
- Loading branch information
Showing
13 changed files
with
192 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
|
||
#include "SpectrogramAnalyzer.h" | ||
|
||
#include "PaletteHandler.h" | ||
|
||
#include <cmath> | ||
|
||
#include <QPainter> | ||
#include <QResizeEvent> | ||
|
||
|
||
static inline uint myMax(uint v1, uint v2) | ||
{ | ||
return v1 > v2 ? v1 : v2; | ||
} | ||
|
||
SpectrogramAnalyzer::SpectrogramAnalyzer(QWidget *parent) | ||
: AnalyzerBase(parent), m_spectrogram(0,0,QImage::Format_Invalid) | ||
{ | ||
setObjectName("SpectrogramAnalyzer"); | ||
this->setAttribute(Qt::WA_OpaquePaintEvent); | ||
|
||
setFps(0); | ||
} | ||
|
||
|
||
void SpectrogramAnalyzer::connectSignals() | ||
{ | ||
this->disconnectSignals(); | ||
} | ||
|
||
void SpectrogramAnalyzer::display(const QImage& img) | ||
{ | ||
m_toBeDrawn = img.mirrored(); | ||
this->update(); | ||
} | ||
|
||
void SpectrogramAnalyzer::resizeEvent(QResizeEvent * event) | ||
{ | ||
int h = event->size().height(), w = event->size().width(); | ||
|
||
m_scope.resize(h); | ||
m_logScope.resize(h); | ||
m_xPos = 0; | ||
|
||
m_spectrogram = QImage(w,h,QImage::Format_RGB32); | ||
m_spectrogram.fill(Qt::black); | ||
m_currentWidth = w; | ||
|
||
event->accept(); | ||
} | ||
|
||
|
||
void SpectrogramAnalyzer::paintEvent(QPaintEvent* event) | ||
{ | ||
QPainter painter(this); | ||
painter.drawImage(this->rect(), m_toBeDrawn); | ||
|
||
event->accept(); | ||
} | ||
|
||
void SpectrogramAnalyzer::transform(QVector<float> &s) | ||
{ | ||
if (m_logScope.size() == 0 || s.size() == 0) | ||
{ | ||
return; | ||
} | ||
|
||
m_fht->spectrum(/*&m_logScope.front(), */&s.front()); | ||
} | ||
|
||
void SpectrogramAnalyzer::analyze(const QVector<float> &s, uint32_t srate) | ||
{ | ||
if (m_scope.size() == 0 || s.size() == 0) | ||
{ | ||
return; | ||
} | ||
|
||
interpolate(s, m_scope); | ||
|
||
double w = 1 / sqrt(srate); | ||
auto scopeSize = m_scope.size(); | ||
for (int y = 0; y < scopeSize/2; y++) | ||
{ | ||
int a = 255*sqrt(w * /*sqrt*/(m_scope[y + 0] * m_scope[y + 0] /*+ m_scope[y + 1] * m_scope[y + 1]*/)); | ||
int b = /*(nb_display_channels == 2 ) ? sqrt(w * hypot(data[1][2 * y + 0], data[1][2 * y + 1])) | ||
:*/ a; | ||
a = std::min(a, 255); | ||
b = std::min(b, 255); | ||
|
||
for (int i = 0; i < 2; i++) | ||
{ | ||
m_spectrogram.setPixel(m_xPos, 2 * y + i, qRgb(a, b, (a + b) >> 1)); | ||
m_spectrogram.setPixel(m_xPos+1, 2 * y + i, qRgb(a, b, (a + b) >> 1)); | ||
} | ||
} | ||
++m_xPos; | ||
if (++m_xPos >= m_currentWidth) | ||
{ | ||
m_xPos = 0; | ||
} | ||
|
||
this->display(m_spectrogram); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/**************************************************************************************** | ||
* Copyright (c) 2003-2005 Max Howell <max.howell@methylblue.com> * | ||
* Copyright (c) 2005-2013 Mark Kretschmann <kretschmann@kde.org> * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify it under * | ||
* the terms of the GNU General Public License as published by the Free Software * | ||
* Foundation; either version 2 of the License, or (at your option) any later * | ||
* version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * | ||
* PARTICULAR PURPOSE. See the GNU General Pulic License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License along with * | ||
* this program. If not, see <http://www.gnu.org/licenses/>. * | ||
****************************************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include "AnalyzerBase.h" | ||
|
||
#include <QImage> | ||
#include <QPixmap> | ||
#include <QSharedPointer> | ||
#include <QSize> | ||
|
||
class QPalette; | ||
|
||
class SpectrogramAnalyzer : public AnalyzerBase | ||
{ | ||
public: | ||
SpectrogramAnalyzer(QWidget *); | ||
|
||
void transform(QVector<float> &s) override; | ||
|
||
public slots: | ||
void connectSignals() override; | ||
|
||
protected: | ||
void paintEvent(QPaintEvent*) override; | ||
void resizeEvent(QResizeEvent * event) override; | ||
void analyze(const QVector<float> &, uint32_t srate) override; | ||
|
||
protected slots: | ||
void display(const QImage&); | ||
|
||
private: | ||
|
||
std::atomic<int> m_xPos; | ||
std::atomic<int> m_currentWidth; | ||
QVector<float> m_scope, m_logScope; //so we don't create a vector every frame | ||
|
||
QImage m_spectrogram; | ||
QImage m_toBeDrawn; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters