chiark / gitweb /
General-purpose event distribution interface
[disorder] / server / normalize.c
CommitLineData
6d2d327c
RK
1/*
2 * This file is part of DisOrder
5aff007d 3 * Copyright (C) 2007, 2008 Richard Kettlewell
6d2d327c
RK
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
717ba987 20/** @file server/normalize.c
6d2d327c
RK
21 * @brief Convert "raw" format output to the configured format
22 *
23 * Currently we invoke sox even for trivial conversions such as byte-swapping.
24 * Ideally we would do all conversion including resampling in this one process
25 * and eliminate the dependency on sox.
26 */
27
05b75f8d 28#include "disorder-server.h"
0ca6d097
RK
29
30static const struct option options[] = {
31 { "help", no_argument, 0, 'h' },
32 { "version", no_argument, 0, 'V' },
33 { "config", required_argument, 0, 'c' },
34 { "debug", no_argument, 0, 'd' },
35 { "no-debug", no_argument, 0, 'D' },
36 { "syslog", no_argument, 0, 's' },
37 { "no-syslog", no_argument, 0, 'S' },
38 { 0, 0, 0, 0 }
39};
40
41/* display usage message and terminate */
42static void help(void) {
43 xprintf("Usage:\n"
44 " disorder-normalize [OPTIONS]\n"
45 "Options:\n"
46 " --help, -h Display usage message\n"
47 " --version, -V Display version number\n"
48 " --config PATH, -c PATH Set configuration file\n"
49 " --debug, -d Turn on debugging\n"
50 " --[no-]syslog Force logging\n"
51 "\n"
52 "Audio format normalizer for DisOrder. Not intended to be run\n"
53 "directly.\n");
54 xfclose(stdout);
55 exit(0);
56}
57
6d2d327c
RK
58/** @brief Copy bytes from one file descriptor to another
59 * @param infd File descriptor read from
60 * @param outfd File descriptor to write to
61 * @param n Number of bytes to copy
62 */
63static void copy(int infd, int outfd, size_t n) {
62234e45 64 char buffer[4096];
65 ssize_t written;
6d2d327c
RK
66
67 while(n > 0) {
62234e45 68 const ssize_t readden = read(infd, buffer,
69 n > sizeof buffer ? sizeof buffer : n);
70 if(readden < 0) {
6d2d327c
RK
71 if(errno == EINTR)
72 continue;
73 else
74 fatal(errno, "read error");
75 }
62234e45 76 if(readden == 0)
6d2d327c 77 fatal(0, "unexpected EOF");
62234e45 78 n -= readden;
79 written = 0;
80 while(written < readden) {
81 const ssize_t w = write(outfd, buffer + written, readden - written);
6d2d327c
RK
82 if(w < 0)
83 fatal(errno, "write error");
62234e45 84 written += w;
6d2d327c
RK
85 }
86 }
87}
88
89static void soxargs(const char ***pp, char **qq,
90 const struct stream_header *header) {
91 *(*pp)++ = "-t.raw";
92 *(*pp)++ = "-s";
93 *qq += sprintf((char *)(*(*pp)++ = *qq), "-r%d", header->rate) + 1;
94 *qq += sprintf((char *)(*(*pp)++ = *qq), "-c%d", header->channels) + 1;
95 /* sox 12.17.9 insists on -b etc; CVS sox insists on -<n> etc; both are
96 * deployed! */
97 switch(config->sox_generation) {
98 case 0:
99 if(header->bits != 8
100 && header->endian != ENDIAN_NATIVE)
101 *(*pp)++ = "-x";
102 switch(header->bits) {
103 case 8: *(*pp)++ = "-b"; break;
104 case 16: *(*pp)++ = "-w"; break;
105 case 32: *(*pp)++ = "-l"; break;
106 case 64: *(*pp)++ = "-d"; break;
107 default: fatal(0, "cannot handle sample size %d", header->bits);
108 }
109 break;
110 case 1:
111 if(header->bits != 8
112 && header->endian != ENDIAN_NATIVE)
113 switch(header->endian) {
114 case ENDIAN_BIG: *(*pp)++ = "-B"; break;
115 case ENDIAN_LITTLE: *(*pp)++ = "-L"; break;
116 }
117 if(header->bits % 8)
118 fatal(0, "cannot handle sample size %d", header->bits);
119 *qq += sprintf((char *)(*(*pp)++ = *qq), "-%d", header->bits / 8) + 1;
120 break;
121 default:
122 fatal(0, "unknown sox_generation %ld", config->sox_generation);
123 }
124}
125
126int main(int argc, char attribute((unused)) **argv) {
127 struct stream_header header, latest_format;
0ca6d097 128 int n, p[2], outfd = -1, logsyslog = !isatty(2);
6d2d327c
RK
129 pid_t pid = -1;
130
131 set_progname(argv);
132 if(!setlocale(LC_CTYPE, ""))
133 fatal(errno, "error calling setlocale");
0ca6d097
RK
134 while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) {
135 switch(n) {
136 case 'h': help();
3fbdc96d 137 case 'V': version("disorder-normalize");
0ca6d097
RK
138 case 'c': configfile = optarg; break;
139 case 'd': debugging = 1; break;
140 case 'D': debugging = 0; break;
141 case 'S': logsyslog = 0; break;
142 case 's': logsyslog = 1; break;
143 default: fatal(0, "invalid option");
144 }
145 }
c00fce3a 146 if(config_read(1))
6d2d327c 147 fatal(0, "cannot read configuration");
0ca6d097 148 if(logsyslog) {
6d2d327c
RK
149 openlog(progname, LOG_PID, LOG_DAEMON);
150 log_default = &log_syslog;
151 }
152 memset(&latest_format, 0, sizeof latest_format);
153 for(;;) {
e7fd1612
RK
154 n = 0;
155 while((size_t)n < sizeof header) {
156 int r = read(0, (char *)&header + n, sizeof header - n);
157
158 if(r < 0) {
159 if(errno != EINTR)
160 fatal(errno, "error reading header");
e99d42b1 161 } else if(r == 0) {
162 if(n)
163 fatal(0, "EOF reading header");
164 break;
165 } else
e7fd1612
RK
166 n += r;
167 }
24fc5e58 168 if(!n)
169 break;
6d2d327c
RK
170 /* Sanity check the header */
171 if(header.rate < 100 || header.rate > 1000000)
172 fatal(0, "implausible rate %"PRId32"Hz (%#"PRIx32")",
173 header.rate, header.rate);
174 if(header.channels < 1 || header.channels > 2)
175 fatal(0, "unsupported channel count %d", header.channels);
176 if(header.bits % 8 || !header.bits || header.bits > 64)
177 fatal(0, "unsupported sample size %d bits", header.bits);
178 if(header.endian != ENDIAN_BIG && header.endian != ENDIAN_LITTLE)
179 fatal(0, "unsupported byte order %x", header.bits);
180 /* Skip empty chunks regardless of their alleged format */
181 if(header.nbytes == 0)
182 continue;
183 /* If the format has changed we stop/start the converter */
184 if(!formats_equal(&header, &latest_format)) {
185 if(pid != -1) {
186 /* There's a running converter, stop it */
187 xclose(outfd);
188 if(waitpid(pid, &n, 0) < 0)
189 fatal(errno, "error calling waitpid");
190 if(n)
191 fatal(0, "sox failed: %#x", n);
192 pid = -1;
193 outfd = -1;
194 }
195 if(!formats_equal(&header, &config->sample_format)) {
196 const char *av[32], **pp = av;
197 char argbuf[1024], *q = argbuf;
198
199 /* Input format doesn't match target, need to start a converter */
200 *pp++ = "sox";
201 soxargs(&pp, &q, &header);
202 *pp++ = "-"; /* stdin */
203 soxargs(&pp, &q, &config->sample_format);
204 *pp++ = "-"; /* stdout */
205 *pp = 0;
206 /* This pipe will be sox's stdin */
207 xpipe(p);
208 if(!(pid = xfork())) {
209 exitfn = _exit;
210 xdup2(p[0], 0);
211 xclose(p[0]);
212 xclose(p[1]);
213 execvp(av[0], (char **)av);
214 fatal(errno, "sox");
215 }
216 xclose(p[0]);
217 outfd = p[1];
218 } else
219 /* Input format matches output, can just copy bytes */
220 outfd = 1;
221 /* Remember current format for next iteration */
222 latest_format = header;
223 }
224 /* Convert or copy this chunk */
225 copy(0, outfd, header.nbytes);
226 }
227 if(outfd != -1)
228 xclose(outfd);
60432d3d
RK
229 if(pid != -1) {
230 /* There's still a converter running */
231 if(waitpid(pid, &n, 0) < 0)
232 fatal(errno, "error calling waitpid");
233 if(n)
234 fatal(0, "sox failed: %#x", n);
235 }
6d2d327c
RK
236 return 0;
237}
238
239/*
240Local Variables:
241c-basic-offset:2
242comment-column:40
243fill-column:79
244indent-tabs-mode:nil
245End:
246*/