Doxyfile: Change web sync directory.
[paraslash.git] / compress.c
1 /*
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file compress.c paraslash's dynamic audio range compressor */
8
9 /*
10 * Used ideas of AudioCompress, (C) 2002-2004 M. Hari Nezumi <magenta@trikuare.cx>
11 */
12
13 #include "para.h"
14 #include "compress_filter.cmdline.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "filter.h"
18 #include "string.h"
19
20 /** the size of the output data buffer */
21 #define COMPRESS_CHUNK_SIZE 40960
22
23 /** data specific to the compress filter */
24 struct private_compress_data {
25 /** the current multiplier */
26 unsigned current_gain;
27 /** points to the configuration data for this instance of the compress filter */
28 struct compress_filter_args_info *conf;
29 /** minimal admissible gain */
30 unsigned min_gain;
31 /** maximal admissible gain */
32 unsigned max_gain;
33 /** number of samples already seen */
34 unsigned num_samples;
35 /** absolute value of the maximal sample in the current block */
36 unsigned peak;
37 };
38
39 static ssize_t compress(char *inbuf, size_t inbuf_len, struct filter_node *fn)
40 {
41 size_t i, length = PARA_MIN((inbuf_len / 2) * 2,
42 (fn->bufsize - fn->loaded) / 2 * 2);
43 struct private_compress_data *pcd = fn->private_data;
44 int16_t *ip = (int16_t *)inbuf, *op = (int16_t *)(fn->buf + fn->loaded);
45 unsigned gain_shift = pcd->conf->inertia_arg + pcd->conf->damp_arg,
46 mask = (1 << pcd->conf->blocksize_arg) - 1;
47
48 if (!length)
49 return 0;
50 for (i = 0; i < length / 2; i++) {
51 /* be careful in that heat, my dear */
52 int sample = *ip++, adjusted_sample;
53
54 if (sample > 0) {
55 adjusted_sample = (sample * pcd->current_gain)
56 >> gain_shift;
57 if (unlikely(adjusted_sample > 32767)) {
58 adjusted_sample = 32767;
59 pcd->current_gain = (3 * pcd->current_gain +
60 (1 << pcd->conf->inertia_arg)) / 4;
61 pcd->peak = 0;
62 } else
63 if (adjusted_sample > pcd->peak)
64 pcd->peak = adjusted_sample;
65 } else {
66 adjusted_sample = -((-sample * pcd->current_gain)
67 >> gain_shift);
68 if (unlikely(adjusted_sample < -32768)) {
69 adjusted_sample = -32768;
70 pcd->current_gain = (3 * pcd->current_gain +
71 (1 << pcd->conf->inertia_arg)) / 4;
72 pcd->peak = 0;
73 } else
74 if (-adjusted_sample > pcd->peak)
75 pcd->peak = -adjusted_sample;
76 }
77 *op++ = adjusted_sample;
78 if (likely(++pcd->num_samples & mask))
79 continue;
80 if (pcd->peak < pcd->conf->target_level_arg) {
81 if (pcd->current_gain < pcd->max_gain)
82 pcd->current_gain++;
83 } else {
84 if (pcd->current_gain > pcd->min_gain + 1)
85 pcd->current_gain -= 2;
86 }
87 // PARA_DEBUG_LOG("gain: %lu, peak: %d\n", pcd->current_gain,
88 // pcd->peak);
89 pcd->peak = 0;
90 // PARA_INFO_LOG("sample: %lu\n", ABS(sample));
91 }
92 fn->loaded += length;
93 return length;
94 }
95
96 static void close_compress(struct filter_node *fn)
97 {
98 free(fn->private_data);
99 free(fn->buf);
100 }
101
102 static void *compress_parse_config(int argc, char **argv)
103 {
104 struct compress_filter_args_info *ret = para_calloc(sizeof(struct compress_filter_args_info));
105 if (!compress_cmdline_parser(argc, argv, ret))
106 return ret;
107 free(ret);
108 return NULL;
109 }
110
111 static void open_compress(struct filter_node *fn)
112 {
113 struct private_compress_data *pcd = para_calloc(
114 sizeof(struct private_compress_data));
115 pcd->conf = fn->conf;
116 fn->private_data = pcd;
117 fn->bufsize = COMPRESS_CHUNK_SIZE;
118 fn->buf = para_malloc(fn->bufsize);
119 pcd->current_gain = 1 << pcd->conf->inertia_arg;
120 pcd->min_gain = 1 << (pcd->conf->inertia_arg - pcd->conf->aggressiveness_arg);
121 pcd->max_gain = 1 << (pcd->conf->inertia_arg + pcd->conf->aggressiveness_arg);
122 }
123
124 /**
125 * the init function of the compress filter
126 *
127 * \param f pointer to the struct to initialize
128 */
129 void compress_init(struct filter *f)
130 {
131 f->open = open_compress;
132 f->close = close_compress;
133 f->convert = compress;
134 f->print_help = compress_cmdline_parser_print_help;
135 f->parse_config = compress_parse_config;
136 }