summaryrefslogtreecommitdiffstats
path: root/apps/dsp.c
diff options
context:
space:
mode:
authorJeffrey Goode <jeffg7@gmail.com>2009-09-25 15:46:38 +0000
committerJeffrey Goode <jeffg7@gmail.com>2009-09-25 15:46:38 +0000
commitcf19ba5599b1cba212705ddb22166acf25eca83c (patch)
tree99e66ab4a5526ec7c36521c19179d96e42d7c3ae /apps/dsp.c
parentb9a17dd0ceb1edc3e8d08522f82f9dfeabe53a53 (diff)
downloadrockbox-cf19ba5599b1cba212705ddb22166acf25eca83c.tar.gz
rockbox-cf19ba5599b1cba212705ddb22166acf25eca83c.zip
Replace limiter with dynamic range compressor
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22832 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/dsp.c')
-rw-r--r--apps/dsp.c638
1 files changed, 236 insertions, 402 deletions
diff --git a/apps/dsp.c b/apps/dsp.c
index e7a6a9182a..a5ceecb048 100644
--- a/apps/dsp.c
+++ b/apps/dsp.c
@@ -137,6 +137,15 @@ struct eq_state
/* 10ch */
};
+struct compressor_menu
+{
+ int threshold; /* dB - from menu */
+ int ratio; /* from menu */
+ int gain; /* dB - from menu */
+ bool soft_knee; /* 0 = hard knee, 1 = soft knee */
+ int release; /* samples - from menu */
+};
+
/* Include header with defines which functions are implemented in assembly
code for the target */
#include <dsp_asm.h>
@@ -171,7 +180,6 @@ struct dsp_config
int32_t tdspeed_percent; /* Speed% * PITCH_SPEED_PRECISION */
bool tdspeed_active; /* Timestretch is in use */
int frac_bits;
- long limiter_preamp; /* limiter amp gain in S7.24 format */
#ifdef HAVE_SW_TONE_CONTROLS
/* Filter struct for software bass/treble controls */
struct eqfilter tone_filter;
@@ -187,7 +195,7 @@ struct dsp_config
channels_process_fn_type apply_crossfeed;
channels_process_fn_type eq_process;
channels_process_fn_type channels_process;
- return_fn_type limiter_process;
+ return_fn_type compressor_process;
};
/* General DSP config */
@@ -253,58 +261,17 @@ static int32_t *resample_buf;
#define RESAMPLE_BUF_LEFT_CHANNEL 0
#define RESAMPLE_BUF_RIGHT_CHANNEL (sample_buf_count/2 * RESAMPLE_RATIO)
-/* limiter */
-/* MAX_COUNT is largest possible sample count in limiter_process. This is
- needed in case time stretch makes the count in dsp_process larger than
- the limiter buffer. */
-#define MAX_COUNT MAX(SMALL_SAMPLE_BUF_COUNT * RESAMPLE_RATIO / 2, LIMITER_BUFFER_SIZE)
-static int count_adjust;
-static bool limiter_buffer_active;
-static bool limiter_buffer_full;
-static bool limiter_buffer_emptying;
-static int32_t limiter_buffer[2][LIMITER_BUFFER_SIZE] IBSS_ATTR;
-static int32_t *start_lim_buf[2] IBSS_ATTR,
- *end_lim_buf[2] IBSS_ATTR;
-static uint16_t lim_buf_peak[LIMITER_BUFFER_SIZE] IBSS_ATTR;
-static uint16_t *start_peak IBSS_ATTR,
- *end_peak IBSS_ATTR;
-static uint16_t out_buf_peak[MAX_COUNT];
-static uint16_t *out_buf_peak_index IBSS_ATTR;
-static uint16_t release_peak IBSS_ATTR;
-static int32_t in_samp IBSS_ATTR,
- samp0 IBSS_ATTR;
-
-static void reset_limiter_buffer(struct dsp_config *dsp);
-static int limiter_buffer_count(bool buf_count);
-static int limiter_process(int count, int32_t *buf[]);
-static uint16_t get_peak_value(int32_t sample);
-
- /* The clip_steps array essentially stores the results of fp_factor from
- * 0 to 12 dB, in 48 equal steps, in S3.28 format. */
-const long clip_steps[49] ICONST_ATTR = { 0x10000000,
- 0x10779AFA, 0x10F2B409, 0x1171654C, 0x11F3C9A0, 0x1279FCAD,
- 0x13041AE9, 0x139241A2, 0x14248EF9, 0x14BB21F9, 0x15561A92,
- 0x15F599A0, 0x1699C0F9, 0x1742B36B, 0x17F094CE, 0x18A38A01,
- 0x195BB8F9, 0x1A1948C5, 0x1ADC619B, 0x1BA52CDC, 0x1C73D51D,
- 0x1D488632, 0x1E236D3A, 0x1F04B8A1, 0x1FEC982C, 0x20DB3D0E,
- 0x21D0D9E2, 0x22CDA2BE, 0x23D1CD41, 0x24DD9099, 0x25F12590,
- 0x270CC693, 0x2830AFD3, 0x295D1F37, 0x2A925471, 0x2BD0911F,
- 0x2D1818B3, 0x2E6930AD, 0x2FC42095, 0x312931EC, 0x3298B072,
- 0x3412EA24, 0x35982F3A, 0x3728D22E, 0x38C52808, 0x3A6D8847,
- 0x3C224CD9, 0x3DE3D264, 0x3FB2783F};
-/* The gain_steps array essentially stores the results of fp_factor from
- * 0 to -12 dB, in 48 equal steps, in S3.28 format. */
-const long gain_steps[49] ICONST_ATTR = { 0x10000000,
- 0xF8BC9C0, 0xF1ADF94, 0xEAD2988, 0xE429058, 0xDDAFD68,
- 0xD765AC1, 0xD149309, 0xCB59186, 0xC594210, 0xBFF9112,
- 0xBA86B88, 0xB53BEF5, 0xB017965, 0xAB18964, 0xA63DDFE,
- 0xA1866BA, 0x9CF1397, 0x987D507, 0x9429BEE, 0x8FF599E,
- 0x8BDFFD3, 0x87E80B0, 0x840CEBE, 0x804DCE8, 0x7CA9E76,
- 0x792070E, 0x75B0AB0, 0x7259DB2, 0x6F1B4BF, 0x6BF44D5,
- 0x68E4342, 0x65EA5A0, 0x63061D6, 0x6036E15, 0x5D7C0D3,
- 0x5AD50CE, 0x5841505, 0x55C04B8, 0x535176A, 0x50F44D9,
- 0x4EA84FE, 0x4C6D00E, 0x4A41E78, 0x48268DF, 0x461A81C,
- 0x441D53E, 0x422E985, 0x404DE62};
+/* compressor */
+/* MAX_COUNT is largest possible sample count in compressor_process */
+#define MAX_COUNT (SMALL_SAMPLE_BUF_COUNT * RESAMPLE_RATIO / 2)
+static struct compressor_menu c_menu;
+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[65] IBSS_ATTR; /* S7.24 format */
+static int32_t gain_buffer[MAX_COUNT] IBSS_ATTR;
+static int32_t release_gain IBSS_ATTR;
+
+static int compressor_process(int count, int32_t *buf[]);
/* Clip sample to signed 16 bit range */
@@ -944,13 +911,6 @@ static void set_gain(struct dsp_config *dsp)
dsp->data.gain = fp_mul(dsp->data.gain, eq_precut, 24);
}
- /* only preamp for the limiter if limiter is active and sample depth
- * allows safe pre-amping (12 dB is OK with 29 or less frac bits) */
- if ((dsp->limiter_preamp) && (dsp->frac_bits <= 29))
- {
- dsp->data.gain = fp_mul(dsp->data.gain, dsp->limiter_preamp, 24);
- }
-
#ifdef HAVE_SW_VOLUME_CONTROL
if (global_settings.volume < SW_VOLUME_MAX ||
global_settings.volume > SW_VOLUME_MIN)
@@ -1308,8 +1268,8 @@ int dsp_process(struct dsp_config *dsp, char *dst, const char *src[], int count)
if (dsp->channels_process)
dsp->channels_process(chunk, t2);
- if (dsp->limiter_process)
- chunk = dsp->limiter_process(chunk, t2);
+ if (dsp->compressor_process)
+ chunk = dsp->compressor_process(chunk, t2);
dsp->output_samples(chunk, &dsp->data, (const int32_t **)t2, (int16_t *)dst);
@@ -1358,15 +1318,6 @@ int dsp_output_count(struct dsp_config *dsp, int count)
if (count > RESAMPLE_BUF_RIGHT_CHANNEL)
count = RESAMPLE_BUF_RIGHT_CHANNEL;
- /* If the limiter buffer is filling, some or all samples will
- * be captured by it, so expect fewer samples coming out. */
- if (limiter_buffer_active && !limiter_buffer_full)
- {
- int empty_space = limiter_buffer_count(false);
- count_adjust = MIN(empty_space, count);
- count -= count_adjust;
- }
-
return count;
}
@@ -1375,13 +1326,6 @@ int dsp_output_count(struct dsp_config *dsp, int count)
*/
int dsp_input_count(struct dsp_config *dsp, int count)
{
- /* If the limiter buffer is filling, the output count was
- * adjusted downward. This adjusts it back so that input
- * count is not affected.
- */
- if (limiter_buffer_active && !limiter_buffer_full)
- count += count_adjust;
-
/* count is now the number of resampled input samples. Convert to
original input samples. */
if (dsp->resample)
@@ -1499,7 +1443,8 @@ intptr_t dsp_configure(struct dsp_config *dsp, int setting, intptr_t value)
dsp_update_functions(dsp);
resampler_new_delta(dsp);
tdspeed_setup(dsp);
- reset_limiter_buffer(dsp);
+ if (dsp == &AUDIO_DSP)
+ release_gain = (1 << 24);
break;
case DSP_FLUSH:
@@ -1508,7 +1453,8 @@ intptr_t dsp_configure(struct dsp_config *dsp, int setting, intptr_t value)
resampler_new_delta(dsp);
dither_init(dsp);
tdspeed_setup(dsp);
- reset_limiter_buffer(dsp);
+ if (dsp == &AUDIO_DSP)
+ release_gain = (1 << 24);
break;
case DSP_SET_TRACK_GAIN:
@@ -1588,369 +1534,257 @@ void dsp_set_replaygain(void)
set_gain(&AUDIO_DSP);
}
-/** RESET THE LIMITER BUFFER
- * Force the limiter buffer to its initial state and discard
- * any samples held there. */
-static void reset_limiter_buffer(struct dsp_config *dsp)
-{
- if (dsp == &AUDIO_DSP)
+/** SET COMPRESSOR
+ * Called by the menu system to configure the compressor process */
+void dsp_set_compressor(int c_threshold, int c_ratio, int c_gain,
+ int c_knee, int c_release)
+{
+ bool changed = false;
+ bool active = (c_threshold < 0);
+ const int comp_ratio[] = {2, 4, 6, 10, 0};
+ int new_ratio = comp_ratio[c_ratio];
+ bool new_knee = (c_knee == 1);
+ int new_release = c_release * NATIVE_FREQUENCY / 1000;
+
+ if (c_menu.threshold != c_threshold)
{
- int i;
- logf(" reset_limiter_buffer");
- for (i = 0; i < 2; i++)
- start_lim_buf[i] = end_lim_buf[i] = limiter_buffer[i];
- start_peak = end_peak = lim_buf_peak;
- limiter_buffer_full = false;
- limiter_buffer_emptying = false;
- release_peak = 0;
+ changed = true;
+ c_menu.threshold = c_threshold;
+ logf(" Compressor Threshold: %d dB\tEnabled: %s",
+ c_menu.threshold, active ? "Yes" : "No");
}
-}
-/** OPERATE THE LIMITER BUFFER
- * Handle all samples entering or exiting the limiter buffer. */
-static inline int set_limiter_buffer(int count, int32_t *buf[])
-{
- int32_t *in_buf[] = {buf[0], buf[1]},
- *out_buf[] = {buf[0], buf[1]};
- int empty_space, i, out_count;
- const long clip_max = AUDIO_DSP.data.clip_max;
- const int ch = AUDIO_DSP.data.num_channels - 1;
- out_buf_peak_index = out_buf_peak;
-
- if (limiter_buffer_emptying)
- /** EMPTY THE BUFFER
- * since the empty flag has been set, assume no inbound samples and
- return all samples in the limiter buffer to the outbound buffer */
+ if (c_menu.ratio != new_ratio)
{
- count = limiter_buffer_count(true);
- out_count = count;
- logf(" Emptying limiter buffer: %d", count);
- while (count-- > 0)
- {
- for (i = 0; i <= ch; i++)
- {
- /* move samples in limiter buffer to output buffer */
- *out_buf[i]++ = *start_lim_buf[i]++;
- if (start_lim_buf[i] == &limiter_buffer[i][LIMITER_BUFFER_SIZE])
- start_lim_buf[i] = limiter_buffer[i];
- /* move limiter buffer peak values to output peak values */
- if (i == 0)
- {
- *out_buf_peak_index++ = *start_peak++;
- if (start_peak == &lim_buf_peak[LIMITER_BUFFER_SIZE])
- start_peak = lim_buf_peak;
- }
- }
- }
- limiter_buffer_full = false;
- limiter_buffer_emptying = false;
+ changed = true;
+ c_menu.ratio = new_ratio;
+ if (c_menu.ratio)
+ logf(" Compressor Ratio: %d:1", c_menu.ratio);
+ else
+ logf(" Compressor Ratio: Limit");
}
- else /* limiter buffer NOT emptying */
+
+ if (c_menu.gain != c_gain)
+ {
+ changed = true;
+ c_menu.gain = c_gain;
+ if (c_menu.gain >= 0)
+ logf(" Compressor Makeup Gain: %d dB", c_menu.gain);
+ else
+ logf(" Compressor Makeup Gain: Auto");
+ }
+
+ if (c_menu.soft_knee != new_knee)
{
- if (count <= 0) return 0;
+ changed = true;
+ c_menu.soft_knee = new_knee;
+ logf(" Compressor Knee: %s", c_menu.soft_knee==1?"Soft":"Hard");
+ }
+
+ if (c_menu.release != new_release)
+ {
+ changed = true;
+ c_menu.release = new_release;
+ logf(" Compressor Release: %d", c_menu.release);
+ }
+
+ if (changed && active)
+ {
+ /* configure variables for compressor operation */
+ int i;
+ const int32_t db[] ={0x000000, /* positive db equivalents in S15.16 format */
+ 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};
- empty_space = limiter_buffer_count(false);
+ struct curve_point
+ {
+ int32_t db; /* S15.16 format */
+ int32_t offset; /* S15.16 format */
+ } db_curve[4];
- if (empty_space > 0)
- /** FILL BUFFER
- * use as many inbound samples as necessary to fill the buffer */
+ /** 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 */
+ db_curve[1].db = c_menu.threshold << 16;
+ db_curve[1].offset = 0;
+ if (c_menu.soft_knee)
{
- /* don't try to fill with more samples than available */
- if (empty_space > count)
- empty_space = count;
- logf(" Filling limiter buffer: %d", empty_space);
- while (empty_space-- > 0)
- {
- for (i = 0; i <= ch; i++)
- {
- /* put inbound samples in the limiter buffer */
- in_samp = *in_buf[i]++;
- *end_lim_buf[i]++ = in_samp;
- if (end_lim_buf[i] == &limiter_buffer[i][LIMITER_BUFFER_SIZE])
- end_lim_buf[i] = limiter_buffer[i];
- if (in_samp < 0) /* make positive for comparison */
- in_samp = -in_samp - 1;
- if (in_samp <= clip_max)
- in_samp = 0; /* disregard if not clipped */
- if (i == 0)
- samp0 = in_samp;
- if (i == ch)
- {
- /* assign peak value for each inbound sample pair */
- *end_peak++ = ((samp0 > 0) || (in_samp > 0)) ?
- get_peak_value(MAX(samp0, in_samp)) : 0;
- if (end_peak == &lim_buf_peak[LIMITER_BUFFER_SIZE])
- end_peak = lim_buf_peak;
- }
- }
- count--;
- }
- /* after buffer fills, the remaining inbound samples are cycled */
+ /* bottom of knee is 3dB below the threshold for soft knee*/
+ db_curve[0].db = db_curve[1].db - (3 << 16);
+ db_curve[0].offset = 0;
+ /* top of knee is 3dB above the threshold for soft knee */
+ db_curve[2].db = db_curve[1].db + (3 << 16);
+ if (c_menu.ratio)
+ /* offset = -3db * (ratio - 1) / ratio */
+ db_curve[2].offset = (int32_t)((long long)(-3 << 16)
+ * (c_menu.ratio - 1) / c_menu.ratio);
+ else
+ /* offset = -3db for hard limit */
+ db_curve[2].offset = (-3 << 16);
}
-
- limiter_buffer_full = (end_lim_buf[0] == start_lim_buf[0]);
- out_count = count;
-
- /** CYCLE BUFFER
- * return buffered samples and backfill limiter buffer with new ones.
- * The buffer is always full when cycling. */
- while (count-- > 0)
+ else
{
- for (i = 0; i <= ch; i++)
- {
- /* copy incoming sample */
- in_samp = *in_buf[i]++;
- /* put limiter buffer sample into outbound buffer */
- *out_buf[i]++ = *start_lim_buf[i]++;
- /* put incoming sample on the end of the limiter buffer */
- *end_lim_buf[i]++ = in_samp;
- /* ring buffer pointer wrap */
- if (start_lim_buf[i] == &limiter_buffer[i][LIMITER_BUFFER_SIZE])
- start_lim_buf[i] = limiter_buffer[i];
- if (end_lim_buf[i] == &limiter_buffer[i][LIMITER_BUFFER_SIZE])
- end_lim_buf[i] = limiter_buffer[i];
- if (in_samp < 0) /* make positive for comparison */
- in_samp = -in_samp - 1;
- if (in_samp <= clip_max)
- in_samp = 0; /* disregard if not clipped */
- if (i == 0)
- {
- samp0 = in_samp;
- /* assign outgoing sample its associated peak value */
- *out_buf_peak_index++ = *start_peak++;
- if (start_peak == &lim_buf_peak[LIMITER_BUFFER_SIZE])
- start_peak = lim_buf_peak;
- }
- if (i == ch)
- {
- /* assign peak value for each inbound sample pair */
- *end_peak++ = ((samp0 > 0) || (in_samp > 0)) ?
- get_peak_value(MAX(samp0, in_samp)) : 0;
- if (end_peak == &lim_buf_peak[LIMITER_BUFFER_SIZE])
- end_peak = lim_buf_peak;
- }
- }
+ /* bottom of knee is at the threshold for hard knee */
+ db_curve[0].db = c_menu.threshold << 16;
+ db_curve[0].offset = 0;
+ /* top of knee is at the threshold for hard knee */
+ db_curve[2].db = c_menu.threshold << 16;
+ db_curve[2].offset = 0;
}
- }
-
- return out_count;
-}
-
-/** RETURN LIMITER BUFFER COUNT
- * If argument is true, returns number of samples in the buffer,
- * otherwise, returns empty space remaining */
-static int limiter_buffer_count(bool buf_count)
-{
- int count;
- if (limiter_buffer_full)
- count = LIMITER_BUFFER_SIZE;
- else if (end_lim_buf[0] >= start_lim_buf[0])
- count = (end_lim_buf[0] - start_lim_buf[0]);
- else
- count = (end_lim_buf[0] - start_lim_buf[0]) + LIMITER_BUFFER_SIZE;
- return buf_count ? count : (LIMITER_BUFFER_SIZE - count);
-}
-
-/** FLUSH THE LIMITER BUFFER
- * Empties the limiter buffer into the buffer pointed to by the argument
- * and returns the number of samples in that buffer */
-int dsp_flush_limiter_buffer(char *dest)
-{
- if ((!limiter_buffer_active) || (limiter_buffer_count(true) <= 0))
- return 0;
-
- logf(" dsp_flush_limiter_buffer");
- int32_t flush_buf[2][LIMITER_BUFFER_SIZE];
- int32_t *src[2] = {flush_buf[0], flush_buf[1]};
-
- limiter_buffer_emptying = true;
- int count = limiter_process(0, src);
- AUDIO_DSP.output_samples(count, &AUDIO_DSP.data,
- (const int32_t **)src, (int16_t *)dest);
- return count;
-}
-
-/** GET PEAK VALUE
- * Return a small value representing how much the sample is clipped. This
- * should only be called if a sample is actually clipped. Sample is a
- * positive value.
- */
-static uint16_t get_peak_value(int32_t sample)
-{
- const int frac_bits = AUDIO_DSP.frac_bits;
- int mid,
- hi = 48,
- lo = 0;
-
- /* shift sample into 28 frac bit range for comparison */
- if (frac_bits > 28)
- sample >>= (frac_bits - 28);
- if (frac_bits < 28)
- sample <<= (28 - frac_bits);
-
- /* if clipped out of range, return maximum value */
- if (sample >= clip_steps[48])
- return 48 * 90;
-
- /* find amount of sample clipping on the table */
- do
- {
- mid = (hi + lo) / 2;
- if (sample < clip_steps[mid])
- hi = mid;
- else if (sample > clip_steps[mid])
- lo = mid;
+ /* 0db input is also max offset point (most compression) */
+ db_curve[3].db = 0;
+ if (c_menu.ratio)
+ /* offset = threshold * (ratio - 1) / ratio */
+ db_curve[3].offset = (int32_t)((long long)(c_menu.threshold << 16)
+ * (c_menu.ratio - 1) / c_menu.ratio);
else
- return mid * 90;
- }
- while (hi > (lo + 1));
-
- /* interpolate linearly between steps (less accurate but faster) */
- return ((hi-1) * 90) + (((sample - clip_steps[hi-1]) * 90) /
- (clip_steps[hi] - clip_steps[hi-1]));
-}
-
-/** SET LIMITER
- * Called by the menu system to configure the limiter process */
-void dsp_set_limiter(int limiter_level)
-{
- if (limiter_level > 0)
- {
- if (!limiter_buffer_active)
+ /* offset = threshold for hard limit */
+ db_curve[3].offset = (c_menu.threshold << 16);
+
+ /* Now set up the comp_curve table with compression offsets in the form
+ of gain factors in S7.24 format */
+ comp_curve[0] = (1 << 24);
+ for (i = 1; i < 64; i++)
{
- /* enable limiter process */
- AUDIO_DSP.limiter_process = limiter_process;
- limiter_buffer_active = true;
+ int32_t this_db = -db[i];
+ /* no compression below the knee */
+ if (this_db <= db_curve[0].db)
+ comp_curve[i] = (1 << 24);
+
+ /* if soft knee and below top of knee, interpolate along soft knee slope */
+ else if (c_menu.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((this_db - db_curve[1].db),
+ -db_curve[1].db, 16), db_curve[3].offset, 16), 16) << 8;
}
- /* limiter preamp is a gain factor in S7.24 format */
- long old_preamp = AUDIO_DSP.limiter_preamp;
- long new_preamp = fp_factor((((long)limiter_level << 24) / 10), 24);
- if (old_preamp != new_preamp)
+ comp_curve[64] = fp_factor(db_curve[3].offset, 16) << 8;
+
+ logf("\n *** Compression Offsets ***");
+ for (i = 0; i <= 3; i++)
{
- AUDIO_DSP.limiter_preamp = new_preamp;
- set_gain(&AUDIO_DSP);
- logf(" Limiter enable: Yes\tLimiter amp: %.8f",
- (float)AUDIO_DSP.limiter_preamp / (1 << 24));
+ logf("Curve[%d]: db: % .1f\toffset: % .4f", i, (float)db_curve[i].db / (1 << 16),
+ (float)db_curve[i].offset / (1 << 16));
}
- }
- else
- {
- /* disable limiter process*/
- if (limiter_buffer_active)
+
+ logf("\nGain factors:");
+ for (i = 1; i <= 64; i++)
{
- AUDIO_DSP.limiter_preamp = (1 << 24);
- set_gain(&AUDIO_DSP);
- /* pcmbuf_flush_limiter_buffer(); */
- limiter_buffer_active = false;
- AUDIO_DSP.limiter_process = NULL;
- reset_limiter_buffer(&AUDIO_DSP);
- logf(" Limiter enable: No\tLimiter amp: %.8f",
- (float)AUDIO_DSP.limiter_preamp / (1 << 24));
+ debugf("%02d: %.6f ", i, (float)comp_curve[i] / (1 << 24));
+ if (i % 4 == 0) debugf("\n");
}
+
+ /* if using auto peak, then makeup gain is max offset - .1dB headroom */
+ int32_t db_makeup = (c_menu.gain == -1) ?
+ -(db_curve[3].offset) - 0x199A : c_menu.gain << 16;
+ comp_makeup_gain = fp_factor(db_makeup, 16) << 8;
+ logf("Makeup gain:\t%.6f", (float)comp_makeup_gain / (1 << 24));
+
+ /* calculate per-sample gain change a rate of 10db over release time */
+ comp_rel_slope = 0xAF0BB2 / c_menu.release;
+ logf("Release slope:\t%.6f", (float)comp_rel_slope / (1 << 24));
+
+ release_gain = (1 << 24);
}
+
+ /* enable/disable the compressor */
+ AUDIO_DSP.compressor_process = active ? compressor_process : NULL;
}
-/** LIMITER PROCESS
- * Checks pre-amplified signal for clipped samples and smoothly reduces gain
- * around the clipped samples using a preset attack/release schedule.
+/** GET COMPRESSION GAIN
+ * Returns the required gain factor in S7.24 format in order to compress the
+ * sample in accordance with the compression curve. Always 1 or less.
*/
-static int limiter_process(int count, int32_t *buf[])
+static inline int32_t get_compression_gain(int32_t sample)
{
- /* Limiter process passes through if limiter buffer isn't active, or the
- * sample depth is too large for safe pre-amping */
- if ((!limiter_buffer_active) || (AUDIO_DSP.frac_bits > 29))
- return count;
+ const int frac_bits = AUDIO_DSP.frac_bits;
- count = set_limiter_buffer(count, buf);
+ /* sample must be positive */
+ if (sample < 0)
+ sample = -sample - 1;
+
+ /* shift sample into 22 frac bit range */
+ if (frac_bits > 22)
+ sample >>= (frac_bits - 22);
+ if (frac_bits < 22)
+ sample <<= (22 - frac_bits);
- if (count <= 0)
- return 0;
+ /* index is 6 MSB, rem is 16 LSB */
+ int index = sample >> 16;
+ int rem = (sample & 0xFFFF) << 8;
- const int attack_slope = 15; /* 15:1 ratio between attack and release */
- const int buffer_count = limiter_buffer_count(true);
+ /* interpolate from the compression curve */
+ return comp_curve[index] + (int32_t)FRACMUL_SHL((comp_curve[index + 1]
+ - comp_curve[index]), rem, 7);
+}
+
+/** COMPRESSOR PROCESS
+ * Changes the gain of the samples according to the compressor curve
+ */
+static int compressor_process(int count, int32_t *buf[])
+{
+ const int num_chan = AUDIO_DSP.data.num_channels;
+ const int32_t fp_one = (1 << 24);
+ int32_t sample_gain, /* S7.24 format */
+ this_gain; /* S7.24 format */
int i, ch;
- uint16_t max_peak = 0,
- gain_peak,
- gain_rem;
- long gain;
- /* step through limiter buffer in reverse order, in order to find the
- * appropriate max_peak for modifying the output buffer */
- for (i = buffer_count - 1; i >= 0; i--)
- {
- const uint16_t peak_i = lim_buf_peak[(start_peak - lim_buf_peak + i) %
- LIMITER_BUFFER_SIZE];
- /* if no attack slope, nothing to do */
- if ((peak_i == 0) && (max_peak == 0)) continue;
- /* if new peak, start attack slope */
- if (peak_i >= max_peak)
- {
- max_peak = peak_i;
- }
- /* keep sloping */
- else
- {
- if (max_peak > attack_slope)
- max_peak -= attack_slope;
- else
- max_peak = 0;
- }
- }
- /* step through output buffer the same way, but this time modifying peak
- * values to create a smooth attack slope. */
- for (i = count - 1; i >= 0; i--)
- {
- /* if no attack slope, nothing to do */
- if ((out_buf_peak[i] == 0) && (max_peak == 0)) continue;
- /* if new peak, start attack slope */
- if (out_buf_peak[i] >= max_peak)
- {
- max_peak = out_buf_peak[i];
- }
- /* keep sloping */
- else
- {
- if (max_peak > attack_slope)
- max_peak -= attack_slope;
- else
- max_peak = 0;
- out_buf_peak[i] = max_peak;
- }
- }
- /* Now step forward through the output buffer, and modify the peak values
+ /* Step forward through the output buffer, and modify the offset values
* to establish a smooth, slow release slope.*/
for (i = 0; i < count; i++)
{
- /* if no release slope, nothing to do */
- if ((out_buf_peak[i] == 0) && (release_peak == 0)) continue;
- /* if new peak, start release slope */
- if (out_buf_peak[i] >= release_peak)
+ sample_gain = fp_one;
+ for (ch = 0; ch < num_chan; ch++)
{
- release_peak = out_buf_peak[i];
+ this_gain = get_compression_gain(buf[ch][i]);
+ if (this_gain < sample_gain)
+ sample_gain = this_gain;
}
- /* keep sloping */
+ /* if no release slope, only apply makeup gain */
+ if ((sample_gain == fp_one) && (release_gain == fp_one))
+ gain_buffer[i] = comp_makeup_gain;
else
{
- release_peak--;
- out_buf_peak[i] = release_peak;
+ /* if larger offset, start release slope */
+ if (sample_gain <= release_gain)
+ release_gain = sample_gain;
+ else /* keep sloping */
+ {
+ if (release_gain < (fp_one - comp_rel_slope))
+ release_gain += comp_rel_slope;
+ else
+ release_gain = fp_one;
+ }
+ /* store offset with release and also apply makeup gain */
+ if ((release_gain == fp_one) && (comp_makeup_gain == fp_one))
+ gain_buffer[i] = fp_one;
+ else
+ gain_buffer[i] = FRACMUL_SHL(release_gain, comp_makeup_gain, 7);
}
}
- /* Implement the limiter: adjust gain of the outbound samples by the gain
- * amounts in the gain steps array corresponding to the peak values. */
+
+ /* Implement the compressor: apply those gain factors to the output
+ * buffer samples */
+
for (i = 0; i < count; i++)
{
- if (out_buf_peak[i] > 0)
+ if (gain_buffer[i] != fp_one)
{
- gain_peak = (out_buf_peak[i] + 1) / 90;
- gain_rem = (out_buf_peak[i] + 1) % 90;
- gain = gain_steps[gain_peak];
- if ((gain_peak < 48) && (gain_rem > 0))
- gain -= gain_rem * ((gain_steps[gain_peak] -
- gain_steps[gain_peak + 1]) / 90);
- for (ch = 0; ch < AUDIO_DSP.data.num_channels; ch++)
- buf[ch][i] = FRACMUL_SHL(buf[ch][i], gain, 3);
+ for (ch = 0; ch < num_chan; ch++)
+ buf[ch][i] = FRACMUL_SHL(buf[ch][i], gain_buffer[i], 7);
}
}
return count;
-}
+}