2 * Copyright (C) 2005-2013 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>
16 #include "compress_filter.cmdline.h"
20 #include "buffer_tree.h"
25 /** Data specific to the compress filter. */
26 struct private_compress_data {
27 /** The current multiplier. */
28 unsigned current_gain;
29 /** Points to the configuration data for this instance of the compress filter. */
30 struct compress_filter_args_info *conf;
31 /** Maximal admissible gain. */
33 /** Number of samples already seen. */
35 /** Absolute value of the maximal sample in the current block. */
39 static void compress_close(struct filter_node *fn)
41 free(fn->private_data);
44 static void compress_post_select(__a_unused struct sched *s, struct task *t)
46 struct filter_node *fn = container_of(t, struct filter_node, task);
47 struct private_compress_data *pcd = fn->private_data;
48 struct btr_node *btrn = fn->btrn;
49 bool inplace = btr_inplace_ok(btrn);
54 unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
55 mask = (1 << pcd->conf->blocksize_arg) - 1;
60 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
65 btr_merge(btrn, fn->min_iqs);
66 length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1;
67 if (length == 0) { /* eof and 1 byte available */
68 ret = -E_COMPRESS_EOF;
71 ip = (int16_t *)inbuf;
75 op = para_malloc(length);
76 for (i = 0; i < length / 2; i++) {
77 /* be careful in that heat, my dear */
78 int sample = *ip++, adjusted_sample;
80 adjusted_sample = sample > 0? sample : -sample;
81 adjusted_sample *= pcd->current_gain;
82 adjusted_sample >>= gain_shift;
83 if (adjusted_sample > 32767) { /* clip */
84 PARA_NOTICE_LOG("clip: sample: %d, adjusted sample: %d\n",
85 sample, adjusted_sample);
86 adjusted_sample = 32767;
87 pcd->current_gain = (3 * pcd->current_gain +
88 (1 << pcd->conf->inertia_arg)) / 4;
90 } else if (adjusted_sample > pcd->peak)
91 pcd->peak = adjusted_sample;
92 op[i] = sample >= 0? adjusted_sample : -adjusted_sample;
93 if (++pcd->num_samples & mask)
95 // PARA_DEBUG_LOG("gain: %u, peak: %u\n", pcd->current_gain,
97 if (pcd->peak < pcd->conf->target_level_arg) {
98 if (pcd->current_gain < pcd->max_gain)
101 pcd->current_gain = PARA_MAX(pcd->current_gain - 2,
102 1U << pcd->conf->inertia_arg);
106 btr_pushdown_one(btrn);
108 btr_consume(btrn, length);
109 btr_add_output((char *)op, length, btrn);
115 btr_remove_node(&fn->btrn);
118 /** TODO: Add sanity checks */
119 static int compress_parse_config(int argc, char **argv, void **config)
121 struct compress_filter_args_info *conf = para_calloc(sizeof(*conf));
123 compress_filter_cmdline_parser(argc, argv, conf);
128 static void compress_open(struct filter_node *fn)
130 struct private_compress_data *pcd = para_calloc(
131 sizeof(struct private_compress_data));
132 pcd->conf = fn->conf;
133 fn->private_data = pcd;
134 fn->min_iqs = 2; /* 16 bit audio */
135 pcd->current_gain = 1 << pcd->conf->inertia_arg;
136 pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
139 static void compress_free_config(void *conf)
141 compress_filter_cmdline_parser_free(conf);
145 * The init function of the compress filter.
147 * \param f Pointer to the struct to initialize.
149 void compress_filter_init(struct filter *f)
151 struct compress_filter_args_info dummy;
153 compress_filter_cmdline_parser_init(&dummy);
154 f->open = compress_open;
155 f->close = compress_close;
156 f->pre_select = generic_filter_pre_select;
157 f->post_select = compress_post_select;
158 f->parse_config = compress_parse_config;
159 f->free_config = compress_free_config;
160 f->help = (struct ggo_help) {
161 .short_help = compress_filter_args_info_help,
162 .detailed_help = compress_filter_args_info_detailed_help