commit 93b5dc79a7c72f20fac69af4cfdf3674601ca33b
parent 8544a03c3c996ecb1b30a1b10a65488ad687256b
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date: Fri, 17 Aug 2012 22:19:18 -0700
Move out palette code
Diffstat:
5 files changed, 104 insertions(+), 51 deletions(-)
diff --git a/src/Makefile.am b/src/Makefile.am
@@ -1,13 +1,14 @@
bin_PROGRAMS = spek
spek_SOURCES = \
- spek.cc \
- spek-audio.c \
- spek-audio.h \
spek-audio-desc.cc \
spek-audio-desc.hh \
+ spek-audio.c \
+ spek-audio.h \
spek-fft.c \
spek-fft.h \
+ spek-palette.c \
+ spek-palette.h \
spek-pipeline.c \
spek-pipeline.h \
spek-platform.cc \
@@ -19,7 +20,8 @@ spek_SOURCES = \
spek-spectrogram.cc \
spek-spectrogram.hh \
spek-window.cc \
- spek-window.hh
+ spek-window.hh \
+ spek.cc
spek_CPPFLAGS = \
-include config.h \
diff --git a/src/spek-palette.c b/src/spek-palette.c
@@ -0,0 +1,61 @@
+/* spek-palette.c
+ *
+ * Copyright (C) 2010-2012 Alexander Kojevnikov <alexander@kojevnikov.com>
+ *
+ * Spek 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Spek 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 Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Spek. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "spek-palette.h"
+
+// Modified version of Dan Bruton's algorithm:
+// http://www.physics.sfasu.edu/astro/color/spectra.html
+uint32_t spek_palette_spectrum(double level)
+{
+ level *= 0.6625;
+ double r = 0.0, g = 0.0, b = 0.0;
+ if (level >= 0 && level < 0.15) {
+ r = (0.15 - level) / (0.15 + 0.075);
+ g = 0.0;
+ b = 1.0;
+ } else if (level >= 0.15 && level < 0.275) {
+ r = 0.0;
+ g = (level - 0.15) / (0.275 - 0.15);
+ b = 1.0;
+ } else if (level >= 0.275 && level < 0.325) {
+ r = 0.0;
+ g = 1.0;
+ b = (0.325 - level) / (0.325 - 0.275);
+ } else if (level >= 0.325 && level < 0.5) {
+ r = (level - 0.325) / (0.5 - 0.325);
+ g = 1.0;
+ b = 0.0;
+ } else if (level >= 0.5 && level < 0.6625) {
+ r = 1.0;
+ g = (0.6625 - level) / (0.6625 - 0.5f);
+ b = 0.0;
+ }
+
+ // Intensity correction.
+ double cf = 1.0;
+ if (level >= 0.0 && level < 0.1) {
+ cf = level / 0.1;
+ }
+ cf *= 255.0;
+
+ // Pack RGB values into a 32-bit uint.
+ uint32_t rr = (uint32_t) (r * cf + 0.5);
+ uint32_t gg = (uint32_t) (g * cf + 0.5);
+ uint32_t bb = (uint32_t) (b * cf + 0.5);
+ return (rr << 16) + (gg << 8) + bb;
+}
diff --git a/src/spek-palette.h b/src/spek-palette.h
@@ -0,0 +1,34 @@
+/* spek-palette.h
+ *
+ * Copyright (C) 2010-2012 Alexander Kojevnikov <alexander@kojevnikov.com>
+ *
+ * Spek 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Spek 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 Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Spek. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SPEK_PALETTE_HH_
+#define SPEK_PALETTE_HH_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+
+uint32_t spek_palette_spectrum(double level);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/src/spek-spectrogram.cc b/src/spek-spectrogram.cc
@@ -21,6 +21,7 @@
#include "spek-audio.h"
#include "spek-audio-desc.hh"
+#include "spek-palette.h"
#include "spek-pipeline.h"
#include "spek-platform.hh"
#include "spek-ruler.hh"
@@ -58,7 +59,7 @@ SpekSpectrogram::SpekSpectrogram(wxFrame *parent) :
// Pre-draw the palette.
for (int y = 0; y < BANDS; y++) {
- uint32_t color = get_color(y / (double) BANDS);
+ uint32_t color = spek_palette_spectrum(y / (double) BANDS);
this->palette.SetRGB(
wxRect(0, BANDS - y - 1, RULER, 1),
color >> 16,
@@ -260,7 +261,7 @@ void SpekSpectrogram::pipeline_cb(int sample, float *values, void *cb_data)
for (int y = 0; y < BANDS; y++) {
double level = log10(1.0 - THRESHOLD + values[y]) / log10_threshold;
if (level > 1.0) level = 1.0;
- uint32_t color = get_color(level);
+ uint32_t color = spek_palette_spectrum(level);
s->image.SetRGB(
sample,
BANDS - y - 1,
@@ -307,46 +308,3 @@ void SpekSpectrogram::start()
Refresh();
}
-
-// Modified version of Dan Bruton's algorithm:
-// http://www.physics.sfasu.edu/astro/color/spectra.html
-// TODO: Move out to a C function.
-uint32_t SpekSpectrogram::get_color(double level)
-{
- level *= 0.6625;
- double r = 0.0, g = 0.0, b = 0.0;
- if (level >= 0 && level < 0.15) {
- r = (0.15 - level) / (0.15 + 0.075);
- g = 0.0;
- b = 1.0;
- } else if (level >= 0.15 && level < 0.275) {
- r = 0.0;
- g = (level - 0.15) / (0.275 - 0.15);
- b = 1.0;
- } else if (level >= 0.275 && level < 0.325) {
- r = 0.0;
- g = 1.0;
- b = (0.325 - level) / (0.325 - 0.275);
- } else if (level >= 0.325 && level < 0.5) {
- r = (level - 0.325) / (0.5 - 0.325);
- g = 1.0;
- b = 0.0;
- } else if (level >= 0.5 && level < 0.6625) {
- r = 1.0;
- g = (0.6625 - level) / (0.6625 - 0.5f);
- b = 0.0;
- }
-
- // Intensity correction.
- double cf = 1.0;
- if (level >= 0.0 && level < 0.1) {
- cf = level / 0.1;
- }
- cf *= 255.0;
-
- // Pack RGB values into a 32-bit uint.
- uint32_t rr = (uint32_t) (r * cf + 0.5);
- uint32_t gg = (uint32_t) (g * cf + 0.5);
- uint32_t bb = (uint32_t) (b * cf + 0.5);
- return (rr << 16) + (gg << 8) + bb;
-}
diff --git a/src/spek-spectrogram.hh b/src/spek-spectrogram.hh
@@ -19,7 +19,6 @@
#ifndef SPEK_SPECTROGRAM_HH_
#define SPEK_SPECTROGRAM_HH_
-#include <stdint.h>
#include <wx/wx.h>
struct spek_audio_properties;
@@ -40,7 +39,6 @@ private:
void start();
static void pipeline_cb(int sample, float *values, void *cb_data);
- static uint32_t get_color(double level);
spek_pipeline *pipeline;
const spek_audio_properties *properties;