spek

Acoustic spectrum analyser
git clone http://git.hanabi.in/repos/spek.git
Log | Files | Refs | README

commit 3cc57547c7a5e07be14480e041d08a77ab79383c
parent 0e76ccb55a78747e976d336723b269d51e18509b
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Sat,  2 Apr 2016 19:26:37 -0700

Add monochrome palette

References #96.

Diffstat:
MMANUAL.md | 3+++
Msrc/spek-palette.cc | 22+++++++++++++++++++++-
Msrc/spek-palette.h | 9++++++++-
Msrc/spek-spectrogram.cc | 20+++++++++++++-------
Msrc/spek-spectrogram.h | 5++++-
5 files changed, 49 insertions(+), 10 deletions(-)

diff --git a/MANUAL.md b/MANUAL.md @@ -56,6 +56,9 @@ On OS X use the Command key instead of Ctrl. `s`, `S` : Change the DFT window size. +`p`, `P` +: Change the palette. + # FILES *~/.config/spek/preferences* diff --git a/src/spek-palette.cc b/src/spek-palette.cc @@ -1,8 +1,10 @@ +#include <assert.h> + #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) +static uint32_t spectrum(double level) { level *= 0.6625; double r = 0.0, g = 0.0, b = 0.0; @@ -41,3 +43,21 @@ uint32_t spek_palette_spectrum(double level) uint32_t bb = (uint32_t) (b * cf + 0.5); return (rr << 16) + (gg << 8) + bb; } + +static uint32_t mono(double level) +{ + uint32_t v = (uint32_t) (level * 255.0 + 0.5); + return (v << 16) + (v << 8) + v; +} + +uint32_t spek_palette(enum palette palette, double level) { + switch (palette) { + case PALETTE_SPECTRUM: + return spectrum(level); + case PALETTE_MONO: + return mono(level); + default: + assert(false); + return 0; + } +} diff --git a/src/spek-palette.h b/src/spek-palette.h @@ -2,4 +2,11 @@ #include <stdint.h> -uint32_t spek_palette_spectrum(double level); +enum palette { + PALETTE_SPECTRUM, + PALETTE_MONO, + PALETTE_COUNT, + PALETTE_DEFAULT = PALETTE_SPECTRUM, +}; + +uint32_t spek_palette(enum palette palette, double level); diff --git a/src/spek-spectrogram.cc b/src/spek-spectrogram.cc @@ -5,7 +5,6 @@ #include "spek-audio.h" #include "spek-events.h" #include "spek-fft.h" -#include "spek-palette.h" #include "spek-pipeline.h" #include "spek-platform.h" #include "spek-ruler.h" @@ -51,7 +50,8 @@ SpekSpectrogram::SpekSpectrogram(wxFrame *parent) : pipeline(NULL), duration(0.0), sample_rate(0), - palette(), + palette(PALETTE_DEFAULT), + palette_image(), image(1, 1), prev_width(-1), fft_bits(FFT_BITS), @@ -108,6 +108,12 @@ void SpekSpectrogram::on_char(wxKeyEvent& evt) } else if (N && evt.GetKeyCode() == 's') { this->fft_bits = spek_max(this->fft_bits - 1, MIN_FFT_BITS); this->create_palette(); + } else if (S && evt.GetKeyCode() == 'P') { + this->palette = (enum palette) ((this->palette + 1) % PALETTE_COUNT); + this->create_palette(); + } else if (N && evt.GetKeyCode() == 'p') { + this->palette = (enum palette) ((this->palette - 1 + PALETTE_COUNT) % PALETTE_COUNT); + this->create_palette(); } else { evt.Skip(); return; @@ -150,7 +156,7 @@ void SpekSpectrogram::on_have_sample(SpekHaveSampleEvent& event) for (int y = 0; y < bands; y++) { double value = fmin(this->urange, fmax(this->lrange, values[y])); double level = (value - this->lrange) / range; - uint32_t color = spek_palette_spectrum(level); + uint32_t color = spek_palette(this->palette, level); this->image.SetRGB( sample, bands - y - 1, @@ -301,7 +307,7 @@ void SpekSpectrogram::render(wxDC& dc) // The palette. if (h - TPAD - BPAD > 0) { - wxBitmap bmp(this->palette.Scale(RULER, h - TPAD - BPAD + 1)); + wxBitmap bmp(this->palette_image.Scale(RULER, h - TPAD - BPAD + 1)); dc.DrawBitmap(bmp, w - RPAD + GAP, TPAD); // Prepare to draw the ruler. @@ -379,10 +385,10 @@ void SpekSpectrogram::stop() void SpekSpectrogram::create_palette() { - this->palette.Create(RULER, bits_to_bands(this->fft_bits)); + this->palette_image.Create(RULER, bits_to_bands(this->fft_bits)); for (int y = 0; y < bits_to_bands(this->fft_bits); y++) { - uint32_t color = spek_palette_spectrum(y / (double)bits_to_bands(this->fft_bits)); - this->palette.SetRGB( + uint32_t color = spek_palette(this->palette, y / (double)bits_to_bands(this->fft_bits)); + this->palette_image.SetRGB( wxRect(0, bits_to_bands(this->fft_bits) - y - 1, RULER, 1), color >> 16, (color >> 8) & 0xFF, diff --git a/src/spek-spectrogram.h b/src/spek-spectrogram.h @@ -4,6 +4,8 @@ #include <wx/wx.h> +#include "spek-palette.h" + class Audio; class FFT; class SpekHaveSampleEvent; @@ -36,7 +38,8 @@ private: wxString desc; double duration; int sample_rate; - wxImage palette; + enum palette palette; + wxImage palette_image; wxImage image; int prev_width; int fft_bits;