Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/mm/sample.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 18 апр. 2020 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21 #include <lsp-plug.in/common/endian.h>
22 #include <lsp-plug.in/test-fw/utest.h>
23 #include <lsp-plug.in/test-fw/ByteBuffer.h>
24 #include <lsp-plug.in/mm/sample.h>
25
26 namespace lsp
27 {
28 // u8 constants
29 static const uint8_t u8s[] = { 0xff, 0xbf, 0x80, 0x3f, 0x01 };
30 static const uint8_t s8s[] = { 0x7f, 0x3f, 0x00, 0xbf, 0x81 };
31
32 static const uint8_t u8t[] = { 0xff, 0xbf, 0x80, 0x3f, 0x00 };
33 static const uint8_t s8t[] = { 0x7f, 0x3f, 0x00, 0xbf, 0x80 };
34
35 static const uint8_t u8f[] = { 0xff, 0xbf, 0x80, 0x41, 0x01 }; // TODO: fix scale
36 static const uint8_t s8f[] = { 0x7f, 0x3f, 0x00, 0xc1, 0x81 }; // TODO: fix scale
37
38 // u16 constants
39 static const uint16_t u16s[] = { 0xffff, 0xbfff, 0x8000, 0x3fff, 0x0001 };
40 static const uint16_t s16s[] = { 0x7fff, 0x3fff, 0x0000, 0xbfff, 0x8001 };
41
42 static const uint16_t u16e8[] = { 0xff00, 0xbf00, 0x8000, 0x3f00, 0x0100 };
43 static const uint16_t s16e8[] = { 0x7f00, 0x3f00, 0x0000, 0xbf00, 0x8100 };
44
45 static const uint16_t u16t[] = { 0xffff, 0xbfff, 0x8000, 0x3fff, 0x0000 };
46 static const uint16_t s16t[] = { 0x7fff, 0x3fff, 0x0000, 0xbfff, 0x8000 };
47
48 static const uint16_t u16f[] = { 0xffff, 0xbfff, 0x8000, 0x4001, 0x0001 }; // TODO: fix scale
49 static const uint16_t s16f[] = { 0x7fff, 0x3fff, 0x0000, 0xc001, 0x8001 }; // TODO: fix scale
50
51 // u24 constants in LE form
52 static const uint8_t u24s[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 0x00, 0x00, 0x80, 0xff, 0xff, 0x3f, 0x01, 0x00, 0x00 };
53 static const uint8_t s24s[] = { 0xff, 0xff, 0x7f, 0xff, 0xff, 0x3f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xbf, 0x01, 0x00, 0x80 };
54
55 static const uint8_t u24e16[] = { 0x00, 0x00, 0xff, 0x00, 0x00, 0xbf, 0x00, 0x00, 0x80, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x01 };
56 static const uint8_t s24e16[] = { 0x00, 0x00, 0x7f, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbf, 0x00, 0x00, 0x81 };
57
58 static const uint8_t u24e8[] = { 0x00, 0xff, 0xff, 0x00, 0xff, 0xbf, 0x00, 0x00, 0x80, 0x00, 0xff, 0x3f, 0x00, 0x01, 0x00 };
59 static const uint8_t s24e8[] = { 0x00, 0xff, 0x7f, 0x00, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00, 0xff, 0xbf, 0x00, 0x01, 0x80 };
60
61 static const uint8_t u24t[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 0x00, 0x00, 0x80, 0xff, 0xff, 0x3f, 0x00, 0x00, 0x00 };
62 static const uint8_t s24t[] = { 0xff, 0xff, 0x7f, 0xff, 0xff, 0x3f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xbf, 0x00, 0x00, 0x80 };
63
64 static const uint8_t u24f[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 0x00, 0x00, 0x80, 0x01, 0x00, 0x40, 0x01, 0x00, 0x00 }; // TODO: fix scale
65 static const uint8_t s24f[] = { 0xff, 0xff, 0x7f, 0xff, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x01, 0x00, 0xc0, 0x01, 0x00, 0x80 }; // TODO: fix scale
66
67 // u32 constants
68 static const uint32_t u32s[] = { 0xffffffff, 0xbfffffff, 0x80000000, 0x3fffffff, 0x00000001 };
69 static const uint32_t s32s[] = { 0x7fffffff, 0x3fffffff, 0x00000000, 0xbfffffff, 0x80000001 };
70
71 static const uint32_t u32e8[] = { 0xffffff00, 0xbfffff00, 0x80000000, 0x3fffff00, 0x00000100 };
72 static const uint32_t s32e8[] = { 0x7fffff00, 0x3fffff00, 0x00000000, 0xbfffff00, 0x80000100 };
73
74 static const uint32_t u32e16[] = { 0xffff0000, 0xbfff0000, 0x80000000, 0x3fff0000, 0x00010000 };
75 static const uint32_t s32e16[] = { 0x7fff0000, 0x3fff0000, 0x00000000, 0xbfff0000, 0x80010000 };
76
77 static const uint32_t u32e24[] = { 0xff000000, 0xbf000000, 0x80000000, 0x3f000000, 0x01000000 };
78 static const uint32_t s32e24[] = { 0x7f000000, 0x3f000000, 0x00000000, 0xbf000000, 0x81000000 };
79
80 static const uint32_t u32f[] = { 0xffffffff, 0xbfffffff, 0x80000000, 0x40000001, 0x00000001 };
81 static const uint32_t s32f[] = { 0x7fffffff, 0x3fffffff, 0x00000000, 0xc0000001, 0x80000001 };
82
83 // float constants
84 static const mm::f32_t f32s[] = { 1.0f, 0.5f, 0.0f, -0.5f, -1.0f };
85 static const mm::f64_t f64s[] = { 1.0, 0.5, 0.0, -0.5, -1.0 };
86 }
87
88 UTEST_BEGIN("runtime.mm", sample)
89 void test_cvt(const char *cvt, const void *dst, size_t nbdst, const void *src, size_t nbsrc, size_t to, size_t from)
90 {
91 printf(" checking %s conversion...\n", cvt);
92
93 ByteBuffer sb(src, nbsrc);
94 ByteBuffer eb(dst, nbdst);
95 ByteBuffer db(nbdst);
96 db.fill_zero();
97
98 UTEST_ASSERT(mm::convert_samples(db.data<uint8_t>(), sb.data<uint8_t>(), 5, to, from));
99 UTEST_ASSERT(sb.valid());
100 UTEST_ASSERT(db.valid());
101
102 // sb.dump("sb"); db.dump("db"); eb.dump("eb");
103 UTEST_ASSERT(
104 ::memcmp(db.data<uint8_t>(), eb.data<uint8_t>(), nbdst) == 0,
105 sb.dump("sb"); db.dump("db"); eb.dump("eb")
106 );
107 }
108
109 void test_cvt_f32(const char *cvt, const void *dst, size_t nbdst, const void *src, size_t nbsrc, size_t to, size_t from, float tol)
110 {
111 printf(" checking %s conversion...\n", cvt);
112
113 ByteBuffer sb(src, nbsrc);
114 ByteBuffer eb(dst, nbdst);
115 ByteBuffer db(nbdst);
116 db.fill_zero();
117
118 UTEST_ASSERT(mm::convert_samples(db.data<uint8_t>(), sb.data<uint8_t>(), 5, to, from));
119 UTEST_ASSERT(sb.valid());
120 UTEST_ASSERT(db.valid());
121
122 mm::f32_t *a = db.data<mm::f32_t>();
123 mm::f32_t *b = eb.data<mm::f32_t>();
124
125 for (size_t i=0; i<5; ++i)
126 {
127 UTEST_ASSERT(
128 (abs(a[i] - b[i]) <= tol),
129
130 sb.dump("sb");
131 printf("db: ");
132 for (size_t j=0; j<5; ++j)
133 printf("%e ", a[j]);
134 printf("\neb: ");
135 for (size_t j=0; j<5; ++j)
136 printf("%e ", b[j]);
137 printf("\n");
138 );
139 }
140 }
141
142 void test_cvt_f64(const char *cvt, const void *dst, size_t nbdst, const void *src, size_t nbsrc, size_t to, size_t from, float tol)
143 {
144 printf(" checking %s conversion...\n", cvt);
145
146 ByteBuffer sb(src, nbsrc);
147 ByteBuffer eb(dst, nbdst);
148 ByteBuffer db(nbdst);
149 db.fill_zero();
150
151 UTEST_ASSERT(mm::convert_samples(db.data<uint8_t>(), sb.data<uint8_t>(), 5, to, from));
152 UTEST_ASSERT(sb.valid());
153 UTEST_ASSERT(db.valid());
154
155 mm::f64_t *a = db.data<mm::f64_t>();
156 mm::f64_t *b = eb.data<mm::f64_t>();
157
158 for (size_t i=0; i<5; ++i)
159 {
160 UTEST_ASSERT(
161 (abs(a[i] - b[i]) <= tol),
162
163 sb.dump("sb");
164 printf("db: ");
165 for (size_t j=0; j<5; ++j)
166 printf("%e ", a[j]);
167 printf("\neb: ");
168 for (size_t j=0; j<5; ++j)
169 printf("%e ", b[j]);
170 printf("\n");
171 );
172 }
173 }
174
175 void test_to_u8()
176 {
177 #define CVT(msg, exp, src, from) \
178 test_cvt(msg " -> u8", exp, sizeof(exp), src, sizeof(src), mm::SFMT_U8_CPU, mm::from);
179
180 CVT("u8 ", u8s, u8s, SFMT_U8_CPU);
181 CVT("u16", u8t, u16s, SFMT_U16_CPU);
182 CVT("u24", u8t, u24s, SFMT_U24_LE);
183 CVT("u32", u8t, u32s, SFMT_U32_CPU);
184
185 CVT("s8 ", u8s, s8s, SFMT_S8_CPU);
186 CVT("s16", u8t, s16s, SFMT_S16_CPU);
187 CVT("s24", u8t, s24s, SFMT_S24_LE);
188 CVT("s32", u8t, s32s, SFMT_S32_CPU);
189
190 CVT("f32", u8f, f32s, SFMT_F32_CPU);
191 CVT("f64", u8f, f64s, SFMT_F64_CPU);
192
193 #undef CVT
194 }
195
196 void test_to_s8()
197 {
198 #define CVT(msg, exp, src, from) \
199 test_cvt(msg " -> s8", exp, sizeof(exp), src, sizeof(src), mm::SFMT_S8_CPU, mm::from);
200
201 CVT("u8 ", s8s, u8s, SFMT_U8_CPU);
202 CVT("u16", s8t, u16s, SFMT_U16_CPU);
203 CVT("u24", s8t, u24s, SFMT_U24_LE);
204 CVT("u32", s8t, u32s, SFMT_U32_CPU);
205
206 CVT("s8 ", s8s, s8s, SFMT_S8_CPU);
207 CVT("s16", s8t, s16s, SFMT_S16_CPU);
208 CVT("s24", s8t, s24s, SFMT_S24_LE);
209 CVT("s32", s8t, s32s, SFMT_S32_CPU);
210
211 CVT("f32", s8f, f32s, SFMT_F32_CPU);
212 CVT("f64", s8f, f64s, SFMT_F64_CPU);
213
214 #undef CVT
215 }
216
217 void test_to_u16()
218 {
219 #define CVT(msg, exp, src, from) \
220 test_cvt(msg " -> u16", exp, sizeof(exp), src, sizeof(src), mm::SFMT_U16_CPU, mm::from);
221
222 CVT("u8 ", u16e8, u8s, SFMT_U8_CPU);
223 CVT("u16", u16s, u16s, SFMT_U16_CPU);
224 CVT("u24", u16t, u24s, SFMT_U24_LE);
225 CVT("u32", u16t, u32s, SFMT_U32_CPU);
226
227 CVT("s8 ", u16e8, s8s, SFMT_S8_CPU);
228 CVT("s16", u16s, s16s, SFMT_S16_CPU);
229 CVT("s24", u16t, s24s, SFMT_S24_LE);
230 CVT("s32", u16t, s32s, SFMT_S32_CPU);
231
232 CVT("f32", u16f, f32s, SFMT_F32_CPU);
233 CVT("f64", u16f, f64s, SFMT_F64_CPU);
234
235 #undef CVT
236 }
237
238 void test_to_s16()
239 {
240 #define CVT(msg, exp, src, from) \
241 test_cvt(msg " -> s16", exp, sizeof(exp), src, sizeof(src), mm::SFMT_S16_CPU, mm::from);
242
243 CVT("u8 ", s16e8, u8s, SFMT_U8_CPU);
244 CVT("u16", s16s, u16s, SFMT_U16_CPU);
245 CVT("u24", s16t, u24s, SFMT_U24_LE);
246 CVT("u32", s16t, u32s, SFMT_U32_CPU);
247
248 CVT("s8 ", s16e8, s8s, SFMT_S8_CPU);
249 CVT("s16", s16s, s16s, SFMT_S16_CPU);
250 CVT("s24", s16t, s24s, SFMT_S24_LE);
251 CVT("s32", s16t, s32s, SFMT_S32_CPU);
252
253 CVT("f32", s16f, f32s, SFMT_F32_CPU);
254 CVT("f64", s16f, f64s, SFMT_F64_CPU);
255
256 #undef CVT
257 }
258
259 void test_to_u24()
260 {
261 #define CVT(msg, exp, src, from) \
262 test_cvt(msg " -> u24", exp, sizeof(exp), src, sizeof(src), mm::SFMT_U24_LE, mm::from);
263
264 CVT("u8 ", u24e16, u8s, SFMT_U8_CPU);
265 CVT("u16", u24e8, u16s, SFMT_U16_CPU);
266 CVT("u24", u24s, u24s, SFMT_U24_LE);
267 CVT("u32", u24t, u32s, SFMT_U32_CPU);
268
269 CVT("s8 ", u24e16, s8s, SFMT_S8_CPU);
270 CVT("s16", u24e8, s16s, SFMT_S16_CPU);
271 CVT("s24", u24s, s24s, SFMT_S24_LE);
272 CVT("s32", u24t, s32s, SFMT_S32_CPU);
273
274 CVT("f32", u24f, f32s, SFMT_F32_CPU);
275 CVT("f64", u24f, f64s, SFMT_F64_CPU);
276
277 #undef CVT
278 }
279
280 void test_to_s24()
281 {
282 #define CVT(msg, exp, src, from) \
283 test_cvt(msg " -> s24", exp, sizeof(exp), src, sizeof(src), mm::SFMT_S24_LE, mm::from);
284
285 CVT("u8 ", s24e16, u8s, SFMT_U8_CPU);
286 CVT("u16", s24e8, u16s, SFMT_U16_CPU);
287 CVT("u24", s24s, u24s, SFMT_U24_LE);
288 CVT("u32", s24t, u32s, SFMT_U32_CPU);
289
290 CVT("s8 ", s24e16, s8s, SFMT_S8_CPU);
291 CVT("s16", s24e8, s16s, SFMT_S16_CPU);
292 CVT("s24", s24s, s24s, SFMT_S24_LE);
293 CVT("s32", s24t, s32s, SFMT_S32_CPU);
294
295 CVT("f32", s24f, f32s, SFMT_F32_CPU);
296 CVT("f64", s24f, f64s, SFMT_F64_CPU);
297
298 #undef CVT
299 }
300
301 void test_to_u32()
302 {
303 #define CVT(msg, exp, src, from) \
304 test_cvt(msg " -> u32", exp, sizeof(exp), src, sizeof(src), mm::SFMT_U32_CPU, mm::from);
305
306 CVT("u8 ", u32e24, u8s, SFMT_U8_CPU);
307 CVT("u16", u32e16, u16s, SFMT_U16_CPU);
308 CVT("u24", u32e8, u24s, SFMT_U24_LE);
309 CVT("u32", u32s, u32s, SFMT_U32_CPU);
310
311 CVT("s8 ", u32e24, s8s, SFMT_S8_CPU);
312 CVT("s16", u32e16, s16s, SFMT_S16_CPU);
313 CVT("s24", u32e8, s24s, SFMT_S24_LE);
314 CVT("s32", u32s, s32s, SFMT_S32_CPU);
315
316 CVT("f32", u32f, f32s, SFMT_F32_CPU);
317 CVT("f64", u32f, f64s, SFMT_F64_CPU);
318
319 #undef CVT
320 }
321
322 void test_to_s32()
323 {
324 #define CVT(msg, exp, src, from) \
325 test_cvt(msg " -> s32", exp, sizeof(exp), src, sizeof(src), mm::SFMT_S32_CPU, mm::from);
326
327 CVT("u8 ", s32e24, u8s, SFMT_U8_CPU);
328 CVT("u16", s32e16, u16s, SFMT_U16_CPU);
329 CVT("u24", s32e8, u24s, SFMT_U24_LE);
330 CVT("u32", s32s, u32s, SFMT_U32_CPU);
331
332 CVT("s8 ", s32e24, s8s, SFMT_S8_CPU);
333 CVT("s16", s32e16, s16s, SFMT_S16_CPU);
334 CVT("s24", s32e8, s24s, SFMT_S24_LE);
335 CVT("s32", s32s, s32s, SFMT_S32_CPU);
336
337 CVT("f32", s32f, f32s, SFMT_F32_CPU);
338 CVT("f64", s32f, f64s, SFMT_F64_CPU);
339
340 #undef CVT
341 }
342
343 void test_to_f32()
344 {
345 #define CVT(msg, tol, src, from) \
346 test_cvt_f32(msg " -> f32", f32s, sizeof(f32s), src, sizeof(src), mm::SFMT_F32_CPU, mm::from, tol);
347
348 CVT("u8 ", 0.04, u8s, SFMT_U8_CPU);
349 CVT("u16", 1e-4, u16s, SFMT_U16_CPU);
350 CVT("u24", 2e-7, u24s, SFMT_U24_LE);
351 CVT("u32", 5e-9, u32s, SFMT_U32_CPU);
352
353 CVT("s8 ", 0.04, s8s, SFMT_S8_CPU);
354 CVT("s16", 1e-4, s16s, SFMT_S16_CPU);
355 CVT("s24", 2e-7, s24s, SFMT_S24_LE);
356 CVT("s32", 5e-9, s32s, SFMT_S32_CPU);
357
358 CVT("f32", 2e-7, f32s, SFMT_F32_CPU);
359 CVT("f64", 5e-10, f64s, SFMT_F64_CPU);
360
361 #undef CVT
362 }
363
364 void test_to_f64()
365 {
366 #define CVT(msg, tol, src, from) \
367 test_cvt_f64(msg " -> f64", f64s, sizeof(f64s), src, sizeof(src), mm::SFMT_F64_CPU, mm::from, tol);
368
369 CVT("u8 ", 0.04, u8s, SFMT_U8_CPU);
370 CVT("u16", 1e-4, u16s, SFMT_U16_CPU);
371 CVT("u24", 2e-7, u24s, SFMT_U24_LE);
372 CVT("u32", 5e-9, u32s, SFMT_U32_CPU);
373
374 CVT("s8 ", 0.04, s8s, SFMT_S8_CPU);
375 CVT("s16", 1e-4, s16s, SFMT_S16_CPU);
376 CVT("s24", 2e-7, s24s, SFMT_S24_LE);
377 CVT("s32", 5e-9, s32s, SFMT_S32_CPU);
378
379 CVT("f32", 2e-7, f32s, SFMT_F32_CPU);
380 CVT("f64", 5e-10, f64s, SFMT_F64_CPU);
381
382 #undef CVT
383 }
384
385 UTEST_MAIN
386 {
387 #define CALL(func) \
388 printf("Calling %s\n", #func); \
389 func();
390
391 CALL(test_to_u8);
392 CALL(test_to_s8);
393 CALL(test_to_u16);
394 CALL(test_to_s16);
395 CALL(test_to_u24);
396 CALL(test_to_s24);
397 CALL(test_to_u32);
398 CALL(test_to_s32);
399 CALL(test_to_f32);
400 CALL(test_to_f64);
401 }
402 UTEST_END;
403
404