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/>.
22 /* Accumulate converted bytes in a dynamic string */
23 static void converted(uint8_t *bytes,
26 struct dynstr *d = cd;
27 dynstr_append_bytes(d, (void *)bytes, nbytes);
30 /* Converter wrapper */
31 static uint8_t *convert(const struct resampler *rs,
32 const uint8_t *input, size_t input_bytes,
33 size_t *output_bytes) {
37 while(input_bytes > 0) {
38 size_t chunk = input_bytes > 1024 ? 1024 : input_bytes;
39 size_t consumed = resample_convert(rs,
45 input_bytes -= consumed;
47 *output_bytes = d->nvec;
48 return (uint8_t *)d->vec;
52 const char *description;
68 /* Conversions that don't change the sample rate */
71 8, 1, 8000, 0, ENDIAN_LITTLE, "", 0,
72 8, 1, 8000, 0, ENDIAN_LITTLE, "", 0
75 "sign flip 8-bit unsigned->signed",
76 8, 1, 8000, 0, ENDIAN_LITTLE, "\x00\x7F\x80\xFF", 4,
77 8, 1, 8000, 1, ENDIAN_LITTLE, "\x80\xFF\x00\x7F", 4
80 "sign flip 8-bit signed->unsigned",
81 8, 1, 8000, 1, ENDIAN_BIG, "\x80\xFF\x00\x7F", 4,
82 8, 1, 8000, 0, ENDIAN_BIG, "\x00\x7F\x80\xFF", 4
86 8, 1, 8000, 0, ENDIAN_LITTLE, "\x00\x7F\x80\xFF", 4,
87 8, 2, 8000, 0, ENDIAN_LITTLE, "\x00\x00\x7F\x7F\x80\x80\xFF\xFF", 8
91 8, 2, 8000, 0, ENDIAN_LITTLE, "\x00\x01\x7F\x02\x80\x03\xFF\x04", 8,
92 8, 1, 8000, 0, ENDIAN_LITTLE, "\x00\x7F\x80\xFF", 4
95 "endian flip little->big",
96 16, 1, 8000, 0, ENDIAN_LITTLE, "\x00\x01\x00\xFF\x01\x00\x01\xFF", 8,
97 16, 1, 8000, 0, ENDIAN_BIG, "\x01\x00\xFF\x00\x00\x01\xFF\x01", 8,
100 "endian flip big->little",
101 16, 1, 8000, 0, ENDIAN_BIG, "\x01\x00\xFF\x00\x00\x01\xFF\x01", 8,
102 16, 1, 8000, 0, ENDIAN_LITTLE, "\x00\x01\x00\xFF\x01\x00\x01\xFF", 8,
106 8, 1, 8000, 0, ENDIAN_BIG, "\x00\x7F\x80\xFF", 4,
107 16, 1, 8000, 0, ENDIAN_BIG, "\x00\x00\x7F\x00\x80\x00\xFF\x00", 8
111 16, 1, 8000, 0, ENDIAN_BIG, "\x00\x00\x7F\xFF\x80\x00\xFF\xFF", 8,
112 8, 1, 8000, 0, ENDIAN_BIG, "\x00\x7F\x80\xFF", 4
114 #if HAVE_SAMPLERATE_H
115 /* Conversions that do change the sample rate */
119 #define NCONVERSIONS (sizeof conversions / sizeof *conversions)
121 static void test_resample(void) {
122 for(size_t n = 0; n < NCONVERSIONS; ++n) {
123 struct resampler rs[1];
126 conversions[n].input_bits,
127 conversions[n].input_channels,
128 conversions[n].input_rate,
129 conversions[n].input_signed,
130 conversions[n].input_endian,
131 conversions[n].output_bits,
132 conversions[n].output_channels,
133 conversions[n].output_rate,
134 conversions[n].output_signed,
135 conversions[n].output_endian);
137 const uint8_t *output = convert(rs,
138 (const uint8_t *)conversions[n].input,
139 conversions[n].input_bytes,
141 if(output_bytes != conversions[n].output_bytes
142 || memcmp(output, conversions[n].output, output_bytes)) {
143 fprintf(stderr, "index %zu description %s mismatch\n",
144 n, conversions[n].description);
146 while(k < conversions[n].output_bytes || k < output_bytes) {
148 fprintf(stderr, "%8zu E:", k);
149 for(j = 0; j < 16; ++j) {
150 if(j + k < conversions[n].output_bytes)
151 fprintf(stderr, " %02x", conversions[n].output[j + k]);
153 fprintf(stderr, "\n G:");
154 for(j = 0; j < 16; ++j) {
155 if(j + k < output_bytes)
156 fprintf(stderr, " %02x", output[j + k]);
158 fprintf(stderr, "\n");