chiark / gitweb /
Consistency check for finished tracks.
[disorder] / clients / resample.c
CommitLineData
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 */
18#include "common.h"
19
20#include <unistd.h>
21#include <locale.h>
22#include <errno.h>
23#include <getopt.h>
24
25#include "resample.h"
26#include "mem.h"
27#include "syscalls.h"
28#include "log.h"
29
30static int input_bits = 16;
31static int input_channels = 2;
32static int input_rate = 44100;
33static int input_signed = 1;
34static int input_endian = ENDIAN_NATIVE;
35static int output_bits = 16;
36static int output_channels = 2;
37static int output_rate = 44100;
38static int output_signed = 1;
39static int output_endian = ENDIAN_NATIVE;
40
41static 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' },
55 { 0, 0, 0, 0 }
56};
57
58/* display usage message and terminate */
59static void help(void) {
60 xprintf("Usage:\n"
61 " resample [OPTIONS] < INPUT > OUTPUT\n"
62 "Options:\n"
63 " --help, -h Display usage message\n"
64 "Input format:\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"
71 "Output format:\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"
79 "\n"
80 "Feeds raw sample data through resample_convert().\n");
81 xfclose(stdout);
82 exit(0);
83}
84
85static void converted(uint8_t *bytes,
86 size_t nbytes,
87 void attribute((unused)) *cd) {
88 while(nbytes > 0) {
89 ssize_t n = write(1, bytes, nbytes);
90 if(n < 0)
91 disorder_fatal(errno, "writing to stdout");
92 bytes += n;
93 nbytes -= n;
94 }
95}
96
97int main(int argc, char **argv) {
98 int n;
99
100 mem_init();
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:",
104 options, 0)) >= 0) {
105 switch(n) {
106 case 'h': help();
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;
112 case 'e':
113 switch(optarg[0]) {
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);
118 }
9602bad8 119 break;
929f577e
RK
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;
125 case 'E':
126 switch(optarg[0]) {
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);
131 }
9602bad8 132 break;
2d0a6606 133 default: disorder_fatal(0, "invalid option");
929f577e
RK
134 }
135 }
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);
142 size_t used = 0;
143 int eof = 0;
144
145 while(used || !eof) {
146 if(!eof) {
147 ssize_t r = read(0, buffer + used, BUFFER_SIZE - used);
148 if(r < 0)
149 disorder_fatal(errno, "reading from stdin");
150 if(r == 0)
151 eof = 1;
152 used += r;
153 }
154 size_t consumed = resample_convert(rs, buffer, used, eof, converted, 0);
155 memmove(buffer, buffer + consumed, used - consumed);
156 used -= consumed;
157 }
158 if(close(1) < 0)
159 disorder_fatal(errno, "closing stdout");
160 return 0;
161}
162
163/*
164Local Variables:
165c-basic-offset:2
166comment-column:40
167fill-column:79
168indent-tabs-mode:nil
169End:
170*/