Consolidate compressor settings into a struct.

Doing that makes things cleaner for later on.

Change-Id: I4e279aa57ace16a348acc0fc09059592325ec95f
This commit is contained in:
Michael Sevakis 2012-04-26 17:19:16 -04:00
parent 97a535d83c
commit 0842d7f7e1
8 changed files with 222 additions and 217 deletions

View File

@ -126,15 +126,20 @@ static int timestretch_callback(int action,const struct menu_item_ex *this_item)
/* compressor submenu */
MENUITEM_SETTING(compressor_threshold,
&global_settings.compressor_threshold, lowlatency_callback);
&global_settings.compressor_settings.threshold,
lowlatency_callback);
MENUITEM_SETTING(compressor_gain,
&global_settings.compressor_makeup_gain, lowlatency_callback);
&global_settings.compressor_settings.makeup_gain,
lowlatency_callback);
MENUITEM_SETTING(compressor_ratio,
&global_settings.compressor_ratio, lowlatency_callback);
&global_settings.compressor_settings.ratio,
lowlatency_callback);
MENUITEM_SETTING(compressor_knee,
&global_settings.compressor_knee, lowlatency_callback);
&global_settings.compressor_settings.knee,
lowlatency_callback);
MENUITEM_SETTING(compressor_release,
&global_settings.compressor_release_time, lowlatency_callback);
&global_settings.compressor_settings.release_time,
lowlatency_callback);
MAKE_MENU(compressor_menu,ID2P(LANG_COMPRESSOR), NULL, Icon_NOICON,
&compressor_threshold, &compressor_gain, &compressor_ratio,
&compressor_knee, &compressor_release);

View File

