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 */ 7 8 9 #include "Resampler.h" 10 #include "MultiAudioUtility.h" 11 #include "debug.h" 12 13 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. 18 */ 19 20 21 Resampler::Resampler(uint32 sourceFormat, uint32 destFormat) 22 : 23 fFunc(&Resampler::_Void) 24 { 25 PRINT(("Resampler() in 0x%x, out 0x%x\n", (unsigned int)sourceFormat, 26 (unsigned int)destFormat)); 27 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; 49 } 50 break; 51 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; 72 } 73 break; 74 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; 95 } 96 break; 97 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; 118 } 119 break; 120 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; 141 } 142 break; 143 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; 164 } 165 break; 166 } 167 } 168 169 170 Resampler::~Resampler() 171 { 172 } 173 174 175 void 176 Resampler::_Void(const void *inputData, uint32 inputStride, 177 void *outputData, uint32 outputStride, uint32 sampleCount) 178 { 179 180 } 181 182 183 void 184 Resampler::_CopyFloat2Float(const void *inputData, uint32 inputStride, 185 void *outputData, uint32 outputStride, uint32 sampleCount) 186 { 187 while (sampleCount > 0) { 188 *(float*)outputData = *(const float*)inputData; 189 190 outputData = (void*)((uint8*)outputData + outputStride); 191 inputData = (void*)((uint8*)inputData + inputStride); 192 193 sampleCount--; 194 } 195 } 196 197 198 void 199 Resampler::_CopyFloat2Double(const void *inputData, uint32 inputStride, 200 void *outputData, uint32 outputStride, uint32 sampleCount) 201 { 202 while (sampleCount > 0) { 203 *(double*)outputData = *(const float*)inputData; 204 205 outputData = (void*)((uint8*)outputData + outputStride); 206 inputData = (void*)((uint8*)inputData + inputStride); 207 208 sampleCount--; 209 } 210 } 211 212 213 void 214 Resampler::_CopyFloat2Int(const void *inputData, uint32 inputStride, 215 void *outputData, uint32 outputStride, uint32 sampleCount) 216 { 217 while (sampleCount > 0) { 218 float data = *(const float*)inputData; 219 if (data <= -1.0f) 220 *(int32*)outputData = INT32_MIN; 221 else if (data >= 1.0f) 222 *(int32*)outputData = INT32_MAX; 223 else 224 *(int32*)outputData = (int32)(data * INT32_MAX); 225 226 outputData = (void*)((uint8*)outputData + outputStride); 227 inputData = (void*)((uint8*)inputData + inputStride); 228 229 sampleCount--; 230 } 231 } 232 233 void 234 Resampler::_CopyFloat2Short(const void *inputData, uint32 inputStride, 235 void *outputData, uint32 outputStride, uint32 sampleCount) 236 { 237 while (sampleCount > 0) { 238 *(int16*)outputData = (int16)(*(const float*)inputData * 32767.0f); 239 240 outputData = (void*)((uint8*)outputData + outputStride); 241 inputData = (void*)((uint8*)inputData + inputStride); 242 243 sampleCount--; 244 } 245 } 246 247 248 void 249 Resampler::_CopyFloat2UChar(const void *inputData, uint32 inputStride, 250 void *outputData, uint32 outputStride, uint32 sampleCount) 251 { 252 while (sampleCount > 0) { 253 *(uint8*)outputData = (uint8)(128.0f + *(const float*)inputData * 127.0f); 254 255 outputData = (void*)((uint8*)outputData + outputStride); 256 inputData = (void*)((uint8*)inputData + inputStride); 257 258 sampleCount--; 259 } 260 } 261 262 263 void 264 Resampler::_CopyFloat2Char(const void *inputData, uint32 inputStride, 265 void *outputData, uint32 outputStride, uint32 sampleCount) 266 { 267 while (sampleCount > 0) { 268 *(int8*)outputData = (int8)(*(const float*)inputData * 127.0f); 269 270 outputData = (void*)((uint8*)outputData + outputStride); 271 inputData = (void*)((uint8*)inputData + inputStride); 272 273 sampleCount--; 274 } 275 } 276 277 278 void 279 Resampler::_CopyDouble2Float(const void *inputData, uint32 inputStride, 280 void *outputData, uint32 outputStride, uint32 sampleCount) 281 { 282 while (sampleCount > 0) { 283 *(float*)outputData = *(const double*)inputData; 284 285 outputData = (void*)((uint8*)outputData + outputStride); 286 inputData = (void*)((uint8*)inputData + inputStride); 287 288 sampleCount--; 289 } 290 } 291 292 293 void 294 Resampler::_CopyDouble2Double(const void *inputData, uint32 inputStride, 295 void *outputData, uint32 outputStride, uint32 sampleCount) 296 { 297 while (sampleCount > 0) { 298 *(double*)outputData = *(const double*)inputData; 299 300 outputData = (void*)((uint8*)outputData + outputStride); 301 inputData = (void*)((uint8*)inputData + inputStride); 302 303 sampleCount--; 304 } 305 } 306 307 308 void 309 Resampler::_CopyDouble2Int(const void *inputData, uint32 inputStride, 310 void *outputData, uint32 outputStride, uint32 sampleCount) 311 { 312 while (sampleCount > 0) { 313 *(int32*)outputData = (int32)(*(const double*)inputData 314 * (double)INT32_MAX); 315 316 outputData = (void*)((uint8*)outputData + outputStride); 317 inputData = (void*)((uint8*)inputData + inputStride); 318 319 sampleCount--; 320 } 321 } 322 323 324 void 325 Resampler::_CopyDouble2Short(const void *inputData, uint32 inputStride, 326 void *outputData, uint32 outputStride, uint32 sampleCount) 327 { 328 while (sampleCount > 0) { 329 *(int16*)outputData = (int16)(*(const double*)inputData * 32767.0f); 330 331 outputData = (void*)((uint8*)outputData + outputStride); 332 inputData = (void*)((uint8*)inputData + inputStride); 333 334 sampleCount--; 335 } 336 } 337 338 339 void 340 Resampler::_CopyDouble2UChar(const void *inputData, uint32 inputStride, 341 void *outputData, uint32 outputStride, uint32 sampleCount) 342 { 343 while (sampleCount > 0) { 344 *(uint8*)outputData = (uint8)(128.0f + *(const double*)inputData * 127.0f); 345 346 outputData = (void*)((uint8*)outputData + outputStride); 347 inputData = (void*)((uint8*)inputData + inputStride); 348 349 sampleCount--; 350 } 351 } 352 353 354 void 355 Resampler::_CopyDouble2Char(const void *inputData, uint32 inputStride, 356 void *outputData, uint32 outputStride, uint32 sampleCount) 357 { 358 while (sampleCount > 0) { 359 *(int8*)outputData = (int8)(*(const double*)inputData * 127.0f); 360 361 outputData = (void*)((uint8*)outputData + outputStride); 362 inputData = (void*)((uint8*)inputData + inputStride); 363 364 sampleCount--; 365 } 366 } 367 368 369 void 370 Resampler::_CopyShort2Float(const void *inputData, uint32 inputStride, 371 void *outputData, uint32 outputStride, uint32 sampleCount) 372 { 373 while (sampleCount > 0) { 374 *(float*)outputData = *(const int16*)inputData / 32767.0f; 375 376 outputData = (void*)((uint8*)outputData + outputStride); 377 inputData = (void*)((uint8*)inputData + inputStride); 378 379 sampleCount--; 380 } 381 } 382 383 384 void 385 Resampler::_CopyShort2Double(const void *inputData, uint32 inputStride, 386 void *outputData, uint32 outputStride, uint32 sampleCount) 387 { 388 while (sampleCount > 0) { 389 *(double*)outputData = *(const int16*)inputData / 32767.0; 390 391 outputData = (void*)((uint8*)outputData + outputStride); 392 inputData = (void*)((uint8*)inputData + inputStride); 393 394 sampleCount--; 395 } 396 } 397 398 399 void 400 Resampler::_CopyShort2Int(const void *inputData, uint32 inputStride, 401 void *outputData, uint32 outputStride, uint32 sampleCount) 402 { 403 while (sampleCount > 0) { 404 *(int32*)outputData = (int32)*(const int16*)inputData << 16; 405 406 outputData = (void*)((uint8*)outputData + outputStride); 407 inputData = (void*)((uint8*)inputData + inputStride); 408 409 sampleCount--; 410 } 411 } 412 413 414 void 415 Resampler::_CopyShort2Short(const void *inputData, uint32 inputStride, 416 void *outputData, uint32 outputStride, uint32 sampleCount) 417 { 418 while (sampleCount > 0) { 419 *(int16*)outputData = *(const int16*)inputData; 420 421 outputData = (void*)((uint8*)outputData + outputStride); 422 inputData = (void*)((uint8*)inputData + inputStride); 423 424 sampleCount--; 425 } 426 } 427 428 429 void 430 Resampler::_CopyShort2UChar(const void *inputData, uint32 inputStride, 431 void *outputData, uint32 outputStride, uint32 sampleCount) 432 { 433 while (sampleCount > 0) { 434 *(uint8*)outputData = 128 + (*(const int16*)inputData >> 8); 435 436 outputData = (void*)((uint8*)outputData + outputStride); 437 inputData = (void*)((uint8*)inputData + inputStride); 438 439 sampleCount--; 440 } 441 } 442 443 444 void 445 Resampler::_CopyShort2Char(const void *inputData, uint32 inputStride, 446 void *outputData, uint32 outputStride, uint32 sampleCount) 447 { 448 while (sampleCount > 0) { 449 *(int8*)outputData = *(const int16*)inputData >> 8; 450 451 outputData = (void*)((uint8*)outputData + outputStride); 452 inputData = (void*)((uint8*)inputData + inputStride); 453 454 sampleCount--; 455 } 456 } 457 458 459 void 460 Resampler::_CopyInt2Float(const void *inputData, uint32 inputStride, 461 void *outputData, uint32 outputStride, uint32 sampleCount) 462 { 463 while (sampleCount > 0) { 464 *(float*)outputData = *(const int32*)inputData / 2147483647.0f; 465 466 outputData = (void*)((uint8*)outputData + outputStride); 467 inputData = (void*)((uint8*)inputData + inputStride); 468 469 sampleCount--; 470 } 471 } 472 473 474 void 475 Resampler::_CopyInt2Double(const void *inputData, uint32 inputStride, 476 void *outputData, uint32 outputStride, uint32 sampleCount) 477 { 478 while (sampleCount > 0) { 479 *(double*)outputData = *(const int32*)inputData / 2147483647.0; 480 481 outputData = (void*)((uint8*)outputData + outputStride); 482 inputData = (void*)((uint8*)inputData + inputStride); 483 484 sampleCount--; 485 } 486 } 487 488 489 void 490 Resampler::_CopyInt2Int(const void *inputData, uint32 inputStride, 491 void *outputData, uint32 outputStride, uint32 sampleCount) 492 { 493 while (sampleCount > 0) { 494 *(int32*)outputData = *(const int32*)inputData; 495 496 outputData = (void*)((uint8*)outputData + outputStride); 497 inputData = (void*)((uint8*)inputData + inputStride); 498 499 sampleCount--; 500 } 501 } 502 503 504 void 505 Resampler::_CopyInt2Short(const void *inputData, uint32 inputStride, 506 void *outputData, uint32 outputStride, uint32 sampleCount) 507 { 508 while (sampleCount > 0) { 509 *(int16*)outputData = *(const int32*)inputData >> 16; 510 511 outputData = (void*)((uint8*)outputData + outputStride); 512 inputData = (void*)((uint8*)inputData + inputStride); 513 514 sampleCount--; 515 } 516 } 517 518 519 void 520 Resampler::_CopyInt2UChar(const void *inputData, uint32 inputStride, 521 void *outputData, uint32 outputStride, uint32 sampleCount) 522 { 523 while (sampleCount > 0) { 524 *(uint8*)outputData = 128 + (*(const int32*)inputData >> 24); 525 526 outputData = (void*)((uint8*)outputData + outputStride); 527 inputData = (void*)((uint8*)inputData + inputStride); 528 529 sampleCount--; 530 } 531 } 532 533 534 void 535 Resampler::_CopyInt2Char(const void *inputData, uint32 inputStride, 536 void *outputData, uint32 outputStride, uint32 sampleCount) 537 { 538 while (sampleCount > 0) { 539 *(uint8*)outputData = *(const int32*)inputData >> 24; 540 541 outputData = (void*)((uint8*)outputData + outputStride); 542 inputData = (void*)((uint8*)inputData + inputStride); 543 544 sampleCount--; 545 } 546 } 547 548 549 void 550 Resampler::_CopyUChar2Float(const void *inputData, uint32 inputStride, 551 void *outputData, uint32 outputStride, uint32 sampleCount) 552 { 553 while (sampleCount > 0) { 554 *(float*)outputData = (*(const uint8*)inputData - 128) / 127.0f; 555 556 outputData = (void*)((uint8*)outputData + outputStride); 557 inputData = (void*)((uint8*)inputData + inputStride); 558 559 sampleCount--; 560 } 561 } 562 563 564 void 565 Resampler::_CopyUChar2Double(const void *inputData, uint32 inputStride, 566 void *outputData, uint32 outputStride, uint32 sampleCount) 567 { 568 while (sampleCount > 0) { 569 *(double*)outputData = (*(const uint8*)inputData - 128) / 127.0; 570 571 outputData = (void*)((uint8*)outputData + outputStride); 572 inputData = (void*)((uint8*)inputData + inputStride); 573 574 sampleCount--; 575 } 576 } 577 578 579 void 580 Resampler::_CopyUChar2Short(const void *inputData, uint32 inputStride, 581 void *outputData, uint32 outputStride, uint32 sampleCount) 582 { 583 while (sampleCount > 0) { 584 *(int16*)outputData = (int16)(*(const uint8*)inputData - 128) << 8; 585 586 outputData = (void*)((uint8*)outputData + outputStride); 587 inputData = (void*)((uint8*)inputData + inputStride); 588 589 sampleCount--; 590 } 591 } 592 593 594 void 595 Resampler::_CopyUChar2Int(const void *inputData, uint32 inputStride, 596 void *outputData, uint32 outputStride, uint32 sampleCount) 597 { 598 while (sampleCount > 0) { 599 *(int32*)outputData = (int32)(*(const uint8*)inputData - 128) << 24; 600 601 outputData = (void*)((uint8*)outputData + outputStride); 602 inputData = (void*)((uint8*)inputData + inputStride); 603 604 sampleCount--; 605 } 606 } 607 608 609 void 610 Resampler::_CopyUChar2UChar(const void *inputData, uint32 inputStride, 611 void *outputData, uint32 outputStride, uint32 sampleCount) 612 { 613 while (sampleCount > 0) { 614 *(uint8*)outputData = *(const uint8*)inputData; 615 616 outputData = (void*)((uint8*)outputData + outputStride); 617 inputData = (void*)((uint8*)inputData + inputStride); 618 619 sampleCount--; 620 } 621 } 622 623 624 void 625 Resampler::_CopyUChar2Char(const void *inputData, uint32 inputStride, 626 void *outputData, uint32 outputStride, uint32 sampleCount) 627 { 628 while (sampleCount > 0) { 629 *(int8*)outputData = *(const uint8*)inputData - 128; 630 631 outputData = (void*)((uint8*)outputData + outputStride); 632 inputData = (void*)((uint8*)inputData + inputStride); 633 634 sampleCount--; 635 } 636 } 637 638 639 void 640 Resampler::_CopyChar2Float(const void *inputData, uint32 inputStride, 641 void *outputData, uint32 outputStride, uint32 sampleCount) 642 { 643 while (sampleCount > 0) { 644 *(float*)outputData = *(const int8*)inputData / 127.0f; 645 646 outputData = (void*)((uint8*)outputData + outputStride); 647 inputData = (void*)((uint8*)inputData + inputStride); 648 649 sampleCount--; 650 } 651 } 652 653 654 void 655 Resampler::_CopyChar2Double(const void *inputData, uint32 inputStride, 656 void *outputData, uint32 outputStride, uint32 sampleCount) 657 { 658 while (sampleCount > 0) { 659 *(double*)outputData = *(const int8*)inputData / 127.0; 660 661 outputData = (void*)((uint8*)outputData + outputStride); 662 inputData = (void*)((uint8*)inputData + inputStride); 663 664 sampleCount--; 665 } 666 } 667 668 669 void 670 Resampler::_CopyChar2Short(const void *inputData, uint32 inputStride, 671 void *outputData, uint32 outputStride, uint32 sampleCount) 672 { 673 while (sampleCount > 0) { 674 *(int16*)outputData = ((int16)*(const int8*)inputData) << 8; 675 676 outputData = (void*)((uint8*)outputData + outputStride); 677 inputData = (void*)((uint8*)inputData + inputStride); 678 679 sampleCount--; 680 } 681 } 682 683 684 void 685 Resampler::_CopyChar2Int(const void *inputData, uint32 inputStride, 686 void *outputData, uint32 outputStride, uint32 sampleCount) 687 { 688 while (sampleCount > 0) { 689 *(int32*)outputData = ((int16)*(const int8*)inputData) << 24; 690 691 outputData = (void*)((uint8*)outputData + outputStride); 692 inputData = (void*)((uint8*)inputData + inputStride); 693 694 sampleCount--; 695 } 696 } 697 698 699 void 700 Resampler::_CopyChar2UChar(const void *inputData, uint32 inputStride, 701 void *outputData, uint32 outputStride, uint32 sampleCount) 702 { 703 while (sampleCount > 0) { 704 *(uint8*)outputData = *(const int8*)inputData + 128; 705 706 outputData = (void*)((uint8*)outputData + outputStride); 707 inputData = (void*)((uint8*)inputData + inputStride); 708 709 sampleCount--; 710 } 711 } 712 713 714 void 715 Resampler::_CopyChar2Char(const void *inputData, uint32 inputStride, 716 void *outputData, uint32 outputStride, uint32 sampleCount) 717 { 718 while (sampleCount > 0) { 719 *(int8*)outputData = *(const int8*)inputData; 720 721 outputData = (void*)((uint8*)outputData + outputStride); 722 inputData = (void*)((uint8*)inputData + inputStride); 723 724 sampleCount--; 725 } 726 } 727 728