1#include <string.h>
2
3#include "wuff_config.h"
4#include "wuff.h"
5#include "wuff_convert.h"
6
7/*
8 * int8 functions.
9 */
10
11WUFF_CONV_FUNC(wuff_int8_to_int8)
12{
13 (void)offset;
14 memcpy(dst, src, samples + head + tail);
15}
16
17WUFF_CONV_FUNC(wuff_int8_to_int16)
18{
19 wuff_sint16 i16;
20 size_t i;
21
22 if (head != 0)
23 {
24 i16 = (src[0] - 128) << 8;
25 memcpy(dst, (wuff_uint8 *)&i16 + offset, head);
26 src += 1;
27 dst += head;
28 }
29
30 for (i = 0; i < samples; i++)
31 {
32 i16 = (src[i] - 128) << 8;
33 memcpy(dst + i * 2, &i16, 2);
34 }
35
36 if (tail != 0)
37 {
38 i16 = (src[samples] - 128) << 8;
39 memcpy(dst + samples * 2, &i16, tail);
40 }
41}
42
43WUFF_CONV_FUNC(wuff_int8_to_int24)
44{
45 wuff_sint32 i24;
46 size_t i;
47
48 if (head != 0)
49 {
50 i24 = (src[0] - 128) << 24;
51 memcpy(dst, (wuff_uint8 *)&i24 + 1 + offset, head);
52 src += 1;
53 dst += head;
54 }
55
56 for (i = 0; i < samples; i++)
57 {
58 i24 = (src[i] - 128) << 24;
59 memcpy(dst + i * 3, (wuff_uint8 *)&i24 + 1, 3);
60 }
61
62 if (tail != 0)
63 {
64 i24 = (src[samples] - 128) << 24;
65 memcpy(dst + samples * 3, (wuff_uint8 *)&i24 + 1, tail);
66 }
67}
68
69WUFF_CONV_FUNC(wuff_int8_to_int32)
70{
71 wuff_sint32 i32;
72 size_t i;
73
74 if (head != 0)
75 {
76 i32 = (src[0] - 128) << 24;
77 memcpy(dst, (wuff_uint8 *)&i32 + offset, head);
78 src += 1;
79 dst += head;
80 }
81
82 for (i = 0; i < samples; i++)
83 {
84 i32 = (src[i] - 128) << 24;
85 memcpy(dst + i * 4, &i32, 4);
86 }
87
88 if (tail != 0)
89 {
90 i32 = (src[samples] - 128) << 24;
91 memcpy(dst + samples * 4, &i32, tail);
92 }
93}
94
95WUFF_CONV_FUNC(wuff_int8_to_float32)
96{
97 float f32;
98 size_t i;
99
100 if (head != 0)
101 {
102 f32 = (float)(src[0] - 128) / 128.0f;
103 memcpy(dst, (wuff_uint8 *)&f32 + offset, head);
104 src += 1;
105 dst += head;
106 }
107
108 for (i = 0; i < samples; i++)
109 {
110 f32 = (float)(src[i] - 128) / 128.0f;
111 memcpy(dst + i * 4, &f32, 4);
112 }
113
114 if (tail != 0)
115 {
116 f32 = (float)(src[samples] - 128) / 128.0f;
117 memcpy(dst + samples * 4, &f32, tail);
118 }
119}
120
121WUFF_CONV_FUNC(wuff_int8_to_float64)
122{
123 double f64;
124 size_t i;
125
126 if (head != 0)
127 {
128 f64 = (double)(src[0] - 128) / 128.0;
129 memcpy(dst, (wuff_uint8 *)&f64 + offset, head);
130 src += 1;
131 dst += head;
132 }
133
134 for (i = 0; i < samples; i++)
135 {
136 f64 = (double)(src[i] - 128) / 128.0;
137 memcpy(dst + i * 8, &f64, 8);
138 }
139
140 if (tail != 0)
141 {
142 f64 = (double)(src[samples] - 128) / 128.0;
143 memcpy(dst + samples * 8, &f64, tail);
144 }
145}
146
147/*
148 * int16 functions.
149 */
150
151WUFF_CONV_FUNC(wuff_int16_to_int8)
152{
153 wuff_sint16 i16;
154 size_t i;
155 (void)offset; (void)head; (void)tail;
156
157 for (i = 0; i < samples; i++)
158 {
159 memcpy(&i16, src + i * 2, 2);
160 dst[i] = (i16 >> 8) + 128;
161 }
162}
163
164WUFF_CONV_FUNC(wuff_int16_to_int16)
165{
166 memcpy(dst, src + offset, samples * 2 + head + tail);
167}
168
169WUFF_CONV_FUNC(wuff_int16_to_int24)
170{
171 wuff_sint16 i16;
172 wuff_sint32 i24;
173 size_t i;
174
175 if (head != 0)
176 {
177 memcpy(&i16, src, 2);
178 i24 = i16 << 16;
179 memcpy(dst, (wuff_uint8 *)&i24 + 1 + offset, head);
180 src += 2;
181 dst += head;
182 }
183
184 for (i = 0; i < samples; i++)
185 {
186 memcpy(&i16, src + i * 2, 2);
187 i24 = i16 << 16;
188 memcpy(dst + i * 3, (wuff_uint8 *)&i24 + 1, 3);
189 }
190
191 if (tail != 0)
192 {
193 memcpy(&i16, src + samples * 2, 2);
194 i24 = i16 << 16;
195 memcpy(dst + samples * 3, (wuff_uint8 *)&i24 + 1, tail);
196 }
197}
198
199WUFF_CONV_FUNC(wuff_int16_to_int32)
200{
201 wuff_sint16 i16;
202 wuff_sint32 i32;
203 size_t i;
204
205 if (head != 0)
206 {
207 memcpy(&i16, src, 2);
208 i32 = i16 << 16;
209 memcpy(dst, (wuff_uint8 *)&i32 + offset, head);
210 src += 2;
211 dst += head;
212 }
213
214 for (i = 0; i < samples; i++)
215 {
216 memcpy(&i16, src + i * 2, 2);
217 i32 = i16 << 16;
218 memcpy(dst + i * 4, &i32, 4);
219 }
220
221 if (tail != 0)
222 {
223 memcpy(&i16, src + samples * 2, 2);
224 i32 = i16 << 16;
225 memcpy(dst + samples * 4, &i32, tail);
226 }
227}
228
229WUFF_CONV_FUNC(wuff_int16_to_float32)
230{
231 wuff_sint16 i16;
232 float f32;
233 size_t i;
234
235 if (head != 0)
236 {
237 memcpy(&i16, src, 2);
238 f32 = (float)i16 / 32768.0f;
239 memcpy(dst, (wuff_uint8 *)&f32 + offset, head);
240 src += 2;
241 dst += head;
242 }
243
244 for (i = 0; i < samples; i++)
245 {
246 memcpy(&i16, src + i * 2, 2);
247 f32 = (float)i16 / 32768.0f;
248 memcpy(dst + i * 4, &f32, 4);
249 }
250
251 if (tail != 0)
252 {
253 memcpy(&i16, src + samples * 2, 2);
254 f32 = (float)i16 / 32768.0f;
255 memcpy(dst + samples * 4, &f32, tail);
256 }
257}
258
259WUFF_CONV_FUNC(wuff_int16_to_float64)
260{
261 wuff_sint16 i16;
262 double f64;
263 size_t i;
264
265 if (head != 0)
266 {
267 memcpy(&i16, src, 2);
268 f64 = (double)i16 / 32768.0;
269 memcpy(dst, (wuff_uint8 *)&f64 + offset, head);
270 src += 2;
271 dst += head;
272 }
273
274 for (i = 0; i < samples; i++)
275 {
276 memcpy(&i16, src + i * 2, 2);
277 f64 = (double)i16 / 32768.0;
278 memcpy(dst + i * 8, &f64, 8);
279 }
280
281 if (tail != 0)
282 {
283 memcpy(&i16, src + samples * 2, 2);
284 f64 = (double)i16 / 32768.0;
285 memcpy(dst + samples * 8, &f64, tail);
286 }
287}
288
289/*
290 * int24 functions.
291 */
292
293WUFF_CONV_FUNC(wuff_int24_to_int8)
294{
295 wuff_sint32 i24 = 0;
296 size_t i;
297 (void)offset; (void)head; (void)tail;
298
299 for (i = 0; i < samples; i++)
300 {
301 memcpy((wuff_uint8 *)&i24 + 1, src + i * 3, 3);
302 dst[i] = (wuff_uint8)((i24 >> 16) + 128);
303 }
304}
305
306WUFF_CONV_FUNC(wuff_int24_to_int16)
307{
308 size_t i;
309
310 if (head != 0)
311 {
312 memcpy(dst, src + 1 + offset, head);
313 src += 3;
314 dst += head;
315 }
316
317 for (i = 0; i < samples; i++)
318 {
319 memcpy(dst + i * 2, src + 1 + i * 3, 2);
320 }
321
322 if (tail != 0)
323 {
324 memcpy(dst + samples * 2, src + 1 + samples * 3, tail);
325 }
326}
327
328WUFF_CONV_FUNC(wuff_int24_to_int24)
329{
330 memcpy(dst, src + offset, samples * 3 + head + tail);
331}
332
333WUFF_CONV_FUNC(wuff_int24_to_int32)
334{
335 wuff_sint32 i32 = 0;
336 size_t i;
337
338 if (head != 0)
339 {
340 memcpy((wuff_uint8 *)&i32 + 1, src, 3);
341 memcpy(dst, (wuff_uint8 *)&i32 + offset, head);
342 src += 3;
343 dst += head;
344 }
345
346 for (i = 0; i < samples; i++)
347 {
348 memcpy((wuff_uint8 *)&i32 + 1, src + i * 3, 3);
349 memcpy(dst + i * 4, &i32, 4);
350 }
351
352 if (tail != 0)
353 {
354 memcpy((wuff_uint8 *)&i32 + 1, src + samples * 3, 3);
355 memcpy(dst + samples * 4, &i32, tail);
356 }
357}
358
359WUFF_CONV_FUNC(wuff_int24_to_float32)
360{
361 wuff_sint32 i24 = 0;
362 float f32;
363 size_t i;
364
365 if (head != 0)
366 {
367 memcpy((wuff_uint8 *)&i24 + 1, src, 3);
368 f32 = (float)((double)i24 / 2147483648.0);
369 memcpy(dst, (wuff_uint8 *)&f32 + offset, head);
370 src += 3;
371 dst += head;
372 }
373
374 for (i = 0; i < samples; i++)
375 {
376 memcpy((wuff_uint8 *)&i24 + 1, src + i * 3, 3);
377 f32 = (float)((double)i24 / 2147483648.0);
378 memcpy(dst + i * 4, &f32, 4);
379 }
380
381 if (tail != 0)
382 {
383 memcpy((wuff_uint8 *)&i24 + 1, src + samples * 3, 3);
384 f32 = (float)((double)i24 / 2147483648.0);
385 memcpy(dst + samples * 4, &f32, tail);
386 }
387}
388
389WUFF_CONV_FUNC(wuff_int24_to_float64)
390{
391 wuff_sint32 i24 = 0;
392 double f64;
393 size_t i;
394
395 if (head != 0)
396 {
397 memcpy((wuff_uint8 *)&i24 + 1, src, 3);
398 f64 = (double)i24 / 2147483648.0;
399 memcpy(dst, (wuff_uint8 *)&f64 + offset, head);
400 src += 3;
401 dst += head;
402 }
403
404 for (i = 0; i < samples; i++)
405 {
406 memcpy((wuff_uint8 *)&i24 + 1, src + i * 3, 3);
407 f64 = (double)i24 / 2147483648.0;
408 memcpy(dst + i * 8, &f64, 8);
409 }
410
411 if (tail != 0)
412 {
413 memcpy((wuff_uint8 *)&i24 + 1, src + samples * 3, 3);
414 f64 = (double)i24 / 2147483648.0;
415 memcpy(dst + samples * 8, &f64, tail);
416 }
417}
418
419/*
420 * int32 functions.
421 */
422
423WUFF_CONV_FUNC(wuff_int32_to_int8)
424{
425 wuff_sint32 i32 = 0;
426 size_t i;
427 (void)offset; (void)head; (void)tail;
428
429 for (i = 0; i < samples; i++)
430 {
431 memcpy(&i32, src + i * 4, 4);
432 dst[i] = (i32 >> 24) + 128;
433 }
434}
435
436WUFF_CONV_FUNC(wuff_int32_to_int16)
437{
438 size_t i;
439
440 if (head != 0)
441 {
442 memcpy(dst, src + 2 + offset, head);
443 src += 4;
444 dst += head;
445 }
446
447 for (i = 0; i < samples; i++)
448 {
449 memcpy(dst + i * 2, src + 2 + i * 4, 2);
450 }
451
452 if (tail != 0)
453 {
454 memcpy(dst + samples * 2, src + 2 + samples * 4, tail);
455 }
456}
457
458WUFF_CONV_FUNC(wuff_int32_to_int24)
459{
460 size_t i;
461
462 if (head != 0)
463 {
464 memcpy(dst, src + 1 + offset, head);
465 src += 4;
466 dst += head;
467 }
468
469 for (i = 0; i < samples; i++)
470 {
471 memcpy(dst + i * 3, src + 1 + i * 4, 3);
472 }
473
474 if (tail != 0)
475 {
476 memcpy(dst + samples * 3, src + 1 + samples * 4, tail);
477 }
478}
479
480WUFF_CONV_FUNC(wuff_int32_to_int32)
481{
482 memcpy(dst, src + offset, samples * 4 + head + tail);
483}
484
485WUFF_CONV_FUNC(wuff_int32_to_float32)
486{
487 wuff_sint32 i32;
488 float f32;
489 size_t i;
490
491 if (head != 0)
492 {
493 memcpy(&i32, src, 4);
494 f32 = (float)((double)i32 / 2147483648.0);
495 memcpy(dst, (wuff_uint8 *)&f32 + offset, head);
496 src += 4;
497 dst += head;
498 }
499
500 for (i = 0; i < samples; i++)
501 {
502 memcpy(&i32, src + i * 4, 4);
503 f32 = (float)((double)i32 / 2147483648.0);
504 memcpy(dst + i * 4, &f32, 4);
505 }
506
507 if (tail != 0)
508 {
509 memcpy(&i32, src + samples * 4, 4);
510 f32 = (float)((double)i32 / 2147483648.0);
511 memcpy(dst + samples * 4, &f32, tail);
512 }
513}
514
515WUFF_CONV_FUNC(wuff_int32_to_float64)
516{
517 wuff_sint32 i32;
518 double f64;
519 size_t i;
520
521 if (head != 0)
522 {
523 memcpy(&i32, src, 4);
524 f64 = (double)i32 / 2147483648.0;
525 memcpy(dst, (wuff_uint8 *)&f64 + offset, head);
526 src += 4;
527 dst += head;
528 }
529
530 for (i = 0; i < samples; i++)
531 {
532 memcpy(&i32, src + i * 4, 4);
533 f64 = (double)i32 / 2147483648.0;
534 memcpy(dst + i * 8, &f64, 8);
535 }
536
537 if (tail != 0)
538 {
539 memcpy(&i32, src + samples * 4, 4);
540 f64 = (double)i32 / 2147483648.0;
541 memcpy(dst + samples * 8, &f64, tail);
542 }
543}
544
545/*
546 * float32 functions.
547 */
548
549WUFF_CONV_FUNC(wuff_float32_to_int8)
550{
551 float f32;
552 size_t i;
553 (void)offset; (void)head; (void)tail;
554
555 for (i = 0; i < samples; i++)
556 {
557 memcpy(&f32, src + i * 4, 4);
558 dst[i] = (wuff_uint8)((f32 * 127.5f) + 128.0f);
559 }
560}
561
562WUFF_CONV_FUNC(wuff_float32_to_int16)
563{
564 float f32;
565 wuff_sint16 i16;
566 size_t i;
567
568 if (head != 0)
569 {
570 memcpy(&f32, src, 4);
571 i16 = (wuff_sint16)(f32 * 32767.5f);
572 memcpy(dst, (wuff_uint8 *)&i16 + offset, head);
573 src += 4;
574 dst += head;
575 }
576
577 for (i = 0; i < samples; i++)
578 {
579 memcpy(&f32, src + i * 4, 4);
580 i16 = (wuff_sint16)(f32 * 32767.5f);
581 memcpy(dst + i * 2, &i16, 2);
582 }
583
584 if (tail != 0)
585 {
586 memcpy(&f32, src + i * 4, 4);
587 i16 = (wuff_sint16)(f32 * 32767.5f);
588 memcpy(dst + i * 2, &i16, tail);
589 }
590}
591
592WUFF_CONV_FUNC(wuff_float32_to_int24)
593{
594 float f32;
595 wuff_sint32 i24;
596 size_t i;
597
598 if (head != 0)
599 {
600 memcpy(&f32, src, 4);
601 i24 = (wuff_sint32)((double)f32 * 2147483647.5);
602 memcpy(dst, (wuff_uint8 *)&i24 + 1 + offset, head);
603 src += 4;
604 dst += head;
605 }
606
607 for (i = 0; i < samples; i++)
608 {
609 memcpy(&f32, src + i * 4, 4);
610 i24 = (wuff_sint32)((double)f32 * 2147483647.5);
611 memcpy(dst + i * 3, (wuff_uint8 *)&i24 + 1, 3);
612 }
613
614 if (tail != 0)
615 {
616 memcpy(&f32, src + samples * 4, 4);
617 i24 = (wuff_sint32)((double)f32 * 2147483647.5);
618 memcpy(dst + samples * 3, (wuff_uint8 *)&i24 + 1, tail);
619 }
620}
621
622WUFF_CONV_FUNC(wuff_float32_to_int32)
623{
624 float f32;
625 wuff_sint32 i32;
626 size_t i;
627
628 if (head != 0)
629 {
630 memcpy(&f32, src, 4);
631 i32 = (wuff_sint32)((double)f32 * 2147483647.5);
632 memcpy(dst, (wuff_uint8 *)&i32 + offset, head);
633 src += 4;
634 dst += head;
635 }
636
637 for (i = 0; i < samples; i++)
638 {
639 memcpy(&f32, src + i * 4, 4);
640 i32 = (wuff_sint32)((double)f32 * 2147483647.5);
641 memcpy(dst + i * 4, &i32, 4);
642 }
643
644 if (tail != 0)
645 {
646 memcpy(&f32, src + samples * 4, 4);
647 i32 = (wuff_sint32)((double)f32 * 2147483647.5);
648 memcpy(dst + samples * 4, &i32, tail);
649 }
650}
651
652WUFF_CONV_FUNC(wuff_float32_to_float32)
653{
654 memcpy(dst, src + offset, samples * 4 + head + tail);
655}
656
657WUFF_CONV_FUNC(wuff_float32_to_float64)
658{
659 float f32;
660 double f64;
661 size_t i;
662
663 if (head != 0)
664 {
665 memcpy(&f32, src, 4);
666 f64 = f32;
667 memcpy(dst, (wuff_uint8 *)&f64 + offset, head);
668 src += 4;
669 dst += head;
670 }
671
672 for (i = 0; i < samples; i++)
673 {
674 memcpy(&f32, src + i * 4, 4);
675 f64 = f32;
676 memcpy(dst + i * 8, &f64, 8);
677 }
678
679 if (tail != 0)
680 {
681 memcpy(&f32, src + samples * 4, 4);
682 f64 = f32;
683 memcpy(dst + samples * 8, &f64, tail);
684 }
685}
686
687/*
688 * float64 functions.
689 */
690
691WUFF_CONV_FUNC(wuff_float64_to_int8)
692{
693 double f64;
694 size_t i;
695 (void)offset; (void)head; (void)tail;
696
697 for (i = 0; i < samples; i++)
698 {
699 memcpy(&f64, src + i * 8, 8);
700 dst[i] = (wuff_uint8)((f64 * 127.5) + 128.0);
701 }
702}
703
704WUFF_CONV_FUNC(wuff_float64_to_int16)
705{
706 double f64;
707 wuff_sint16 i16;
708 size_t i;
709
710 if (head != 0)
711 {
712 memcpy(&f64, src, 8);
713 i16 = (wuff_sint16)(f64 * 32767.5);
714 memcpy(dst, (wuff_uint8 *)&i16 + offset, head);
715 src += 8;
716 dst += head;
717 }
718
719 for (i = 0; i < samples; i++)
720 {
721 memcpy(&f64, src + i * 8, 8);
722 i16 = (wuff_sint16)(f64 * 32767.5);
723 memcpy(dst + i * 2, &i16, 2);
724 }
725
726 if (tail != 0)
727 {
728 memcpy(&f64, src + i * 8, 8);
729 i16 = (wuff_sint16)(f64 * 32767.5);
730 memcpy(dst + i * 2, &i16, tail);
731 }
732}
733
734WUFF_CONV_FUNC(wuff_float64_to_int24)
735{
736 double f64;
737 wuff_sint32 i24;
738 size_t i;
739
740 if (head != 0)
741 {
742 memcpy(&f64, src, 8);
743 i24 = (wuff_sint32)(f64 * 2147483647.5);
744 memcpy(dst, (wuff_uint8 *)&i24 + 1 + offset, head);
745 src += 8;
746 dst += head;
747 }
748
749 for (i = 0; i < samples; i++)
750 {
751 memcpy(&f64, src + i * 8, 8);
752 i24 = (wuff_sint32)(f64 * 2147483647.5);
753 memcpy(dst + i * 3, (wuff_uint8 *)&i24 + 1, 3);
754 }
755
756 if (tail != 0)
757 {
758 memcpy(&f64, src + samples * 8, 8);
759 i24 = (wuff_sint32)(f64 * 2147483647.5);
760 memcpy(dst + samples * 3, (wuff_uint8 *)&i24 + 1, tail);
761 }
762}
763
764WUFF_CONV_FUNC(wuff_float64_to_int32)
765{
766 double f64;
767 wuff_sint32 i32;
768 size_t i;
769
770 if (head != 0)
771 {
772 memcpy(&f64, src, 8);
773 i32 = (wuff_sint32)(f64 * 2147483647.5);
774 memcpy(dst, (wuff_uint8 *)&i32 + offset, head);
775 src += 8;
776 dst += head;
777 }
778
779 for (i = 0; i < samples; i++)
780 {
781 memcpy(&f64, src + i * 8, 8);
782 i32 = (wuff_sint32)(f64 * 2147483647.5);
783 memcpy(dst + i * 4, &i32, 4);
784 }
785
786 if (tail != 0)
787 {
788 memcpy(&f64, src + samples * 8, 8);
789 i32 = (wuff_sint32)(f64 * 2147483647.5);
790 memcpy(dst + samples * 4, &i32, tail);
791 }
792}
793
794WUFF_CONV_FUNC(wuff_float64_to_float32)
795{
796 double f64;
797 float f32;
798 size_t i;
799
800 if (head != 0)
801 {
802 memcpy(&f64, src, 8);
803 f32 = (float)f64;
804 memcpy(dst, (wuff_uint8 *)&f32 + offset, head);
805 src += 8;
806 dst += head;
807 }
808
809 for (i = 0; i < samples; i++)
810 {
811 memcpy(&f64, src + i * 8, 8);
812 f32 = (float)f64;
813 memcpy(dst + i * 4, &f32, 4);
814 }
815
816 if (tail != 0)
817 {
818 memcpy(&f64, src + samples * 8, 8);
819 f32 = (float)f64;
820 memcpy(dst + samples * 4, &f32, tail);
821 }
822}
823
824WUFF_CONV_FUNC(wuff_float64_to_float64)
825{
826 memcpy(dst, src + offset, samples * 8 + head + tail);
827}
828