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