From 4eb33aef1e9827cfb4ec7b12e9c843a01d8d9a91 Mon Sep 17 00:00:00 2001 From: Mike Walters Date: Fri, 11 Sep 2015 00:16:55 +0100 Subject: [PATCH] Pre-calculate color map Huge time savings in spectrogram generation. Bit hacky at the moment with hard-coded size scattered everywhere. Plan is to rejig this for custom color maps in the future, ref #4 --- spectrogram.cpp | 7 ++++++- spectrogram.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/spectrogram.cpp b/spectrogram.cpp index e6e1ec4..640937d 100644 --- a/spectrogram.cpp +++ b/spectrogram.cpp @@ -16,6 +16,11 @@ Spectrogram::Spectrogram() zoomLevel = 0; powerMax = 0.0f; powerMin = -50.0f; + + for (int i = 0; i < 256; i++) { + float p = (float)i / 256; + colormap[i] = QColor::fromHsvF(p * 0.83f, 1.0, 1.0 - p).rgba(); + } } Spectrogram::~Spectrogram() @@ -68,7 +73,7 @@ void Spectrogram::paintEvent(QPaintEvent *event) float normPower = (line[x] - powerMax) * -1.0f / powerRange; normPower = clamp(normPower, 0.0f, 1.0f); - image.setPixel(x, y, QColor::fromHsvF(normPower * 0.83f, 1.0, 1.0 - normPower).rgba()); + image.setPixel(x, y, colormap[(uint8_t)(normPower * (256 - 1))]); } } diff --git a/spectrogram.h b/spectrogram.h index d9cc36e..bad1d79 100644 --- a/spectrogram.h +++ b/spectrogram.h @@ -37,6 +37,7 @@ private: FFT *fft = nullptr; std::unique_ptr window; fftwf_complex *lineBuffer = nullptr; + uint colormap[256]; int sampleRate; int fftSize;