Implement --log-timing for server and audiod.
[paraslash.git] / amp_filter.c
1 /*
2  * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file amp_filter.c Paraslash's amplify filter. */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "amp_filter.cmdline.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "filter.h"
17 #include "string.h"
18 #include "error.h"
19
20 /** The size of the output data buffer. */
21 #define AMP_CHUNK_SIZE 40960
22
23 extern char *stat_item_values[NUM_STAT_ITEMS];
24
25 /** Data specific to the amplify filter. */
26 struct private_amp_data {
27         /** Points to the configuration data for this instance of this filter. */
28         struct amp_filter_args_info *conf;
29         /** Amplification factor. */
30         unsigned amp;
31 };
32
33 static ssize_t amp_convert(char *inbuf, size_t inbuf_len, struct filter_node *fn)
34 {
35         size_t i, length = PARA_MIN((inbuf_len / 2),
36                 (fn->bufsize - fn->loaded) / 2);
37         struct private_amp_data *pad = fn->private_data;
38         int16_t *ip = (int16_t *)inbuf, *op = (int16_t *)(fn->buf + fn->loaded);
39         int factor = 64 + pad->amp;
40
41         if (!length)
42                 return 0;
43
44         if (pad->amp == 0) {
45                 memcpy(op, ip, length * 2);
46                 goto out;
47         }
48         for (i = 0; i < length; i++) {
49                 int x = (ip[i] * factor) >> 6;
50
51                 op[i] = x;
52                 if (op[i] != x)
53                         op[i] = (x >= 32768)? 32767 : -32768;
54         }
55 out:
56         fn->loaded += length * 2;
57         return length * 2;
58 }
59
60 static void amp_close(struct filter_node *fn)
61 {
62         free(fn->private_data);
63         free(fn->buf);
64 }
65
66 static int amp_parse_config(int argc, char **argv, void **config)
67 {
68         struct amp_filter_args_info *amp_conf = para_calloc(sizeof(*amp_conf));
69         int ret = -E_AMP_SYNTAX;
70
71         if (amp_cmdline_parser(argc, argv, amp_conf))
72                 goto err;
73         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
74         if (amp_conf->amp_arg < 0)
75                 goto err;
76         PARA_NOTICE_LOG("amplification: %u (scaling factor: %1.2f)\n",
77                 amp_conf->amp_arg, amp_conf->amp_arg / 64.0 + 1.0);
78         *config = amp_conf;
79         return 1;
80 err:
81         free(amp_conf);
82         return ret;
83 }
84
85 static void amp_open(struct filter_node *fn)
86 {
87         struct private_amp_data *pad = para_calloc(sizeof(*pad));
88
89         pad->conf = fn->conf;
90         fn->private_data = pad;
91         if (!pad->conf->amp_given && stat_item_values[SI_AMPLIFICATION])
92                 sscanf(stat_item_values[SI_AMPLIFICATION], "%u", &pad->amp);
93         else
94                 pad->amp = pad->conf->amp_arg;
95         fn->bufsize = AMP_CHUNK_SIZE;
96         fn->buf = para_malloc(fn->bufsize);
97 }
98
99 /**
100  * The init function of the amplify filter.
101  *
102  * \param f Pointer to the struct to initialize.
103  */
104 void amp_filter_init(struct filter *f)
105 {
106         struct amp_filter_args_info dummy;
107
108         amp_cmdline_parser_init(&dummy);
109         f->open = amp_open;
110         f->close = amp_close;
111         f->convert = amp_convert;
112         f->parse_config = amp_parse_config;
113         f->help = (struct ggo_help) {
114                 .short_help = amp_filter_args_info_help,
115                 .detailed_help = amp_filter_args_info_detailed_help
116         };
117 }