2 * This file is part of DisOrder
3 * Copyright (C) 2007, 2008 Richard Kettlewell
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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file server/normalize.c
19 * @brief Convert "raw" format output to the configured format
21 * Currently we invoke sox even for trivial conversions such as byte-swapping.
22 * Ideally we would do all conversion including resampling in this one process
23 * and eliminate the dependency on sox.
26 #include "disorder-server.h"
28 static const struct option options
[] = {
29 { "help", no_argument
, 0, 'h' },
30 { "version", no_argument
, 0, 'V' },
31 { "config", required_argument
, 0, 'c' },
32 { "debug", no_argument
, 0, 'd' },
33 { "no-debug", no_argument
, 0, 'D' },
34 { "syslog", no_argument
, 0, 's' },
35 { "no-syslog", no_argument
, 0, 'S' },
39 /* display usage message and terminate */
40 static void help(void) {
42 " disorder-normalize [OPTIONS]\n"
44 " --help, -h Display usage message\n"
45 " --version, -V Display version number\n"
46 " --config PATH, -c PATH Set configuration file\n"
47 " --debug, -d Turn on debugging\n"
48 " --[no-]syslog Force logging\n"
50 "Audio format normalizer for DisOrder. Not intended to be run\n"
56 /** @brief Copy bytes from one file descriptor to another
57 * @param infd File descriptor read from
58 * @param outfd File descriptor to write to
59 * @param n Number of bytes to copy
61 static void copy(int infd
, int outfd
, size_t n
) {
66 const ssize_t readden
= read(infd
, buffer
,
67 n
> sizeof buffer ?
sizeof buffer
: n
);
72 disorder_fatal(errno
, "read error");
75 disorder_fatal(0, "unexpected EOF");
78 while(written
< readden
) {
79 const ssize_t w
= write(outfd
, buffer
+ written
, readden
- written
);
81 disorder_fatal(errno
, "write error");
87 static void soxargs(const char ***pp
, char **qq
,
88 const struct stream_header
*header
) {
91 *qq
+= sprintf((char *)(*(*pp
)++ = *qq
), "-r%d", header
->rate
) + 1;
92 *qq
+= sprintf((char *)(*(*pp
)++ = *qq
), "-c%d", header
->channels
) + 1;
93 /* sox 12.17.9 insists on -b etc; CVS sox insists on -<n> etc; both are
95 switch(config
->sox_generation
) {
98 && header
->endian
!= ENDIAN_NATIVE
)
100 switch(header
->bits
) {
101 case 8: *(*pp
)++ = "-b"; break;
102 case 16: *(*pp
)++ = "-w"; break;
103 case 32: *(*pp
)++ = "-l"; break;
104 case 64: *(*pp
)++ = "-d"; break;
105 default: disorder_fatal(0, "cannot handle sample size %d", header
->bits
);
110 && header
->endian
!= ENDIAN_NATIVE
)
111 switch(header
->endian
) {
112 case ENDIAN_BIG
: *(*pp
)++ = "-B"; break;
113 case ENDIAN_LITTLE
: *(*pp
)++ = "-L"; break;
116 disorder_fatal(0, "cannot handle sample size %d", header
->bits
);
117 *qq
+= sprintf((char *)(*(*pp
)++ = *qq
), "-%d", header
->bits
/ 8) + 1;
120 disorder_fatal(0, "unknown sox_generation %ld", config
->sox_generation
);
124 int main(int argc
, char attribute((unused
)) **argv
) {
125 struct stream_header header
, latest_format
;
126 int n
, p
[2], outfd
= -1, logsyslog
= !isatty(2);
130 if(!setlocale(LC_CTYPE
, ""))
131 disorder_fatal(errno
, "error calling setlocale");
132 while((n
= getopt_long(argc
, argv
, "hVc:dDSs", options
, 0)) >= 0) {
135 case 'V': version("disorder-normalize");
136 case 'c': configfile
= optarg
; break;
137 case 'd': debugging
= 1; break;
138 case 'D': debugging
= 0; break;
139 case 'S': logsyslog
= 0; break;
140 case 's': logsyslog
= 1; break;
141 default: disorder_fatal(0, "invalid option");
144 if(config_read(1, NULL
))
145 disorder_fatal(0, "cannot read configuration");
147 openlog(progname
, LOG_PID
, LOG_DAEMON
);
148 log_default
= &log_syslog
;
150 memset(&latest_format
, 0, sizeof latest_format
);
153 while((size_t)n
< sizeof header
) {
154 int r
= read(0, (char *)&header
+ n
, sizeof header
- n
);
158 disorder_fatal(errno
, "error reading header");
161 disorder_fatal(0, "EOF reading header");
168 /* Sanity check the header */
169 if(header
.rate
< 100 || header
.rate
> 1000000)
170 disorder_fatal(0, "implausible rate %"PRId32
"Hz (%#"PRIx32
")",
171 header
.rate
, header
.rate
);
172 if(header
.channels
< 1 || header
.channels
> 2)
173 disorder_fatal(0, "unsupported channel count %d", header
.channels
);
174 if(header
.bits
% 8 || !header
.bits
|| header
.bits
> 64)
175 disorder_fatal(0, "unsupported sample size %d bits", header
.bits
);
176 if(header
.endian
!= ENDIAN_BIG
&& header
.endian
!= ENDIAN_LITTLE
)
177 disorder_fatal(0, "unsupported byte order %x", header
.bits
);
178 /* Skip empty chunks regardless of their alleged format */
179 if(header
.nbytes
== 0)
181 /* If the format has changed we stop/start the converter */
182 if(!formats_equal(&header
, &latest_format
)) {
184 /* There's a running converter, stop it */
186 if(waitpid(pid
, &n
, 0) < 0)
187 disorder_fatal(errno
, "error calling waitpid");
189 disorder_fatal(0, "sox failed: %#x", n
);
193 if(!formats_equal(&header
, &config
->sample_format
)) {
194 const char *av
[32], **pp
= av
;
195 char argbuf
[1024], *q
= argbuf
;
197 /* Input format doesn't match target, need to start a converter */
199 soxargs(&pp
, &q
, &header
);
200 *pp
++ = "-"; /* stdin */
201 soxargs(&pp
, &q
, &config
->sample_format
);
202 *pp
++ = "-"; /* stdout */
204 /* This pipe will be sox's stdin */
206 if(!(pid
= xfork())) {
211 execvp(av
[0], (char **)av
);
212 disorder_fatal(errno
, "sox");
217 /* Input format matches output, can just copy bytes */
219 /* Remember current format for next iteration */
220 latest_format
= header
;
222 /* Convert or copy this chunk */
223 copy(0, outfd
, header
.nbytes
);
228 /* There's still a converter running */
229 if(waitpid(pid
, &n
, 0) < 0)
230 disorder_fatal(errno
, "error calling waitpid");
232 disorder_fatal(0, "sox failed: %#x", n
);