3rdparty/licenseReport: Add seperate LGPL checks
[haiku.git] / src / add-ons / media / media-add-ons / multi_audio / Resampler.cpp
blob5ff5e77fb0e49c6e1aa3504fdac30ef147ffcdd8
1 /*
2 * Copyright 2012 Jérôme Leveque
3 * Copyright 2012 Jérôme Duval
4 * Copyright 2003 Marcus Overhagen
5 * Distributed under the terms of the MIT License.
6 */
9 #include "Resampler.h"
10 #include "MultiAudioUtility.h"
11 #include "debug.h"
14 /*! A simple resampling class for the multi_audio add-ons.
15 You pick the conversion function on object creation,
16 and then call the Resample() function, specifying data pointer,
17 offset (in bytes) to the next sample, and count of samples.
21 Resampler::Resampler(uint32 sourceFormat, uint32 destFormat)
23 fFunc(&Resampler::_Void)
25 PRINT(("Resampler() in 0x%x, out 0x%x\n", (unsigned int)sourceFormat,
26 (unsigned int)destFormat));
28 switch (sourceFormat) {
29 case media_raw_audio_format::B_AUDIO_FLOAT:
30 switch (destFormat) {
31 case media_raw_audio_format::B_AUDIO_FLOAT:
32 fFunc = &Resampler::_CopyFloat2Float;
33 break;
34 case media_raw_audio_format::B_AUDIO_DOUBLE:
35 fFunc = &Resampler::_CopyFloat2Double;
36 break;
37 case media_raw_audio_format::B_AUDIO_INT:
38 fFunc = &Resampler::_CopyFloat2Int;
39 break;
40 case media_raw_audio_format::B_AUDIO_SHORT:
41 fFunc = &Resampler::_CopyFloat2Short;
42 break;
43 case media_raw_audio_format::B_AUDIO_UCHAR:
44 fFunc = &Resampler::_CopyFloat2UChar;
45 break;
46 case media_raw_audio_format::B_AUDIO_CHAR:
47 fFunc = &Resampler::_CopyFloat2Char;
48 break;
50 break;
52 case media_raw_audio_format::B_AUDIO_DOUBLE:
53 switch (destFormat) {
54 case media_raw_audio_format::B_AUDIO_FLOAT:
55 fFunc = &Resampler::_CopyDouble2Float;
56 break;
57 case media_raw_audio_format::B_AUDIO_DOUBLE:
58 fFunc = &Resampler::_CopyDouble2Double;
59 break;
60 case media_raw_audio_format::B_AUDIO_INT:
61 fFunc = &Resampler::_CopyDouble2Int;
62 break;
63 case media_raw_audio_format::B_AUDIO_SHORT:
64 fFunc = &Resampler::_CopyDouble2Short;
65 break;
66 case media_raw_audio_format::B_AUDIO_UCHAR:
67 fFunc = &Resampler::_CopyDouble2UChar;
68 break;
69 case media_raw_audio_format::B_AUDIO_CHAR:
70 fFunc = &Resampler::_CopyDouble2Char;
71 break;
73 break;
75 case media_raw_audio_format::B_AUDIO_INT:
76 switch (destFormat) {
77 case media_raw_audio_format::B_AUDIO_FLOAT:
78 fFunc = &Resampler::_CopyInt2Float;
79 break;
80 case media_raw_audio_format::B_AUDIO_DOUBLE:
81 fFunc = &Resampler::_CopyInt2Double;
82 break;
83 case media_raw_audio_format::B_AUDIO_INT:
84 fFunc = &Resampler::_CopyInt2Int;
85 break;
86 case media_raw_audio_format::B_AUDIO_SHORT:
87 fFunc = &Resampler::_CopyInt2Short;
88 break;
89 case media_raw_audio_format::B_AUDIO_UCHAR:
90 fFunc = &Resampler::_CopyInt2UChar;
91 break;
92 case media_raw_audio_format::B_AUDIO_CHAR:
93 fFunc = &Resampler::_CopyInt2Char;
94 break;
96 break;
98 case media_raw_audio_format::B_AUDIO_SHORT:
99 switch (destFormat) {
100 case media_raw_audio_format::B_AUDIO_FLOAT:
101 fFunc = &Resampler::_CopyShort2Float;
102 break;
103 case media_raw_audio_format::B_AUDIO_DOUBLE:
104 fFunc = &Resampler::_CopyShort2Double;
105 break;
106 case media_raw_audio_format::B_AUDIO_INT:
107 fFunc = &Resampler::_CopyShort2Int;
108 break;
109 case media_raw_audio_format::B_AUDIO_SHORT:
110 fFunc = &Resampler::_CopyShort2Short;
111 break;
112 case media_raw_audio_format::B_AUDIO_UCHAR:
113 fFunc = &Resampler::_CopyShort2UChar;
114 break;
115 case media_raw_audio_format::B_AUDIO_CHAR:
116 fFunc = &Resampler::_CopyShort2Char;
117 break;
119 break;
121 case media_raw_audio_format::B_AUDIO_UCHAR:
122 switch (destFormat) {
123 case media_raw_audio_format::B_AUDIO_FLOAT:
124 fFunc = &Resampler::_CopyUChar2Float;
125 break;
126 case media_raw_audio_format::B_AUDIO_DOUBLE:
127 fFunc = &Resampler::_CopyUChar2Double;
128 break;
129 case media_raw_audio_format::B_AUDIO_INT:
130 fFunc = &Resampler::_CopyUChar2Int;
131 break;
132 case media_raw_audio_format::B_AUDIO_SHORT:
133 fFunc = &Resampler::_CopyUChar2Short;
134 break;
135 case media_raw_audio_format::B_AUDIO_UCHAR:
136 fFunc = &Resampler::_CopyUChar2UChar;
137 break;
138 case media_raw_audio_format::B_AUDIO_CHAR:
139 fFunc = &Resampler::_CopyUChar2Char;
140 break;
142 break;
144 case media_raw_audio_format::B_AUDIO_CHAR:
145 switch (destFormat) {
146 case media_raw_audio_format::B_AUDIO_FLOAT:
147 fFunc = &Resampler::_CopyChar2Float;
148 break;
149 case media_raw_audio_format::B_AUDIO_DOUBLE:
150 fFunc = &Resampler::_CopyChar2Double;
151 break;
152 case media_raw_audio_format::B_AUDIO_INT:
153 fFunc = &Resampler::_CopyChar2Int;
154 break;
155 case media_raw_audio_format::B_AUDIO_SHORT:
156 fFunc = &Resampler::_CopyChar2Short;
157 break;
158 case media_raw_audio_format::B_AUDIO_UCHAR:
159 fFunc = &Resampler::_CopyChar2UChar;
160 break;
161 case media_raw_audio_format::B_AUDIO_CHAR:
162 fFunc = &Resampler::_CopyChar2Char;
163 break;
165 break;
170 Resampler::~Resampler()
175 void
176 Resampler::_Void(const void *inputData, uint32 inputStride,
177 void *outputData, uint32 outputStride, uint32 sampleCount)
183 void
184 Resampler::_CopyFloat2Float(const void *inputData, uint32 inputStride,
185 void *outputData, uint32 outputStride, uint32 sampleCount)
187 while (sampleCount > 0) {
188 *(float*)outputData = *(const float*)inputData;
190 outputData = (void*)((uint8*)outputData + outputStride);
191 inputData = (void*)((uint8*)inputData + inputStride);
193 sampleCount--;
198 void
199 Resampler::_CopyFloat2Double(const void *inputData, uint32 inputStride,
200 void *outputData, uint32 outputStride, uint32 sampleCount)
202 while (sampleCount > 0) {
203 *(double*)outputData = *(const float*)inputData;
205 outputData = (void*)((uint8*)outputData + outputStride);
206 inputData = (void*)((uint8*)inputData + inputStride);
208 sampleCount--;
213 void
214 Resampler::_CopyFloat2Int(const void *inputData, uint32 inputStride,
215 void *outputData, uint32 outputStride, uint32 sampleCount)
217 while (sampleCount > 0) {
218 *(int32*)outputData = (int32)(*(const float*)inputData * 2147483647.0f);
220 outputData = (void*)((uint8*)outputData + outputStride);
221 inputData = (void*)((uint8*)inputData + inputStride);
223 sampleCount--;
227 void
228 Resampler::_CopyFloat2Short(const void *inputData, uint32 inputStride,
229 void *outputData, uint32 outputStride, uint32 sampleCount)
231 while (sampleCount > 0) {
232 *(int16*)outputData = (int16)(*(const float*)inputData * 32767.0f);
234 outputData = (void*)((uint8*)outputData + outputStride);
235 inputData = (void*)((uint8*)inputData + inputStride);
237 sampleCount--;
242 void
243 Resampler::_CopyFloat2UChar(const void *inputData, uint32 inputStride,
244 void *outputData, uint32 outputStride, uint32 sampleCount)
246 while (sampleCount > 0) {
247 *(uint8*)outputData = (uint8)(128.0f + *(const float*)inputData * 127.0f);
249 outputData = (void*)((uint8*)outputData + outputStride);
250 inputData = (void*)((uint8*)inputData + inputStride);
252 sampleCount--;
257 void
258 Resampler::_CopyFloat2Char(const void *inputData, uint32 inputStride,
259 void *outputData, uint32 outputStride, uint32 sampleCount)
261 while (sampleCount > 0) {
262 *(int8*)outputData = (int8)(*(const float*)inputData * 127.0f);
264 outputData = (void*)((uint8*)outputData + outputStride);
265 inputData = (void*)((uint8*)inputData + inputStride);
267 sampleCount--;
272 void
273 Resampler::_CopyDouble2Float(const void *inputData, uint32 inputStride,
274 void *outputData, uint32 outputStride, uint32 sampleCount)
276 while (sampleCount > 0) {
277 *(float*)outputData = *(const double*)inputData;
279 outputData = (void*)((uint8*)outputData + outputStride);
280 inputData = (void*)((uint8*)inputData + inputStride);
282 sampleCount--;
287 void
288 Resampler::_CopyDouble2Double(const void *inputData, uint32 inputStride,
289 void *outputData, uint32 outputStride, uint32 sampleCount)
291 while (sampleCount > 0) {
292 *(double*)outputData = *(const double*)inputData;
294 outputData = (void*)((uint8*)outputData + outputStride);
295 inputData = (void*)((uint8*)inputData + inputStride);
297 sampleCount--;
302 void
303 Resampler::_CopyDouble2Int(const void *inputData, uint32 inputStride,
304 void *outputData, uint32 outputStride, uint32 sampleCount)
306 while (sampleCount > 0) {
307 *(int32*)outputData = (int32)(*(const double*)inputData * 2147483647.0f);
309 outputData = (void*)((uint8*)outputData + outputStride);
310 inputData = (void*)((uint8*)inputData + inputStride);
312 sampleCount--;
317 void
318 Resampler::_CopyDouble2Short(const void *inputData, uint32 inputStride,
319 void *outputData, uint32 outputStride, uint32 sampleCount)
321 while (sampleCount > 0) {
322 *(int16*)outputData = (int16)(*(const double*)inputData * 32767.0f);
324 outputData = (void*)((uint8*)outputData + outputStride);
325 inputData = (void*)((uint8*)inputData + inputStride);
327 sampleCount--;
332 void
333 Resampler::_CopyDouble2UChar(const void *inputData, uint32 inputStride,
334 void *outputData, uint32 outputStride, uint32 sampleCount)
336 while (sampleCount > 0) {
337 *(uint8*)outputData = (uint8)(128.0f + *(const double*)inputData * 127.0f);
339 outputData = (void*)((uint8*)outputData + outputStride);
340 inputData = (void*)((uint8*)inputData + inputStride);
342 sampleCount--;
347 void
348 Resampler::_CopyDouble2Char(const void *inputData, uint32 inputStride,
349 void *outputData, uint32 outputStride, uint32 sampleCount)
351 while (sampleCount > 0) {
352 *(int8*)outputData = (int8)(*(const double*)inputData * 127.0f);
354 outputData = (void*)((uint8*)outputData + outputStride);
355 inputData = (void*)((uint8*)inputData + inputStride);
357 sampleCount--;
362 void
363 Resampler::_CopyShort2Float(const void *inputData, uint32 inputStride,
364 void *outputData, uint32 outputStride, uint32 sampleCount)
366 while (sampleCount > 0) {
367 *(float*)outputData = *(const int16*)inputData / 32767.0f;
369 outputData = (void*)((uint8*)outputData + outputStride);
370 inputData = (void*)((uint8*)inputData + inputStride);
372 sampleCount--;
377 void
378 Resampler::_CopyShort2Double(const void *inputData, uint32 inputStride,
379 void *outputData, uint32 outputStride, uint32 sampleCount)
381 while (sampleCount > 0) {
382 *(double*)outputData = *(const int16*)inputData / 32767.0;
384 outputData = (void*)((uint8*)outputData + outputStride);
385 inputData = (void*)((uint8*)inputData + inputStride);
387 sampleCount--;
392 void
393 Resampler::_CopyShort2Int(const void *inputData, uint32 inputStride,
394 void *outputData, uint32 outputStride, uint32 sampleCount)
396 while (sampleCount > 0) {
397 *(int32*)outputData = (int32)*(const int16*)inputData << 16;
399 outputData = (void*)((uint8*)outputData + outputStride);
400 inputData = (void*)((uint8*)inputData + inputStride);
402 sampleCount--;
407 void
408 Resampler::_CopyShort2Short(const void *inputData, uint32 inputStride,
409 void *outputData, uint32 outputStride, uint32 sampleCount)
411 while (sampleCount > 0) {
412 *(int16*)outputData = *(const int16*)inputData;
414 outputData = (void*)((uint8*)outputData + outputStride);
415 inputData = (void*)((uint8*)inputData + inputStride);
417 sampleCount--;
422 void
423 Resampler::_CopyShort2UChar(const void *inputData, uint32 inputStride,
424 void *outputData, uint32 outputStride, uint32 sampleCount)
426 while (sampleCount > 0) {
427 *(uint8*)outputData = 128 + (*(const int16*)inputData >> 8);
429 outputData = (void*)((uint8*)outputData + outputStride);
430 inputData = (void*)((uint8*)inputData + inputStride);
432 sampleCount--;
437 void
438 Resampler::_CopyShort2Char(const void *inputData, uint32 inputStride,
439 void *outputData, uint32 outputStride, uint32 sampleCount)
441 while (sampleCount > 0) {
442 *(int8*)outputData = *(const int16*)inputData >> 8;
444 outputData = (void*)((uint8*)outputData + outputStride);
445 inputData = (void*)((uint8*)inputData + inputStride);
447 sampleCount--;
452 void
453 Resampler::_CopyInt2Float(const void *inputData, uint32 inputStride,
454 void *outputData, uint32 outputStride, uint32 sampleCount)
456 while (sampleCount > 0) {
457 *(float*)outputData = *(const int32*)inputData / 2147483647.0f;
459 outputData = (void*)((uint8*)outputData + outputStride);
460 inputData = (void*)((uint8*)inputData + inputStride);
462 sampleCount--;
467 void
468 Resampler::_CopyInt2Double(const void *inputData, uint32 inputStride,
469 void *outputData, uint32 outputStride, uint32 sampleCount)
471 while (sampleCount > 0) {
472 *(double*)outputData = *(const int32*)inputData / 2147483647.0;
474 outputData = (void*)((uint8*)outputData + outputStride);
475 inputData = (void*)((uint8*)inputData + inputStride);
477 sampleCount--;
482 void
483 Resampler::_CopyInt2Int(const void *inputData, uint32 inputStride,
484 void *outputData, uint32 outputStride, uint32 sampleCount)
486 while (sampleCount > 0) {
487 *(int32*)outputData = *(const int32*)inputData;
489 outputData = (void*)((uint8*)outputData + outputStride);
490 inputData = (void*)((uint8*)inputData + inputStride);
492 sampleCount--;
497 void
498 Resampler::_CopyInt2Short(const void *inputData, uint32 inputStride,
499 void *outputData, uint32 outputStride, uint32 sampleCount)
501 while (sampleCount > 0) {
502 *(int16*)outputData = *(const int32*)inputData >> 16;
504 outputData = (void*)((uint8*)outputData + outputStride);
505 inputData = (void*)((uint8*)inputData + inputStride);
507 sampleCount--;
512 void
513 Resampler::_CopyInt2UChar(const void *inputData, uint32 inputStride,
514 void *outputData, uint32 outputStride, uint32 sampleCount)
516 while (sampleCount > 0) {
517 *(uint8*)outputData = 128 + (*(const int32*)inputData >> 24);
519 outputData = (void*)((uint8*)outputData + outputStride);
520 inputData = (void*)((uint8*)inputData + inputStride);
522 sampleCount--;
527 void
528 Resampler::_CopyInt2Char(const void *inputData, uint32 inputStride,
529 void *outputData, uint32 outputStride, uint32 sampleCount)
531 while (sampleCount > 0) {
532 *(uint8*)outputData = *(const int32*)inputData >> 24;
534 outputData = (void*)((uint8*)outputData + outputStride);
535 inputData = (void*)((uint8*)inputData + inputStride);
537 sampleCount--;
542 void
543 Resampler::_CopyUChar2Float(const void *inputData, uint32 inputStride,
544 void *outputData, uint32 outputStride, uint32 sampleCount)
546 while (sampleCount > 0) {
547 *(float*)outputData = (*(const uint8*)inputData - 128) / 127.0f;
549 outputData = (void*)((uint8*)outputData + outputStride);
550 inputData = (void*)((uint8*)inputData + inputStride);
552 sampleCount--;
557 void
558 Resampler::_CopyUChar2Double(const void *inputData, uint32 inputStride,
559 void *outputData, uint32 outputStride, uint32 sampleCount)
561 while (sampleCount > 0) {
562 *(double*)outputData = (*(const uint8*)inputData - 128) / 127.0;
564 outputData = (void*)((uint8*)outputData + outputStride);
565 inputData = (void*)((uint8*)inputData + inputStride);
567 sampleCount--;
572 void
573 Resampler::_CopyUChar2Short(const void *inputData, uint32 inputStride,
574 void *outputData, uint32 outputStride, uint32 sampleCount)
576 while (sampleCount > 0) {
577 *(int16*)outputData = (int16)(*(const uint8*)inputData - 128) << 8;
579 outputData = (void*)((uint8*)outputData + outputStride);
580 inputData = (void*)((uint8*)inputData + inputStride);
582 sampleCount--;
587 void
588 Resampler::_CopyUChar2Int(const void *inputData, uint32 inputStride,
589 void *outputData, uint32 outputStride, uint32 sampleCount)
591 while (sampleCount > 0) {
592 *(int32*)outputData = (int32)(*(const uint8*)inputData - 128) << 24;
594 outputData = (void*)((uint8*)outputData + outputStride);
595 inputData = (void*)((uint8*)inputData + inputStride);
597 sampleCount--;
602 void
603 Resampler::_CopyUChar2UChar(const void *inputData, uint32 inputStride,
604 void *outputData, uint32 outputStride, uint32 sampleCount)
606 while (sampleCount > 0) {
607 *(uint8*)outputData = *(const uint8*)inputData;
609 outputData = (void*)((uint8*)outputData + outputStride);
610 inputData = (void*)((uint8*)inputData + inputStride);
612 sampleCount--;
617 void
618 Resampler::_CopyUChar2Char(const void *inputData, uint32 inputStride,
619 void *outputData, uint32 outputStride, uint32 sampleCount)
621 while (sampleCount > 0) {
622 *(int8*)outputData = *(const uint8*)inputData - 128;
624 outputData = (void*)((uint8*)outputData + outputStride);
625 inputData = (void*)((uint8*)inputData + inputStride);
627 sampleCount--;
632 void
633 Resampler::_CopyChar2Float(const void *inputData, uint32 inputStride,
634 void *outputData, uint32 outputStride, uint32 sampleCount)
636 while (sampleCount > 0) {
637 *(float*)outputData = *(const int8*)inputData / 127.0f;
639 outputData = (void*)((uint8*)outputData + outputStride);
640 inputData = (void*)((uint8*)inputData + inputStride);
642 sampleCount--;
647 void
648 Resampler::_CopyChar2Double(const void *inputData, uint32 inputStride,
649 void *outputData, uint32 outputStride, uint32 sampleCount)
651 while (sampleCount > 0) {
652 *(double*)outputData = *(const int8*)inputData / 127.0;
654 outputData = (void*)((uint8*)outputData + outputStride);
655 inputData = (void*)((uint8*)inputData + inputStride);
657 sampleCount--;
662 void
663 Resampler::_CopyChar2Short(const void *inputData, uint32 inputStride,
664 void *outputData, uint32 outputStride, uint32 sampleCount)
666 while (sampleCount > 0) {
667 *(int16*)outputData = ((int16)*(const int8*)inputData) << 8;
669 outputData = (void*)((uint8*)outputData + outputStride);
670 inputData = (void*)((uint8*)inputData + inputStride);
672 sampleCount--;
677 void
678 Resampler::_CopyChar2Int(const void *inputData, uint32 inputStride,
679 void *outputData, uint32 outputStride, uint32 sampleCount)
681 while (sampleCount > 0) {
682 *(int32*)outputData = ((int16)*(const int8*)inputData) << 24;
684 outputData = (void*)((uint8*)outputData + outputStride);
685 inputData = (void*)((uint8*)inputData + inputStride);
687 sampleCount--;
692 void
693 Resampler::_CopyChar2UChar(const void *inputData, uint32 inputStride,
694 void *outputData, uint32 outputStride, uint32 sampleCount)
696 while (sampleCount > 0) {
697 *(uint8*)outputData = *(const int8*)inputData + 128;
699 outputData = (void*)((uint8*)outputData + outputStride);
700 inputData = (void*)((uint8*)inputData + inputStride);
702 sampleCount--;
707 void
708 Resampler::_CopyChar2Char(const void *inputData, uint32 inputStride,
709 void *outputData, uint32 outputStride, uint32 sampleCount)
711 while (sampleCount > 0) {
712 *(int8*)outputData = *(const int8*)inputData;
714 outputData = (void*)((uint8*)outputData + outputStride);
715 inputData = (void*)((uint8*)inputData + inputStride);
717 sampleCount--;