936ddc3afe2cf5449d03109aac87925818a3739b
[paraslash.git] / compress.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file compress.c paraslash's dynamic audio range compressor */
20
21 /*
22 * Used ideas of AudioCompress, (C) 2002-2004 M. Hari Nezumi <magenta@trikuare.cx>
23 */
24
25 #include "para.h"
26 #include "compress_filter.cmdline.h"
27 #include "list.h"
28 #include "sched.h"
29 #include "filter.h"
30 #include "string.h"
31
32 /** the size of the output data buffer */
33 #define COMPRESS_CHUNK_SIZE 40960
34
35 /** data specific to the compress filter */
36 struct private_compress_data {
37 /** the current multiplier */
38 unsigned current_gain;
39 /** points to the configuration data for this instance of the compress filter */
40 struct compress_filter_args_info *conf;
41 /** minimal admissible gain */
42 unsigned min_gain;
43 /** maximal admissible gain */
44 unsigned max_gain;
45 /** number of samples already seen */
46 unsigned num_samples;
47 /** absolute value of the maximal sample in the current block */
48 unsigned peak;
49 };
50
51 static ssize_t compress(char *inbuf, size_t inbuf_len, struct filter_node *fn)
52 {
53 size_t i, length = PARA_MIN((inbuf_len / 2) * 2,
54 (fn->bufsize - fn->loaded) / 2 * 2);
55 struct private_compress_data *pcd = fn->private_data;
56 int16_t *ip = (int16_t *)inbuf, *op = (int16_t *)(fn->buf + fn->loaded);
57 unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
58 mask = (1 << pcd->conf->blocksize_arg) - 1;
59
60 if (!length)
61 return 0;
62 for (i = 0; i < length / 2; i++) {
63 /* be careful in that heat, my dear */
64 int sample = *ip++, adjusted_sample;
65
66 if (sample > 0) {
67 adjusted_sample = (sample * pcd->current_gain)
68 >> gain_shift;
69 if (unlikely(adjusted_sample > 32767)) {
70 adjusted_sample = 32767;
71 pcd->current_gain = (3 * pcd->current_gain +
72 (1 << pcd->conf->inertia_arg)) / 4;
73 pcd->peak = 0;
74 } else
75 if (adjusted_sample > pcd->peak)
76 pcd->peak = sample;
77 } else {
78 adjusted_sample = -((-sample * pcd->current_gain)
79 >> gain_shift);
80 if (unlikely(adjusted_sample < -32768)) {
81 adjusted_sample = -32768;
82 pcd->current_gain = (3 * pcd->current_gain +
83 (1 << pcd->conf->inertia_arg)) / 4;
84 pcd->peak = 0;
85 } else
86 if (-adjusted_sample > pcd->peak)
87 pcd->peak = -adjusted_sample;
88 }
89 *op++ = adjusted_sample;
90 if (likely(++pcd->num_samples & mask))
91 continue;
92 if (pcd->peak < pcd->conf->target_level_arg) {
93 if (pcd->current_gain < pcd->max_gain)
94 pcd->current_gain++;
95 } else {
96 if (pcd->current_gain > pcd->min_gain + 1)
97 pcd->current_gain -= 2;
98 }
99 // PARA_DEBUG_LOG("gain: %lu, peak: %d\n", pcd->current_gain,
100 // pcd->peak);
101 pcd->peak = 0;
102 // PARA_INFO_LOG("sample: %lu\n", ABS(sample));
103 }
104 fn->loaded += length;
105 return length;
106 }
107
108 static void close_compress(struct filter_node *fn)
109 {
110 free(fn->private_data);
111 free(fn->buf);
112 }
113
114 static void *compress_parse_config(int argc, char **argv)
115 {
116 struct compress_filter_args_info *ret = para_calloc(sizeof(struct compress_filter_args_info));
117 if (!compress_cmdline_parser(argc, argv, ret))
118 return ret;
119 free(ret);
120 return NULL;
121 }
122
123 static void open_compress(struct filter_node *fn)
124 {
125 struct private_compress_data *pcd = para_calloc(
126 sizeof(struct private_compress_data));
127 pcd->conf = fn->conf;
128 fn->private_data = pcd;
129 fn->bufsize = COMPRESS_CHUNK_SIZE;
130 fn->buf = para_malloc(fn->bufsize);
131 pcd->current_gain = 1 << pcd->conf->inertia_arg;
132 pcd->min_gain = 1 << (pcd->conf->inertia_arg - pcd->conf->aggressiveness_arg);
133 pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
134 }
135
136 /** the init function of the compress filter */
137 void compress_init(struct filter *f)
138 {
139 f->open = open_compress;
140 f->close = close_compress;
141 f->convert = compress;
142 f->print_help = compress_cmdline_parser_print_help;
143 f->parse_config = compress_parse_config;
144 }