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
;
51 static const uint8_t simple_bytes_u
[] = {
55 static const uint8_t simple_bytes_u2
[] = {
56 0, 0, 127, 127, 128, 128, 255, 255,
59 static const uint8_t simple_bytes_s
[] = {
63 static const uint8_t simple_words_u_le
[] = {
64 0, 1, 0, 255, 1, 0, 1, 255
67 static const uint8_t simple_words_u_be
[] = {
68 1, 0, 255, 0, 0, 1, 255, 1
72 const char *description
;
85 const uint8_t *output
;
88 /* Conversions that don't change the sample rate */
91 8, 1, 8000, 0, ENDIAN_LITTLE
, (const uint8_t *)"", 0,
92 8, 1, 8000, 0, ENDIAN_LITTLE
, (const uint8_t *)"", 0
96 8, 1, 8000, 0, ENDIAN_LITTLE
, simple_bytes_u
, 4,
97 8, 1, 8000, 1, ENDIAN_LITTLE
, simple_bytes_s
, 4
101 8, 1, 8000, 0, ENDIAN_LITTLE
, simple_bytes_u
, 4,
102 8, 2, 8000, 0, ENDIAN_LITTLE
, simple_bytes_u2
, 8
106 16, 1, 8000, 0, ENDIAN_LITTLE
, simple_words_u_le
, 8,
107 16, 1, 8000, 0, ENDIAN_BIG
, simple_words_u_be
, 8,
109 #if HAVE_SAMPLERATE_H
110 /* Conversions that do change the sample rate */
114 #define NCONVERSIONS (sizeof conversions / sizeof *conversions)
116 static void test_resample(void) {
117 for(size_t n
= 0; n
< NCONVERSIONS
; ++n
) {
118 struct resampler rs
[1];
121 conversions
[n
].input_bits
,
122 conversions
[n
].input_channels
,
123 conversions
[n
].input_rate
,
124 conversions
[n
].input_signed
,
125 conversions
[n
].input_endian
,
126 conversions
[n
].output_bits
,
127 conversions
[n
].output_channels
,
128 conversions
[n
].output_rate
,
129 conversions
[n
].output_signed
,
130 conversions
[n
].output_endian
);
132 const uint8_t *output
= convert(rs
,
133 conversions
[n
].input
,
134 conversions
[n
].input_bytes
,
136 if(output_bytes
!= conversions
[n
].output_bytes
137 || memcmp(output
, conversions
[n
].output
, output_bytes
)) {
138 fprintf(stderr
, "index %zu description %s mismatch\n",
139 n
, conversions
[n
].description
);
141 while(k
< conversions
[n
].output_bytes
|| k
< output_bytes
) {
143 fprintf(stderr
, "%8zu E:", k
);
144 for(j
= 0; j
< 16; ++j
) {
145 if(j
+ k
< conversions
[n
].output_bytes
)
146 fprintf(stderr
, " %02x", conversions
[n
].output
[j
+ k
]);
148 fprintf(stderr
, "\n G:");
149 for(j
= 0; j
< 16; ++j
) {
150 if(j
+ k
< output_bytes
)
151 fprintf(stderr
, " %02x", output
[j
+ k
]);
153 fprintf(stderr
, "\n");