@ -780,9 +780,6 @@ void settings_apply(bool read_disk)
{
#ifdef HAVE_LCD_BITMAP
int rc;
#endif
#if CONFIG_CODEC == SWCODEC
int i;
#endif
sound_settings_apply();
@ -991,7 +988,7 @@ void settings_apply(bool read_disk)
/* Configure software equalizer, hardware eq is handled in audio_init() */
dsp_set_eq(global_settings.eq_enabled);
dsp_set_eq_precut(global_settings.eq_precut);
for(i = 0; i < 5; i++) {
for(int i = 0; i < 5; i++) {
dsp_set_eq_coefs(i);
}
@ -999,7 +996,7 @@ void settings_apply(bool read_disk)
#ifdef HAVE_PITCHSCREEN
dsp_timestretch_enable(global_settings.timestretch_enabled);
#endif
dsp_set_compressor();
dsp_set_compressor(&global_settings.compressor_settings);
#endif
#ifdef HAVE_SPDIF_POWER

View File

@ -772,11 +772,14 @@ struct user_settings
#endif
#if CONFIG_CODEC == SWCODEC
int compressor_threshold;
int compressor_makeup_gain;
int compressor_ratio;
int compressor_knee;
int compressor_release_time;
struct compressor_settings
{
int threshold;
int makeup_gain;
int ratio;
int knee;
int release_time;
} compressor_settings;
#endif
int sleeptimer_duration; /* In minutes; 0=off */

View File

@ -436,7 +436,7 @@ static void crossfeed_cross_set(int val)
static void compressor_set(int val)
{
(void)val;
dsp_set_compressor();
dsp_set_compressor(&global_settings.compressor_settings);
}
static const char* db_format(char* buffer, size_t buffer_size, int value,
@ -1482,25 +1482,26 @@ const struct settings_list settings[] = {
#endif
/* compressor */
INT_SETTING_NOWRAP(F_SOUNDSETTING, compressor_threshold,
INT_SETTING_NOWRAP(F_SOUNDSETTING, compressor_settings.threshold,
LANG_COMPRESSOR_THRESHOLD, 0,
"compressor threshold", UNIT_DB, 0, -24,
-3, formatter_unit_0_is_off, getlang_unit_0_is_off, compressor_set),
CHOICE_SETTING(F_SOUNDSETTING|F_NO_WRAP, compressor_makeup_gain,
-3, formatter_unit_0_is_off, getlang_unit_0_is_off,
compressor_set),
CHOICE_SETTING(F_SOUNDSETTING|F_NO_WRAP, compressor_settings.makeup_gain,
LANG_COMPRESSOR_GAIN, 1, "compressor makeup gain",
"off,auto", compressor_set, 2,
ID2P(LANG_OFF), ID2P(LANG_AUTO)),
CHOICE_SETTING(F_SOUNDSETTING|F_NO_WRAP, compressor_ratio,
CHOICE_SETTING(F_SOUNDSETTING|F_NO_WRAP, compressor_settings.ratio,
LANG_COMPRESSOR_RATIO, 1, "compressor ratio",
"2:1,4:1,6:1,10:1,limit", compressor_set, 5,
ID2P(LANG_COMPRESSOR_RATIO_2), ID2P(LANG_COMPRESSOR_RATIO_4),
ID2P(LANG_COMPRESSOR_RATIO_6), ID2P(LANG_COMPRESSOR_RATIO_10),
ID2P(LANG_COMPRESSOR_RATIO_LIMIT)),
CHOICE_SETTING(F_SOUNDSETTING|F_NO_WRAP, compressor_knee,
CHOICE_SETTING(F_SOUNDSETTING|F_NO_WRAP, compressor_settings.knee,
LANG_COMPRESSOR_KNEE, 1, "compressor knee",
"hard knee,soft knee", compressor_set, 2,
ID2P(LANG_COMPRESSOR_HARD_KNEE), ID2P(LANG_COMPRESSOR_SOFT_KNEE)),
INT_SETTING_NOWRAP(F_SOUNDSETTING, compressor_release_time,
INT_SETTING_NOWRAP(F_SOUNDSETTING, compressor_settings.release_time,
LANG_COMPRESSOR_RELEASE, 500,
"compressor release time", UNIT_MS, 100, 1000,
100, NULL, NULL, compressor_set),

View File

@ -29,6 +29,8 @@
/*#define LOGF_ENABLE*/
#include "logf.h"
static struct compressor_settings curr_set; /* Cached settings */
static int32_t comp_rel_slope IBSS_ATTR; /* S7.24 format */
static int32_t comp_makeup_gain IBSS_ATTR; /* S7.24 format */
static int32_t comp_curve[66] IBSS_ATTR; /* S7.24 format */
@ -38,214 +40,210 @@ static int32_t release_gain IBSS_ATTR; /* S7.24 format */
/** COMPRESSOR UPDATE
* Called via the menu system to configure the compressor process */
bool compressor_update(void)
bool compressor_update(const struct compressor_settings *settings)
{
static int curr_set[5];
int new_set[5] = {
global_settings.compressor_threshold,
global_settings.compressor_makeup_gain,
global_settings.compressor_ratio,
global_settings.compressor_knee,
global_settings.compressor_release_time};
/* make menu values useful */
int threshold = new_set[0];
bool auto_gain = (new_set[1] == 1);
const int comp_ratios[] = {2, 4, 6, 10, 0};
int ratio = comp_ratios[new_set[2]];
bool soft_knee = (new_set[3] == 1);
int release = new_set[4] * NATIVE_FREQUENCY / 1000;
/* make settings values useful */
int threshold = settings->threshold;
bool auto_gain = settings->makeup_gain == 1;
static const int comp_ratios[] = { 2, 4, 6, 10, 0 };
int ratio = comp_ratios[settings->ratio];
bool soft_knee = settings->knee == 1;
int release = settings->release_time * NATIVE_FREQUENCY / 1000;
bool changed = false;
bool active = (threshold < 0);
bool active = threshold < 0;
for (int i = 0; i < 5; i++)
if (memcmp(settings, &curr_set, sizeof (curr_set)))
{
if (curr_set[i] != new_set[i])
{
changed = true;
curr_set[i] = new_set[i];
/* Compressor settings have changed since last call */
changed = true;
#if defined(ROCKBOX_HAS_LOGF) && defined(LOGF_ENABLE)
switch (i)
{
case 0:
logf(" Compressor Threshold: %d dB\tEnabled: %s",
threshold, active ? "Yes" : "No");
break;
case 1:
logf(" Compressor Makeup Gain: %s",
auto_gain ? "Auto" : "Off");
break;
case 2:
if (ratio)
{ logf(" Compressor Ratio: %d:1", ratio); }
else
{ logf(" Compressor Ratio: Limit"); }
break;
case 3:
logf(" Compressor Knee: %s", soft_knee?"Soft":"Hard");
break;
case 4:
logf(" Compressor Release: %d", release);
break;
}
#endif
if (settings->threshold != curr_set.threshold)
{
logf(" Compressor Threshold: %d dB\tEnabled: %s",
threshold, active ? "Yes" : "No");
}
}
if (changed && active)
{
/* configure variables for compressor operation */
static const int32_t db[] = {
/* positive db equivalents in S15.16 format */
0x000000, 0x241FA4, 0x1E1A5E, 0x1A94C8,
0x181518, 0x1624EA, 0x148F82, 0x1338BD,
0x120FD2, 0x1109EB, 0x101FA4, 0x0F4BB6,
0x0E8A3C, 0x0DD840, 0x0D3377, 0x0C9A0E,
0x0C0A8C, 0x0B83BE, 0x0B04A5, 0x0A8C6C,
0x0A1A5E, 0x09ADE1, 0x094670, 0x08E398,
0x0884F6, 0x082A30, 0x07D2FA, 0x077F0F,
0x072E31, 0x06E02A, 0x0694C8, 0x064BDF,
0x060546, 0x05C0DA, 0x057E78, 0x053E03,
0x04FF5F, 0x04C273, 0x048726, 0x044D64,
0x041518, 0x03DE30, 0x03A89B, 0x037448,
0x03412A, 0x030F32, 0x02DE52, 0x02AE80,
0x027FB0, 0x0251D6, 0x0224EA, 0x01F8E2,
0x01CDB4, 0x01A359, 0x0179C9, 0x0150FC,
0x0128EB, 0x010190, 0x00DAE4, 0x00B4E1,
0x008F82, 0x006AC1, 0x004699, 0x002305};
struct curve_point
if (settings->makeup_gain != curr_set.makeup_gain)
{
int32_t db; /* S15.16 format */
int32_t offset; /* S15.16 format */
} db_curve[5];
/** Set up the shape of the compression curve first as decibel
values */
/* db_curve[0] = bottom of knee
[1] = threshold
[2] = top of knee
[3] = 0 db input
[4] = ~+12db input (2 bits clipping overhead) */
db_curve[1].db = threshold << 16;
if (soft_knee)
logf(" Compressor Makeup Gain: %s",
auto_gain ? "Auto" : "Off");
}
if (settings->ratio != cur_set.ratio)
{
/* bottom of knee is 3dB below the threshold for soft knee*/
db_curve[0].db = db_curve[1].db - (3 << 16);
/* top of knee is 3dB above the threshold for soft knee */
db_curve[2].db = db_curve[1].db + (3 << 16);
if (ratio)
/* offset = -3db * (ratio - 1) / ratio */
db_curve[2].offset = (int32_t)((long long)(-3 << 16)
* (ratio - 1) / ratio);
{ logf(" Compressor Ratio: %d:1", ratio); }
else
/* offset = -3db for hard limit */
db_curve[2].offset = (-3 << 16);
{ logf(" Compressor Ratio: Limit"); }
}
else
{
/* bottom of knee is at the threshold for hard knee */
db_curve[0].db = threshold << 16;
/* top of knee is at the threshold for hard knee */
db_curve[2].db = threshold << 16;
db_curve[2].offset = 0;
}
/* Calculate 0db and ~+12db offsets */
db_curve[4].db = 0xC0A8C; /* db of 2 bits clipping */
if (ratio)
{
/* offset = threshold * (ratio - 1) / ratio */
db_curve[3].offset = (int32_t)((long long)(threshold << 16)
* (ratio - 1) / ratio);
db_curve[4].offset = (int32_t)((long long)-db_curve[4].db
* (ratio - 1) / ratio) + db_curve[3].offset;
}
else
{
/* offset = threshold for hard limit */
db_curve[3].offset = (threshold << 16);
db_curve[4].offset = -db_curve[4].db + db_curve[3].offset;
}
/** Now set up the comp_curve table with compression offsets in the
form of gain factors in S7.24 format */
/* comp_curve[0] is 0 (-infinity db) input */
comp_curve[0] = UNITY;
/* comp_curve[1 to 63] are intermediate compression values
corresponding to the 6 MSB of the input values of a non-clipped
signal */
for (int i = 1; i < 64; i++)
{
/* db constants are stored as positive numbers;
make them negative here */
int32_t this_db = -db[i];
/* no compression below the knee */
if (this_db <= db_curve[0].db)
comp_curve[i] = UNITY;
/* if soft knee and below top of knee,
interpolate along soft knee slope */
else if (soft_knee && (this_db <= db_curve[2].db))
comp_curve[i] = fp_factor(fp_mul(
((this_db - db_curve[0].db) / 6),
db_curve[2].offset, 16), 16) << 8;
/* interpolate along ratio slope above the knee */
else
comp_curve[i] = fp_factor(fp_mul(
fp_div((db_curve[1].db - this_db), db_curve[1].db, 16),
db_curve[3].offset, 16), 16) << 8;
}
/* comp_curve[64] is the compression level of a maximum level,
non-clipped signal */
comp_curve[64] = fp_factor(db_curve[3].offset, 16) << 8;
/* comp_curve[65] is the compression level of a maximum level,
clipped signal */
comp_curve[65] = fp_factor(db_curve[4].offset, 16) << 8;
#if defined(ROCKBOX_HAS_LOGF) && defined(LOGF_ENABLE)
logf("\n *** Compression Offsets ***");
/* some settings for display only, not used in calculations */
db_curve[0].offset = 0;
db_curve[1].offset = 0;
db_curve[3].db = 0;
for (int i = 0; i <= 4; i++)
{
logf("Curve[%d]: db: % 6.2f\toffset: % 6.2f", i,
(float)db_curve[i].db / (1 << 16),
(float)db_curve[i].offset / (1 << 16));
}
logf("\nGain factors:");
for (int i = 1; i <= 65; i++)
{
debugf("%02d: %.6f ", i, (float)comp_curve[i] / UNITY);
if (i % 4 == 0) debugf("\n");
}
debugf("\n");
#endif
/* if using auto peak, then makeup gain is max offset -
.1dB headroom */
comp_makeup_gain = auto_gain ?
fp_factor(-(db_curve[3].offset) - 0x199A, 16) << 8 : UNITY;
logf("Makeup gain:\t%.6f", (float)comp_makeup_gain / UNITY);
/* calculate per-sample gain change a rate of 10db over release time
*/
comp_rel_slope = 0xAF0BB2 / release;
logf("Release slope:\t%.6f", (float)comp_rel_slope / UNITY);
release_gain = UNITY;
if (settings->knee != cur_set.knee)
{
logf(" Compressor Knee: %s", soft_knee?"Soft":"Hard");
}
if (settings->release_time != cur_set.release_time)
{
logf(" Compressor Release: %d", release);
}
#endif
curr_set = *settings;
}
if (!changed || !active)
return active;
/* configure variables for compressor operation */
static const int32_t db[] = {
/* positive db equivalents in S15.16 format */
0x000000, 0x241FA4, 0x1E1A5E, 0x1A94C8,
0x181518, 0x1624EA, 0x148F82, 0x1338BD,
0x120FD2, 0x1109EB, 0x101FA4, 0x0F4BB6,
0x0E8A3C, 0x0DD840, 0x0D3377, 0x0C9A0E,
0x0C0A8C, 0x0B83BE, 0x0B04A5, 0x0A8C6C,
0x0A1A5E, 0x09ADE1, 0x094670, 0x08E398,
0x0884F6, 0x082A30, 0x07D2FA, 0x077F0F,
0x072E31, 0x06E02A, 0x0694C8, 0x064BDF,
0x060546, 0x05C0DA, 0x057E78, 0x053E03,
0x04FF5F, 0x04C273, 0x048726, 0x044D64,
0x041518, 0x03DE30, 0x03A89B, 0x037448,
0x03412A, 0x030F32, 0x02DE52, 0x02AE80,
0x027FB0, 0x0251D6, 0x0224EA, 0x01F8E2,
0x01CDB4, 0x01A359, 0x0179C9, 0x0150FC,
0x0128EB, 0x010190, 0x00DAE4, 0x00B4E1,
0x008F82, 0x006AC1, 0x004699, 0x002305};
struct curve_point
{
int32_t db; /* S15.16 format */
int32_t offset; /* S15.16 format */
} db_curve[5];
/** Set up the shape of the compression curve first as decibel
values */
/* db_curve[0] = bottom of knee
[1] = threshold
[2] = top of knee
[3] = 0 db input
[4] = ~+12db input (2 bits clipping overhead) */
db_curve[1].db = threshold << 16;
if (soft_knee)
{
/* bottom of knee is 3dB below the threshold for soft knee*/
db_curve[0].db = db_curve[1].db - (3 << 16);
/* top of knee is 3dB above the threshold for soft knee */
db_curve[2].db = db_curve[1].db + (3 << 16);
if (ratio)
/* offset = -3db * (ratio - 1) / ratio */
db_curve[2].offset = (int32_t)((long long)(-3 << 16)
* (ratio - 1) / ratio);
else
/* offset = -3db for hard limit */
db_curve[2].offset = (-3 << 16);
}
else
{
/* bottom of knee is at the threshold for hard knee */
db_curve[0].db = threshold << 16;
/* top of knee is at the threshold for hard knee */
db_curve[2].db = threshold << 16;
db_curve[2].offset = 0;
}
/* Calculate 0db and ~+12db offsets */
db_curve[4].db = 0xC0A8C; /* db of 2 bits clipping */
if (ratio)
{
/* offset = threshold * (ratio - 1) / ratio */
db_curve[3].offset = (int32_t)((long long)(threshold << 16)
* (ratio - 1) / ratio);
db_curve[4].offset = (int32_t)((long long)-db_curve[4].db
* (ratio - 1) / ratio) + db_curve[3].offset;
}
else
{
/* offset = threshold for hard limit */
db_curve[3].offset = (threshold << 16);
db_curve[4].offset = -db_curve[4].db + db_curve[3].offset;
}
/** Now set up the comp_curve table with compression offsets in the
form of gain factors in S7.24 format */
/* comp_curve[0] is 0 (-infinity db) input */
comp_curve[0] = UNITY;
/* comp_curve[1 to 63] are intermediate compression values
corresponding to the 6 MSB of the input values of a non-clipped
signal */
for (int i = 1; i < 64; i++)
{
/* db constants are stored as positive numbers;
make them negative here */
int32_t this_db = -db[i];
/* no compression below the knee */
if (this_db <= db_curve[0].db)
comp_curve[i] = UNITY;
/* if soft knee and below top of knee,
interpolate along soft knee slope */
else if (soft_knee && (this_db <= db_curve[2].db))
comp_curve[i] = fp_factor(fp_mul(
((this_db - db_curve[0].db) / 6),
db_curve[2].offset, 16), 16) << 8;
/* interpolate along ratio slope above the knee */
else
comp_curve[i] = fp_factor(fp_mul(
fp_div((db_curve[1].db - this_db), db_curve[1].db, 16),
db_curve[3].offset, 16), 16) << 8;
}
/* comp_curve[64] is the compression level of a maximum level,
non-clipped signal */
comp_curve[64] = fp_factor(db_curve[3].offset, 16) << 8;
/* comp_curve[65] is the compression level of a maximum level,
clipped signal */
comp_curve[65] = fp_factor(db_curve[4].offset, 16) << 8;
#if defined(ROCKBOX_HAS_LOGF) && defined(LOGF_ENABLE)
logf("\n *** Compression Offsets ***");
/* some settings for display only, not used in calculations */
db_curve[0].offset = 0;
db_curve[1].offset = 0;
db_curve[3].db = 0;
for (int i = 0; i <= 4; i++)
{
logf("Curve[%d]: db: % 6.2f\toffset: % 6.2f", i,
(float)db_curve[i].db / (1 << 16),
(float)db_curve[i].offset / (1 << 16));
}
logf("\nGain factors:");
for (int i = 1; i <= 65; i++)
{
debugf("%02d: %.6f ", i, (float)comp_curve[i] / UNITY);
if (i % 4 == 0) debugf("\n");
}
debugf("\n");
#endif
/* if using auto peak, then makeup gain is max offset -
.1dB headroom */
comp_makeup_gain = auto_gain ?
fp_factor(-(db_curve[3].offset) - 0x199A, 16) << 8 : UNITY;
logf("Makeup gain:\t%.6f", (float)comp_makeup_gain / UNITY);
/* calculate per-sample gain change a rate of 10db over release time
*/
comp_rel_slope = 0xAF0BB2 / release;
logf("Release slope:\t%.6f", (float)comp_rel_slope / UNITY);
release_gain = UNITY;
return active;
}

View File

@ -23,7 +23,7 @@
#define COMPRESSOR_H
void compressor_process(int count, struct dsp_data *data, int32_t *buf[]);
bool compressor_update(void);
bool compressor_update(const struct compressor_settings *settings);
void compressor_reset(void);
#endif /* COMPRESSOR_H */

View File

@ -1565,9 +1565,9 @@ void dsp_set_replaygain(void)
/** SET COMPRESSOR
* Called by the menu system to configure the compressor process */
void dsp_set_compressor(void)
void dsp_set_compressor(const struct compressor_settings *settings)
{
/* enable/disable the compressor */
AUDIO_DSP.compressor_process = compressor_update() ?
AUDIO_DSP.compressor_process = compressor_update(settings) ?
compressor_process : NULL;
}

View File

@ -120,6 +120,7 @@ int32_t sound_get_pitch(void);
void dsp_set_timestretch(int32_t percent);
int32_t dsp_get_timestretch(void);
int dsp_callback(int msg, intptr_t param);
void dsp_set_compressor(void);
struct compressor_settings;
void dsp_set_compressor(const struct compressor_settings *settings);
#endif