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/>.
18 /** @file clients/resample.c
19 * @brief Audio resampler
33 static int input_bits
= 16;
34 static int input_channels
= 2;
35 static int input_rate
= 44100;
36 static int input_signed
= 1;
37 static int input_endian
= ENDIAN_NATIVE
;
38 static int output_bits
= 16;
39 static int output_channels
= 2;
40 static int output_rate
= 44100;
41 static int output_signed
= 1;
42 static int output_endian
= ENDIAN_NATIVE
;
44 static const struct option options
[] = {
45 { "help", no_argument
, 0, 'h' },
46 { "input-bits", required_argument
, 0, 'b' },
47 { "input-channels", required_argument
, 0, 'c' },
48 { "input-rate", required_argument
, 0, 'r' },
49 { "input-signed", no_argument
, 0, 's' },
50 { "input-unsigned", no_argument
, 0, 'u' },
51 { "input-endian", required_argument
, 0, 'e' },
52 { "output-bits", required_argument
, 0, 'B' },
53 { "output-channels", required_argument
, 0, 'C' },
54 { "output-rate", required_argument
, 0, 'R' },
55 { "output-signed", no_argument
, 0, 'S' },
56 { "output-unsigned", no_argument
, 0, 'U' },
57 { "output-endian", required_argument
, 0, 'E' },
61 /* display usage message and terminate */
62 static void attribute((noreturn
)) help(void) {
64 " resample [OPTIONS] < INPUT > OUTPUT\n"
66 " --help, -h Display usage message\n"
68 " --input-bits, -b N Bits/sample (16)\n"
69 " --input-channels, -c N Samples/frame (2)\n"
70 " --input-rate, -r N Frames/second (44100)\n"
71 " --input-signed, -s Signed samples (yes)\n"
72 " --input-unsigned, -u Unsigned samples\n"
73 " --input-endian, -e big|little Sample endianness (native)\n"
75 " --output-bits, -B N Bits/sample (16)\n"
76 " --output-channels, -C N Samples/frame (2)\n"
77 " --output-rate, -R N Frames/second (44100)\n"
78 " --output-signed, -S Signed samples (yes)\n"
79 " --output-unsigned, -U Unsigned samples\n"
80 " --output-endian, -E big|little Sample endianness (native)\n"
81 "Defaults are in brackets.\n"
83 "Feeds raw sample data through resample_convert().\n");
88 static void converted(uint8_t *bytes
,
90 void attribute((unused
)) *cd
) {
92 ssize_t n
= write(1, bytes
, nbytes
);
94 disorder_fatal(errno
, "writing to stdout");
100 int main(int argc
, char **argv
) {
104 if(!setlocale(LC_CTYPE
, ""))
105 disorder_fatal(errno
, "error calling setlocale");
106 while((n
= getopt_long(argc
, argv
, "+hb:c:r:sue:B:C:R:SUE:",
110 case 'b': input_bits
= atoi(optarg
); break;
111 case 'c': input_channels
= atoi(optarg
); break;
112 case 'r': input_rate
= atoi(optarg
); break;
113 case 's': input_signed
= 1; break;
114 case 'u': input_signed
= 1; break;
117 case 'b': case 'B': input_endian
= ENDIAN_BIG
; break;
118 case 'l': case 'L': input_endian
= ENDIAN_LITTLE
; break;
119 case 'n': case 'N': input_endian
= ENDIAN_NATIVE
; break;
120 default: disorder_fatal(0, "unknown endianness '%s'", optarg
);
123 case 'B': output_bits
= atoi(optarg
); break;
124 case 'C': output_channels
= atoi(optarg
); break;
125 case 'R': output_rate
= atoi(optarg
); break;
126 case 'S': output_signed
= 1; break;
127 case 'U': output_signed
= 1; break;
130 case 'b': case 'B': output_endian
= ENDIAN_BIG
; break;
131 case 'l': case 'L': output_endian
= ENDIAN_LITTLE
; break;
132 case 'n': case 'N': output_endian
= ENDIAN_NATIVE
; break;
133 default: disorder_fatal(0, "unknown endianness '%s'", optarg
);
136 default: disorder_fatal(0, "invalid option");
139 struct resampler rs
[1];
140 resample_init(rs
, input_bits
, input_channels
, input_rate
, input_signed
,
141 input_endian
, output_bits
, output_channels
, output_rate
,
142 output_signed
, output_endian
);
143 #define BUFFER_SIZE (1024 * 1024)
144 uint8_t *buffer
= xmalloc_noptr(BUFFER_SIZE
);
148 while(used
|| !eof
) {
150 ssize_t r
= read(0, buffer
+ used
, BUFFER_SIZE
- used
);
152 disorder_fatal(errno
, "reading from stdin");
157 size_t consumed
= resample_convert(rs
, buffer
, used
, eof
, converted
, 0);
158 memmove(buffer
, buffer
+ consumed
, used
- consumed
);
162 disorder_fatal(errno
, "closing stdout");