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"
20 /** The size of the output data buffer. */
21 #define COMPRESS_CHUNK_SIZE 40960
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. */
31 /** Maximal admissible gain. */
33 /** Number of samples already seen. */
35 /** Absolute value of the maximal sample in the current block. */
39 static ssize_t
compress(char *inbuf
, size_t inbuf_len
, struct filter_node
*fn
)
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;
50 for (i
= 0; i
< length
/ 2; i
++) {
51 /* be careful in that heat, my dear */
52 int sample
= *ip
++, adjusted_sample
;
54 adjusted_sample
= (PARA_ABS(sample
) * pcd
->current_gain
)
56 if (unlikely(adjusted_sample
> 32767)) { /* clip */
57 PARA_NOTICE_LOG("clip: sample: %d, adjusted sample: %d\n",
58 sample
, adjusted_sample
);
59 adjusted_sample
= 32767;
60 pcd
->current_gain
= (3 * pcd
->current_gain
+
61 (1 << pcd
->conf
->inertia_arg
)) / 4;
64 pcd
->peak
= PARA_MAX(pcd
->peak
, adjusted_sample
);
65 *op
++ = sample
>= 0? adjusted_sample
: -adjusted_sample
;
66 if (likely(++pcd
->num_samples
& mask
))
68 if (pcd
->peak
< pcd
->conf
->target_level_arg
) {
69 if (pcd
->current_gain
< pcd
->max_gain
)
72 if (pcd
->current_gain
> pcd
->min_gain
+ 1)
73 pcd
->current_gain
-= 2;
75 PARA_DEBUG_LOG("gain: %lu, peak: %d\n", pcd
->current_gain
,
83 static void close_compress(struct filter_node
*fn
)
85 free(fn
->private_data
);
89 static void *compress_parse_config(int argc
, char **argv
)
91 struct compress_filter_args_info
*ret
= para_calloc(sizeof(struct compress_filter_args_info
));
92 if (!compress_cmdline_parser(argc
, argv
, ret
))
98 static void open_compress(struct filter_node
*fn
)
100 struct private_compress_data
*pcd
= para_calloc(
101 sizeof(struct private_compress_data
));
102 pcd
->conf
= fn
->conf
;
103 fn
->private_data
= pcd
;
104 fn
->bufsize
= COMPRESS_CHUNK_SIZE
;
105 fn
->buf
= para_malloc(fn
->bufsize
);
106 pcd
->current_gain
= 1 << pcd
->conf
->inertia_arg
;
107 pcd
->min_gain
= 1 << (pcd
->conf
->inertia_arg
- pcd
->conf
->aggressiveness_arg
);
108 pcd
->max_gain
= 1 << (pcd
->conf
->inertia_arg
+ pcd
->conf
->aggressiveness_arg
);
112 * The init function of the compress filter.
114 * \param f Pointer to the struct to initialize.
116 void compress_init(struct filter
*f
)
118 f
->open
= open_compress
;
119 f
->close
= close_compress
;
120 f
->convert
= compress
;
121 f
->print_help
= compress_cmdline_parser_print_help
;
122 f
->parse_config
= compress_parse_config
;