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, see <http://www.gnu.org/licenses/>.
16 ***/
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include <stdio.h>
23 #include <math.h>
24
25 #include <check.h>
26
27 #include <pulse/sample.h>
28 #include <pulse/volume.h>
29
30 #include <pulsecore/macro.h>
31 #include <pulsecore/endianmacros.h>
32 #include <pulsecore/memblock.h>
33 #include <pulsecore/sample-util.h>
34 #include <pulsecore/mix.h>
35
36 /* PA_SAMPLE_U8 */
37 static const uint8_t u8_result[3][10] = {
38 { 0x00, 0xff, 0x7f, 0x80, 0x9f, 0x3f, 0x01, 0xf0, 0x20, 0x21 },
39 { 0x0c, 0xf2, 0x7f, 0x80, 0x9b, 0x45, 0x0d, 0xe4, 0x29, 0x2a },
40 { 0x00, 0xff, 0x7e, 0x80, 0xba, 0x04, 0x00, 0xff, 0x00, 0x00 },
41 };
42
43 /* PA_SAMPLE_ALAW */
44 static const uint8_t alaw_result[3][10] = {
45 { 0x00, 0xff, 0x7f, 0x80, 0x9f, 0x3f, 0x01, 0xf0, 0x20, 0x21 },
46 { 0x06, 0xf2, 0x72, 0x86, 0x92, 0x32, 0x07, 0xf6, 0x26, 0x27 },
47 { 0x31, 0xec, 0x6d, 0xb1, 0x8c, 0x2d, 0x36, 0xe1, 0x2a, 0x2a },
48 };
49
50 /* PA_SAMPLE_ULAW */
51 static const uint8_t ulaw_result[3][10] = {
52 { 0x00, 0xff, 0x7f, 0x80, 0x9f, 0x3f, 0x01, 0xf0, 0x20, 0x21 },
53 { 0x03, 0xff, 0xff, 0x83, 0xa2, 0x42, 0x04, 0xf2, 0x23, 0x24 },
54 { 0x00, 0xff, 0xff, 0x80, 0x91, 0x31, 0x00, 0xe9, 0x12, 0x13 },
55 };
56
57 static const uint16_t s16ne_result[3][10] = {
58 { 0x0000, 0xffff, 0x7fff, 0x8000, 0x9fff, 0x3fff, 0x0001, 0xf000, 0x0020, 0x0021 },
59 { 0x0000, 0xffff, 0x7332, 0x8ccd, 0xa998, 0x3998, 0x0000, 0xf199, 0x001c, 0x001d },
60 { 0x0000, 0xfffe, 0x7fff, 0x8000, 0x8000, 0x7997, 0x0001, 0xe199, 0x003c, 0x003e },
61 };
62
63 static const float float32ne_result[3][10] = {
64 { 0.000000, -1.000000, 1.000000, 4711.000000, 0.222000, 0.330000, -0.300000, 99.000000, -0.555000, -0.123000 },
65 { 0.000000, -0.899987, 0.899987, 4239.837402, 0.199797, 0.296996, -0.269996, 89.098679, -0.499493, -0.110698 },
66 { 0.000000, -1.899987, 1.899987, 8950.837891, 0.421797, 0.626996, -0.569996, 188.098679, -1.054493, -0.233698 },
67 };
68
69 static const uint32_t s32ne_result[3][10] = {
70 { 0x00000001, 0xffff0002, 0x7fff0003, 0x80000004, 0x9fff0005, 0x3fff0006, 0x00010007, 0xf0000008, 0x00200009, 0x0021000a },
71 { 0x00000000, 0xffff199b, 0x7332199c, 0x8ccd0003, 0xa998d99e, 0x3998999f, 0x0000e66c, 0xf199a007, 0x001cccc8, 0x001db32e },
72 { 0x00000001, 0xfffe199d, 0x7fffffff, 0x80000000, 0x80000000, 0x799799a5, 0x0001e673, 0xe199a00f, 0x003cccd1, 0x003eb338 },
73 };
74
75 /* attention: result is in BE, not NE! */
76 static const uint8_t s24be_result[3][30] = {
77 { 0x00, 0x00, 0x01, 0xff, 0xff, 0x02, 0x7f, 0xff, 0x03, 0x80, 0x00, 0x04, 0x9f, 0xff, 0x05, 0x3f, 0xff, 0x06, 0x01, 0x00, 0x07, 0xf0, 0x00, 0x08, 0x20, 0x00, 0x09, 0x21, 0x00, 0x0a },
78 { 0x00, 0x00, 0x00, 0xff, 0xff, 0x1b, 0x73, 0x32, 0x1c, 0x8c, 0xcd, 0x03, 0xa9, 0x98, 0xde, 0x39, 0x98, 0x9f, 0x00, 0xe6, 0x6c, 0xf1, 0x99, 0xa7, 0x1c, 0xcc, 0xc8, 0x1d, 0xb3, 0x2e },
79 { 0x00, 0x00, 0x01, 0xff, 0xfe, 0x1d, 0x7f, 0xff, 0xff, 0x80, 0x00, 0x00, 0x80, 0x00, 0x00, 0x79, 0x97, 0xa5, 0x01, 0xe6, 0x73, 0xe1, 0x99, 0xaf, 0x3c, 0xcc, 0xd1, 0x3e, 0xb3, 0x38 },
80 };
81
82 static const uint32_t s24_32ne_result[3][10] = {
83 { 0x00000001, 0xffff0002, 0x7fff0003, 0x80000004, 0x9fff0005, 0x3fff0006, 0x00010007, 0xf0000008, 0x00200009, 0x0021000a },
84 { 0x00000000, 0x00ff199b, 0x00ff199c, 0x00000003, 0x00ff199e, 0x00ff199f, 0x0000e66c, 0x00000007, 0x001cccc8, 0x001db32e },
85 { 0x00000001, 0x00fe199d, 0x00fe199f, 0x00000007, 0x00fe19a3, 0x00fe19a5, 0x0001e673, 0x0000000f, 0x003cccd1, 0x003eb338 },
86 };
87
compare_block(const pa_sample_spec * ss,const pa_memchunk * chunk,int iter)88 static void compare_block(const pa_sample_spec *ss, const pa_memchunk *chunk, int iter) {
89 void *d;
90 unsigned i;
91
92 d = pa_memblock_acquire(chunk->memblock);
93
94 switch (ss->format) {
95 case PA_SAMPLE_U8: {
96 const uint8_t *v = u8_result[iter];
97 uint8_t *u = d;
98
99 for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
100 fail_unless(*u == *v);
101 ++u;
102 ++v;
103 }
104 break;
105 }
106
107 case PA_SAMPLE_ALAW: {
108 const uint8_t *v = alaw_result[iter];
109 uint8_t *u = d;
110
111 for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
112 fail_unless(*u == *v);
113 ++u;
114 ++v;
115 }
116 break;
117 }
118
119 case PA_SAMPLE_ULAW: {
120 const uint8_t *v = ulaw_result[iter];
121 uint8_t *u = d;
122
123 for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
124 fail_unless(*u == *v);
125 ++u;
126 ++v;
127 }
128 break;
129 }
130
131 case PA_SAMPLE_S16NE:
132 case PA_SAMPLE_S16RE: {
133 const uint16_t *v = s16ne_result[iter];
134 uint16_t *u = d;
135
136 for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
137 uint16_t uu = PA_MAYBE_UINT16_SWAP(ss->format != PA_SAMPLE_S16NE, *u);
138 fail_unless(uu == *v);
139 ++u;
140 ++v;
141 }
142 break;
143 }
144
145 case PA_SAMPLE_FLOAT32NE:
146 case PA_SAMPLE_FLOAT32RE: {
147 const float *v = float32ne_result[iter];
148 float *u = d;
149
150 for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
151 float uu = ss->format == PA_SAMPLE_FLOAT32NE ? *u : PA_READ_FLOAT32RE(u);
152 fail_unless(fabsf(uu - *v) <= 1e-6f);
153 ++u;
154 ++v;
155 }
156 break;
157 }
158
159 case PA_SAMPLE_S32NE:
160 case PA_SAMPLE_S32RE: {
161 const uint32_t *v = s32ne_result[iter];
162 uint32_t *u = d;
163
164 for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
165 uint32_t uu = PA_MAYBE_UINT32_SWAP(ss->format != PA_SAMPLE_S32NE, *u);
166 fail_unless(uu == *v);
167 ++u;
168 ++v;
169 }
170 break;
171 }
172
173 case PA_SAMPLE_S24_32NE:
174 case PA_SAMPLE_S24_32RE: {
175 const uint32_t *v = s24_32ne_result[iter];
176 uint32_t *u = d;
177
178 for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
179 uint32_t uu = PA_MAYBE_UINT32_SWAP(ss->format != PA_SAMPLE_S24_32NE, *u);
180 fail_unless(uu == *v);
181 ++u;
182 ++v;
183 }
184 break;
185 }
186
187 case PA_SAMPLE_S24NE:
188 case PA_SAMPLE_S24RE: {
189 const uint8_t *v = s24be_result[iter];
190 uint8_t *u = d;
191
192 for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
193 uint32_t uu = ss->format == PA_SAMPLE_S24LE ? PA_READ24LE(u) : PA_READ24BE(u);
194 fail_unless(uu == PA_READ24BE(v));
195
196 u += 3;
197 v += 3;
198 }
199 break;
200 }
201
202 default:
203 pa_assert_not_reached();
204 }
205
206 pa_memblock_release(chunk->memblock);
207 }
208
generate_block(pa_mempool * pool,const pa_sample_spec * ss)209 static pa_memblock* generate_block(pa_mempool *pool, const pa_sample_spec *ss) {
210 pa_memblock *r;
211 void *d;
212 unsigned i;
213
214 pa_assert_se(r = pa_memblock_new(pool, pa_frame_size(ss) * 10));
215 d = pa_memblock_acquire(r);
216
217 switch (ss->format) {
218
219 case PA_SAMPLE_U8:
220 case PA_SAMPLE_ULAW:
221 case PA_SAMPLE_ALAW: {
222 memcpy(d, u8_result[0], sizeof(u8_result[0]));
223 break;
224 }
225
226 case PA_SAMPLE_S16NE:
227 case PA_SAMPLE_S16RE: {
228 if (ss->format == PA_SAMPLE_S16RE) {
229 uint16_t *u = d;
230 for (i = 0; i < 10; i++)
231 u[i] = PA_UINT16_SWAP(s16ne_result[0][i]);
232 } else
233 memcpy(d, s16ne_result[0], sizeof(s16ne_result[0]));
234 break;
235 }
236
237 case PA_SAMPLE_S24_32NE:
238 case PA_SAMPLE_S24_32RE:
239 case PA_SAMPLE_S32NE:
240 case PA_SAMPLE_S32RE: {
241 if (ss->format == PA_SAMPLE_S24_32RE || ss->format == PA_SAMPLE_S32RE) {
242 uint32_t *u = d;
243 for (i = 0; i < 10; i++)
244 u[i] = PA_UINT32_SWAP(s32ne_result[0][i]);
245 } else
246 memcpy(d, s32ne_result[0], sizeof(s32ne_result[0]));
247 break;
248 }
249
250 case PA_SAMPLE_S24NE:
251 case PA_SAMPLE_S24RE:
252 if (ss->format == PA_SAMPLE_S24LE) {
253 uint8_t *u = d;
254 for (i = 0; i < 30; i += 3)
255 PA_WRITE24LE(&u[i], PA_READ24BE(&s24be_result[0][i]));
256 } else
257 memcpy(d, s24be_result[0], sizeof(s24be_result[0]));
258 break;
259
260 case PA_SAMPLE_FLOAT32NE:
261 case PA_SAMPLE_FLOAT32RE: {
262 if (ss->format == PA_SAMPLE_FLOAT32RE) {
263 float *u = d;
264 for (i = 0; i < 10; i++)
265 PA_WRITE_FLOAT32RE(&u[i], float32ne_result[0][i]);
266 } else
267 memcpy(d, float32ne_result[0], sizeof(float32ne_result[0]));
268
269 break;
270 }
271
272 default:
273 pa_assert_not_reached();
274 }
275
276 pa_memblock_release(r);
277
278 return r;
279 }
280
START_TEST(mix_test)281 START_TEST (mix_test) {
282 pa_mempool *pool;
283 pa_sample_spec a;
284 pa_cvolume v;
285
286 if (!getenv("MAKE_CHECK"))
287 pa_log_set_level(PA_LOG_DEBUG);
288
289 fail_unless((pool = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0, true)) != NULL);
290
291 a.channels = 1;
292 a.rate = 44100;
293
294 v.channels = a.channels;
295 v.values[0] = pa_sw_volume_from_linear(0.9);
296
297 for (a.format = 0; a.format < PA_SAMPLE_MAX; a.format ++) {
298 pa_memchunk i, j, k;
299 pa_mix_info m[2];
300 void *ptr;
301
302 pa_log_debug("=== mixing: %s", pa_sample_format_to_string(a.format));
303
304 /* Generate block */
305 i.memblock = generate_block(pool, &a);
306 i.length = pa_memblock_get_length(i.memblock);
307 i.index = 0;
308
309 /* Make a copy */
310 j = i;
311 pa_memblock_ref(j.memblock);
312 pa_memchunk_make_writable(&j, 0);
313
314 /* Adjust volume of the copy */
315 pa_volume_memchunk(&j, &a, &v);
316
317 compare_block(&a, &j, 1);
318
319 m[0].chunk = i;
320 m[0].volume.values[0] = PA_VOLUME_NORM;
321 m[0].volume.channels = a.channels;
322 m[1].chunk = j;
323 m[1].volume.values[0] = PA_VOLUME_NORM;
324 m[1].volume.channels = a.channels;
325
326 k.memblock = pa_memblock_new(pool, i.length);
327 k.length = i.length;
328 k.index = 0;
329
330 ptr = pa_memblock_acquire_chunk(&k);
331 pa_mix(m, 2, ptr, k.length, &a, NULL, false);
332 pa_memblock_release(k.memblock);
333
334 compare_block(&a, &k, 2);
335
336 pa_memblock_unref(i.memblock);
337 pa_memblock_unref(j.memblock);
338 pa_memblock_unref(k.memblock);
339 }
340
341 pa_mempool_unref(pool);
342 }
343 END_TEST
344
main(int argc,char * argv[])345 int main(int argc, char *argv[]) {
346 int failed = 0;
347 Suite *s;
348 TCase *tc;
349 SRunner *sr;
350
351 s = suite_create("Mix");
352 tc = tcase_create("mix");
353 tcase_add_test(tc, mix_test);
354 suite_add_tcase(s, tc);
355
356 sr = srunner_create(s);
357 srunner_run_all(sr, CK_NORMAL);
358 failed = srunner_ntests_failed(sr);
359 srunner_free(sr);
360
361 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
362 }
363