]> code.delx.au - pulseaudio/blob - src/tests/resampler-test.c
add a simple fully-automatic fully-linearupmixer/downmixer and enable it by default
[pulseaudio] / src / tests / resampler-test.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27
28 #include <pulse/sample.h>
29 #include <pulse/volume.h>
30
31 #include <pulsecore/resampler.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/endianmacros.h>
34 #include <pulsecore/memblock.h>
35 #include <pulsecore/sample-util.h>
36
37 #include <liboil/liboil.h>
38
39 static float swap_float(float a) {
40 uint32_t *b = (uint32_t*) &a;
41 *b = PA_UINT32_SWAP(*b);
42 return a;
43 }
44
45 static void dump_block(const pa_sample_spec *ss, const pa_memchunk *chunk) {
46 void *d;
47 unsigned i;
48
49 d = pa_memblock_acquire(chunk->memblock);
50
51 switch (ss->format) {
52
53 case PA_SAMPLE_U8:
54 case PA_SAMPLE_ULAW:
55 case PA_SAMPLE_ALAW: {
56 uint8_t *u = d;
57
58 for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
59 printf("0x%02x ", *(u++));
60
61 break;
62 }
63
64 case PA_SAMPLE_S16NE:
65 case PA_SAMPLE_S16RE: {
66 uint16_t *u = d;
67
68 for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
69 printf("0x%04x ", *(u++));
70
71 break;
72 }
73
74 case PA_SAMPLE_S32NE:
75 case PA_SAMPLE_S32RE: {
76 uint32_t *u = d;
77
78 for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
79 printf("0x%08x ", *(u++));
80
81 break;
82 }
83
84 case PA_SAMPLE_FLOAT32NE:
85 case PA_SAMPLE_FLOAT32RE: {
86 float *u = d;
87
88 for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
89 printf("%1.3g ", ss->format == PA_SAMPLE_FLOAT32NE ? *u : swap_float(*u));
90 u++;
91 }
92
93 break;
94 }
95
96 default:
97 pa_assert_not_reached();
98 }
99
100 printf("\n");
101
102 pa_memblock_release(chunk->memblock);
103 }
104
105 static pa_memblock* generate_block(pa_mempool *pool, const pa_sample_spec *ss) {
106 pa_memblock *r;
107 void *d;
108 unsigned i;
109
110 pa_assert_se(r = pa_memblock_new(pool, pa_frame_size(ss) * 10));
111 d = pa_memblock_acquire(r);
112
113 switch (ss->format) {
114
115 case PA_SAMPLE_U8:
116 case PA_SAMPLE_ULAW:
117 case PA_SAMPLE_ALAW: {
118 uint8_t *u = d;
119
120 u[0] = 0x00;
121 u[1] = 0xFF;
122 u[2] = 0x7F;
123 u[3] = 0x80;
124 u[4] = 0x9f;
125 u[5] = 0x3f;
126 u[6] = 0x1;
127 u[7] = 0xF0;
128 u[8] = 0x20;
129 u[9] = 0x21;
130 break;
131 }
132
133 case PA_SAMPLE_S16NE:
134 case PA_SAMPLE_S16RE: {
135 uint16_t *u = d;
136
137 u[0] = 0x0000;
138 u[1] = 0xFFFF;
139 u[2] = 0x7FFF;
140 u[3] = 0x8000;
141 u[4] = 0x9fff;
142 u[5] = 0x3fff;
143 u[6] = 0x1;
144 u[7] = 0xF000;
145 u[8] = 0x20;
146 u[9] = 0x21;
147 break;
148 }
149
150 case PA_SAMPLE_S32NE:
151 case PA_SAMPLE_S32RE: {
152 uint32_t *u = d;
153
154 u[0] = 0x00000001;
155 u[1] = 0xFFFF0002;
156 u[2] = 0x7FFF0003;
157 u[3] = 0x80000004;
158 u[4] = 0x9fff0005;
159 u[5] = 0x3fff0006;
160 u[6] = 0x10007;
161 u[7] = 0xF0000008;
162 u[8] = 0x200009;
163 u[9] = 0x21000A;
164 break;
165 }
166
167 case PA_SAMPLE_FLOAT32NE:
168 case PA_SAMPLE_FLOAT32RE: {
169 float *u = d;
170
171 u[0] = 0.0;
172 u[1] = -1.0;
173 u[2] = 1.0;
174 u[3] = 4711;
175 u[4] = 0.222;
176 u[5] = 0.33;
177 u[6] = -.3;
178 u[7] = 99;
179 u[8] = -0.555;
180 u[9] = -.123;
181
182 if (ss->format == PA_SAMPLE_FLOAT32RE)
183 for (i = 0; i < 10; i++)
184 u[i] = swap_float(u[i]);
185
186 break;
187 }
188
189 default:
190 pa_assert_not_reached();
191 }
192
193 pa_memblock_release(r);
194
195 return r;
196 }
197
198 int main(int argc, char *argv[]) {
199 pa_mempool *pool;
200 pa_sample_spec a, b;
201 pa_cvolume v;
202
203 oil_init();
204 pa_log_set_maximal_level(PA_LOG_DEBUG);
205
206 pa_assert_se(pool = pa_mempool_new(FALSE));
207
208 a.channels = b.channels = 1;
209 a.rate = b.rate = 44100;
210
211 v.channels = a.channels;
212 v.values[0] = pa_sw_volume_from_linear(0.5);
213
214 for (a.format = 0; a.format < PA_SAMPLE_MAX; a.format ++) {
215 for (b.format = 0; b.format < PA_SAMPLE_MAX; b.format ++) {
216
217 pa_resampler *forth, *back;
218 pa_memchunk i, j, k;
219
220 printf("=== %s -> %s -> %s -> /2\n",
221 pa_sample_format_to_string(a.format),
222 pa_sample_format_to_string(b.format),
223 pa_sample_format_to_string(a.format));
224
225 pa_assert_se(forth = pa_resampler_new(pool, &a, NULL, &b, NULL, PA_RESAMPLER_AUTO, 0));
226 pa_assert_se(back = pa_resampler_new(pool, &b, NULL, &a, NULL, PA_RESAMPLER_AUTO, 0));
227
228 i.memblock = generate_block(pool, &a);
229 i.length = pa_memblock_get_length(i.memblock);
230 i.index = 0;
231 pa_resampler_run(forth, &i, &j);
232 pa_resampler_run(back, &j, &k);
233
234 dump_block(&a, &i);
235 dump_block(&b, &j);
236 dump_block(&a, &k);
237
238 pa_memblock_unref(j.memblock);
239 pa_memblock_unref(k.memblock);
240
241 pa_volume_memchunk(&i, &a, &v);
242 dump_block(&a, &i);
243
244 pa_memblock_unref(i.memblock);
245
246 pa_resampler_free(forth);
247 pa_resampler_free(back);
248 }
249 }
250
251 pa_mempool_free(pool);
252
253 return 0;
254 }