Commit | Line | Data |
---|---|---|
929f577e RK |
1 | /* |
2 | * This file is part of DisOrder. | |
3 | * Copyright (C) 2009 Richard Kettlewell | |
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 3 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, | |
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. | |
9602bad8 | 14 | * |
929f577e RK |
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/>. | |
17 | */ | |
1a164e63 RK |
18 | /** @file clients/resample.c |
19 | * @brief Audio resampler | |
20 | */ | |
929f577e RK |
21 | #include "common.h" |
22 | ||
23 | #include <unistd.h> | |
24 | #include <locale.h> | |
25 | #include <errno.h> | |
26 | #include <getopt.h> | |
27 | ||
28 | #include "resample.h" | |
29 | #include "mem.h" | |
30 | #include "syscalls.h" | |
31 | #include "log.h" | |
32 | ||
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; | |
43 | ||
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' }, | |
58 | { 0, 0, 0, 0 } | |
59 | }; | |
60 | ||
61 | /* display usage message and terminate */ | |
16bf32dc | 62 | static void attribute((noreturn)) help(void) { |
929f577e RK |
63 | xprintf("Usage:\n" |
64 | " resample [OPTIONS] < INPUT > OUTPUT\n" | |
65 | "Options:\n" | |
66 | " --help, -h Display usage message\n" | |
67 | "Input format:\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" | |
74 | "Output format:\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" | |
82 | "\n" | |
83 | "Feeds raw sample data through resample_convert().\n"); | |
84 | xfclose(stdout); | |
85 | exit(0); | |
86 | } | |
87 | ||
88 | static void converted(uint8_t *bytes, | |
89 | size_t nbytes, | |
90 | void attribute((unused)) *cd) { | |
91 | while(nbytes > 0) { | |
92 | ssize_t n = write(1, bytes, nbytes); | |
93 | if(n < 0) | |
94 | disorder_fatal(errno, "writing to stdout"); | |
95 | bytes += n; | |
96 | nbytes -= n; | |
97 | } | |
98 | } | |
99 | ||
100 | int main(int argc, char **argv) { | |
101 | int n; | |
102 | ||
103 | mem_init(); | |
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:", | |
107 | options, 0)) >= 0) { | |
108 | switch(n) { | |
109 | case 'h': help(); | |
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; | |
115 | case 'e': | |
116 | switch(optarg[0]) { | |
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); | |
121 | } | |
9602bad8 | 122 | break; |
929f577e RK |
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; | |
128 | case 'E': | |
129 | switch(optarg[0]) { | |
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); | |
134 | } | |
9602bad8 | 135 | break; |
2d0a6606 | 136 | default: disorder_fatal(0, "invalid option"); |
929f577e RK |
137 | } |
138 | } | |
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); | |
145 | size_t used = 0; | |
146 | int eof = 0; | |
147 | ||
148 | while(used || !eof) { | |
149 | if(!eof) { | |
150 | ssize_t r = read(0, buffer + used, BUFFER_SIZE - used); | |
151 | if(r < 0) | |
152 | disorder_fatal(errno, "reading from stdin"); | |
153 | if(r == 0) | |
154 | eof = 1; | |
155 | used += r; | |
156 | } | |
157 | size_t consumed = resample_convert(rs, buffer, used, eof, converted, 0); | |
158 | memmove(buffer, buffer + consumed, used - consumed); | |
159 | used -= consumed; | |
160 | } | |
161 | if(close(1) < 0) | |
162 | disorder_fatal(errno, "closing stdout"); | |
163 | return 0; | |
164 | } | |
165 | ||
166 | /* | |
167 | Local Variables: | |
168 | c-basic-offset:2 | |
169 | comment-column:40 | |
170 | fill-column:79 | |
171 | indent-tabs-mode:nil | |
172 | End: | |
173 | */ |