spek

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

commit 8a8f9cc87fcc371e4f3fc26a08d149faeb5d54e4
parent 3cc57547c7a5e07be14480e041d08a77ab79383c
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Sat,  2 Apr 2016 20:12:52 -0700

Add SoX palette

Closes #96.

Diffstat:
Msrc/spek-palette.cc | 34++++++++++++++++++++++++++++++++++
Msrc/spek-palette.h | 3++-
2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/src/spek-palette.cc b/src/spek-palette.cc @@ -1,4 +1,5 @@ #include <assert.h> +#include <math.h> #include "spek-palette.h" @@ -44,6 +45,37 @@ static uint32_t spectrum(double level) return (rr << 16) + (gg << 8) + bb; } +// The default palette used by SoX and written by Rob Sykes. +static uint32_t sox(double level) +{ + double r = 0.0; + if (level >= 0.13 && level < 0.73) { + r = sin((level - 0.13) / 0.60 * M_PI / 2.0); + } else if (level >= 0.73) { + r = 1.0; + } + + double g = 0.0; + if (level >= 0.6 && level < 0.91) { + g = sin((level - 0.6) / 0.31 * M_PI / 2.0); + } else if (level >= 0.91) { + g = 1.0; + } + + double b = 0.0; + if (level < 0.60) { + b = 0.5 * sin(level / 0.6 * M_PI); + } else if (level >= 0.78) { + b = (level - 0.78) / 0.22; + } + + // Pack RGB values into a 32-bit uint. + uint32_t rr = (uint32_t) (r * 255.0 + 0.5); + uint32_t gg = (uint32_t) (g * 255.0 + 0.5); + uint32_t bb = (uint32_t) (b * 255.0 + 0.5); + return (rr << 16) + (gg << 8) + bb; +} + static uint32_t mono(double level) { uint32_t v = (uint32_t) (level * 255.0 + 0.5); @@ -54,6 +86,8 @@ uint32_t spek_palette(enum palette palette, double level) { switch (palette) { case PALETTE_SPECTRUM: return spectrum(level); + case PALETTE_SOX: + return sox(level); case PALETTE_MONO: return mono(level); default: diff --git a/src/spek-palette.h b/src/spek-palette.h @@ -4,9 +4,10 @@ enum palette { PALETTE_SPECTRUM, + PALETTE_SOX, PALETTE_MONO, PALETTE_COUNT, - PALETTE_DEFAULT = PALETTE_SPECTRUM, + PALETTE_DEFAULT = PALETTE_SOX, }; uint32_t spek_palette(enum palette palette, double level);