Atlas - testautomation_audio.c

Home / ext / SDL / test Lines: 1 | Size: 57097 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/** 2 * Original code: automated SDL audio test written by Edgar Simo "bobbens" 3 * New/updated tests: aschiffler at ferzkopp dot net 4 */ 5 6/* quiet windows compiler warnings */ 7#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) 8#define _CRT_SECURE_NO_WARNINGS 9#endif 10 11#include <math.h> 12#include <stdio.h> 13 14#include <SDL3/SDL.h> 15#include <SDL3/SDL_test.h> 16#include "testautomation_suites.h" 17 18static bool test_double_isfinite(double d) 19{ 20 union { 21 Uint64 u64; 22 double d; 23 } d_u; 24 d_u.d = d; 25 return (d_u.u64 & 0x7ff0000000000000ULL) != 0x7ff0000000000000ULL; 26} 27 28/* ================= Test Case Implementation ================== */ 29 30/* Fixture */ 31 32static void SDLCALL audioSetUp(void **arg) 33{ 34 /* Start SDL audio subsystem */ 35 bool ret = SDL_InitSubSystem(SDL_INIT_AUDIO); 36 SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO)"); 37 SDLTest_AssertCheck(ret == true, "Check result from SDL_InitSubSystem(SDL_INIT_AUDIO)"); 38 if (!ret) { 39 SDLTest_LogError("%s", SDL_GetError()); 40 } 41} 42 43static void SDLCALL audioTearDown(void *arg) 44{ 45 /* Remove a possibly created file from SDL disk writer audio driver; ignore errors */ 46 (void)remove("sdlaudio.raw"); 47 48 SDLTest_AssertPass("Cleanup of test files completed"); 49} 50 51#if 0 /* !!! FIXME: maybe update this? */ 52/* Global counter for callback invocation */ 53static int g_audio_testCallbackCounter; 54 55/* Global accumulator for total callback length */ 56static int g_audio_testCallbackLength; 57 58/* Test callback function */ 59static void SDLCALL audio_testCallback(void *userdata, Uint8 *stream, int len) 60{ 61 /* track that callback was called */ 62 g_audio_testCallbackCounter++; 63 g_audio_testCallbackLength += len; 64} 65#endif 66 67static SDL_AudioDeviceID g_audio_id = 0; 68 69/* Test case functions */ 70 71/** 72 * Stop and restart audio subsystem 73 * 74 * \sa SDL_QuitSubSystem 75 * \sa SDL_InitSubSystem 76 */ 77static int SDLCALL audio_quitInitAudioSubSystem(void *arg) 78{ 79 /* Stop SDL audio subsystem */ 80 SDL_QuitSubSystem(SDL_INIT_AUDIO); 81 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)"); 82 83 /* Restart audio again */ 84 audioSetUp(NULL); 85 86 return TEST_COMPLETED; 87} 88 89/** 90 * Start and stop audio directly 91 * 92 * \sa SDL_InitAudio 93 * \sa SDL_QuitAudio 94 */ 95static int SDLCALL audio_initQuitAudio(void *arg) 96{ 97 int result; 98 int i, iMax; 99 const char *audioDriver; 100 const char *hint = SDL_GetHint(SDL_HINT_AUDIO_DRIVER); 101 102 /* Stop SDL audio subsystem */ 103 SDL_QuitSubSystem(SDL_INIT_AUDIO); 104 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)"); 105 106 /* Loop over all available audio drivers */ 107 iMax = SDL_GetNumAudioDrivers(); 108 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()"); 109 SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax); 110 for (i = 0; i < iMax; i++) { 111 audioDriver = SDL_GetAudioDriver(i); 112 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i); 113 SDLTest_Assert(audioDriver != NULL, "Audio driver name is not NULL"); 114 SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver); /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */ 115 116 if (hint && SDL_strcmp(audioDriver, hint) != 0) { 117 continue; 118 } 119 120 /* Call Init */ 121 SDL_SetHint(SDL_HINT_AUDIO_DRIVER, audioDriver); 122 result = SDL_InitSubSystem(SDL_INIT_AUDIO); 123 SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO) with driver='%s'", audioDriver); 124 SDLTest_AssertCheck(result == true, "Validate result value; expected: true got: %d", result); 125 126 /* Call Quit */ 127 SDL_QuitSubSystem(SDL_INIT_AUDIO); 128 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)"); 129 } 130 131 /* NULL driver specification */ 132 audioDriver = NULL; 133 134 /* Call Init */ 135 SDL_SetHint(SDL_HINT_AUDIO_DRIVER, audioDriver); 136 result = SDL_InitSubSystem(SDL_INIT_AUDIO); 137 SDLTest_AssertPass("Call to SDL_AudioInit(NULL)"); 138 SDLTest_AssertCheck(result == true, "Validate result value; expected: true got: %d", result); 139 140 /* Call Quit */ 141 SDL_QuitSubSystem(SDL_INIT_AUDIO); 142 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)"); 143 144 /* Restart audio again */ 145 audioSetUp(NULL); 146 147 return TEST_COMPLETED; 148} 149 150/** 151 * Start, open, close and stop audio 152 * 153 * \sa SDL_InitAudio 154 * \sa SDL_OpenAudioDevice 155 * \sa SDL_CloseAudioDevice 156 * \sa SDL_QuitAudio 157 */ 158static int SDLCALL audio_initOpenCloseQuitAudio(void *arg) 159{ 160 int result; 161 int i, iMax, j, k; 162 const char *audioDriver; 163 SDL_AudioSpec desired; 164 const char *hint = SDL_GetHint(SDL_HINT_AUDIO_DRIVER); 165 166 /* Stop SDL audio subsystem */ 167 SDL_QuitSubSystem(SDL_INIT_AUDIO); 168 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)"); 169 170 /* Loop over all available audio drivers */ 171 iMax = SDL_GetNumAudioDrivers(); 172 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()"); 173 SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax); 174 for (i = 0; i < iMax; i++) { 175 audioDriver = SDL_GetAudioDriver(i); 176 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i); 177 SDLTest_Assert(audioDriver != NULL, "Audio driver name is not NULL"); 178 SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver); /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */ 179 180 if (hint && SDL_strcmp(audioDriver, hint) != 0) { 181 continue; 182 } 183 184 /* Change specs */ 185 for (j = 0; j < 2; j++) { 186 187 /* Call Init */ 188 SDL_SetHint(SDL_HINT_AUDIO_DRIVER, audioDriver); 189 result = SDL_InitSubSystem(SDL_INIT_AUDIO); 190 SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO) with driver='%s'", audioDriver); 191 SDLTest_AssertCheck(result == true, "Validate result value; expected: true got: %d", result); 192 193 /* Set spec */ 194 SDL_zero(desired); 195 switch (j) { 196 case 0: 197 /* Set standard desired spec */ 198 desired.freq = 22050; 199 desired.format = SDL_AUDIO_S16; 200 desired.channels = 2; 201 break; 202 203 case 1: 204 /* Set custom desired spec */ 205 desired.freq = 48000; 206 desired.format = SDL_AUDIO_F32; 207 desired.channels = 2; 208 break; 209 } 210 211 /* Call Open (maybe multiple times) */ 212 for (k = 0; k <= j; k++) { 213 result = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &desired); 214 if (k == 0) { 215 g_audio_id = result; 216 } 217 SDLTest_AssertPass("Call to SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, desired_spec_%d), call %d", j, k + 1); 218 SDLTest_AssertCheck(result > 0, "Verify return value; expected: > 0, got: %d", result); 219 } 220 221 /* Call Close (maybe multiple times) */ 222 for (k = 0; k <= j; k++) { 223 SDL_CloseAudioDevice(g_audio_id); 224 SDLTest_AssertPass("Call to SDL_CloseAudioDevice(), call %d", k + 1); 225 } 226 227 /* Call Quit (maybe multiple times) */ 228 for (k = 0; k <= j; k++) { 229 SDL_QuitSubSystem(SDL_INIT_AUDIO); 230 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO), call %d", k + 1); 231 } 232 233 } /* spec loop */ 234 } /* driver loop */ 235 236 /* Restart audio again */ 237 audioSetUp(NULL); 238 239 return TEST_COMPLETED; 240} 241 242/** 243 * Pause and unpause audio 244 * 245 * \sa SDL_PauseAudioDevice 246 * \sa SDL_PlayAudioDevice 247 */ 248static int SDLCALL audio_pauseUnpauseAudio(void *arg) 249{ 250 int iMax; 251 int i, j /*, k, l*/; 252 int result; 253 const char *audioDriver; 254 SDL_AudioSpec desired; 255 const char *hint = SDL_GetHint(SDL_HINT_AUDIO_DRIVER); 256 257 /* Stop SDL audio subsystem */ 258 SDL_QuitSubSystem(SDL_INIT_AUDIO); 259 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)"); 260 261 /* Loop over all available audio drivers */ 262 iMax = SDL_GetNumAudioDrivers(); 263 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()"); 264 SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax); 265 for (i = 0; i < iMax; i++) { 266 audioDriver = SDL_GetAudioDriver(i); 267 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i); 268 SDLTest_Assert(audioDriver != NULL, "Audio driver name is not NULL"); 269 SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver); /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */ 270 271 if (hint && SDL_strcmp(audioDriver, hint) != 0) { 272 continue; 273 } 274 275 /* Change specs */ 276 for (j = 0; j < 2; j++) { 277 278 /* Call Init */ 279 SDL_SetHint(SDL_HINT_AUDIO_DRIVER, audioDriver); 280 result = SDL_InitSubSystem(SDL_INIT_AUDIO); 281 SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO) with driver='%s'", audioDriver); 282 SDLTest_AssertCheck(result == true, "Validate result value; expected: true got: %d", result); 283 284 /* Set spec */ 285 SDL_zero(desired); 286 switch (j) { 287 case 0: 288 /* Set standard desired spec */ 289 desired.freq = 22050; 290 desired.format = SDL_AUDIO_S16; 291 desired.channels = 2; 292 break; 293 294 case 1: 295 /* Set custom desired spec */ 296 desired.freq = 48000; 297 desired.format = SDL_AUDIO_F32; 298 desired.channels = 2; 299 break; 300 } 301 302 /* Call Open */ 303 g_audio_id = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &desired); 304 result = g_audio_id; 305 SDLTest_AssertPass("Call to SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, desired_spec_%d)", j); 306 SDLTest_AssertCheck(result > 0, "Verify return value; expected > 0 got: %d", result); 307 308#if 0 /* !!! FIXME: maybe update this? */ 309 /* Start and stop audio multiple times */ 310 for (l = 0; l < 3; l++) { 311 SDLTest_Log("Pause/Unpause iteration: %d", l + 1); 312 313 /* Reset callback counters */ 314 g_audio_testCallbackCounter = 0; 315 g_audio_testCallbackLength = 0; 316 317 /* Un-pause audio to start playing (maybe multiple times) */ 318 for (k = 0; k <= j; k++) { 319 SDL_PlayAudioDevice(g_audio_id); 320 SDLTest_AssertPass("Call to SDL_PlayAudioDevice(g_audio_id), call %d", k + 1); 321 } 322 323 /* Wait for callback */ 324 int totalDelay = 0; 325 do { 326 SDL_Delay(10); 327 totalDelay += 10; 328 } while (g_audio_testCallbackCounter == 0 && totalDelay < 1000); 329 SDLTest_AssertCheck(g_audio_testCallbackCounter > 0, "Verify callback counter; expected: >0 got: %d", g_audio_testCallbackCounter); 330 SDLTest_AssertCheck(g_audio_testCallbackLength > 0, "Verify callback length; expected: >0 got: %d", g_audio_testCallbackLength); 331 332 /* Pause audio to stop playing (maybe multiple times) */ 333 for (k = 0; k <= j; k++) { 334 const int pause_on = (k == 0) ? 1 : SDLTest_RandomIntegerInRange(99, 9999); 335 if (pause_on) { 336 SDL_PauseAudioDevice(g_audio_id); 337 SDLTest_AssertPass("Call to SDL_PauseAudioDevice(g_audio_id), call %d", k + 1); 338 } else { 339 SDL_PlayAudioDevice(g_audio_id); 340 SDLTest_AssertPass("Call to SDL_PlayAudioDevice(g_audio_id), call %d", k + 1); 341 } 342 } 343 344 /* Ensure callback is not called again */ 345 const int originalCounter = g_audio_testCallbackCounter; 346 SDL_Delay(totalDelay + 10); 347 SDLTest_AssertCheck(originalCounter == g_audio_testCallbackCounter, "Verify callback counter; expected: %d, got: %d", originalCounter, g_audio_testCallbackCounter); 348 } 349#endif 350 351 /* Call Close */ 352 SDL_CloseAudioDevice(g_audio_id); 353 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()"); 354 355 /* Call Quit */ 356 SDL_QuitSubSystem(SDL_INIT_AUDIO); 357 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)"); 358 359 } /* spec loop */ 360 } /* driver loop */ 361 362 /* Restart audio again */ 363 audioSetUp(NULL); 364 365 return TEST_COMPLETED; 366} 367 368/** 369 * Enumerate and name available audio devices (playback and recording). 370 * 371 * \sa SDL_GetNumAudioDevices 372 * \sa SDL_GetAudioDeviceName 373 */ 374static int SDLCALL audio_enumerateAndNameAudioDevices(void *arg) 375{ 376 int t; 377 int i, n; 378 const char *name; 379 SDL_AudioDeviceID *devices; 380 381 /* Iterate over types: t=0 playback device, t=1 recording device */ 382 for (t = 0; t < 2; t++) { 383 /* Get number of devices. */ 384 devices = (t) ? SDL_GetAudioRecordingDevices(&n) : SDL_GetAudioPlaybackDevices(&n); 385 SDLTest_AssertPass("Call to SDL_GetAudio%sDevices(%i)", (t) ? "Recording" : "Playback", t); 386 SDLTest_Log("Number of %s devices < 0, reported as %i", (t) ? "recording" : "playback", n); 387 SDLTest_AssertCheck(n >= 0, "Validate result is >= 0, got: %i", n); 388 389 /* List devices. */ 390 if (n > 0) { 391 SDLTest_AssertCheck(devices != NULL, "Validate devices is not NULL if n > 0"); 392 for (i = 0; i < n; i++) { 393 name = SDL_GetAudioDeviceName(devices[i]); 394 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i)", i); 395 SDLTest_AssertCheck(name != NULL, "Verify result from SDL_GetAudioDeviceName(%i) is not NULL", i); 396 if (name != NULL) { 397 SDLTest_AssertCheck(name[0] != '\0', "verify result from SDL_GetAudioDeviceName(%i) is not empty, got: '%s'", i, name); 398 } 399 } 400 } 401 SDL_free(devices); 402 } 403 404 return TEST_COMPLETED; 405} 406 407/** 408 * Negative tests around enumeration and naming of audio devices. 409 * 410 * \sa SDL_GetNumAudioDevices 411 * \sa SDL_GetAudioDeviceName 412 */ 413static int SDLCALL audio_enumerateAndNameAudioDevicesNegativeTests(void *arg) 414{ 415 return TEST_COMPLETED; /* nothing in here atm since these interfaces changed in SDL3. */ 416} 417 418/** 419 * Checks available audio driver names. 420 * 421 * \sa SDL_GetNumAudioDrivers 422 * \sa SDL_GetAudioDriver 423 */ 424static int SDLCALL audio_printAudioDrivers(void *arg) 425{ 426 int i, n; 427 const char *name; 428 429 /* Get number of drivers */ 430 n = SDL_GetNumAudioDrivers(); 431 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()"); 432 SDLTest_AssertCheck(n >= 0, "Verify number of audio drivers >= 0, got: %i", n); 433 434 /* List drivers. */ 435 if (n > 0) { 436 for (i = 0; i < n; i++) { 437 name = SDL_GetAudioDriver(i); 438 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%i)", i); 439 SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL"); 440 if (name != NULL) { 441 SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name); 442 } 443 } 444 } 445 446 return TEST_COMPLETED; 447} 448 449/** 450 * Checks current audio driver name with initialized audio. 451 * 452 * \sa SDL_GetCurrentAudioDriver 453 */ 454static int SDLCALL audio_printCurrentAudioDriver(void *arg) 455{ 456 /* Check current audio driver */ 457 const char *name = SDL_GetCurrentAudioDriver(); 458 SDLTest_AssertPass("Call to SDL_GetCurrentAudioDriver()"); 459 SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL"); 460 if (name != NULL) { 461 SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name); 462 } 463 464 return TEST_COMPLETED; 465} 466 467/* Definition of all formats, channels, and frequencies used to test audio conversions */ 468static SDL_AudioFormat g_audioFormats[] = { 469 SDL_AUDIO_S8, SDL_AUDIO_U8, 470 SDL_AUDIO_S16LE, SDL_AUDIO_S16BE, 471 SDL_AUDIO_S32LE, SDL_AUDIO_S32BE, 472 SDL_AUDIO_F32LE, SDL_AUDIO_F32BE 473}; 474static const char *g_audioFormatsVerbose[] = { 475 "SDL_AUDIO_S8", "SDL_AUDIO_U8", 476 "SDL_AUDIO_S16LE", "SDL_AUDIO_S16BE", 477 "SDL_AUDIO_S32LE", "SDL_AUDIO_S32BE", 478 "SDL_AUDIO_F32LE", "SDL_AUDIO_F32BE" 479}; 480static SDL_AudioFormat g_invalidAudioFormats[] = { 481 (SDL_AudioFormat)SDL_DEFINE_AUDIO_FORMAT(SDL_AUDIO_MASK_SIGNED, SDL_AUDIO_MASK_BIG_ENDIAN, SDL_AUDIO_MASK_FLOAT, SDL_AUDIO_MASK_BITSIZE) 482}; 483static const char *g_invalidAudioFormatsVerbose[] = { 484 "SDL_AUDIO_UNKNOWN" 485}; 486static const int g_numAudioFormats = SDL_arraysize(g_audioFormats); 487static const int g_numInvalidAudioFormats = SDL_arraysize(g_invalidAudioFormats); 488static Uint8 g_audioChannels[] = { 1, 2, 4, 6 }; 489static const int g_numAudioChannels = SDL_arraysize(g_audioChannels); 490static int g_audioFrequencies[] = { 11025, 22050, 44100, 48000 }; 491static const int g_numAudioFrequencies = SDL_arraysize(g_audioFrequencies); 492 493/* Verify the audio formats are laid out as expected */ 494SDL_COMPILE_TIME_ASSERT(SDL_AUDIO_U8_FORMAT, SDL_AUDIO_U8 == SDL_AUDIO_BITSIZE(8)); 495SDL_COMPILE_TIME_ASSERT(SDL_AUDIO_S8_FORMAT, SDL_AUDIO_S8 == (SDL_AUDIO_BITSIZE(8) | SDL_AUDIO_MASK_SIGNED)); 496SDL_COMPILE_TIME_ASSERT(SDL_AUDIO_S16LE_FORMAT, SDL_AUDIO_S16LE == (SDL_AUDIO_BITSIZE(16) | SDL_AUDIO_MASK_SIGNED)); 497SDL_COMPILE_TIME_ASSERT(SDL_AUDIO_S16BE_FORMAT, SDL_AUDIO_S16BE == (SDL_AUDIO_S16LE | SDL_AUDIO_MASK_BIG_ENDIAN)); 498SDL_COMPILE_TIME_ASSERT(SDL_AUDIO_S32LE_FORMAT, SDL_AUDIO_S32LE == (SDL_AUDIO_BITSIZE(32) | SDL_AUDIO_MASK_SIGNED)); 499SDL_COMPILE_TIME_ASSERT(SDL_AUDIO_S32BE_FORMAT, SDL_AUDIO_S32BE == (SDL_AUDIO_S32LE | SDL_AUDIO_MASK_BIG_ENDIAN)); 500SDL_COMPILE_TIME_ASSERT(SDL_AUDIO_F32LE_FORMAT, SDL_AUDIO_F32LE == (SDL_AUDIO_BITSIZE(32) | SDL_AUDIO_MASK_FLOAT | SDL_AUDIO_MASK_SIGNED)); 501SDL_COMPILE_TIME_ASSERT(SDL_AUDIO_F32BE_FORMAT, SDL_AUDIO_F32BE == (SDL_AUDIO_F32LE | SDL_AUDIO_MASK_BIG_ENDIAN)); 502 503/** 504 * Call to SDL_GetAudioFormatName 505 * 506 * \sa SDL_GetAudioFormatName 507 */ 508static int SDLCALL audio_getAudioFormatName(void *arg) 509{ 510 const char *error; 511 int i; 512 SDL_AudioFormat format; 513 const char *result; 514 515 /* audio formats */ 516 for (i = 0; i < g_numAudioFormats; i++) { 517 format = g_audioFormats[i]; 518 SDLTest_Log("Audio Format: %s (%d)", g_audioFormatsVerbose[i], format); 519 520 /* Get name of format */ 521 result = SDL_GetAudioFormatName(format); 522 SDLTest_AssertPass("Call to SDL_GetAudioFormatName()"); 523 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL"); 524 if (result != NULL) { 525 SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty"); 526 SDLTest_AssertCheck(SDL_strcmp(result, g_audioFormatsVerbose[i]) == 0, 527 "Verify result text; expected: %s, got %s", g_audioFormatsVerbose[i], result); 528 } 529 } 530 531 /* Negative cases */ 532 533 /* Invalid Formats */ 534 SDL_ClearError(); 535 SDLTest_AssertPass("Call to SDL_ClearError()"); 536 for (i = 0; i < g_numInvalidAudioFormats; i++) { 537 format = g_invalidAudioFormats[i]; 538 result = SDL_GetAudioFormatName(format); 539 SDLTest_AssertPass("Call to SDL_GetAudioFormatName(%d)", format); 540 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL"); 541 if (result != NULL) { 542 SDLTest_AssertCheck(result[0] != '\0', 543 "Verify result is non-empty; got: %s", result); 544 SDLTest_AssertCheck(SDL_strcmp(result, g_invalidAudioFormatsVerbose[i]) == 0, 545 "Validate name is UNKNOWN, expected: '%s', got: '%s'", g_invalidAudioFormatsVerbose[i], result); 546 } 547 error = SDL_GetError(); 548 SDLTest_AssertPass("Call to SDL_GetError()"); 549 SDLTest_AssertCheck(error == NULL || error[0] == '\0', "Validate that error message is empty"); 550 } 551 552 return TEST_COMPLETED; 553} 554 555/** 556 * Builds various audio conversion structures 557 * 558 * \sa SDL_CreateAudioStream 559 */ 560static int SDLCALL audio_buildAudioStream(void *arg) 561{ 562 SDL_AudioStream *stream; 563 SDL_AudioSpec spec1; 564 SDL_AudioSpec spec2; 565 int i, ii, j, jj, k, kk; 566 567 SDL_zero(spec1); 568 SDL_zero(spec2); 569 570 /* Call Quit */ 571 SDL_QuitSubSystem(SDL_INIT_AUDIO); 572 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)"); 573 574 /* No conversion needed */ 575 spec1.format = SDL_AUDIO_S16LE; 576 spec1.channels = 2; 577 spec1.freq = 22050; 578 stream = SDL_CreateAudioStream(&spec1, &spec1); 579 SDLTest_AssertPass("Call to SDL_CreateAudioStream(spec1 ==> spec1)"); 580 SDLTest_AssertCheck(stream != NULL, "Verify stream value; expected: != NULL, got: %p", stream); 581 SDL_DestroyAudioStream(stream); 582 583 /* Typical conversion */ 584 spec1.format = SDL_AUDIO_S8; 585 spec1.channels = 1; 586 spec1.freq = 22050; 587 spec2.format = SDL_AUDIO_S16LE; 588 spec2.channels = 2; 589 spec2.freq = 44100; 590 stream = SDL_CreateAudioStream(&spec1, &spec2); 591 SDLTest_AssertPass("Call to SDL_CreateAudioStream(spec1 ==> spec2)"); 592 SDLTest_AssertCheck(stream != NULL, "Verify stream value; expected: != NULL, got: %p", stream); 593 SDL_DestroyAudioStream(stream); 594 595 /* All source conversions with random conversion targets, allow 'null' conversions */ 596 for (i = 0; i < g_numAudioFormats; i++) { 597 for (j = 0; j < g_numAudioChannels; j++) { 598 for (k = 0; k < g_numAudioFrequencies; k++) { 599 spec1.format = g_audioFormats[i]; 600 spec1.channels = g_audioChannels[j]; 601 spec1.freq = g_audioFrequencies[k]; 602 ii = SDLTest_RandomIntegerInRange(0, g_numAudioFormats - 1); 603 jj = SDLTest_RandomIntegerInRange(0, g_numAudioChannels - 1); 604 kk = SDLTest_RandomIntegerInRange(0, g_numAudioFrequencies - 1); 605 spec2.format = g_audioFormats[ii]; 606 spec2.channels = g_audioChannels[jj]; 607 spec2.freq = g_audioFrequencies[kk]; 608 stream = SDL_CreateAudioStream(&spec1, &spec2); 609 610 SDLTest_AssertPass("Call to SDL_CreateAudioStream(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)", 611 i, g_audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, g_audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq); 612 SDLTest_AssertCheck(stream != NULL, "Verify stream value; expected: != NULL, got: %p", stream); 613 if (stream == NULL) { 614 SDLTest_LogError("%s", SDL_GetError()); 615 } 616 SDL_DestroyAudioStream(stream); 617 } 618 } 619 } 620 621 /* Restart audio again */ 622 audioSetUp(NULL); 623 624 return TEST_COMPLETED; 625} 626 627/** 628 * Checks calls with invalid input to SDL_CreateAudioStream 629 * 630 * \sa SDL_CreateAudioStream 631 */ 632static int SDLCALL audio_buildAudioStreamNegative(void *arg) 633{ 634 const char *error; 635 SDL_AudioStream *stream; 636 SDL_AudioSpec spec1; 637 SDL_AudioSpec spec2; 638 int i; 639 char message[256]; 640 641 SDL_zero(spec1); 642 SDL_zero(spec2); 643 644 /* Valid format */ 645 spec1.format = SDL_AUDIO_S8; 646 spec1.channels = 1; 647 spec1.freq = 22050; 648 spec2.format = SDL_AUDIO_S16LE; 649 spec2.channels = 2; 650 spec2.freq = 44100; 651 652 SDL_ClearError(); 653 SDLTest_AssertPass("Call to SDL_ClearError()"); 654 655 /* Invalid conversions */ 656 for (i = 1; i < 64; i++) { 657 /* Valid format to start with */ 658 spec1.format = SDL_AUDIO_S8; 659 spec1.channels = 1; 660 spec1.freq = 22050; 661 spec2.format = SDL_AUDIO_S16LE; 662 spec2.channels = 2; 663 spec2.freq = 44100; 664 665 SDL_ClearError(); 666 SDLTest_AssertPass("Call to SDL_ClearError()"); 667 668 /* Set various invalid format inputs */ 669 SDL_strlcpy(message, "Invalid: ", 256); 670 if (i & 1) { 671 SDL_strlcat(message, " spec1.format", 256); 672 spec1.format = 0; 673 } 674 if (i & 2) { 675 SDL_strlcat(message, " spec1.channels", 256); 676 spec1.channels = 0; 677 } 678 if (i & 4) { 679 SDL_strlcat(message, " spec1.freq", 256); 680 spec1.freq = 0; 681 } 682 if (i & 8) { 683 SDL_strlcat(message, " spec2.format", 256); 684 spec2.format = 0; 685 } 686 if (i & 16) { 687 SDL_strlcat(message, " spec2.channels", 256); 688 spec2.channels = 0; 689 } 690 if (i & 32) { 691 SDL_strlcat(message, " spec2.freq", 256); 692 spec2.freq = 0; 693 } 694 SDLTest_Log("%s", message); 695 stream = SDL_CreateAudioStream(&spec1, &spec2); 696 SDLTest_AssertPass("Call to SDL_CreateAudioStream(spec1 ==> spec2)"); 697 SDLTest_AssertCheck(stream == NULL, "Verify stream value; expected: NULL, got: %p", stream); 698 error = SDL_GetError(); 699 SDLTest_AssertPass("Call to SDL_GetError()"); 700 SDLTest_AssertCheck(error != NULL && error[0] != '\0', "Validate that error message was not NULL or empty"); 701 SDL_DestroyAudioStream(stream); 702 } 703 704 SDL_ClearError(); 705 SDLTest_AssertPass("Call to SDL_ClearError()"); 706 707 return TEST_COMPLETED; 708} 709 710/** 711 * Checks current audio status. 712 * 713 * \sa SDL_GetAudioDeviceStatus 714 */ 715static int SDLCALL audio_getAudioStatus(void *arg) 716{ 717 return TEST_COMPLETED; /* no longer a thing in SDL3. */ 718} 719 720/** 721 * Opens, checks current audio status, and closes a device. 722 * 723 * \sa SDL_GetAudioStatus 724 */ 725static int SDLCALL audio_openCloseAndGetAudioStatus(void *arg) 726{ 727 return TEST_COMPLETED; /* not a thing in SDL3. */ 728} 729 730/** 731 * Locks and unlocks open audio device. 732 * 733 * \sa SDL_LockAudioDevice 734 * \sa SDL_UnlockAudioDevice 735 */ 736static int SDLCALL audio_lockUnlockOpenAudioDevice(void *arg) 737{ 738 return TEST_COMPLETED; /* not a thing in SDL3 */ 739} 740 741/** 742 * Convert audio using various conversion structures 743 * 744 * \sa SDL_CreateAudioStream 745 */ 746static int SDLCALL audio_convertAudio(void *arg) 747{ 748 SDL_AudioStream *stream; 749 SDL_AudioSpec spec1; 750 SDL_AudioSpec spec2; 751 int c; 752 char message[128]; 753 int i, ii, j, jj, k, kk; 754 755 SDL_zero(spec1); 756 SDL_zero(spec2); 757 758 /* Iterate over bitmask that determines which parameters are modified in the conversion */ 759 for (c = 1; c < 8; c++) { 760 SDL_strlcpy(message, "Changing:", 128); 761 if (c & 1) { 762 SDL_strlcat(message, " Format", 128); 763 } 764 if (c & 2) { 765 SDL_strlcat(message, " Channels", 128); 766 } 767 if (c & 4) { 768 SDL_strlcat(message, " Frequencies", 128); 769 } 770 SDLTest_Log("%s", message); 771 /* All source conversions with random conversion targets */ 772 for (i = 0; i < g_numAudioFormats; i++) { 773 for (j = 0; j < g_numAudioChannels; j++) { 774 for (k = 0; k < g_numAudioFrequencies; k++) { 775 spec1.format = g_audioFormats[i]; 776 spec1.channels = g_audioChannels[j]; 777 spec1.freq = g_audioFrequencies[k]; 778 779 /* Ensure we have a different target format */ 780 do { 781 if (c & 1) { 782 ii = SDLTest_RandomIntegerInRange(0, g_numAudioFormats - 1); 783 } else { 784 ii = 1; 785 } 786 if (c & 2) { 787 jj = SDLTest_RandomIntegerInRange(0, g_numAudioChannels - 1); 788 } else { 789 jj = j; 790 } 791 if (c & 4) { 792 kk = SDLTest_RandomIntegerInRange(0, g_numAudioFrequencies - 1); 793 } else { 794 kk = k; 795 } 796 } while ((i == ii) && (j == jj) && (k == kk)); 797 spec2.format = g_audioFormats[ii]; 798 spec2.channels = g_audioChannels[jj]; 799 spec2.freq = g_audioFrequencies[kk]; 800 801 stream = SDL_CreateAudioStream(&spec1, &spec2); 802 SDLTest_AssertPass("Call to SDL_CreateAudioStream(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)", 803 i, g_audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, g_audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq); 804 SDLTest_AssertCheck(stream != NULL, "Verify stream value; expected: != NULL, got: %p", stream); 805 if (stream == NULL) { 806 SDLTest_LogError("%s", SDL_GetError()); 807 } else { 808 Uint8 *dst_buf = NULL, *src_buf = NULL; 809 int dst_len = 0, src_len = 0, real_dst_len = 0; 810 int l = 64, m; 811 int src_framesize, dst_framesize; 812 int src_silence, dst_silence; 813 814 src_framesize = SDL_AUDIO_FRAMESIZE(spec1); 815 dst_framesize = SDL_AUDIO_FRAMESIZE(spec2); 816 817 src_len = l * src_framesize; 818 SDLTest_Log("Creating dummy sample buffer of %i length (%i bytes)", l, src_len); 819 src_buf = (Uint8 *)SDL_malloc(src_len); 820 SDLTest_AssertCheck(src_buf != NULL, "Check src data buffer to convert is not NULL"); 821 if (src_buf == NULL) { 822 SDL_DestroyAudioStream(stream); 823 return TEST_ABORTED; 824 } 825 826 src_silence = SDL_GetSilenceValueForFormat(spec1.format); 827 SDL_memset(src_buf, src_silence, src_len); 828 829 dst_len = ((int)((((Sint64)l * spec2.freq) - 1) / spec1.freq) + 1) * dst_framesize; 830 dst_buf = (Uint8 *)SDL_malloc(dst_len); 831 SDLTest_AssertCheck(dst_buf != NULL, "Check dst data buffer to convert is not NULL"); 832 if (dst_buf == NULL) { 833 SDL_DestroyAudioStream(stream); 834 SDL_free(src_buf); 835 return TEST_ABORTED; 836 } 837 838 real_dst_len = SDL_GetAudioStreamAvailable(stream); 839 SDLTest_AssertCheck(0 == real_dst_len, "Verify available (pre-put); expected: %i; got: %i", 0, real_dst_len); 840 841 /* Run the audio converter */ 842 if (!SDL_PutAudioStreamData(stream, src_buf, src_len) || 843 !SDL_FlushAudioStream(stream)) { 844 SDL_DestroyAudioStream(stream); 845 SDL_free(src_buf); 846 SDL_free(dst_buf); 847 return TEST_ABORTED; 848 } 849 850 real_dst_len = SDL_GetAudioStreamAvailable(stream); 851 SDLTest_AssertCheck(dst_len == real_dst_len, "Verify available (post-put); expected: %i; got: %i", dst_len, real_dst_len); 852 853 real_dst_len = SDL_GetAudioStreamData(stream, dst_buf, dst_len); 854 SDLTest_AssertCheck(dst_len == real_dst_len, "Verify result value; expected: %i; got: %i", dst_len, real_dst_len); 855 if (dst_len != real_dst_len) { 856 SDL_DestroyAudioStream(stream); 857 SDL_free(src_buf); 858 SDL_free(dst_buf); 859 return TEST_ABORTED; 860 } 861 862 real_dst_len = SDL_GetAudioStreamAvailable(stream); 863 SDLTest_AssertCheck(0 == real_dst_len, "Verify available (post-get); expected: %i; got: %i", 0, real_dst_len); 864 865 dst_silence = SDL_GetSilenceValueForFormat(spec2.format); 866 867 for (m = 0; m < dst_len; ++m) { 868 if (dst_buf[m] != dst_silence) { 869 SDLTest_LogError("Output buffer is not silent"); 870 SDL_DestroyAudioStream(stream); 871 SDL_free(src_buf); 872 SDL_free(dst_buf); 873 return TEST_ABORTED; 874 } 875 } 876 877 SDL_DestroyAudioStream(stream); 878 /* Free converted buffer */ 879 SDL_free(src_buf); 880 SDL_free(dst_buf); 881 } 882 } 883 } 884 } 885 } 886 887 return TEST_COMPLETED; 888} 889 890/** 891 * Opens, checks current connected status, and closes a device. 892 * 893 * \sa SDL_AudioDeviceConnected 894 */ 895static int SDLCALL audio_openCloseAudioDeviceConnected(void *arg) 896{ 897 return TEST_COMPLETED; /* not a thing in SDL3. */ 898} 899 900static double sine_wave_sample(const Sint64 idx, const Sint64 rate, const Sint64 freq, const double phase) 901{ 902 /* Using integer modulo to avoid precision loss caused by large floating 903 * point numbers. Sint64 is needed for the large integer multiplication. 904 * The integers are assumed to be non-negative so that modulo is always 905 * non-negative. 906 * sin(i / rate * freq * 2 * PI + phase) 907 * = sin(mod(i / rate * freq, 1) * 2 * PI + phase) 908 * = sin(mod(i * freq, rate) / rate * 2 * PI + phase) */ 909 return SDL_sin(((double)(idx * freq % rate)) / ((double)rate) * (SDL_PI_D * 2) + phase); 910} 911 912/* Split the data into randomly sized chunks */ 913static int put_audio_data_split(SDL_AudioStream* stream, const void* buf, int len) 914{ 915 SDL_AudioSpec spec; 916 int frame_size; 917 int ret = SDL_GetAudioStreamFormat(stream, &spec, NULL); 918 919 if (!ret) { 920 return -1; 921 } 922 923 frame_size = SDL_AUDIO_FRAMESIZE(spec); 924 925 while (len > 0) { 926 int n = SDLTest_RandomIntegerInRange(1, 10000) * frame_size; 927 n = SDL_min(n, len); 928 ret = SDL_PutAudioStreamData(stream, buf, n); 929 930 if (!ret) { 931 return -1; 932 } 933 934 buf = ((const Uint8*) buf) + n; 935 len -= n; 936 } 937 938 return 0; 939} 940 941/* Read the data in randomly sized chunks */ 942static int get_audio_data_split(SDL_AudioStream* stream, void* buf, int len) { 943 SDL_AudioSpec spec; 944 int frame_size; 945 int ret = SDL_GetAudioStreamFormat(stream, NULL, &spec); 946 int total = 0; 947 948 if (!ret) { 949 return -1; 950 } 951 952 frame_size = SDL_AUDIO_FRAMESIZE(spec); 953 954 while (len > 0) { 955 int n = SDLTest_RandomIntegerInRange(1, 10000) * frame_size; 956 n = SDL_min(n, len); 957 958 ret = SDL_GetAudioStreamData(stream, buf, n); 959 960 if (ret <= 0) { 961 return total ? total : -1; 962 } 963 964 buf = ((Uint8*) buf) + ret; 965 total += ret; 966 len -= ret; 967 } 968 969 return total; 970} 971 972/* Convert the data in chunks, putting/getting randomly sized chunks until finished */ 973static int convert_audio_chunks(SDL_AudioStream* stream, const void* src, int srclen, void* dst, int dstlen) 974{ 975 SDL_AudioSpec src_spec, dst_spec; 976 int src_frame_size, dst_frame_size; 977 int total_in = 0, total_out = 0; 978 int ret = SDL_GetAudioStreamFormat(stream, &src_spec, &dst_spec); 979 980 if (!ret) { 981 return -1; 982 } 983 984 src_frame_size = SDL_AUDIO_FRAMESIZE(src_spec); 985 dst_frame_size = SDL_AUDIO_FRAMESIZE(dst_spec); 986 987 while ((total_in < srclen) || (total_out < dstlen)) { 988 /* Make sure we put in more than the padding frames so we get non-zero output */ 989 const int RESAMPLER_MAX_PADDING_FRAMES = 7; /* Should match RESAMPLER_MAX_PADDING_FRAMES in SDL */ 990 int to_put = SDLTest_RandomIntegerInRange(RESAMPLER_MAX_PADDING_FRAMES + 1, 40000) * src_frame_size; 991 int to_get = SDLTest_RandomIntegerInRange(1, (int)((40000.0f * dst_spec.freq) / src_spec.freq)) * dst_frame_size; 992 to_put = SDL_min(to_put, srclen - total_in); 993 to_get = SDL_min(to_get, dstlen - total_out); 994 995 if (to_put) 996 { 997 ret = put_audio_data_split(stream, (const Uint8*)(src) + total_in, to_put); 998 999 if (ret < 0) { 1000 return total_out ? total_out : ret; 1001 } 1002 1003 total_in += to_put; 1004 1005 if (total_in == srclen) { 1006 ret = SDL_FlushAudioStream(stream); 1007 1008 if (!ret) { 1009 return total_out ? total_out : -1; 1010 } 1011 } 1012 } 1013 1014 if (to_get) 1015 { 1016 ret = get_audio_data_split(stream, (Uint8*)(dst) + total_out, to_get); 1017 1018 if ((ret == 0) && (total_in == srclen)) { 1019 ret = -1; 1020 } 1021 1022 if (ret < 0) { 1023 return total_out ? total_out : ret; 1024 } 1025 1026 total_out += ret; 1027 } 1028 } 1029 1030 return total_out; 1031} 1032 1033/** 1034 * Check signal-to-noise ratio and maximum error of audio resampling. 1035 * 1036 * \sa https://wiki.libsdl.org/SDL_CreateAudioStream 1037 * \sa https://wiki.libsdl.org/SDL_DestroyAudioStream 1038 * \sa https://wiki.libsdl.org/SDL_PutAudioStreamData 1039 * \sa https://wiki.libsdl.org/SDL_FlushAudioStream 1040 * \sa https://wiki.libsdl.org/SDL_GetAudioStreamData 1041 */ 1042static int SDLCALL audio_resampleLoss(void *arg) 1043{ 1044 /* Note: always test long input time (>= 5s from experience) in some test 1045 * cases because an improper implementation may suffer from low resampling 1046 * precision with long input due to e.g. doing subtraction with large floats. */ 1047 struct test_spec_t { 1048 int time; 1049 int freq; 1050 double phase; 1051 int rate_in; 1052 int rate_out; 1053 double signal_to_noise; 1054 double max_error; 1055 } test_specs[] = { 1056 { 50, 440, 0, 44100, 48000, 80, 0.0010 }, 1057 { 50, 5000, SDL_PI_D / 2, 20000, 10000, 999, 0.0001 }, 1058 { 50, 440, 0, 22050, 96000, 79, 0.0120 }, 1059 { 50, 440, 0, 96000, 22050, 80, 0.0002 }, 1060 { 0 } 1061 }; 1062 1063 int spec_idx = 0; 1064 int min_channels = 1; 1065 int max_channels = 1 /*8*/; 1066 int num_channels = min_channels; 1067 1068 for (spec_idx = 0; test_specs[spec_idx].time > 0;) { 1069 const struct test_spec_t *spec = &test_specs[spec_idx]; 1070 const int frames_in = spec->time * spec->rate_in; 1071 const int frames_target = spec->time * spec->rate_out; 1072 const int len_in = (frames_in * num_channels) * (int)sizeof(float); 1073 const int len_target = (frames_target * num_channels) * (int)sizeof(float); 1074 const int max_target = len_target * 2; 1075 1076 SDL_AudioSpec tmpspec1, tmpspec2; 1077 Uint64 tick_beg = 0; 1078 Uint64 tick_end = 0; 1079 int i = 0; 1080 int j = 0; 1081 SDL_AudioStream *stream = NULL; 1082 float *buf_in = NULL; 1083 float *buf_out = NULL; 1084 int len_out = 0; 1085 double max_error = 0; 1086 double sum_squared_error = 0; 1087 double sum_squared_value = 0; 1088 double signal_to_noise = 0; 1089 1090 SDL_zero(tmpspec1); 1091 SDL_zero(tmpspec2); 1092 1093 SDLTest_AssertPass("Test resampling of %i s %i Hz %f phase sine wave from sampling rate of %i Hz to %i Hz", 1094 spec->time, spec->freq, spec->phase, spec->rate_in, spec->rate_out); 1095 1096 tmpspec1.format = SDL_AUDIO_F32; 1097 tmpspec1.channels = num_channels; 1098 tmpspec1.freq = spec->rate_in; 1099 tmpspec2.format = SDL_AUDIO_F32; 1100 tmpspec2.channels = num_channels; 1101 tmpspec2.freq = spec->rate_out; 1102 stream = SDL_CreateAudioStream(&tmpspec1, &tmpspec2); 1103 SDLTest_AssertPass("Call to SDL_CreateAudioStream(SDL_AUDIO_F32, %i, %i, SDL_AUDIO_F32, %i, %i)", num_channels, spec->rate_in, num_channels, spec->rate_out); 1104 SDLTest_AssertCheck(stream != NULL, "Expected SDL_CreateAudioStream to succeed."); 1105 if (stream == NULL) { 1106 return TEST_ABORTED; 1107 } 1108 1109 buf_in = (float *)SDL_malloc(len_in); 1110 SDLTest_AssertCheck(buf_in != NULL, "Expected input buffer to be created."); 1111 if (buf_in == NULL) { 1112 SDL_DestroyAudioStream(stream); 1113 return TEST_ABORTED; 1114 } 1115 1116 for (i = 0; i < frames_in; ++i) { 1117 float f = (float)sine_wave_sample(i, spec->rate_in, spec->freq, spec->phase); 1118 for (j = 0; j < num_channels; ++j) { 1119 *(buf_in + (i * num_channels) + j) = f; 1120 } 1121 } 1122 1123 tick_beg = SDL_GetPerformanceCounter(); 1124 1125 buf_out = (float *)SDL_malloc(max_target); 1126 SDLTest_AssertCheck(buf_out != NULL, "Expected output buffer to be created."); 1127 if (buf_out == NULL) { 1128 SDL_DestroyAudioStream(stream); 1129 SDL_free(buf_in); 1130 return TEST_ABORTED; 1131 } 1132 1133 len_out = convert_audio_chunks(stream, buf_in, len_in, buf_out, max_target); 1134 SDLTest_AssertPass("Call to convert_audio_chunks(stream, buf_in, %i, buf_out, %i)", len_in, len_target); 1135 SDLTest_AssertCheck(len_out == len_target, "Expected output length to be %i, got %i.", 1136 len_target, len_out); 1137 SDL_free(buf_in); 1138 if (len_out != len_target) { 1139 SDL_DestroyAudioStream(stream); 1140 SDL_free(buf_out); 1141 return TEST_ABORTED; 1142 } 1143 1144 tick_end = SDL_GetPerformanceCounter(); 1145 SDLTest_Log("Resampling used %f seconds.", ((double)(tick_end - tick_beg)) / SDL_GetPerformanceFrequency()); 1146 1147 for (i = 0; i < frames_target; ++i) { 1148 const double target = sine_wave_sample(i, spec->rate_out, spec->freq, spec->phase); 1149 for (j = 0; j < num_channels; ++j) { 1150 const float output = *(buf_out + (i * num_channels) + j); 1151 const double error = SDL_fabs(target - output); 1152 max_error = SDL_max(max_error, error); 1153 sum_squared_error += error * error; 1154 sum_squared_value += target * target; 1155 } 1156 } 1157 SDL_DestroyAudioStream(stream); 1158 SDL_free(buf_out); 1159 signal_to_noise = 10 * SDL_log10(sum_squared_value / sum_squared_error); /* decibel */ 1160 SDLTest_AssertCheck(test_double_isfinite(sum_squared_value), "Sum of squared target should be finite."); 1161 SDLTest_AssertCheck(test_double_isfinite(sum_squared_error), "Sum of squared error should be finite."); 1162 /* Infinity is theoretically possible when there is very little to no noise */ 1163 SDLTest_AssertCheck(!ISNAN(signal_to_noise), "Signal-to-noise ratio should not be NaN."); 1164 SDLTest_AssertCheck(test_double_isfinite(max_error), "Maximum conversion error should be finite."); 1165 SDLTest_AssertCheck(signal_to_noise >= spec->signal_to_noise, "Conversion signal-to-noise ratio %f dB should be no less than %f dB.", 1166 signal_to_noise, spec->signal_to_noise); 1167 SDLTest_AssertCheck(max_error <= spec->max_error, "Maximum conversion error %f should be no more than %f.", 1168 max_error, spec->max_error); 1169 1170 if (++num_channels > max_channels) { 1171 num_channels = min_channels; 1172 ++spec_idx; 1173 } 1174 } 1175 1176 return TEST_COMPLETED; 1177} 1178 1179/** 1180 * Check accuracy converting between audio formats. 1181 * 1182 * \sa SDL_ConvertAudioSamples 1183 */ 1184static int SDLCALL audio_convertAccuracy(void *arg) 1185{ 1186 static SDL_AudioFormat formats[] = { SDL_AUDIO_S8, SDL_AUDIO_U8, SDL_AUDIO_S16, SDL_AUDIO_S32 }; 1187 static const char* format_names[] = { "S8", "U8", "S16", "S32" }; 1188 1189 int src_num = 65537 + 2048 + 48 + 256 + 100000; 1190 int src_len = src_num * sizeof(float); 1191 float* src_data = SDL_malloc(src_len); 1192 int i, j; 1193 1194 SDLTest_AssertCheck(src_data != NULL, "Expected source buffer to be created."); 1195 if (src_data == NULL) { 1196 return TEST_ABORTED; 1197 } 1198 1199 j = 0; 1200 1201 /* Generate a uniform range of floats between [-1.0, 1.0] */ 1202 for (i = 0; i < 65537; ++i) { 1203 src_data[j++] = ((float)i - 32768.0f) / 32768.0f; 1204 } 1205 1206 /* Generate floats close to 1.0 */ 1207 const float max_val = 16777216.0f; 1208 1209 for (i = 0; i < 1024; ++i) { 1210 float f = (max_val + (float)(512 - i)) / max_val; 1211 src_data[j++] = f; 1212 src_data[j++] = -f; 1213 } 1214 1215 for (i = 0; i < 24; ++i) { 1216 float f = (max_val + (float)(3u << i)) / max_val; 1217 src_data[j++] = f; 1218 src_data[j++] = -f; 1219 } 1220 1221 /* Generate floats far outside the [-1.0, 1.0] range */ 1222 for (i = 0; i < 128; ++i) { 1223 float f = 2.0f + (float) i; 1224 src_data[j++] = f; 1225 src_data[j++] = -f; 1226 } 1227 1228 /* Fill the rest with random floats between [-1.0, 1.0] */ 1229 for (i = 0; i < 100000; ++i) { 1230 src_data[j++] = SDLTest_RandomSint32() / 2147483648.0f; 1231 } 1232 1233 /* Shuffle the data for good measure */ 1234 for (i = src_num - 1; i > 0; --i) { 1235 float f = src_data[i]; 1236 j = SDLTest_RandomIntegerInRange(0, i); 1237 src_data[i] = src_data[j]; 1238 src_data[j] = f; 1239 } 1240 1241 for (i = 0; i < SDL_arraysize(formats); ++i) { 1242 SDL_AudioSpec src_spec, tmp_spec; 1243 Uint64 convert_begin, convert_end; 1244 Uint8 *tmp_data, *dst_data; 1245 int tmp_len, dst_len; 1246 int ret; 1247 1248 SDL_zero(src_spec); 1249 SDL_zero(tmp_spec); 1250 1251 SDL_AudioFormat format = formats[i]; 1252 const char* format_name = format_names[i]; 1253 1254 /* Formats with > 23 bits can represent every value exactly */ 1255 float min_delta = 1.0f; 1256 float max_delta = -1.0f; 1257 1258 /* Subtract 1 bit to account for sign */ 1259 int bits = SDL_AUDIO_BITSIZE(format) - 1; 1260 float target_max_delta = (bits > 23) ? 0.0f : (1.0f / (float)(1 << bits)); 1261 float target_min_delta = -target_max_delta; 1262 1263 src_spec.format = SDL_AUDIO_F32; 1264 src_spec.channels = 1; 1265 src_spec.freq = 44100; 1266 1267 tmp_spec.format = format; 1268 tmp_spec.channels = 1; 1269 tmp_spec.freq = 44100; 1270 1271 convert_begin = SDL_GetPerformanceCounter(); 1272 1273 tmp_data = NULL; 1274 tmp_len = 0; 1275 ret = SDL_ConvertAudioSamples(&src_spec, (const Uint8*) src_data, src_len, &tmp_spec, &tmp_data, &tmp_len); 1276 SDLTest_AssertCheck(ret == true, "Expected SDL_ConvertAudioSamples(F32->%s) to succeed", format_name); 1277 if (!ret) { 1278 SDL_free(src_data); 1279 return TEST_ABORTED; 1280 } 1281 1282 dst_data = NULL; 1283 dst_len = 0; 1284 ret = SDL_ConvertAudioSamples(&tmp_spec, tmp_data, tmp_len, &src_spec, &dst_data, &dst_len); 1285 SDLTest_AssertCheck(ret == true, "Expected SDL_ConvertAudioSamples(%s->F32) to succeed", format_name); 1286 if (!ret) { 1287 SDL_free(tmp_data); 1288 SDL_free(src_data); 1289 return TEST_ABORTED; 1290 } 1291 1292 convert_end = SDL_GetPerformanceCounter(); 1293 SDLTest_Log("Conversion via %s took %f seconds.", format_name, ((double)(convert_end - convert_begin)) / SDL_GetPerformanceFrequency()); 1294 1295 SDL_free(tmp_data); 1296 1297 for (j = 0; j < src_num; ++j) { 1298 float x = src_data[j]; 1299 float y = ((float*)dst_data)[j]; 1300 float d = SDL_clamp(x, -1.0f, 1.0f) - y; 1301 1302 min_delta = SDL_min(min_delta, d); 1303 max_delta = SDL_max(max_delta, d); 1304 } 1305 1306 SDLTest_AssertCheck(min_delta >= target_min_delta, "%s has min delta of %+f, should be >= %+f", format_name, min_delta, target_min_delta); 1307 SDLTest_AssertCheck(max_delta <= target_max_delta, "%s has max delta of %+f, should be <= %+f", format_name, max_delta, target_max_delta); 1308 1309 SDL_free(dst_data); 1310 } 1311 1312 SDL_free(src_data); 1313 1314 return TEST_COMPLETED; 1315} 1316 1317/** 1318 * Check accuracy when switching between formats 1319 * 1320 * \sa SDL_SetAudioStreamFormat 1321 */ 1322static int SDLCALL audio_formatChange(void *arg) 1323{ 1324 int i; 1325 SDL_AudioSpec spec1, spec2, spec3; 1326 int frames_1, frames_2, frames_3; 1327 int length_1, length_2, length_3; 1328 int result = 0; 1329 int status = TEST_ABORTED; 1330 float* buffer_1 = NULL; 1331 float* buffer_2 = NULL; 1332 float* buffer_3 = NULL; 1333 SDL_AudioStream* stream = NULL; 1334 double max_error = 0; 1335 double sum_squared_error = 0; 1336 double sum_squared_value = 0; 1337 double signal_to_noise = 0; 1338 double target_max_error = 0.02; 1339 double target_signal_to_noise = 75.0; 1340 int sine_freq = 500; 1341 1342 SDL_zero(spec1); 1343 SDL_zero(spec2); 1344 SDL_zero(spec3); 1345 1346 spec1.format = SDL_AUDIO_F32; 1347 spec1.channels = 1; 1348 spec1.freq = 20000; 1349 1350 spec2.format = SDL_AUDIO_F32; 1351 spec2.channels = 1; 1352 spec2.freq = 40000; 1353 1354 spec3.format = SDL_AUDIO_F32; 1355 spec3.channels = 1; 1356 spec3.freq = 80000; 1357 1358 frames_1 = spec1.freq; 1359 frames_2 = spec2.freq; 1360 frames_3 = spec3.freq * 2; 1361 1362 length_1 = (int)(frames_1 * sizeof(*buffer_1)); 1363 buffer_1 = (float*) SDL_malloc(length_1); 1364 if (!SDLTest_AssertCheck(buffer_1 != NULL, "Expected buffer_1 to be created.")) { 1365 goto cleanup; 1366 } 1367 1368 length_2 = (int)(frames_2 * sizeof(*buffer_2)); 1369 buffer_2 = (float*) SDL_malloc(length_2); 1370 if (!SDLTest_AssertCheck(buffer_2 != NULL, "Expected buffer_2 to be created.")) { 1371 goto cleanup; 1372 } 1373 1374 length_3 = (int)(frames_3 * sizeof(*buffer_3)); 1375 buffer_3 = (float*) SDL_malloc(length_3); 1376 if (!SDLTest_AssertCheck(buffer_3 != NULL, "Expected buffer_3 to be created.")) { 1377 goto cleanup; 1378 } 1379 1380 for (i = 0; i < frames_1; ++i) { 1381 buffer_1[i] = (float) sine_wave_sample(i, spec1.freq, sine_freq, 0.0f); 1382 } 1383 1384 for (i = 0; i < frames_2; ++i) { 1385 buffer_2[i] = (float) sine_wave_sample(i, spec2.freq, sine_freq, 0.0f); 1386 } 1387 1388 stream = SDL_CreateAudioStream(NULL, NULL); 1389 if (!SDLTest_AssertCheck(stream != NULL, "Expected SDL_CreateAudioStream to succeed")) { 1390 goto cleanup; 1391 } 1392 1393 result = SDL_SetAudioStreamFormat(stream, &spec1, &spec3); 1394 if (!SDLTest_AssertCheck(result == true, "Expected SDL_SetAudioStreamFormat(spec1, spec3) to succeed")) { 1395 goto cleanup; 1396 } 1397 1398 result = SDL_GetAudioStreamAvailable(stream); 1399 if (!SDLTest_AssertCheck(result == 0, "Expected SDL_GetAudioStreamAvailable return 0")) { 1400 goto cleanup; 1401 } 1402 1403 result = SDL_PutAudioStreamData(stream, buffer_1, length_1); 1404 if (!SDLTest_AssertCheck(result == true, "Expected SDL_PutAudioStreamData(buffer_1) to succeed")) { 1405 goto cleanup; 1406 } 1407 1408 result = SDL_FlushAudioStream(stream); 1409 if (!SDLTest_AssertCheck(result == true, "Expected SDL_FlushAudioStream to succeed")) { 1410 goto cleanup; 1411 } 1412 1413 result = SDL_SetAudioStreamFormat(stream, &spec2, &spec3); 1414 if (!SDLTest_AssertCheck(result == true, "Expected SDL_SetAudioStreamFormat(spec2, spec3) to succeed")) { 1415 goto cleanup; 1416 } 1417 1418 result = SDL_PutAudioStreamData(stream, buffer_2, length_2); 1419 if (!SDLTest_AssertCheck(result == true, "Expected SDL_PutAudioStreamData(buffer_1) to succeed")) { 1420 goto cleanup; 1421 } 1422 1423 result = SDL_FlushAudioStream(stream); 1424 if (!SDLTest_AssertCheck(result == true, "Expected SDL_FlushAudioStream to succeed")) { 1425 goto cleanup; 1426 } 1427 1428 result = SDL_GetAudioStreamAvailable(stream); 1429 if (!SDLTest_AssertCheck(result == length_3, "Expected SDL_GetAudioStreamAvailable to return %i, got %i", length_3, result)) { 1430 goto cleanup; 1431 } 1432 1433 result = SDL_GetAudioStreamData(stream, buffer_3, length_3); 1434 if (!SDLTest_AssertCheck(result == length_3, "Expected SDL_GetAudioStreamData to return %i, got %i", length_3, result)) { 1435 goto cleanup; 1436 } 1437 1438 result = SDL_GetAudioStreamAvailable(stream); 1439 if (!SDLTest_AssertCheck(result == 0, "Expected SDL_GetAudioStreamAvailable to return 0")) { 1440 goto cleanup; 1441 } 1442 1443 for (i = 0; i < frames_3; ++i) { 1444 const float output = buffer_3[i]; 1445 const float target = (float) sine_wave_sample(i, spec3.freq, sine_freq, 0.0f); 1446 const double error = SDL_fabs(target - output); 1447 max_error = SDL_max(max_error, error); 1448 sum_squared_error += error * error; 1449 sum_squared_value += target * target; 1450 } 1451 1452 signal_to_noise = 10 * SDL_log10(sum_squared_value / sum_squared_error); /* decibel */ 1453 SDLTest_AssertCheck(test_double_isfinite(sum_squared_value), "Sum of squared target should be finite."); 1454 SDLTest_AssertCheck(test_double_isfinite(sum_squared_error), "Sum of squared error should be finite."); 1455 /* Infinity is theoretically possible when there is very little to no noise */ 1456 SDLTest_AssertCheck(!ISNAN(signal_to_noise), "Signal-to-noise ratio should not be NaN."); 1457 SDLTest_AssertCheck(test_double_isfinite(max_error), "Maximum conversion error should be finite."); 1458 SDLTest_AssertCheck(signal_to_noise >= target_signal_to_noise, "Conversion signal-to-noise ratio %f dB should be no less than %f dB.", 1459 signal_to_noise, target_signal_to_noise); 1460 SDLTest_AssertCheck(max_error <= target_max_error, "Maximum conversion error %f should be no more than %f.", 1461 max_error, target_max_error); 1462 1463 status = TEST_COMPLETED; 1464 1465cleanup: 1466 SDL_free(buffer_1); 1467 SDL_free(buffer_2); 1468 SDL_free(buffer_3); 1469 SDL_DestroyAudioStream(stream); 1470 1471 return status; 1472} 1473/* ================= Test Case References ================== */ 1474 1475/* Audio test cases */ 1476static const SDLTest_TestCaseReference audioTestGetAudioFormatName = { 1477 audio_getAudioFormatName, "audio_getAudioFormatName", "Call to SDL_GetAudioFormatName", TEST_ENABLED 1478}; 1479 1480static const SDLTest_TestCaseReference audioTest1 = { 1481 audio_enumerateAndNameAudioDevices, "audio_enumerateAndNameAudioDevices", "Enumerate and name available audio devices (playback and recording)", TEST_ENABLED 1482}; 1483 1484static const SDLTest_TestCaseReference audioTest2 = { 1485 audio_enumerateAndNameAudioDevicesNegativeTests, "audio_enumerateAndNameAudioDevicesNegativeTests", "Negative tests around enumeration and naming of audio devices.", TEST_ENABLED 1486}; 1487 1488static const SDLTest_TestCaseReference audioTest3 = { 1489 audio_printAudioDrivers, "audio_printAudioDrivers", "Checks available audio driver names.", TEST_ENABLED 1490}; 1491 1492static const SDLTest_TestCaseReference audioTest4 = { 1493 audio_printCurrentAudioDriver, "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED 1494}; 1495 1496static const SDLTest_TestCaseReference audioTest5 = { 1497 audio_buildAudioStream, "audio_buildAudioStream", "Builds various audio conversion structures.", TEST_ENABLED 1498}; 1499 1500static const SDLTest_TestCaseReference audioTest6 = { 1501 audio_buildAudioStreamNegative, "audio_buildAudioStreamNegative", "Checks calls with invalid input to SDL_CreateAudioStream", TEST_ENABLED 1502}; 1503 1504static const SDLTest_TestCaseReference audioTest7 = { 1505 audio_getAudioStatus, "audio_getAudioStatus", "Checks current audio status.", TEST_ENABLED 1506}; 1507 1508static const SDLTest_TestCaseReference audioTest8 = { 1509 audio_openCloseAndGetAudioStatus, "audio_openCloseAndGetAudioStatus", "Opens and closes audio device and get audio status.", TEST_ENABLED 1510}; 1511 1512static const SDLTest_TestCaseReference audioTest9 = { 1513 audio_lockUnlockOpenAudioDevice, "audio_lockUnlockOpenAudioDevice", "Locks and unlocks an open audio device.", TEST_ENABLED 1514}; 1515 1516static const SDLTest_TestCaseReference audioTest10 = { 1517 audio_convertAudio, "audio_convertAudio", "Convert audio using available formats.", TEST_ENABLED 1518}; 1519 1520/* TODO: enable test when SDL_AudioDeviceConnected has been implemented. */ 1521 1522static const SDLTest_TestCaseReference audioTest11 = { 1523 audio_openCloseAudioDeviceConnected, "audio_openCloseAudioDeviceConnected", "Opens and closes audio device and get connected status.", TEST_DISABLED 1524}; 1525 1526static const SDLTest_TestCaseReference audioTest12 = { 1527 audio_quitInitAudioSubSystem, "audio_quitInitAudioSubSystem", "Quit and re-init audio subsystem.", TEST_ENABLED 1528}; 1529 1530static const SDLTest_TestCaseReference audioTest13 = { 1531 audio_initQuitAudio, "audio_initQuitAudio", "Init and quit audio drivers directly.", TEST_ENABLED 1532}; 1533 1534static const SDLTest_TestCaseReference audioTest14 = { 1535 audio_initOpenCloseQuitAudio, "audio_initOpenCloseQuitAudio", "Cycle through init, open, close and quit with various audio specs.", TEST_ENABLED 1536}; 1537 1538static const SDLTest_TestCaseReference audioTest15 = { 1539 audio_pauseUnpauseAudio, "audio_pauseUnpauseAudio", "Pause and Unpause audio for various audio specs while testing callback.", TEST_ENABLED 1540}; 1541 1542static const SDLTest_TestCaseReference audioTest16 = { 1543 audio_resampleLoss, "audio_resampleLoss", "Check signal-to-noise ratio and maximum error of audio resampling.", TEST_ENABLED 1544}; 1545 1546static const SDLTest_TestCaseReference audioTest17 = { 1547 audio_convertAccuracy, "audio_convertAccuracy", "Check accuracy converting between audio formats.", TEST_ENABLED 1548}; 1549 1550static const SDLTest_TestCaseReference audioTest18 = { 1551 audio_formatChange, "audio_formatChange", "Check handling of format changes.", TEST_ENABLED 1552}; 1553 1554/* Sequence of Audio test cases */ 1555static const SDLTest_TestCaseReference *audioTests[] = { 1556 &audioTestGetAudioFormatName, 1557 &audioTest1, &audioTest2, &audioTest3, &audioTest4, &audioTest5, &audioTest6, 1558 &audioTest7, &audioTest8, &audioTest9, &audioTest10, &audioTest11, 1559 &audioTest12, &audioTest13, &audioTest14, &audioTest15, &audioTest16, 1560 &audioTest17, &audioTest18, NULL 1561}; 1562 1563/* Audio test suite (global) */ 1564SDLTest_TestSuiteReference audioTestSuite = { 1565 "Audio", 1566 audioSetUp, 1567 audioTests, 1568 audioTearDown 1569}; 1570
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.