Skip to content

Commit

Permalink
Add SoX palette
Browse files Browse the repository at this point in the history
Closes #96.
  • Loading branch information
alexkay committed Apr 3, 2016
1 parent 3cc5754 commit 8a8f9cc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/spek-palette.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <assert.h>
#include <math.h>

#include "spek-palette.h"

Expand Down Expand Up @@ -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);
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion src/spek-palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 8a8f9cc

Please sign in to comment.