#include "SpectrogramWidget.h" #include "GradientDialog.h" #include #include #include #include #include #include #include #include #include #include #include #include SpectrogramWidget::SpectrogramWidget(QWidget *parent) : QOpenGLWidget(parent) { setAttribute(Qt::WA_OpaquePaintEvent); QSurfaceFormat fmt; fmt.setVersion(3, 3); fmt.setProfile(QSurfaceFormat::CoreProfile); fmt.setSwapInterval(1); fmt.setSwapBehavior(QSurfaceFormat::DoubleBuffer); setFormat(fmt); setMinimumSize(400, 200); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_barHeights.resize(BAR_COUNT); m_barPeaksVec.resize(BAR_COUNT); loadBarColors(); auto *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, QOverload<>::of(&SpectrogramWidget::update)); timer->start(16); } SpectrogramWidget::~SpectrogramWidget() { makeCurrent(); glDeleteTextures(1, &m_heatmapTex); glDeleteVertexArrays(1, &m_vaoBars); doneCurrent(); } void SpectrogramWidget::receiveFft(const QVector &mag) { QMutexLocker lk(&m_queueMtx); while (m_fftQueue.size() >= 4) m_fftQueue.dequeue(); m_fftQueue.enqueue(mag); update(); } void SpectrogramWidget::reset() { { QMutexLocker lk(&m_queueMtx); m_fftQueue.clear(); } m_currentMag .fill(0.f); m_barSmoothed.fill(0.f); m_barPeak .fill(0.f); m_peakTimer .fill(0); update(); } void SpectrogramWidget::initializeGL() { initializeOpenGLFunctions(); initShaders(); buildHeatmapTexture(); glGenVertexArrays(1, &m_vaoBars); glDisable(GL_BLEND); } void SpectrogramWidget::initShaders() { auto load = [](const QString &path) -> QByteArray { QFile f(path); if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) { qWarning() << "Cannot open shader:" << path; return {}; } return f.readAll(); }; if (!m_barsProg.addShaderFromSourceCode(QOpenGLShader::Vertex, load(":/shaders/bars.vert"))) qWarning() << "Bars vert error:" << m_barsProg.log(); if (!m_barsProg.addShaderFromSourceCode(QOpenGLShader::Fragment, load(":/shaders/bars.frag"))) qWarning() << "Bars frag error:" << m_barsProg.log(); if (!m_barsProg.link()) qWarning() << "Bars link error:" << m_barsProg.log(); m_uBarHeights = m_barsProg.uniformLocation("barHeights"); m_uBarPeaks = m_barsProg.uniformLocation("barPeaks"); m_uBarCount = m_barsProg.uniformLocation("barCount"); m_uBarWidth = m_barsProg.uniformLocation("barWidth"); m_uGap = m_barsProg.uniformLocation("gap"); m_uBottomY = m_barsProg.uniformLocation("bottomY"); m_uTopClip = m_barsProg.uniformLocation("topClip"); m_uHeatmap = m_barsProg.uniformLocation("heatmap"); } void SpectrogramWidget::buildHeatmapTexture() { updateHeatmapTexture(); // use current m_stops } void SpectrogramWidget::updateHeatmapTexture() { if (m_heatmapTex) { glDeleteTextures(1, &m_heatmapTex); m_heatmapTex = 0; } if (m_stops.size() < 2) m_stops = { QColor(0,0,0), QColor(255,255,255) }; const int nSegs = m_stops.size() - 1; auto lerp = [](int x, int y, float f) { return std::clamp(int(x + (y - x) * f), 0, 255); }; for (int i = 0; i < 256; i++) { float t = float(i) / 255.f * nSegs; int seg = std::min(int(t), nSegs - 1); float f = t - seg; QColor a = m_stops[seg], b = m_stops[seg+1]; m_heatmap[i] = qRgb(lerp(a.red(), b.red(), f), lerp(a.green(), b.green(), f), lerp(a.blue(), b.blue(), f)); } glGenTextures(1, &m_heatmapTex); glBindTexture(GL_TEXTURE_1D, m_heatmapTex); std::vector td(256 * 3); for (int i = 0; i < 256; i++) { td[3*i+0] = quint8(qRed (m_heatmap[i])); td[3*i+1] = quint8(qGreen(m_heatmap[i])); td[3*i+2] = quint8(qBlue (m_heatmap[i])); } glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, td.data()); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); } void SpectrogramWidget::setStops(const QVector &stops) { if (stops.size() < 2) return; m_stops = stops; makeCurrent(); updateHeatmapTexture(); doneCurrent(); update(); saveBarColors(); } void SpectrogramWidget::loadBarColors() { QString configDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.subwave"; QDir().mkpath(configDir); QString path = configDir + "/barcolors.json"; QFile f(path); if (!f.open(QIODevice::ReadOnly)) return; QJsonDocument doc = QJsonDocument::fromJson(f.readAll()); f.close(); if (!doc.isArray()) return; QJsonArray arr = doc.array(); QVector loaded; for (const auto &v : arr) { QJsonObject obj = v.toObject(); int r = obj["r"].toInt(0); int g = obj["g"].toInt(0); int b = obj["b"].toInt(0); loaded.append(QColor(r, g, b)); } if (loaded.size() >= 2) m_stops = loaded; } void SpectrogramWidget::saveBarColors() { QString configDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.subwave"; QDir().mkpath(configDir); QString path = configDir + "/barcolors.json"; QFile f(path); if (!f.open(QIODevice::WriteOnly)) return; QJsonArray arr; for (const QColor &c : m_stops) { QJsonObject obj; obj["r"] = c.red(); obj["g"] = c.green(); obj["b"] = c.blue(); arr.append(obj); } QJsonDocument doc(arr); f.write(doc.toJson()); f.close(); } void SpectrogramWidget::mousePressEvent(QMouseEvent *e) { if (e->button() == Qt::LeftButton) { GradientDialog dlg(m_stops, this); if (dlg.exec() == QDialog::Accepted) { setStops(dlg.stops()); } } QOpenGLWidget::mousePressEvent(e); } void SpectrogramWidget::computeBars() { const int fftLen = m_currentMag.size(); const double logMin = std::log10(20.0), logMax = std::log10(22050.0); QVector bars(BAR_COUNT, 0.f); for (int b = 0; b < BAR_COUNT; b++) { double f1 = std::pow(10.0, logMin + (logMax-logMin)* b /BAR_COUNT); double f2 = std::pow(10.0, logMin + (logMax-logMin)*(b+1)/BAR_COUNT); int bin1 = std::clamp(int(f1/22050.0*fftLen), 0, fftLen-1); int bin2 = std::clamp(int(f2/22050.0*fftLen), bin1, fftLen-1); float sum = 0.f; int cnt = 0; for (int k = bin1; k <= bin2; k++) { sum += m_currentMag[k]; cnt++; } bars[b] = cnt > 0 ? sum/cnt : 0.f; } for (int b = 0; b < BAR_COUNT; b++) { float db = bars[b] > 0 ? 20.f*std::log10(bars[b]) : DB_MIN; bars[b] = std::clamp((db - DB_MIN) / (DB_MAX - DB_MIN), 0.f, 1.f); } for (int b = 0; b < BAR_COUNT; b++) { m_barSmoothed[b] = m_barSmoothed[b]*0.65f + bars[b]*0.35f; if (m_barSmoothed[b] >= m_barPeak[b]) { m_barPeak[b] = m_barSmoothed[b]; m_peakTimer[b] = 25; } else if (m_peakTimer[b] > 0) { m_peakTimer[b]--; } else { m_barPeak[b] = std::max(0.f, m_barPeak[b] - 0.006f); } m_barHeights [b] = m_barSmoothed[b]; m_barPeaksVec[b] = m_barPeak[b]; } } void SpectrogramWidget::paintGL() { { QMutexLocker lk(&m_queueMtx); while (!m_fftQueue.isEmpty()) m_currentMag = m_fftQueue.dequeue(); } computeBars(); QColor bg = palette().color(QPalette::Window); glClearColor(bg.redF(), bg.greenF(), bg.blueF(), 1.0f); glClear(GL_COLOR_BUFFER_BIT); if (m_barsProg.isLinked()) { const float barWidth = 2.0f / BAR_COUNT; const float gap = barWidth * 0.2f; const float drawWidth = barWidth - gap; m_barsProg.bind(); glUniform1fv(m_uBarHeights, BAR_COUNT, m_barHeights .constData()); glUniform1fv(m_uBarPeaks, BAR_COUNT, m_barPeaksVec.constData()); glUniform1i (m_uBarCount, BAR_COUNT); glUniform1f (m_uBarWidth, drawWidth); glUniform1f (m_uGap, gap); glUniform1f (m_uBottomY, -1.0f); glUniform1f (m_uTopClip, 1.0f); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_1D, m_heatmapTex); glUniform1i(m_uHeatmap, 0); glBindVertexArray(m_vaoBars); glDrawArraysInstanced(GL_TRIANGLES, 0, 6, BAR_COUNT); glBindVertexArray(0); m_barsProg.release(); } }