2 * This file is part of DisOrder
3 * Copyright (C) 2007-2010 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/decode.c
19 * @brief General-purpose decoder for use by speaker process
24 #include <vorbis/vorbisfile.h>
26 #include <FLAC/stream_decoder.h>
31 /** @brief Encoding lookup table type */
33 /** @brief Glob pattern matching file */
35 /** @brief Decoder function */
41 char input_buffer[INPUT_BUFFER_SIZE];
44 /** @brief Write a block header
45 * @param rate Sample rate in Hz
46 * @param channels Channel count (currently only 1 or 2 supported)
47 * @param bits Bits per sample (must be a multiple of 8, no more than 64)
48 * @param nbytes Total number of data bytes
49 * @param endian @ref ENDIAN_BIG or @ref ENDIAN_LITTLE
51 * Checks that the sample format is a supported one (so other calls do not have
52 * to) and calls disorder_fatal() on error.
54 void output_header(int rate,
59 struct stream_header header;
61 if(bits <= 0 || bits % 8 || bits > 64)
62 disorder_fatal(0, "decoding %s: unsupported sample size %d bits",
64 if(channels <= 0 || channels > 2)
65 disorder_fatal(0, "decoding %s: unsupported channel count %d",
68 disorder_fatal(0, "decoding %s: nonsensical sample rate %dHz", path, rate);
71 header.channels = channels;
72 header.endian = endian;
73 header.nbytes = nbytes;
74 if(fwrite(&header, sizeof header, 1, outputfp) < 1)
75 disorder_fatal(errno, "decoding %s: writing format header", path);
78 /** @brief Lookup table of decoders */
79 static const struct decoder decoders[] = {
80 { "*.mp3", decode_mp3 },
81 { "*.MP3", decode_mp3 },
82 { "*.ogg", decode_ogg },
83 { "*.OGG", decode_ogg },
84 { "*.flac", decode_flac },
85 { "*.FLAC", decode_flac },
86 { "*.wav", decode_wav },
87 { "*.WAV", decode_wav },
91 static const struct option options[] = {
92 { "help", no_argument, 0, 'h' },
93 { "version", no_argument, 0, 'V' },
97 /* Display usage message and terminate. */
98 static void help(void) {
100 " disorder-decode [OPTIONS] PATH\n"
102 " --help, -h Display usage message\n"
103 " --version, -V Display version number\n"
105 "Audio decoder for DisOrder. Only intended to be used by speaker\n"
106 "process, not for normal users.\n");
111 int main(int argc, char **argv) {
116 if(!setlocale(LC_CTYPE, "")) disorder_fatal(errno, "calling setlocale");
117 while((n = getopt_long(argc, argv, "hV", options, 0)) >= 0) {
120 case 'V': version("disorder-decode");
121 default: disorder_fatal(0, "invalid option");
125 disorder_fatal(0, "missing filename");
126 if(optind + 1 < argc)
127 disorder_fatal(0, "excess arguments");
128 if((e = getenv("DISORDER_RAW_FD"))) {
129 if(!(outputfp = fdopen(atoi(e), "wb")))
130 disorder_fatal(errno, "fdopen");
136 && fnmatch(decoders[n].pattern, path, 0) != 0;
139 if(!decoders[n].pattern)
140 disorder_fatal(0, "cannot determine file type for %s", path);
141 decoders[n].decode();