2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file compress_filter.c Paraslash's dynamic audio range compressor. */
10 * Uses ideas of AudioCompress, (C) 2002-2004 M. Hari Nezumi <magenta@trikuare.cx>
17 #include "compress_filter.cmdline.h"
21 #include "buffer_tree.h"
26 /** The size of the output data buffer. */
27 #define COMPRESS_CHUNK_SIZE 40960
29 /** Data specific to the compress filter. */
30 struct private_compress_data {
31 /** The current multiplier. */
32 unsigned current_gain;
33 /** Points to the configuration data for this instance of the compress filter. */
34 struct compress_filter_args_info *conf;
35 /** Maximal admissible gain. */
37 /** Number of samples already seen. */
39 /** Absolute value of the maximal sample in the current block. */
43 static void close_compress(struct filter_node *fn)
45 free(fn->private_data);
49 static void compress_post_select(__a_unused struct sched *s, struct task *t)
51 struct filter_node *fn = container_of(t, struct filter_node, task);
52 struct private_compress_data *pcd = fn->private_data;
53 struct btr_node *btrn = fn->btrn;
54 bool inplace = btr_inplace_ok(btrn);
59 unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
60 mask = (1 << pcd->conf->blocksize_arg) - 1;
65 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
70 btr_merge(btrn, fn->min_iqs);
71 length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1;
72 if (length == 0) { /* eof and 1 byte available */
73 ret = -E_COMPRESS_EOF;
76 ip = (int16_t *)inbuf;
80 op = para_malloc(length);
81 for (i = 0; i < length / 2; i++) {
82 /* be careful in that heat, my dear */
83 int sample = *ip++, adjusted_sample = (PARA_ABS(sample) *
84 pcd->current_gain) >> gain_shift;
85 if (adjusted_sample > 32767) { /* clip */
86 PARA_NOTICE_LOG("clip: sample: %d, adjusted sample: %d\n",
87 sample, adjusted_sample);
88 adjusted_sample = 32767;
89 pcd->current_gain = (3 * pcd->current_gain +
90 (1 << pcd->conf->inertia_arg)) / 4;
93 pcd->peak = PARA_MAX(pcd->peak, adjusted_sample);
94 op[i] = sample >= 0? adjusted_sample : -adjusted_sample;
95 if (++pcd->num_samples & mask)
97 // PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
99 if (pcd->peak < pcd->conf->target_level_arg) {
100 if (pcd->current_gain < pcd->max_gain)
103 pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
104 1U << pcd->conf->inertia_arg);
108 btr_pushdown_one(btrn);
110 btr_consume(btrn, length);
111 btr_add_output((char *)op, length, btrn);
117 btr_remove_node(btrn);
120 /** TODO: Add sanity checks */
121 static int compress_parse_config(int argc, char **argv, void **config)
124 struct compress_filter_args_info *compress_conf
125 = para_calloc(sizeof(*compress_conf));
127 ret = -E_COMPRESS_SYNTAX;
128 if (compress_cmdline_parser(argc, argv, compress_conf))
130 *config = compress_conf;
137 static void open_compress(struct filter_node *fn)
139 struct private_compress_data *pcd = para_calloc(
140 sizeof(struct private_compress_data));
141 pcd->conf = fn->conf;
142 fn->private_data = pcd;
143 fn->min_iqs = 2; /* 16 bit audio */
144 fn->bufsize = COMPRESS_CHUNK_SIZE;
145 fn->buf = para_malloc(fn->bufsize);
146 pcd->current_gain = 1 << pcd->conf->inertia_arg;
147 pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
150 static void compress_free_config(void *conf)
152 compress_cmdline_parser_free(conf);
156 * The init function of the compress filter.
158 * \param f Pointer to the struct to initialize.
160 void compress_filter_init(struct filter *f)
162 struct compress_filter_args_info dummy;
164 compress_cmdline_parser_init(&dummy);
165 f->open = open_compress;
166 f->close = close_compress;
167 f->pre_select = generic_filter_pre_select;
168 f->post_select = compress_post_select;
169 f->parse_config = compress_parse_config;
170 f->free_config = compress_free_config;
171 f->help = (struct ggo_help) {
172 .short_help = compress_filter_args_info_help,
173 .detailed_help = compress_filter_args_info_detailed_help