cleanup/sync the main loop of *_OpenDevice functions to pick audio format
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
diff --git a/src/audio/alsa/SDL_alsa_audio.c b/src/audio/alsa/SDL_alsa_audio.c
index c42b730..4a2eec7 100644
--- a/src/audio/alsa/SDL_alsa_audio.c
+++ b/src/audio/alsa/SDL_alsa_audio.c
@@ -598,10 +598,7 @@ ALSA_OpenDevice(_THIS, void *handle, const char *devname)
}
/* Try for a closest match on audio format */
- status = -1;
- for (test_format = SDL_FirstAudioFormat(this->spec.format);
- test_format && (status < 0);) {
- status = 0; /* if we can't support a format, it'll become -1. */
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
switch (test_format) {
case AUDIO_U8:
format = SND_PCM_FORMAT_U8;
@@ -634,19 +631,14 @@ ALSA_OpenDevice(_THIS, void *handle, const char *devname)
format = SND_PCM_FORMAT_FLOAT_BE;
break;
default:
- status = -1;
- break;
- }
- if (status >= 0) {
- status = ALSA_snd_pcm_hw_params_set_format(pcm_handle,
- hwparams, format);
+ continue;
}
- if (status < 0) {
- test_format = SDL_NextAudioFormat();
+ if (ALSA_snd_pcm_hw_params_set_format(pcm_handle, hwparams, format) >= 0) {
+ break;
}
}
- if (status < 0) {
- return SDL_SetError("ALSA: Couldn't find any hardware audio formats");
+ if (!test_format) {
+ return SDL_SetError("%s: Unsupported audio format", "alsa");
}
this->spec.format = test_format;
diff --git a/src/audio/android/SDL_androidaudio.c b/src/audio/android/SDL_androidaudio.c
index cdf4948..92bb591 100644
--- a/src/audio/android/SDL_androidaudio.c
+++ b/src/audio/android/SDL_androidaudio.c
@@ -55,20 +55,18 @@ ANDROIDAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
return SDL_OutOfMemory();
}
- test_format = SDL_FirstAudioFormat(this->spec.format);
- while (test_format != 0) { /* no "UNKNOWN" constant */
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
if ((test_format == AUDIO_U8) ||
(test_format == AUDIO_S16) ||
(test_format == AUDIO_F32)) {
this->spec.format = test_format;
break;
}
- test_format = SDL_NextAudioFormat();
}
- if (test_format == 0) {
+ if (!test_format) {
/* Didn't find a compatible format :( */
- return SDL_SetError("No compatible audio format!");
+ return SDL_SetError("%s: Unsupported audio format", "android");
}
if (Android_JNI_OpenAudioDevice(iscapture, &this->spec) < 0) {
diff --git a/src/audio/arts/SDL_artsaudio.c b/src/audio/arts/SDL_artsaudio.c
index 6806b1b..359eec4 100644
--- a/src/audio/arts/SDL_artsaudio.c
+++ b/src/audio/arts/SDL_artsaudio.c
@@ -219,8 +219,8 @@ static int
ARTS_OpenDevice(_THIS, void *handle, const char *devname)
{
int rc = 0;
- int bits = 0, frag_spec = 0;
- SDL_AudioFormat test_format = 0, format = 0;
+ int bits, frag_spec = 0;
+ SDL_AudioFormat test_format = 0;
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
@@ -231,32 +231,24 @@ ARTS_OpenDevice(_THIS, void *handle, const char *devname)
SDL_zerop(this->hidden);
/* Try for a closest match on audio format */
- for (test_format = SDL_FirstAudioFormat(this->spec.format);
- !format && test_format;) {
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
#ifdef DEBUG_AUDIO
fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
#endif
switch (test_format) {
case AUDIO_U8:
- bits = 8;
- format = 1;
- break;
case AUDIO_S16LSB:
- bits = 16;
- format = 1;
break;
default:
- format = 0;
- break;
- }
- if (!format) {
- test_format = SDL_NextAudioFormat();
+ continue;
}
+ break;
}
- if (format == 0) {
- return SDL_SetError("Couldn't find any hardware audio formats");
+ if (!test_format) {
+ return SDL_SetError("%s: Unsupported audio format", "arts");
}
this->spec.format = test_format;
+ bits = SDL_AUDIO_BITSIZE(test_format);
if ((rc = SDL_NAME(arts_init) ()) != 0) {
return SDL_SetError("Unable to initialize ARTS: %s",
diff --git a/src/audio/coreaudio/SDL_coreaudio.m b/src/audio/coreaudio/SDL_coreaudio.m
index 95823e0..3b0ef9d 100644
--- a/src/audio/coreaudio/SDL_coreaudio.m
+++ b/src/audio/coreaudio/SDL_coreaudio.m
@@ -1018,8 +1018,8 @@ static int
COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
{
AudioStreamBasicDescription *strdesc;
- SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
- SDL_bool valid_datatype = SDL_FALSE, iscapture = this->iscapture;
+ SDL_AudioFormat test_format;
+ SDL_bool iscapture = this->iscapture;
SDL_AudioDevice **new_open_devices;
/* Initialize all variables that we clean on shutdown */
@@ -1076,8 +1076,7 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
strdesc->mSampleRate = this->spec.freq;
strdesc->mFramesPerPacket = 1;
- while ((!valid_datatype) && (test_format)) {
- this->spec.format = test_format;
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
/* CoreAudio handles most of SDL's formats natively, but not U16, apparently. */
switch (test_format) {
case AUDIO_U8:
@@ -1088,26 +1087,26 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
case AUDIO_S32MSB:
case AUDIO_F32LSB:
case AUDIO_F32MSB:
- valid_datatype = SDL_TRUE;
- strdesc->mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
- if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
- strdesc->mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
-
- if (SDL_AUDIO_ISFLOAT(this->spec.format))
- strdesc->mFormatFlags |= kLinearPCMFormatFlagIsFloat;
- else if (SDL_AUDIO_ISSIGNED(this->spec.format))
- strdesc->mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
break;
default:
- test_format = SDL_NextAudioFormat();
- break;
+ continue;
}
+ break;
}
- if (!valid_datatype) { /* shouldn't happen, but just in case... */
+ if (!test_format) { /* shouldn't happen, but just in case... */
return SDL_SetError("Unsupported audio format");
}
+ this->spec.format = test_format;
+ strdesc->mBitsPerChannel = SDL_AUDIO_BITSIZE(test_format);
+ if (SDL_AUDIO_ISBIGENDIAN(test_format))
+ strdesc->mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
+
+ if (SDL_AUDIO_ISFLOAT(test_format))
+ strdesc->mFormatFlags |= kLinearPCMFormatFlagIsFloat;
+ else if (SDL_AUDIO_ISSIGNED(test_format))
+ strdesc->mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
strdesc->mBytesPerFrame = strdesc->mChannelsPerFrame * strdesc->mBitsPerChannel / 8;
strdesc->mBytesPerPacket = strdesc->mBytesPerFrame * strdesc->mFramesPerPacket;
diff --git a/src/audio/directsound/SDL_directsound.c b/src/audio/directsound/SDL_directsound.c
index 1143027..86e58a1 100644
--- a/src/audio/directsound/SDL_directsound.c
+++ b/src/audio/directsound/SDL_directsound.c
@@ -477,10 +477,9 @@ DSOUND_OpenDevice(_THIS, void *handle, const char *devname)
{
const DWORD numchunks = 8;
HRESULT result;
- SDL_bool valid_format = SDL_FALSE;
SDL_bool tried_format = SDL_FALSE;
SDL_bool iscapture = this->iscapture;
- SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
+ SDL_AudioFormat test_format;
LPGUID guid = (LPGUID) handle;
DWORD bufsize;
@@ -511,7 +510,7 @@ DSOUND_OpenDevice(_THIS, void *handle, const char *devname)
}
}
- while ((!valid_format) && (test_format)) {
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
switch (test_format) {
case AUDIO_U8:
case AUDIO_S16:
@@ -548,19 +547,21 @@ DSOUND_OpenDevice(_THIS, void *handle, const char *devname)
rc = iscapture ? CreateCaptureBuffer(this, bufsize, &wfmt) : CreateSecondary(this, bufsize, &wfmt);
if (rc == 0) {
this->hidden->num_buffers = numchunks;
- valid_format = SDL_TRUE;
+ break;
}
}
- break;
+ continue;
+ default:
+ continue;
}
- test_format = SDL_NextAudioFormat();
+ break;
}
- if (!valid_format) {
+ if (!test_format) {
if (tried_format) {
return -1; /* CreateSecondary() should have called SDL_SetError(). */
}
- return SDL_SetError("DirectSound: Unsupported audio format");
+ return SDL_SetError("%s: Unsupported audio format", "directsound");
}
/* Playback buffers will auto-start playing in DSOUND_WaitDevice() */
diff --git a/src/audio/emscripten/SDL_emscriptenaudio.c b/src/audio/emscripten/SDL_emscriptenaudio.c
index 764c8e1..157892f 100644
--- a/src/audio/emscripten/SDL_emscriptenaudio.c
+++ b/src/audio/emscripten/SDL_emscriptenaudio.c
@@ -194,7 +194,6 @@ EMSCRIPTENAUDIO_CloseDevice(_THIS)
static int
EMSCRIPTENAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
{
- SDL_bool valid_format = SDL_FALSE;
SDL_AudioFormat test_format;
SDL_bool iscapture = this->iscapture;
int result;
@@ -229,22 +228,21 @@ EMSCRIPTENAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
return SDL_SetError("Web Audio API is not available!");
}
- test_format = SDL_FirstAudioFormat(this->spec.format);
- while ((!valid_format) && (test_format)) {
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
switch (test_format) {
case AUDIO_F32: /* web audio only supports floats */
- this->spec.format = test_format;
-
- valid_format = SDL_TRUE;
break;
+ default:
+ continue;
}
- test_format = SDL_NextAudioFormat();
+ break;
}
- if (!valid_format) {
+ if (!test_format) {
/* Didn't find a compatible format :( */
- return SDL_SetError("No compatible audio format!");
+ return SDL_SetError("%s: Unsupported audio format", "emscripten");
}
+ this->spec.format = test_format;
/* Initialize all variables that we clean on shutdown */
#if 0 /* !!! FIXME: currently not used. Can we move some stuff off the SDL2 namespace? --ryan. */
diff --git a/src/audio/fusionsound/SDL_fsaudio.c b/src/audio/fusionsound/SDL_fsaudio.c
index 89affb6..92c214c 100644
--- a/src/audio/fusionsound/SDL_fsaudio.c
+++ b/src/audio/fusionsound/SDL_fsaudio.c
@@ -177,7 +177,7 @@ static int
SDL_FS_OpenDevice(_THIS, void *handle, const char *devname)
{
int bytes;
- SDL_AudioFormat test_format = 0, format = 0;
+ SDL_AudioFormat test_format;
FSSampleFormat fs_format;
FSStreamDescription desc;
DirectResult ret;
@@ -191,45 +191,34 @@ SDL_FS_OpenDevice(_THIS, void *handle, const char *devname)
SDL_zerop(this->hidden);
/* Try for a closest match on audio format */
- for (test_format = SDL_FirstAudioFormat(this->spec.format);
- !format && test_format;) {
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
#ifdef DEBUG_AUDIO
fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
#endif
switch (test_format) {
case AUDIO_U8:
fs_format = FSSF_U8;
- bytes = 1;
- format = 1;
break;
case AUDIO_S16SYS:
fs_format = FSSF_S16;
- bytes = 2;
- format = 1;
break;
case AUDIO_S32SYS:
fs_format = FSSF_S32;
- bytes = 4;
- format = 1;
break;
case AUDIO_F32SYS:
fs_format = FSSF_FLOAT;
- bytes = 4;
- format = 1;
break;
default:
- format = 0;
- break;
- }
- if (!format) {
- test_format = SDL_NextAudioFormat();
+ continue;
}
+ break;
}
- if (format == 0) {
- return SDL_SetError("Couldn't find any hardware audio formats");
+ if (!test_format) {
+ return SDL_SetError("%s: Unsupported audio format", "fusionsound");
}
this->spec.format = test_format;
+ bytes = SDL_AUDIO_BITSIZE(test_format) / 8;
/* Retrieve the main sound interface. */
ret = SDL_NAME(FusionSoundCreate) (&this->hidden->fs);
diff --git a/src/audio/haiku/SDL_haikuaudio.cc b/src/audio/haiku/SDL_haikuaudio.cc
index 9e37247..c23b63e 100644
--- a/src/audio/haiku/SDL_haikuaudio.cc
+++ b/src/audio/haiku/SDL_haikuaudio.cc
@@ -122,9 +122,8 @@ UnmaskSignals(sigset_t * omask)
static int
HAIKUAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
{
- int valid_datatype = 0;
media_raw_audio_format format;
- SDL_AudioFormat test_format = SDL_FirstAudioFormat(_this->spec.format);
+ SDL_AudioFormat test_format;
/* Initialize all variables that we clean on shutdown */
_this->hidden = new SDL_PrivateAudioData;
@@ -138,9 +137,7 @@ HAIKUAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
format.byte_order = B_MEDIA_LITTLE_ENDIAN;
format.frame_rate = (float) _this->spec.freq;
format.channel_count = _this->spec.channels; /* !!! FIXME: support > 2? */
- while ((!valid_datatype) && (test_format)) {
- valid_datatype = 1;
- _this->spec.format = test_format;
+ for (test_format = SDL_FirstAudioFormat(_this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
switch (test_format) {
case AUDIO_S8:
format.format = media_raw_audio_format::B_AUDIO_CHAR;
@@ -178,15 +175,15 @@ HAIKUAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
break;
default:
- valid_datatype = 0;
- test_format = SDL_NextAudioFormat();
- break;
+ continue;
}
+ break;
}
- if (!valid_datatype) { /* shouldn't happen, but just in case... */
- return SDL_SetError("Unsupported audio format");
+ if (!test_format) { /* shouldn't happen, but just in case... */
+ return SDL_SetError("%s: Unsupported audio format", "haiku");
}
+ _this->spec.format = test_format;
/* Calculate the final parameters for this audio specification */
SDL_CalculateAudioSpec(&_this->spec);
diff --git a/src/audio/nas/SDL_nasaudio.c b/src/audio/nas/SDL_nasaudio.c
index a6ef419..adedb2e 100644
--- a/src/audio/nas/SDL_nasaudio.c
+++ b/src/audio/nas/SDL_nasaudio.c
@@ -237,26 +237,6 @@ NAS_CloseDevice(_THIS)
SDL_free(this->hidden);
}
-static unsigned char
-sdlformat_to_auformat(unsigned int fmt)
-{
- switch (fmt) {
- case AUDIO_U8:
- return AuFormatLinearUnsigned8;
- case AUDIO_S8:
- return AuFormatLinearSigned8;
- case AUDIO_U16LSB:
- return AuFormatLinearUnsigned16LSB;
- case AUDIO_U16MSB:
- return AuFormatLinearUnsigned16MSB;
- case AUDIO_S16LSB:
- return AuFormatLinearSigned16LSB;
- case AUDIO_S16MSB:
- return AuFormatLinearSigned16MSB;
- }
- return AuNone;
-}
-
static AuBool
event_handler(AuServer * aud, AuEvent * ev, AuEventHandlerRec * hnd)
{
@@ -336,7 +316,7 @@ NAS_OpenDevice(_THIS, void *handle, const char *devname)
AuElement elms[3];
int buffer_size;
SDL_bool iscapture = this->iscapture;
- SDL_AudioFormat test_format, format;
+ SDL_AudioFormat test_format, format = 0;
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
@@ -347,16 +327,33 @@ NAS_OpenDevice(_THIS, void *handle, const char *devname)
SDL_zerop(this->hidden);
/* Try for a closest match on audio format */
- format = 0;
- for (test_format = SDL_FirstAudioFormat(this->spec.format);
- !format && test_format;) {
- format = sdlformat_to_auformat(test_format);
- if (format == AuNone) {
- test_format = SDL_NextAudioFormat();
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
+ switch (test_format) {
+ case AUDIO_U8:
+ format = AuFormatLinearUnsigned8;
+ break;
+ case AUDIO_S8:
+ format = AuFormatLinearSigned8;
+ break;
+ case AUDIO_U16LSB:
+ format = AuFormatLinearUnsigned16LSB;
+ break;
+ case AUDIO_U16MSB:
+ format = AuFormatLinearUnsigned16MSB;
+ break;
+ case AUDIO_S16LSB:
+ format = AuFormatLinearSigned16LSB;
+ break;
+ case AUDIO_S16MSB:
+ format = AuFormatLinearSigned16MSB;
+ break;
+ default:
+ continue;
}
+ break;
}
- if (format == 0) {
- return SDL_SetError("NAS: Couldn't find any hardware audio formats");
+ if (!test_format) {
+ return SDL_SetError("%s: Unsupported audio format", "nas");
}
this->spec.format = test_format;
diff --git a/src/audio/netbsd/SDL_netbsdaudio.c b/src/audio/netbsd/SDL_netbsdaudio.c
index 13556b7..4e710d3 100644
--- a/src/audio/netbsd/SDL_netbsdaudio.c
+++ b/src/audio/netbsd/SDL_netbsdaudio.c
@@ -205,7 +205,8 @@ static int
NETBSDAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
{
SDL_bool iscapture = this->iscapture;
- SDL_AudioFormat format = 0;
+ SDL_AudioFormat test_format;
+ int encoding = AUDIO_ENCODING_NONE;
audio_info_t info, hwinfo;
struct audio_prinfo *prinfo = iscapture ? &info.record : &info.play;
@@ -245,54 +246,46 @@ NETBSDAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
}
#endif
- prinfo->encoding = AUDIO_ENCODING_NONE;
prinfo->sample_rate = this->spec.freq;
prinfo->channels = this->spec.channels;
- for (format = SDL_FirstAudioFormat(this->spec.format); format;) {
- switch (format) {
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
+ switch (test_format) {
case AUDIO_U8:
- prinfo->encoding = AUDIO_ENCODING_ULINEAR;
- prinfo->precision = 8;
+ encoding = AUDIO_ENCODING_ULINEAR;
break;
case AUDIO_S8:
- prinfo->encoding = AUDIO_ENCODING_SLINEAR;
- prinfo->precision = 8;
+ encoding = AUDIO_ENCODING_SLINEAR;
break;
case AUDIO_S16LSB:
- prinfo->encoding = AUDIO_ENCODING_SLINEAR_LE;
- prinfo->precision = 16;
+ encoding = AUDIO_ENCODING_SLINEAR_LE;
break;
case AUDIO_S16MSB:
- prinfo->encoding = AUDIO_ENCODING_SLINEAR_BE;
- prinfo->precision = 16;
+ encoding = AUDIO_ENCODING_SLINEAR_BE;
break;
case AUDIO_U16LSB:
- prinfo->encoding = AUDIO_ENCODING_ULINEAR_LE;
- prinfo->precision = 16;
+ encoding = AUDIO_ENCODING_ULINEAR_LE;
break;
case AUDIO_U16MSB:
- prinfo->encoding = AUDIO_ENCODING_ULINEAR_BE;
- prinfo->precision = 16;
+ encoding = AUDIO_ENCODING_ULINEAR_BE;
break;
case AUDIO_S32LSB:
- prinfo->encoding = AUDIO_ENCODING_SLINEAR_LE;
- prinfo->precision = 32;
+ encoding = AUDIO_ENCODING_SLINEAR_LE;
break;
case AUDIO_S32MSB:
- prinfo->encoding = AUDIO_ENCODING_SLINEAR_BE;
- prinfo->precision = 32;
+ encoding = AUDIO_ENCODING_SLINEAR_BE;
break;
+ default:
+ continue;
}
- if (prinfo->encoding != AUDIO_ENCODING_NONE) {
- break;
- }
- format = SDL_NextAudioFormat();
+ break;
}
- if (prinfo->encoding == AUDIO_ENCODING_NONE) {
- return SDL_SetError("No supported encoding for 0x%x", this->spec.format);
+ if (!test_format) {
+ return SDL_SetError("%s: Unsupported audio format", "netbsd");
}
+ prinfo->encoding = encoding;
+ prinfo->precision = SDL_AUDIO_BITSIZE(test_format);
info.hiwat = 5;
info.lowat = 3;
@@ -305,7 +298,7 @@ NETBSDAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
}
/* Final spec used for the device. */
- this->spec.format = format;
+ this->spec.format = test_format;
this->spec.freq = prinfo->sample_rate;
this->spec.channels = prinfo->channels;
diff --git a/src/audio/openslES/SDL_openslES.c b/src/audio/openslES/SDL_openslES.c
index d0c3769..3190435 100644
--- a/src/audio/openslES/SDL_openslES.c
+++ b/src/audio/openslES/SDL_openslES.c
@@ -418,15 +418,14 @@ openslES_CreatePCMPlayer(_THIS)
/* Just go with signed 16-bit audio as it's the most compatible */
this->spec.format = AUDIO_S16SYS;
#else
- SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
- while (test_format != 0) {
+ SDL_AudioFormat test_format;
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
if (SDL_AUDIO_ISSIGNED(test_format) && SDL_AUDIO_ISINT(test_format)) {
break;
}
- test_format = SDL_NextAudioFormat();
}
- if (test_format == 0) {
+ if (!test_format) {
/* Didn't find a compatible format : */
LOGI( "No compatible audio format, using signed 16-bit audio" );
test_format = AUDIO_S16SYS;
diff --git a/src/audio/os2/SDL_os2audio.c b/src/audio/os2/SDL_os2audio.c
index 9decfe9..247d14a 100644
--- a/src/audio/os2/SDL_os2audio.c
+++ b/src/audio/os2/SDL_os2audio.c
@@ -251,7 +251,7 @@ static void OS2_CloseDevice(_THIS)
static int OS2_OpenDevice(_THIS, void *handle, const char *devname)
{
SDL_PrivateAudioData *pAData;
- SDL_AudioFormat SDLAudioFmt;
+ SDL_AudioFormat test_format;
MCI_AMP_OPEN_PARMS stMCIAmpOpen;
MCI_BUFFER_PARMS stMCIBuffer;
ULONG ulRC;
@@ -263,14 +263,13 @@ static int OS2_OpenDevice(_THIS, void *handle, const char *devname)
SDL_zero(stMCIAmpOpen);
SDL_zero(stMCIBuffer);
- for (SDLAudioFmt = SDL_FirstAudioFormat(_this->spec.format);
- SDLAudioFmt != 0; SDLAudioFmt = SDL_NextAudioFormat()) {
- if (SDLAudioFmt == AUDIO_U8 || SDLAudioFmt == AUDIO_S16)
+ for (test_format = SDL_FirstAudioFormat(_this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
+ if (test_format == AUDIO_U8 || test_format == AUDIO_S16)
break;
}
- if (SDLAudioFmt == 0) {
+ if (!test_format) {
debug_os2("Unsupported audio format, AUDIO_S16 used");
- SDLAudioFmt = AUDIO_S16;
+ test_format = AUDIO_S16;
}
pAData = (SDL_PrivateAudioData *) SDL_calloc(1, sizeof(struct SDL_PrivateAudioData));
@@ -330,7 +329,7 @@ static int OS2_OpenDevice(_THIS, void *handle, const char *devname)
&stMCIAmpSet, 0);
}
- _this->spec.format = SDLAudioFmt;
+ _this->spec.format = test_format;
_this->spec.channels = _this->spec.channels > 1 ? 2 : 1;
if (_this->spec.freq < 8000) {
_this->spec.freq = 8000;
@@ -342,7 +341,7 @@ static int OS2_OpenDevice(_THIS, void *handle, const char *devname)
/* Setup mixer. */
pAData->stMCIMixSetup.ulFormatTag = MCI_WAVE_FORMAT_PCM;
- pAData->stMCIMixSetup.ulBitsPerSample = SDL_AUDIO_BITSIZE(SDLAudioFmt);
+ pAData->stMCIMixSetup.ulBitsPerSample = SDL_AUDIO_BITSIZE(test_format);
pAData->stMCIMixSetup.ulSamplesPerSec = _this->spec.freq;
pAData->stMCIMixSetup.ulChannels = _this->spec.channels;
pAData->stMCIMixSetup.ulDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
diff --git a/src/audio/paudio/SDL_paudio.c b/src/audio/paudio/SDL_paudio.c
index aaadeda..602aa60 100644
--- a/src/audio/paudio/SDL_paudio.c
+++ b/src/audio/paudio/SDL_paudio.c
@@ -228,7 +228,7 @@ PAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
const char *workaround = SDL_getenv("SDL_DSP_NOSELECT");
char audiodev[1024];
const char *err = NULL;
- int format;
+ int flags;
int bytes_per_sample;
SDL_AudioFormat test_format;
audio_init paud_init;
@@ -316,63 +316,44 @@ PAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
paud_init.channels = this->spec.channels;
/* Try for a closest match on audio format */
- format = 0;
- for (test_format = SDL_FirstAudioFormat(this->spec.format);
- !format && test_format;) {
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
#ifdef DEBUG_AUDIO
fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
#endif
switch (test_format) {
case AUDIO_U8:
- bytes_per_sample = 1;
- paud_init.bits_per_sample = 8;
- paud_init.flags = TWOS_COMPLEMENT | FIXED;
- format = 1;
+ flags = TWOS_COMPLEMENT | FIXED;
break;
case AUDIO_S8:
- bytes_per_sample = 1;
- paud_init.bits_per_sample = 8;
- paud_init.flags = SIGNED | TWOS_COMPLEMENT | FIXED;
- format = 1;
+ flags = SIGNED | TWOS_COMPLEMENT | FIXED;
break;
case AUDIO_S16LSB:
- bytes_per_sample = 2;
- paud_init.bits_per_sample = 16;
- paud_init.flags = SIGNED | TWOS_COMPLEMENT | FIXED;
- format = 1;
+ flags = SIGNED | TWOS_COMPLEMENT | FIXED;
break;
case AUDIO_S16MSB:
- bytes_per_sample = 2;
- paud_init.bits_per_sample = 16;
- paud_init.flags = BIG_ENDIAN | SIGNED | TWOS_COMPLEMENT | FIXED;
- format = 1;
+ flags = BIG_ENDIAN | SIGNED | TWOS_COMPLEMENT | FIXED;
break;
case AUDIO_U16LSB:
- bytes_per_sample = 2;
- paud_init.bits_per_sample = 16;
- paud_init.flags = TWOS_COMPLEMENT | FIXED;
- format = 1;
+ flags = TWOS_COMPLEMENT | FIXED;
break;
case AUDIO_U16MSB:
- bytes_per_sample = 2;
- paud_init.bits_per_sample = 16;
- paud_init.flags = BIG_ENDIAN | TWOS_COMPLEMENT | FIXED;
- format = 1;
+ flags = BIG_ENDIAN | TWOS_COMPLEMENT | FIXED;
break;
default:
- break;
- }
- if (!format) {
- test_format = SDL_NextAudioFormat();
+ continue;
}
+ break;
}
- if (format == 0) {
+ if (!test_format) {
#ifdef DEBUG_AUDIO
fprintf(stderr, "Couldn't find any hardware audio formats\n");
#endif
- return SDL_SetError("Couldn't find any hardware audio formats");
+ return SDL_SetError("%s: Unsupported audio format", "paud");
}
this->spec.format = test_format;
+ paud_init.bits_per_sample = SDL_AUDIO_BITSIZE(test_format);
+ bytes_per_sample = SDL_AUDIO_BITSIZE(test_format) / 8;
+ paud_init.flags = flags;
/*
* We know the buffer size and the max number of subsequent writes
@@ -406,28 +387,25 @@ PAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
if (ioctl(fd, AUDIO_INIT, &paud_init) < 0) {
switch (paud_init.rc) {
case 1:
- err = "Couldn't set audio format: DSP can't do play requests";
+ err = "DSP can't do play requests";
break;
case 2:
- err = "Couldn't set audio format: DSP can't do record requests";
+ err = "DSP can't do record requests";
break;
case 4:
- err = "Couldn't set audio format: request was invalid";
+ err = "request was invalid";
break;
case 5:
- err = "Couldn't set audio format: conflict with open's flags";
+ err = "conflict with open's flags";
break;
case 6:
- err = "Couldn't set audio format: out of DSP MIPS or memory";
+ err = "out of DSP MIPS or memory";
break;
default:
- err = "Couldn't set audio format: not documented in sys/audio.h";
+ err = "not documented in sys/audio.h";
break;
}
- }
-
- if (err != NULL) {
- return SDL_SetError("Paudio: %s", err);
+ return SDL_SetError("paud: Couldn't set audio format (%s)", err);
}
/* Allocate mixing buffer */
diff --git a/src/audio/pulseaudio/SDL_pulseaudio.c b/src/audio/pulseaudio/SDL_pulseaudio.c
index 07825d0..9f954ea 100644
--- a/src/audio/pulseaudio/SDL_pulseaudio.c
+++ b/src/audio/pulseaudio/SDL_pulseaudio.c
@@ -547,14 +547,14 @@ static int
PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
{
struct SDL_PrivateAudioData *h = NULL;
- Uint16 test_format = 0;
+ SDL_AudioFormat test_format;
pa_sample_spec paspec;
pa_buffer_attr paattr;
pa_channel_map pacmap;
pa_stream_flags_t flags = 0;
const char *name = NULL;
SDL_bool iscapture = this->iscapture;
- int state = 0;
+ int state = 0, format = PA_SAMPLE_INVALID;
int rc = 0;
/* Initialize all variables that we clean on shutdown */
@@ -565,48 +565,43 @@ PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname)
}
SDL_zerop(this->hidden);
- paspec.format = PA_SAMPLE_INVALID;
-
/* Try for a closest match on audio format */
- for (test_format = SDL_FirstAudioFormat(this->spec.format);
- (paspec.format == PA_SAMPLE_INVALID) && test_format;) {
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
#ifdef DEBUG_AUDIO
fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
#endif
switch (test_format) {
case AUDIO_U8:
- paspec.format = PA_SAMPLE_U8;
+ format = PA_SAMPLE_U8;
break;
case AUDIO_S16LSB:
- paspec.format = PA_SAMPLE_S16LE;
+ format = PA_SAMPLE_S16LE;
break;
case AUDIO_S16MSB:
- paspec.format = PA_SAMPLE_S16BE;
+ format = PA_SAMPLE_S16BE;
break;
case AUDIO_S32LSB:
- paspec.format = PA_SAMPLE_S32LE;
+ format = PA_SAMPLE_S32LE;
break;
case AUDIO_S32MSB:
- paspec.format = PA_SAMPLE_S32BE;
+ format = PA_SAMPLE_S32BE;
break;
case AUDIO_F32LSB:
- paspec.format = PA_SAMPLE_FLOAT32LE;
+ format = PA_SAMPLE_FLOAT32LE;
break;
case AUDIO_F32MSB:
- paspec.format = PA_SAMPLE_FLOAT32BE;
+ format = PA_SAMPLE_FLOAT32BE;
break;
default:
- paspec.format = PA_SAMPLE_INVALID;
- break;
- }
- if (paspec.format == PA_SAMPLE_INVALID) {
- test_format = SDL_NextAudioFormat();
+ continue;
}
+ break;
}
- if (paspec.format == PA_SAMPLE_INVALID) {
- return SDL_SetError("Couldn't find any hardware audio formats");
+ if (!test_format) {
+ return SDL_SetError("%s: Unsupported audio format", "pulseaudio");
}
this->spec.format = test_format;
+ paspec.format = format;
/* Calculate the final parameters for this audio specification */
#ifdef PA_STREAM_ADJUST_LATENCY
diff --git a/src/audio/qsa/SDL_qsa_audio.c b/src/audio/qsa/SDL_qsa_audio.c
index b2a864c..b940cde 100644
--- a/src/audio/qsa/SDL_qsa_audio.c
+++ b/src/audio/qsa/SDL_qsa_audio.c
@@ -263,8 +263,7 @@ QSA_OpenDevice(_THIS, void *handle, const char *devname)
SDL_Bool iscapture = this->iscapture;
int status = 0;
int format = 0;
- SDL_AudioFormat test_format = 0;
- int found = 0;
+ SDL_AudioFormat test_format;
snd_pcm_channel_setup_t csetup;
snd_pcm_channel_params_t cparams;
@@ -303,89 +302,49 @@ QSA_OpenDevice(_THIS, void *handle, const char *devname)
}
/* Try for a closest match on audio format */
- format = 0;
- /* can't use format as SND_PCM_SFMT_U8 = 0 in qsa */
- found = 0;
-
- for (test_format = SDL_FirstAudioFormat(this->spec.format); !found;) {
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
/* if match found set format to equivalent QSA format */
switch (test_format) {
case AUDIO_U8:
- {
- format = SND_PCM_SFMT_U8;
- found = 1;
- }
+ format = SND_PCM_SFMT_U8;
break;
case AUDIO_S8:
- {
- format = SND_PCM_SFMT_S8;
- found = 1;
- }
+ format = SND_PCM_SFMT_S8;
break;
case AUDIO_S16LSB:
- {
- format = SND_PCM_SFMT_S16_LE;
- found = 1;
- }
+ format = SND_PCM_SFMT_S16_LE;
break;
case AUDIO_S16MSB:
- {
- format = SND_PCM_SFMT_S16_BE;
- found = 1;
- }
+ format = SND_PCM_SFMT_S16_BE;
break;
case AUDIO_U16LSB:
- {
- format = SND_PCM_SFMT_U16_LE;
- found = 1;
- }
+ format = SND_PCM_SFMT_U16_LE;
break;
case AUDIO_U16MSB:
- {
- format = SND_PCM_SFMT_U16_BE;
- found = 1;
- }
+ format = SND_PCM_SFMT_U16_BE;
break;
case AUDIO_S32LSB:
- {
- format = SND_PCM_SFMT_S32_LE;
- found = 1;
- }
+ format = SND_PCM_SFMT_S32_LE;
break;
case AUDIO_S32MSB:
- {
- format = SND_PCM_SFMT_S32_BE;
- found = 1;
- }
+ format = SND_PCM_SFMT_S32_BE;
break;
case AUDIO_F32LSB:
- {
- format = SND_PCM_SFMT_FLOAT_LE;
- found = 1;
- }
+ format = SND_PCM_SFMT_FLOAT_LE;
break;
case AUDIO_F32MSB:
- {
- format = SND_PCM_SFMT_FLOAT_BE;
- found = 1;
- }
+ format = SND_PCM_SFMT_FLOAT_BE;
break;
default:
- {
- break;
- }
- }
-
- if (!found) {
- test_format = SDL_NextAudioFormat();
+ continue;
}
+ break;
}
-
/* assumes test_format not 0 on success */
- if (test_format == 0) {
- return SDL_SetError("QSA: Couldn't find any hardware audio formats");
+ /* can't use format as SND_PCM_SFMT_U8 = 0 in qsa */
+ if (!test_format) {
+ return SDL_SetError("%s: Unsupported audio format", "qsa");
}
-
this->spec.format = test_format;
/* Set the audio format */
diff --git a/src/audio/sndio/SDL_sndioaudio.c b/src/audio/sndio/SDL_sndioaudio.c
index 9dc53d4..0c25072 100644
--- a/src/audio/sndio/SDL_sndioaudio.c
+++ b/src/audio/sndio/SDL_sndioaudio.c
@@ -239,10 +239,9 @@ SNDIO_CloseDevice(_THIS)
static int
SNDIO_OpenDevice(_THIS, void *handle, const char *devname)
{
- SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
+ SDL_AudioFormat test_format;
struct sio_par par;
SDL_bool iscapture = this->iscapture;
- int status;
this->hidden = (struct SDL_PrivateAudioData *)
SDL_malloc(sizeof(*this->hidden));
@@ -274,8 +273,7 @@ SNDIO_OpenDevice(_THIS, void *handle, const char *devname)
par.appbufsz = par.round * 2;
/* Try for a closest match on audio format */
- status = -1;
- while (test_format && (status < 0)) {
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
if (!SDL_AUDIO_ISFLOAT(test_format)) {
par.le = SDL_AUDIO_ISLITTLEENDIAN(test_format) ? 1 : 0;
par.sig = SDL_AUDIO_ISSIGNED(test_format) ? 1 : 0;
@@ -291,15 +289,13 @@ SNDIO_OpenDevice(_THIS, void *handle, const char *devname)
continue;
}
if ((par.bits == 8 * par.bps) || (par.msb)) {
- status = 0;
break;
}
}
- test_format = SDL_NextAudioFormat();
}
- if (status < 0) {
- return SDL_SetError("sndio: Couldn't find any hardware audio formats");
+ if (!test_format) {
+ return SDL_SetError("%s: Unsupported audio format", "sndio");
}
if ((par.bps == 4) && (par.sig) && (par.le))
diff --git a/src/audio/wasapi/SDL_wasapi.c b/src/audio/wasapi/SDL_wasapi.c
index b556f58..2006746 100644
--- a/src/audio/wasapi/SDL_wasapi.c
+++ b/src/audio/wasapi/SDL_wasapi.c
@@ -550,7 +550,7 @@ WASAPI_PrepDevice(_THIS, const SDL_bool updatestream)
}
if (!test_format) {
- return SDL_SetError("WASAPI: Unsupported audio format");
+ return SDL_SetError("%s: Unsupported audio format", "wasapi");
}
ret = IAudioClient_GetDevicePeriod(client, &default_period, NULL);
diff --git a/src/audio/winmm/SDL_winmm.c b/src/audio/winmm/SDL_winmm.c
index e1baf04..23a8ce8 100644
--- a/src/audio/winmm/SDL_winmm.c
+++ b/src/audio/winmm/SDL_winmm.c
@@ -285,8 +285,8 @@ PrepWaveFormat(_THIS, UINT devId, WAVEFORMATEX *pfmt, const int iscapture)
static int
WINMM_OpenDevice(_THIS, void *handle, const char *devname)
{
- SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
- SDL_bool valid_datatype = SDL_FALSE, iscapture = this->iscapture;
+ SDL_AudioFormat test_format;
+ SDL_bool iscapture = this->iscapture;
MMRESULT result;
WAVEFORMATEX waveformat;
UINT devId = WAVE_MAPPER; /* WAVE_MAPPER == choose system's default */
@@ -313,7 +313,7 @@ WINMM_OpenDevice(_THIS, void *handle, const char *devname)
if (this->spec.channels > 2)
this->spec.channels = 2; /* !!! FIXME: is this right? */
- while ((!valid_datatype) && (test_format)) {
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
switch (test_format) {
case AUDIO_U8:
case AUDIO_S16:
@@ -321,20 +321,17 @@ WINMM_OpenDevice(_THIS, void *handle, const char *devname)
case AUDIO_F32:
this->spec.format = test_format;
if (PrepWaveFormat(this, devId, &waveformat, iscapture)) {
- valid_datatype = SDL_TRUE;
- } else {
- test_format = SDL_NextAudioFormat();
+ break;
}
- break;
-
+ continue;
default:
- test_format = SDL_NextAudioFormat();
- break;
+ continue;
}
+ break;
}
- if (!valid_datatype) {
- return SDL_SetError("Unsupported audio format");
+ if (!test_format) {
+ return SDL_SetError("%s: Unsupported audio format", "winmm");
}
/* Update the fragment size as size in bytes */