2 * This file is part of DisOrder.
3 * Copyright (C) 2009 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/>.
30 static int input_bits = 16;
31 static int input_channels = 2;
32 static int input_rate = 44100;
33 static int input_signed = 1;
34 static int input_endian = ENDIAN_NATIVE;
35 static int output_bits = 16;
36 static int output_channels = 2;
37 static int output_rate = 44100;
38 static int output_signed = 1;
39 static int output_endian = ENDIAN_NATIVE;
41 static const struct option options[] = {
42 { "help", no_argument, 0, 'h' },
43 { "input-bits", required_argument, 0, 'b' },
44 { "input-channels", required_argument, 0, 'c' },
45 { "input-rate", required_argument, 0, 'r' },
46 { "input-signed", no_argument, 0, 's' },
47 { "input-unsigned", no_argument, 0, 'u' },
48 { "input-endian", required_argument, 0, 'e' },
49 { "output-bits", required_argument, 0, 'B' },
50 { "output-channels", required_argument, 0, 'C' },
51 { "output-rate", required_argument, 0, 'R' },
52 { "output-signed", no_argument, 0, 'S' },
53 { "output-unsigned", no_argument, 0, 'U' },
54 { "output-endian", required_argument, 0, 'E' },
58 /* display usage message and terminate */
59 static void help(void) {
61 " resample [OPTIONS] < INPUT > OUTPUT\n"
63 " --help, -h Display usage message\n"
65 " --input-bits, -b N Bits/sample (16)\n"
66 " --input-channels, -c N Samples/frame (2)\n"
67 " --input-rate, -r N Frames/second (44100)\n"
68 " --input-signed, -s Signed samples (yes)\n"
69 " --input-unsigned, -u Unsigned samples\n"
70 " --input-endian, -e big|little Sample endianness (native)\n"
72 " --output-bits, -B N Bits/sample (16)\n"
73 " --output-channels, -C N Samples/frame (2)\n"
74 " --output-rate, -R N Frames/second (44100)\n"
75 " --output-signed, -S Signed samples (yes)\n"
76 " --output-unsigned, -U Unsigned samples\n"
77 " --output-endian, -E big|little Sample endianness (native)\n"
78 "Defaults are in brackets.\n"
80 "Feeds raw sample data through resample_convert().\n");
85 static void converted(uint8_t *bytes,
87 void attribute((unused)) *cd) {
89 ssize_t n = write(1, bytes, nbytes);
91 disorder_fatal(errno, "writing to stdout");
97 int main(int argc, char **argv) {
101 if(!setlocale(LC_CTYPE, ""))
102 disorder_fatal(errno, "error calling setlocale");
103 while((n = getopt_long(argc, argv, "+hb:c:r:sue:B:C:R:SUE:",
107 case 'b': input_bits = atoi(optarg); break;
108 case 'c': input_channels = atoi(optarg); break;
109 case 'r': input_rate = atoi(optarg); break;
110 case 's': input_signed = 1; break;
111 case 'u': input_signed = 1; break;
114 case 'b': case 'B': input_endian = ENDIAN_BIG; break;
115 case 'l': case 'L': input_endian = ENDIAN_LITTLE; break;
116 case 'n': case 'N': input_endian = ENDIAN_NATIVE; break;
117 default: disorder_fatal(0, "unknown endianness '%s'", optarg);
120 case 'B': output_bits = atoi(optarg); break;
121 case 'C': output_channels = atoi(optarg); break;
122 case 'R': output_rate = atoi(optarg); break;
123 case 'S': output_signed = 1; break;
124 case 'U': output_signed = 1; break;
127 case 'b': case 'B': output_endian = ENDIAN_BIG; break;
128 case 'l': case 'L': output_endian = ENDIAN_LITTLE; break;
129 case 'n': case 'N': output_endian = ENDIAN_NATIVE; break;
130 default: disorder_fatal(0, "unknown endianness '%s'", optarg);
133 default: disorder_fatal(0, "invalid option");
136 struct resampler rs[1];
137 resample_init(rs, input_bits, input_channels, input_rate, input_signed,
138 input_endian, output_bits, output_channels, output_rate,
139 output_signed, output_endian);
140 #define BUFFER_SIZE (1024 * 1024)
141 uint8_t *buffer = xmalloc_noptr(BUFFER_SIZE);
145 while(used || !eof) {
147 ssize_t r = read(0, buffer + used, BUFFER_SIZE - used);
149 disorder_fatal(errno, "reading from stdin");
154 size_t consumed = resample_convert(rs, buffer, used, eof, converted, 0);
155 memmove(buffer, buffer + consumed, used - consumed);
159 disorder_fatal(errno, "closing stdout");