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