2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file compress.c Paraslash's dynamic audio range compressor. */
10 * Uses ideas of AudioCompress, (C) 2002-2004 M. Hari Nezumi <magenta@trikuare.cx>
14 #include "compress_filter.cmdline.h"
21 /** The size of the output data buffer. */
22 #define COMPRESS_CHUNK_SIZE 40960
24 extern char *stat_item_values[NUM_STAT_ITEMS];
26 /** Data specific to the compress filter. */
27 struct private_compress_data {
28 /** The current multiplier. */
29 unsigned current_gain;
30 /** Points to the configuration data for this instance of the compress filter. */
31 struct compress_filter_args_info *conf;
32 /** Maximal admissible gain. */
34 /** Number of samples already seen. */
36 /** Absolute value of the maximal sample in the current block. */
40 static ssize_t compress(char *inbuf, size_t inbuf_len, struct filter_node *fn)
42 size_t i, length = PARA_MIN((inbuf_len / 2) * 2,
43 (fn->bufsize - fn->loaded) / 2 * 2);
44 struct private_compress_data *pcd = fn->private_data;
45 int16_t *ip = (int16_t *)inbuf, *op = (int16_t *)(fn->buf + fn->loaded);
46 unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
47 mask = (1 << pcd->conf->blocksize_arg) - 1;
51 for (i = 0; i < length / 2; i++) {
52 /* be careful in that heat, my dear */
53 int sample = *ip++, adjusted_sample = (PARA_ABS(sample) *
54 pcd->current_gain) >> gain_shift;
55 if (unlikely(adjusted_sample > 32767)) { /* clip */
56 PARA_NOTICE_LOG("clip: sample: %d, adjusted sample: %d\n",
57 sample, adjusted_sample);
58 adjusted_sample = 32767;
59 pcd->current_gain = (3 * pcd->current_gain +
60 (1 << pcd->conf->inertia_arg)) / 4;
63 pcd->peak = PARA_MAX(pcd->peak, adjusted_sample);
64 *op++ = sample >= 0? adjusted_sample : -adjusted_sample;
65 if (likely(++pcd->num_samples & mask))
67 // PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
69 if (pcd->peak < pcd->conf->target_level_arg) {
70 if (pcd->current_gain < pcd->max_gain)
73 pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
74 1 << pcd->conf->inertia_arg);
81 static void close_compress(struct filter_node *fn)
83 free(fn->private_data);
87 /** TODO: Add sanity checks */
88 static int compress_parse_config(int argc, char **argv, void **config)
91 struct compress_filter_args_info *compress_conf
92 = para_calloc(sizeof(*compress_conf));
94 ret = -E_COMPRESS_SYNTAX;
95 if (compress_cmdline_parser(argc, argv, compress_conf))
97 *config = compress_conf;
104 static void open_compress(struct filter_node *fn)
106 struct private_compress_data *pcd = para_calloc(
107 sizeof(struct private_compress_data));
108 pcd->conf = fn->conf;
109 fn->private_data = pcd;
110 fn->bufsize = COMPRESS_CHUNK_SIZE;
111 fn->buf = para_malloc(fn->bufsize);
112 pcd->current_gain = 1 << pcd->conf->inertia_arg;
113 pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
117 * The init function of the compress filter.
119 * \param f Pointer to the struct to initialize.
121 void compress_filter_init(struct filter *f)
123 f->open = open_compress;
124 f->close = close_compress;
125 f->convert = compress;
126 f->print_help = compress_cmdline_parser_print_help;
127 f->parse_config = compress_parse_config;