]>
code.delx.au - pulseaudio/blob - src/tests/resampler-test.c
2 This file is part of PulseAudio.
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 of the License,
7 or (at your option) any later version.
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.
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
26 #include <pulse/sample.h>
27 #include <pulse/volume.h>
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>
35 #include <liboil/liboil.h>
37 static float swap_float(float a
) {
38 uint32_t *b
= (uint32_t*) &a
;
39 *b
= PA_UINT32_SWAP(*b
);
43 static void dump_block(const pa_sample_spec
*ss
, const pa_memchunk
*chunk
) {
47 d
= pa_memblock_acquire(chunk
->memblock
);
53 case PA_SAMPLE_ALAW
: {
56 for (i
= 0; i
< chunk
->length
/ pa_frame_size(ss
); i
++)
57 printf("0x%02x ", *(u
++));
63 case PA_SAMPLE_S16RE
: {
66 for (i
= 0; i
< chunk
->length
/ pa_frame_size(ss
); i
++)
67 printf("0x%04x ", *(u
++));
73 case PA_SAMPLE_S32RE
: {
76 for (i
= 0; i
< chunk
->length
/ pa_frame_size(ss
); i
++)
77 printf("0x%08x ", *(u
++));
82 case PA_SAMPLE_FLOAT32NE
:
83 case PA_SAMPLE_FLOAT32RE
: {
86 for (i
= 0; i
< chunk
->length
/ pa_frame_size(ss
); i
++) {
87 printf("%1.3g ", ss
->format
== PA_SAMPLE_FLOAT32NE
? *u
: swap_float(*u
));
95 pa_assert_not_reached();
100 pa_memblock_release(chunk
->memblock
);
103 static pa_memblock
* generate_block(pa_mempool
*pool
, const pa_sample_spec
*ss
) {
108 pa_assert_se(r
= pa_memblock_new(pool
, pa_frame_size(ss
) * 10));
109 d
= pa_memblock_acquire(r
);
111 switch (ss
->format
) {
115 case PA_SAMPLE_ALAW
: {
131 case PA_SAMPLE_S16NE
:
132 case PA_SAMPLE_S16RE
: {
148 case PA_SAMPLE_S32NE
:
149 case PA_SAMPLE_S32RE
: {
165 case PA_SAMPLE_FLOAT32NE
:
166 case PA_SAMPLE_FLOAT32RE
: {
180 if (ss
->format
== PA_SAMPLE_FLOAT32RE
)
181 for (i
= 0; i
< 10; i
++)
182 u
[i
] = swap_float(u
[i
]);
188 pa_assert_not_reached();
191 pa_memblock_release(r
);
196 int main(int argc
, char *argv
[]) {
202 pa_log_set_maximal_level(PA_LOG_DEBUG
);
204 pa_assert_se(pool
= pa_mempool_new(FALSE
));
206 a
.channels
= b
.channels
= 1;
207 a
.rate
= b
.rate
= 44100;
209 v
.channels
= a
.channels
;
210 v
.values
[0] = pa_sw_volume_from_linear(0.5);
212 for (a
.format
= 0; a
.format
< PA_SAMPLE_MAX
; a
.format
++) {
213 for (b
.format
= 0; b
.format
< PA_SAMPLE_MAX
; b
.format
++) {
215 pa_resampler
*forth
, *back
;
218 printf("=== %s -> %s -> %s -> /2\n",
219 pa_sample_format_to_string(a
.format
),
220 pa_sample_format_to_string(b
.format
),
221 pa_sample_format_to_string(a
.format
));
223 pa_assert_se(forth
= pa_resampler_new(pool
, &a
, NULL
, &b
, NULL
, PA_RESAMPLER_AUTO
, 0));
224 pa_assert_se(back
= pa_resampler_new(pool
, &b
, NULL
, &a
, NULL
, PA_RESAMPLER_AUTO
, 0));
226 i
.memblock
= generate_block(pool
, &a
);
227 i
.length
= pa_memblock_get_length(i
.memblock
);
229 pa_resampler_run(forth
, &i
, &j
);
230 pa_resampler_run(back
, &j
, &k
);
236 pa_memblock_unref(j
.memblock
);
237 pa_memblock_unref(k
.memblock
);
239 pa_volume_memchunk(&i
, &a
, &v
);
242 pa_memblock_unref(i
.memblock
);
244 pa_resampler_free(forth
);
245 pa_resampler_free(back
);
249 pa_mempool_free(pool
);