🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1167 lines
45 KiB

  1. /**
  2. * Original code: automated SDL audio test written by Edgar Simo "bobbens"
  3. * New/updated tests: aschiffler at ferzkopp dot net
  4. */
  5. /* quiet windows compiler warnings */
  6. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  7. #define _CRT_SECURE_NO_WARNINGS
  8. #endif
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "SDL.h"
  13. #include "SDL_test.h"
  14. /* ================= Test Case Implementation ================== */
  15. /* Fixture */
  16. void _audioSetUp(void *arg)
  17. {
  18. /* Start SDL audio subsystem */
  19. int ret = SDL_InitSubSystem(SDL_INIT_AUDIO);
  20. SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO)");
  21. SDLTest_AssertCheck(ret == 0, "Check result from SDL_InitSubSystem(SDL_INIT_AUDIO)");
  22. if (ret != 0) {
  23. SDLTest_LogError("%s", SDL_GetError());
  24. }
  25. }
  26. void _audioTearDown(void *arg)
  27. {
  28. /* Remove a possibly created file from SDL disk writer audio driver; ignore errors */
  29. (void)remove("sdlaudio.raw");
  30. SDLTest_AssertPass("Cleanup of test files completed");
  31. }
  32. /* Global counter for callback invocation */
  33. int _audio_testCallbackCounter;
  34. /* Global accumulator for total callback length */
  35. int _audio_testCallbackLength;
  36. /* Test callback function */
  37. void SDLCALL _audio_testCallback(void *userdata, Uint8 *stream, int len)
  38. {
  39. /* track that callback was called */
  40. _audio_testCallbackCounter++;
  41. _audio_testCallbackLength += len;
  42. }
  43. /* Test case functions */
  44. /**
  45. * \brief Stop and restart audio subsystem
  46. *
  47. * \sa https://wiki.libsdl.org/SDL_QuitSubSystem
  48. * \sa https://wiki.libsdl.org/SDL_InitSubSystem
  49. */
  50. int audio_quitInitAudioSubSystem()
  51. {
  52. /* Stop SDL audio subsystem */
  53. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  54. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  55. /* Restart audio again */
  56. _audioSetUp(NULL);
  57. return TEST_COMPLETED;
  58. }
  59. /**
  60. * \brief Start and stop audio directly
  61. *
  62. * \sa https://wiki.libsdl.org/SDL_InitAudio
  63. * \sa https://wiki.libsdl.org/SDL_QuitAudio
  64. */
  65. int audio_initQuitAudio()
  66. {
  67. int result;
  68. int i, iMax;
  69. const char *audioDriver;
  70. /* Stop SDL audio subsystem */
  71. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  72. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  73. /* Loop over all available audio drivers */
  74. iMax = SDL_GetNumAudioDrivers();
  75. SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
  76. SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
  77. for (i = 0; i < iMax; i++) {
  78. audioDriver = SDL_GetAudioDriver(i);
  79. SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
  80. SDLTest_Assert(audioDriver != NULL, "Audio driver name is not NULL");
  81. SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver); /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */
  82. /* Call Init */
  83. result = SDL_AudioInit(audioDriver);
  84. SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
  85. SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
  86. /* Call Quit */
  87. SDL_AudioQuit();
  88. SDLTest_AssertPass("Call to SDL_AudioQuit()");
  89. }
  90. /* NULL driver specification */
  91. audioDriver = NULL;
  92. /* Call Init */
  93. result = SDL_AudioInit(audioDriver);
  94. SDLTest_AssertPass("Call to SDL_AudioInit(NULL)");
  95. SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
  96. /* Call Quit */
  97. SDL_AudioQuit();
  98. SDLTest_AssertPass("Call to SDL_AudioQuit()");
  99. /* Restart audio again */
  100. _audioSetUp(NULL);
  101. return TEST_COMPLETED;
  102. }
  103. /**
  104. * \brief Start, open, close and stop audio
  105. *
  106. * \sa https://wiki.libsdl.org/SDL_InitAudio
  107. * \sa https://wiki.libsdl.org/SDL_OpenAudio
  108. * \sa https://wiki.libsdl.org/SDL_CloseAudio
  109. * \sa https://wiki.libsdl.org/SDL_QuitAudio
  110. */
  111. int audio_initOpenCloseQuitAudio()
  112. {
  113. int result, expectedResult;
  114. int i, iMax, j, k;
  115. const char *audioDriver;
  116. SDL_AudioSpec desired;
  117. /* Stop SDL audio subsystem */
  118. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  119. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  120. /* Loop over all available audio drivers */
  121. iMax = SDL_GetNumAudioDrivers();
  122. SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
  123. SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
  124. for (i = 0; i < iMax; i++) {
  125. audioDriver = SDL_GetAudioDriver(i);
  126. SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
  127. SDLTest_Assert(audioDriver != NULL, "Audio driver name is not NULL");
  128. SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver); /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */
  129. /* Change specs */
  130. for (j = 0; j < 2; j++) {
  131. /* Call Init */
  132. result = SDL_AudioInit(audioDriver);
  133. SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
  134. SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
  135. /* Set spec */
  136. SDL_memset(&desired, 0, sizeof(desired));
  137. switch (j) {
  138. case 0:
  139. /* Set standard desired spec */
  140. desired.freq = 22050;
  141. desired.format = AUDIO_S16SYS;
  142. desired.channels = 2;
  143. desired.samples = 4096;
  144. desired.callback = _audio_testCallback;
  145. desired.userdata = NULL;
  146. break;
  147. case 1:
  148. /* Set custom desired spec */
  149. desired.freq = 48000;
  150. desired.format = AUDIO_F32SYS;
  151. desired.channels = 2;
  152. desired.samples = 2048;
  153. desired.callback = _audio_testCallback;
  154. desired.userdata = NULL;
  155. break;
  156. }
  157. /* Call Open (maybe multiple times) */
  158. for (k = 0; k <= j; k++) {
  159. result = SDL_OpenAudio(&desired, NULL);
  160. SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL), call %d", j, k + 1);
  161. expectedResult = (k == 0) ? 0 : -1;
  162. SDLTest_AssertCheck(result == expectedResult, "Verify return value; expected: %d, got: %d", expectedResult, result);
  163. }
  164. /* Call Close (maybe multiple times) */
  165. for (k = 0; k <= j; k++) {
  166. SDL_CloseAudio();
  167. SDLTest_AssertPass("Call to SDL_CloseAudio(), call %d", k + 1);
  168. }
  169. /* Call Quit (maybe multiple times) */
  170. for (k = 0; k <= j; k++) {
  171. SDL_AudioQuit();
  172. SDLTest_AssertPass("Call to SDL_AudioQuit(), call %d", k + 1);
  173. }
  174. } /* spec loop */
  175. } /* driver loop */
  176. /* Restart audio again */
  177. _audioSetUp(NULL);
  178. return TEST_COMPLETED;
  179. }
  180. /**
  181. * \brief Pause and unpause audio
  182. *
  183. * \sa https://wiki.libsdl.org/SDL_PauseAudio
  184. */
  185. int audio_pauseUnpauseAudio()
  186. {
  187. int result;
  188. int i, iMax, j, k, l;
  189. int totalDelay;
  190. int pause_on;
  191. int originalCounter;
  192. const char *audioDriver;
  193. SDL_AudioSpec desired;
  194. /* Stop SDL audio subsystem */
  195. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  196. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  197. /* Loop over all available audio drivers */
  198. iMax = SDL_GetNumAudioDrivers();
  199. SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
  200. SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
  201. for (i = 0; i < iMax; i++) {
  202. audioDriver = SDL_GetAudioDriver(i);
  203. SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
  204. SDLTest_Assert(audioDriver != NULL, "Audio driver name is not NULL");
  205. SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver); /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */
  206. /* Change specs */
  207. for (j = 0; j < 2; j++) {
  208. /* Call Init */
  209. result = SDL_AudioInit(audioDriver);
  210. SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
  211. SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
  212. /* Set spec */
  213. SDL_memset(&desired, 0, sizeof(desired));
  214. switch (j) {
  215. case 0:
  216. /* Set standard desired spec */
  217. desired.freq = 22050;
  218. desired.format = AUDIO_S16SYS;
  219. desired.channels = 2;
  220. desired.samples = 4096;
  221. desired.callback = _audio_testCallback;
  222. desired.userdata = NULL;
  223. break;
  224. case 1:
  225. /* Set custom desired spec */
  226. desired.freq = 48000;
  227. desired.format = AUDIO_F32SYS;
  228. desired.channels = 2;
  229. desired.samples = 2048;
  230. desired.callback = _audio_testCallback;
  231. desired.userdata = NULL;
  232. break;
  233. }
  234. /* Call Open */
  235. result = SDL_OpenAudio(&desired, NULL);
  236. SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL)", j);
  237. SDLTest_AssertCheck(result == 0, "Verify return value; expected: 0 got: %d", result);
  238. /* Start and stop audio multiple times */
  239. for (l = 0; l < 3; l++) {
  240. SDLTest_Log("Pause/Unpause iteration: %d", l + 1);
  241. /* Reset callback counters */
  242. _audio_testCallbackCounter = 0;
  243. _audio_testCallbackLength = 0;
  244. /* Un-pause audio to start playing (maybe multiple times) */
  245. pause_on = 0;
  246. for (k = 0; k <= j; k++) {
  247. SDL_PauseAudio(pause_on);
  248. SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k + 1);
  249. }
  250. /* Wait for callback */
  251. totalDelay = 0;
  252. do {
  253. SDL_Delay(10);
  254. totalDelay += 10;
  255. } while (_audio_testCallbackCounter == 0 && totalDelay < 1000);
  256. SDLTest_AssertCheck(_audio_testCallbackCounter > 0, "Verify callback counter; expected: >0 got: %d", _audio_testCallbackCounter);
  257. SDLTest_AssertCheck(_audio_testCallbackLength > 0, "Verify callback length; expected: >0 got: %d", _audio_testCallbackLength);
  258. /* Pause audio to stop playing (maybe multiple times) */
  259. for (k = 0; k <= j; k++) {
  260. pause_on = (k == 0) ? 1 : SDLTest_RandomIntegerInRange(99, 9999);
  261. SDL_PauseAudio(pause_on);
  262. SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k + 1);
  263. }
  264. /* Ensure callback is not called again */
  265. originalCounter = _audio_testCallbackCounter;
  266. SDL_Delay(totalDelay + 10);
  267. SDLTest_AssertCheck(originalCounter == _audio_testCallbackCounter, "Verify callback counter; expected: %d, got: %d", originalCounter, _audio_testCallbackCounter);
  268. }
  269. /* Call Close */
  270. SDL_CloseAudio();
  271. SDLTest_AssertPass("Call to SDL_CloseAudio()");
  272. /* Call Quit */
  273. SDL_AudioQuit();
  274. SDLTest_AssertPass("Call to SDL_AudioQuit()");
  275. } /* spec loop */
  276. } /* driver loop */
  277. /* Restart audio again */
  278. _audioSetUp(NULL);
  279. return TEST_COMPLETED;
  280. }
  281. /**
  282. * \brief Enumerate and name available audio devices (output and capture).
  283. *
  284. * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
  285. * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
  286. */
  287. int audio_enumerateAndNameAudioDevices()
  288. {
  289. int t, tt;
  290. int i, n, nn;
  291. const char *name, *nameAgain;
  292. /* Iterate over types: t=0 output device, t=1 input/capture device */
  293. for (t = 0; t < 2; t++) {
  294. /* Get number of devices. */
  295. n = SDL_GetNumAudioDevices(t);
  296. SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(%i)", t);
  297. SDLTest_Log("Number of %s devices < 0, reported as %i", (t) ? "capture" : "output", n);
  298. SDLTest_AssertCheck(n >= 0, "Validate result is >= 0, got: %i", n);
  299. /* Variation of non-zero type */
  300. if (t == 1) {
  301. tt = t + SDLTest_RandomIntegerInRange(1, 10);
  302. nn = SDL_GetNumAudioDevices(tt);
  303. SDLTest_AssertCheck(n == nn, "Verify result from SDL_GetNumAudioDevices(%i), expected same number of audio devices %i, got %i", tt, n, nn);
  304. nn = SDL_GetNumAudioDevices(-tt);
  305. SDLTest_AssertCheck(n == nn, "Verify result from SDL_GetNumAudioDevices(%i), expected same number of audio devices %i, got %i", -tt, n, nn);
  306. }
  307. /* List devices. */
  308. if (n > 0) {
  309. for (i = 0; i < n; i++) {
  310. name = SDL_GetAudioDeviceName(i, t);
  311. SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
  312. SDLTest_AssertCheck(name != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, t);
  313. if (name != NULL) {
  314. SDLTest_AssertCheck(name[0] != '\0', "verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, t, name);
  315. if (t == 1) {
  316. /* Also try non-zero type */
  317. tt = t + SDLTest_RandomIntegerInRange(1, 10);
  318. nameAgain = SDL_GetAudioDeviceName(i, tt);
  319. SDLTest_AssertCheck(nameAgain != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, tt);
  320. if (nameAgain != NULL) {
  321. SDLTest_AssertCheck(nameAgain[0] != '\0', "Verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, tt, nameAgain);
  322. SDLTest_AssertCheck(SDL_strcmp(name, nameAgain) == 0,
  323. "Verify SDL_GetAudioDeviceName(%i, %i) and SDL_GetAudioDeviceName(%i %i) return the same string",
  324. i, t, i, tt);
  325. }
  326. }
  327. }
  328. }
  329. }
  330. }
  331. return TEST_COMPLETED;
  332. }
  333. /**
  334. * \brief Negative tests around enumeration and naming of audio devices.
  335. *
  336. * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
  337. * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
  338. */
  339. int audio_enumerateAndNameAudioDevicesNegativeTests()
  340. {
  341. int t;
  342. int i, j, no, nc;
  343. const char *name;
  344. /* Get number of devices. */
  345. no = SDL_GetNumAudioDevices(0);
  346. SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
  347. nc = SDL_GetNumAudioDevices(1);
  348. SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(1)");
  349. /* Invalid device index when getting name */
  350. for (t = 0; t < 2; t++) {
  351. /* Negative device index */
  352. i = SDLTest_RandomIntegerInRange(-10, -1);
  353. name = SDL_GetAudioDeviceName(i, t);
  354. SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
  355. SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result NULL, expected NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
  356. /* Device index past range */
  357. for (j = 0; j < 3; j++) {
  358. i = (t) ? nc + j : no + j;
  359. name = SDL_GetAudioDeviceName(i, t);
  360. SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
  361. SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result, expected: NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
  362. }
  363. /* Capture index past capture range but within output range */
  364. if ((no > 0) && (no > nc) && (t == 1)) {
  365. i = no - 1;
  366. name = SDL_GetAudioDeviceName(i, t);
  367. SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
  368. SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result, expected: NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
  369. }
  370. }
  371. return TEST_COMPLETED;
  372. }
  373. /**
  374. * \brief Checks available audio driver names.
  375. *
  376. * \sa https://wiki.libsdl.org/SDL_GetNumAudioDrivers
  377. * \sa https://wiki.libsdl.org/SDL_GetAudioDriver
  378. */
  379. int audio_printAudioDrivers()
  380. {
  381. int i, n;
  382. const char *name;
  383. /* Get number of drivers */
  384. n = SDL_GetNumAudioDrivers();
  385. SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
  386. SDLTest_AssertCheck(n >= 0, "Verify number of audio drivers >= 0, got: %i", n);
  387. /* List drivers. */
  388. if (n > 0) {
  389. for (i = 0; i < n; i++) {
  390. name = SDL_GetAudioDriver(i);
  391. SDLTest_AssertPass("Call to SDL_GetAudioDriver(%i)", i);
  392. SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
  393. if (name != NULL) {
  394. SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
  395. }
  396. }
  397. }
  398. return TEST_COMPLETED;
  399. }
  400. /**
  401. * \brief Checks current audio driver name with initialized audio.
  402. *
  403. * \sa https://wiki.libsdl.org/SDL_GetCurrentAudioDriver
  404. */
  405. int audio_printCurrentAudioDriver()
  406. {
  407. /* Check current audio driver */
  408. const char *name = SDL_GetCurrentAudioDriver();
  409. SDLTest_AssertPass("Call to SDL_GetCurrentAudioDriver()");
  410. SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
  411. if (name != NULL) {
  412. SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
  413. }
  414. return TEST_COMPLETED;
  415. }
  416. /* Definition of all formats, channels, and frequencies used to test audio conversions */
  417. const int _numAudioFormats = 18;
  418. SDL_AudioFormat _audioFormats[] = { AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S16SYS, AUDIO_S16, AUDIO_U16LSB,
  419. AUDIO_U16MSB, AUDIO_U16SYS, AUDIO_U16, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S32SYS, AUDIO_S32,
  420. AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_F32SYS, AUDIO_F32 };
  421. const char *_audioFormatsVerbose[] = { "AUDIO_S8", "AUDIO_U8", "AUDIO_S16LSB", "AUDIO_S16MSB", "AUDIO_S16SYS", "AUDIO_S16", "AUDIO_U16LSB",
  422. "AUDIO_U16MSB", "AUDIO_U16SYS", "AUDIO_U16", "AUDIO_S32LSB", "AUDIO_S32MSB", "AUDIO_S32SYS", "AUDIO_S32",
  423. "AUDIO_F32LSB", "AUDIO_F32MSB", "AUDIO_F32SYS", "AUDIO_F32" };
  424. const int _numAudioChannels = 4;
  425. Uint8 _audioChannels[] = { 1, 2, 4, 6 };
  426. const int _numAudioFrequencies = 4;
  427. int _audioFrequencies[] = { 11025, 22050, 44100, 48000 };
  428. /**
  429. * \brief Builds various audio conversion structures
  430. *
  431. * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
  432. */
  433. int audio_buildAudioCVT()
  434. {
  435. int result;
  436. SDL_AudioCVT cvt;
  437. SDL_AudioSpec spec1;
  438. SDL_AudioSpec spec2;
  439. int i, ii, j, jj, k, kk;
  440. /* No conversion needed */
  441. spec1.format = AUDIO_S16LSB;
  442. spec1.channels = 2;
  443. spec1.freq = 22050;
  444. result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
  445. spec1.format, spec1.channels, spec1.freq);
  446. SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec1)");
  447. SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0, got: %i", result);
  448. /* Typical conversion */
  449. spec1.format = AUDIO_S8;
  450. spec1.channels = 1;
  451. spec1.freq = 22050;
  452. spec2.format = AUDIO_S16LSB;
  453. spec2.channels = 2;
  454. spec2.freq = 44100;
  455. result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
  456. spec2.format, spec2.channels, spec2.freq);
  457. SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
  458. SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
  459. /* All source conversions with random conversion targets, allow 'null' conversions */
  460. for (i = 0; i < _numAudioFormats; i++) {
  461. for (j = 0; j < _numAudioChannels; j++) {
  462. for (k = 0; k < _numAudioFrequencies; k++) {
  463. spec1.format = _audioFormats[i];
  464. spec1.channels = _audioChannels[j];
  465. spec1.freq = _audioFrequencies[k];
  466. ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
  467. jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
  468. kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
  469. spec2.format = _audioFormats[ii];
  470. spec2.channels = _audioChannels[jj];
  471. spec2.freq = _audioFrequencies[kk];
  472. result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
  473. spec2.format, spec2.channels, spec2.freq);
  474. SDLTest_AssertPass("Call to SDL_BuildAudioCVT(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)",
  475. i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
  476. SDLTest_AssertCheck(result == 0 || result == 1, "Verify result value; expected: 0 or 1, got: %i", result);
  477. if (result < 0) {
  478. SDLTest_LogError("%s", SDL_GetError());
  479. } else {
  480. SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
  481. }
  482. }
  483. }
  484. }
  485. return TEST_COMPLETED;
  486. }
  487. /**
  488. * \brief Checkes calls with invalid input to SDL_BuildAudioCVT
  489. *
  490. * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
  491. */
  492. int audio_buildAudioCVTNegative()
  493. {
  494. const char *expectedError = "Parameter 'cvt' is invalid";
  495. const char *error;
  496. int result;
  497. SDL_AudioCVT cvt;
  498. SDL_AudioSpec spec1;
  499. SDL_AudioSpec spec2;
  500. int i;
  501. char message[256];
  502. /* Valid format */
  503. spec1.format = AUDIO_S8;
  504. spec1.channels = 1;
  505. spec1.freq = 22050;
  506. spec2.format = AUDIO_S16LSB;
  507. spec2.channels = 2;
  508. spec2.freq = 44100;
  509. SDL_ClearError();
  510. SDLTest_AssertPass("Call to SDL_ClearError()");
  511. /* NULL input for CVT buffer */
  512. result = SDL_BuildAudioCVT((SDL_AudioCVT *)NULL, spec1.format, spec1.channels, spec1.freq,
  513. spec2.format, spec2.channels, spec2.freq);
  514. SDLTest_AssertPass("Call to SDL_BuildAudioCVT(NULL,...)");
  515. SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
  516. error = SDL_GetError();
  517. SDLTest_AssertPass("Call to SDL_GetError()");
  518. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  519. if (error != NULL) {
  520. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  521. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  522. }
  523. /* Invalid conversions */
  524. for (i = 1; i < 64; i++) {
  525. /* Valid format to start with */
  526. spec1.format = AUDIO_S8;
  527. spec1.channels = 1;
  528. spec1.freq = 22050;
  529. spec2.format = AUDIO_S16LSB;
  530. spec2.channels = 2;
  531. spec2.freq = 44100;
  532. SDL_ClearError();
  533. SDLTest_AssertPass("Call to SDL_ClearError()");
  534. /* Set various invalid format inputs */
  535. SDL_strlcpy(message, "Invalid: ", 256);
  536. if (i & 1) {
  537. SDL_strlcat(message, " spec1.format", 256);
  538. spec1.format = 0;
  539. }
  540. if (i & 2) {
  541. SDL_strlcat(message, " spec1.channels", 256);
  542. spec1.channels = 0;
  543. }
  544. if (i & 4) {
  545. SDL_strlcat(message, " spec1.freq", 256);
  546. spec1.freq = 0;
  547. }
  548. if (i & 8) {
  549. SDL_strlcat(message, " spec2.format", 256);
  550. spec2.format = 0;
  551. }
  552. if (i & 16) {
  553. SDL_strlcat(message, " spec2.channels", 256);
  554. spec2.channels = 0;
  555. }
  556. if (i & 32) {
  557. SDL_strlcat(message, " spec2.freq", 256);
  558. spec2.freq = 0;
  559. }
  560. SDLTest_Log("%s", message);
  561. result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
  562. spec2.format, spec2.channels, spec2.freq);
  563. SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
  564. SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
  565. error = SDL_GetError();
  566. SDLTest_AssertPass("Call to SDL_GetError()");
  567. SDLTest_AssertCheck(error != NULL && error[0] != '\0', "Validate that error message was not NULL or empty");
  568. }
  569. SDL_ClearError();
  570. SDLTest_AssertPass("Call to SDL_ClearError()");
  571. return TEST_COMPLETED;
  572. }
  573. /**
  574. * \brief Checks current audio status.
  575. *
  576. * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
  577. */
  578. int audio_getAudioStatus()
  579. {
  580. SDL_AudioStatus result;
  581. /* Check current audio status */
  582. result = SDL_GetAudioStatus();
  583. SDLTest_AssertPass("Call to SDL_GetAudioStatus()");
  584. SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
  585. "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
  586. SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
  587. return TEST_COMPLETED;
  588. }
  589. /**
  590. * \brief Opens, checks current audio status, and closes a device.
  591. *
  592. * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
  593. */
  594. int audio_openCloseAndGetAudioStatus()
  595. {
  596. SDL_AudioStatus result;
  597. int i;
  598. int count;
  599. const char *device;
  600. SDL_AudioDeviceID id;
  601. SDL_AudioSpec desired, obtained;
  602. /* Get number of devices. */
  603. count = SDL_GetNumAudioDevices(0);
  604. SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
  605. if (count > 0) {
  606. for (i = 0; i < count; i++) {
  607. /* Get device name */
  608. device = SDL_GetAudioDeviceName(i, 0);
  609. SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
  610. SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
  611. if (device == NULL) {
  612. return TEST_ABORTED;
  613. }
  614. /* Set standard desired spec */
  615. desired.freq = 22050;
  616. desired.format = AUDIO_S16SYS;
  617. desired.channels = 2;
  618. desired.samples = 4096;
  619. desired.callback = _audio_testCallback;
  620. desired.userdata = NULL;
  621. /* Open device */
  622. id = SDL_OpenAudioDevice(device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
  623. SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
  624. SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %" SDL_PRIu32, id);
  625. if (id > 1) {
  626. /* Check device audio status */
  627. result = SDL_GetAudioDeviceStatus(id);
  628. SDLTest_AssertPass("Call to SDL_GetAudioDeviceStatus()");
  629. SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
  630. "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
  631. SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
  632. /* Close device again */
  633. SDL_CloseAudioDevice(id);
  634. SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
  635. }
  636. }
  637. } else {
  638. SDLTest_Log("No devices to test with");
  639. }
  640. return TEST_COMPLETED;
  641. }
  642. /**
  643. * \brief Locks and unlocks open audio device.
  644. *
  645. * \sa https://wiki.libsdl.org/SDL_LockAudioDevice
  646. * \sa https://wiki.libsdl.org/SDL_UnlockAudioDevice
  647. */
  648. int audio_lockUnlockOpenAudioDevice()
  649. {
  650. int i;
  651. int count;
  652. const char *device;
  653. SDL_AudioDeviceID id;
  654. SDL_AudioSpec desired, obtained;
  655. /* Get number of devices. */
  656. count = SDL_GetNumAudioDevices(0);
  657. SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
  658. if (count > 0) {
  659. for (i = 0; i < count; i++) {
  660. /* Get device name */
  661. device = SDL_GetAudioDeviceName(i, 0);
  662. SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
  663. SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
  664. if (device == NULL) {
  665. return TEST_ABORTED;
  666. }
  667. /* Set standard desired spec */
  668. desired.freq = 22050;
  669. desired.format = AUDIO_S16SYS;
  670. desired.channels = 2;
  671. desired.samples = 4096;
  672. desired.callback = _audio_testCallback;
  673. desired.userdata = NULL;
  674. /* Open device */
  675. id = SDL_OpenAudioDevice(device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
  676. SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
  677. SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %" SDL_PRIu32, id);
  678. if (id > 1) {
  679. /* Lock to protect callback */
  680. SDL_LockAudioDevice(id);
  681. SDLTest_AssertPass("SDL_LockAudioDevice(%" SDL_PRIu32 ")", id);
  682. /* Simulate callback processing */
  683. SDL_Delay(10);
  684. SDLTest_Log("Simulate callback processing - delay");
  685. /* Unlock again */
  686. SDL_UnlockAudioDevice(id);
  687. SDLTest_AssertPass("SDL_UnlockAudioDevice(%" SDL_PRIu32 ")", id);
  688. /* Close device again */
  689. SDL_CloseAudioDevice(id);
  690. SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
  691. }
  692. }
  693. } else {
  694. SDLTest_Log("No devices to test with");
  695. }
  696. return TEST_COMPLETED;
  697. }
  698. /**
  699. * \brief Convert audio using various conversion structures
  700. *
  701. * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
  702. * \sa https://wiki.libsdl.org/SDL_ConvertAudio
  703. */
  704. int audio_convertAudio()
  705. {
  706. int result;
  707. SDL_AudioCVT cvt;
  708. SDL_AudioSpec spec1;
  709. SDL_AudioSpec spec2;
  710. int c;
  711. char message[128];
  712. int i, ii, j, jj, k, kk, l, ll;
  713. /* Iterate over bitmask that determines which parameters are modified in the conversion */
  714. for (c = 1; c < 8; c++) {
  715. SDL_strlcpy(message, "Changing:", 128);
  716. if (c & 1) {
  717. SDL_strlcat(message, " Format", 128);
  718. }
  719. if (c & 2) {
  720. SDL_strlcat(message, " Channels", 128);
  721. }
  722. if (c & 4) {
  723. SDL_strlcat(message, " Frequencies", 128);
  724. }
  725. SDLTest_Log("%s", message);
  726. /* All source conversions with random conversion targets */
  727. for (i = 0; i < _numAudioFormats; i++) {
  728. for (j = 0; j < _numAudioChannels; j++) {
  729. for (k = 0; k < _numAudioFrequencies; k++) {
  730. spec1.format = _audioFormats[i];
  731. spec1.channels = _audioChannels[j];
  732. spec1.freq = _audioFrequencies[k];
  733. /* Ensure we have a different target format */
  734. do {
  735. if (c & 1) {
  736. ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
  737. } else {
  738. ii = 1;
  739. }
  740. if (c & 2) {
  741. jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
  742. } else {
  743. jj = j;
  744. }
  745. if (c & 4) {
  746. kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
  747. } else {
  748. kk = k;
  749. }
  750. } while ((i == ii) && (j == jj) && (k == kk));
  751. spec2.format = _audioFormats[ii];
  752. spec2.channels = _audioChannels[jj];
  753. spec2.freq = _audioFrequencies[kk];
  754. result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
  755. spec2.format, spec2.channels, spec2.freq);
  756. SDLTest_AssertPass("Call to SDL_BuildAudioCVT(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)",
  757. i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
  758. SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
  759. if (result != 1) {
  760. SDLTest_LogError("%s", SDL_GetError());
  761. } else {
  762. SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
  763. if (cvt.len_mult < 1) {
  764. return TEST_ABORTED;
  765. }
  766. /* Create some random data to convert */
  767. l = 64;
  768. ll = l * cvt.len_mult;
  769. SDLTest_Log("Creating dummy sample buffer of %i length (%i bytes)", l, ll);
  770. cvt.len = l;
  771. cvt.buf = (Uint8 *)SDL_malloc(ll);
  772. SDLTest_AssertCheck(cvt.buf != NULL, "Check data buffer to convert is not NULL");
  773. if (cvt.buf == NULL) {
  774. return TEST_ABORTED;
  775. }
  776. /* Convert the data */
  777. result = SDL_ConvertAudio(&cvt);
  778. SDLTest_AssertPass("Call to SDL_ConvertAudio()");
  779. SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0; got: %i", result);
  780. SDLTest_AssertCheck(cvt.buf != NULL, "Verify conversion buffer is not NULL");
  781. SDLTest_AssertCheck(cvt.len_ratio > 0.0, "Verify conversion length ratio; expected: >0; got: %f", cvt.len_ratio);
  782. /* Free converted buffer */
  783. SDL_free(cvt.buf);
  784. cvt.buf = NULL;
  785. }
  786. }
  787. }
  788. }
  789. }
  790. return TEST_COMPLETED;
  791. }
  792. /**
  793. * \brief Opens, checks current connected status, and closes a device.
  794. *
  795. * \sa https://wiki.libsdl.org/SDL_AudioDeviceConnected
  796. */
  797. int audio_openCloseAudioDeviceConnected()
  798. {
  799. int result = -1;
  800. int i;
  801. int count;
  802. const char *device;
  803. SDL_AudioDeviceID id;
  804. SDL_AudioSpec desired, obtained;
  805. /* Get number of devices. */
  806. count = SDL_GetNumAudioDevices(0);
  807. SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
  808. if (count > 0) {
  809. for (i = 0; i < count; i++) {
  810. /* Get device name */
  811. device = SDL_GetAudioDeviceName(i, 0);
  812. SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
  813. SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
  814. if (device == NULL) {
  815. return TEST_ABORTED;
  816. }
  817. /* Set standard desired spec */
  818. desired.freq = 22050;
  819. desired.format = AUDIO_S16SYS;
  820. desired.channels = 2;
  821. desired.samples = 4096;
  822. desired.callback = _audio_testCallback;
  823. desired.userdata = NULL;
  824. /* Open device */
  825. id = SDL_OpenAudioDevice(device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
  826. SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
  827. SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >1, got: %" SDL_PRIu32, id);
  828. if (id > 1) {
  829. /* TODO: enable test code when function is available in SDL2 */
  830. #ifdef AUDIODEVICECONNECTED_DEFINED
  831. /* Get connected status */
  832. result = SDL_AudioDeviceConnected(id);
  833. SDLTest_AssertPass("Call to SDL_AudioDeviceConnected()");
  834. #endif
  835. SDLTest_AssertCheck(result == 1, "Verify returned value; expected: 1; got: %i", result);
  836. /* Close device again */
  837. SDL_CloseAudioDevice(id);
  838. SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
  839. }
  840. }
  841. } else {
  842. SDLTest_Log("No devices to test with");
  843. }
  844. return TEST_COMPLETED;
  845. }
  846. static double sine_wave_sample(const Sint64 idx, const Sint64 rate, const Sint64 freq, const double phase)
  847. {
  848. /* Using integer modulo to avoid precision loss caused by large floating
  849. * point numbers. Sint64 is needed for the large integer multiplication.
  850. * The integers are assumed to be non-negative so that modulo is always
  851. * non-negative.
  852. * sin(i / rate * freq * 2 * M_PI + phase)
  853. * = sin(mod(i / rate * freq, 1) * 2 * M_PI + phase)
  854. * = sin(mod(i * freq, rate) / rate * 2 * M_PI + phase) */
  855. return SDL_sin(((double) (idx * freq % rate)) / ((double) rate) * (M_PI * 2) + phase);
  856. }
  857. /**
  858. * \brief Check signal-to-noise ratio and maximum error of audio resampling.
  859. *
  860. * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
  861. * \sa https://wiki.libsdl.org/SDL_ConvertAudio
  862. */
  863. int audio_resampleLoss()
  864. {
  865. /* Note: always test long input time (>= 5s from experience) in some test
  866. * cases because an improper implementation may suffer from low resampling
  867. * precision with long input due to e.g. doing subtraction with large floats. */
  868. struct test_spec_t {
  869. int time;
  870. int freq;
  871. double phase;
  872. int rate_in;
  873. int rate_out;
  874. double signal_to_noise;
  875. double max_error;
  876. } test_specs[] = {
  877. { 50, 440, 0, 44100, 48000, 60, 0.0025 },
  878. { 50, 5000, M_PI / 2, 20000, 10000, 65, 0.0010 },
  879. { 0 }
  880. };
  881. int spec_idx = 0;
  882. for (spec_idx = 0; test_specs[spec_idx].time > 0; ++spec_idx) {
  883. const struct test_spec_t *spec = &test_specs[spec_idx];
  884. const int frames_in = spec->time * spec->rate_in;
  885. const int frames_target = spec->time * spec->rate_out;
  886. const int len_in = frames_in * (int)sizeof(float);
  887. const int len_target = frames_target * (int)sizeof(float);
  888. Uint64 tick_beg = 0;
  889. Uint64 tick_end = 0;
  890. SDL_AudioCVT cvt;
  891. int i = 0;
  892. int ret = 0;
  893. double max_error = 0;
  894. double sum_squared_error = 0;
  895. double sum_squared_value = 0;
  896. double signal_to_noise = 0;
  897. SDLTest_AssertPass("Test resampling of %i s %i Hz %f phase sine wave from sampling rate of %i Hz to %i Hz",
  898. spec->time, spec->freq, spec->phase, spec->rate_in, spec->rate_out);
  899. ret = SDL_BuildAudioCVT(&cvt, AUDIO_F32, 1, spec->rate_in, AUDIO_F32, 1, spec->rate_out);
  900. SDLTest_AssertPass("Call to SDL_BuildAudioCVT(&cvt, AUDIO_F32, 1, %i, AUDIO_F32, 1, %i)", spec->rate_in, spec->rate_out);
  901. SDLTest_AssertCheck(ret == 1, "Expected SDL_BuildAudioCVT to succeed and conversion to be needed.");
  902. if (ret != 1) {
  903. return TEST_ABORTED;
  904. }
  905. cvt.buf = (Uint8 *)SDL_malloc(len_in * cvt.len_mult);
  906. SDLTest_AssertCheck(cvt.buf != NULL, "Expected input buffer to be created.");
  907. if (cvt.buf == NULL) {
  908. return TEST_ABORTED;
  909. }
  910. cvt.len = len_in;
  911. for (i = 0; i < frames_in; ++i) {
  912. *(((float *) cvt.buf) + i) = (float)sine_wave_sample(i, spec->rate_in, spec->freq, spec->phase);
  913. }
  914. tick_beg = SDL_GetPerformanceCounter();
  915. ret = SDL_ConvertAudio(&cvt);
  916. tick_end = SDL_GetPerformanceCounter();
  917. SDLTest_AssertPass("Call to SDL_ConvertAudio(&cvt)");
  918. SDLTest_AssertCheck(ret == 0, "Expected SDL_ConvertAudio to succeed.");
  919. SDLTest_AssertCheck(cvt.len_cvt == len_target, "Expected output length %i, got %i.", len_target, cvt.len_cvt);
  920. if (ret != 0 || cvt.len_cvt != len_target) {
  921. SDL_free(cvt.buf);
  922. return TEST_ABORTED;
  923. }
  924. SDLTest_Log("Resampling used %f seconds.", ((double) (tick_end - tick_beg)) / SDL_GetPerformanceFrequency());
  925. for (i = 0; i < frames_target; ++i) {
  926. const float output = *(((float *) cvt.buf) + i);
  927. const double target = sine_wave_sample(i, spec->rate_out, spec->freq, spec->phase);
  928. const double error = SDL_fabs(target - output);
  929. max_error = SDL_max(max_error, error);
  930. sum_squared_error += error * error;
  931. sum_squared_value += target * target;
  932. }
  933. SDL_free(cvt.buf);
  934. signal_to_noise = 10 * SDL_log10(sum_squared_value / sum_squared_error); /* decibel */
  935. SDLTest_AssertCheck(isfinite(sum_squared_value), "Sum of squared target should be finite.");
  936. SDLTest_AssertCheck(isfinite(sum_squared_error), "Sum of squared error should be finite.");
  937. /* Infinity is theoretically possible when there is very little to no noise */
  938. SDLTest_AssertCheck(!isnan(signal_to_noise), "Signal-to-noise ratio should not be NaN.");
  939. SDLTest_AssertCheck(isfinite(max_error), "Maximum conversion error should be finite.");
  940. SDLTest_AssertCheck(signal_to_noise >= spec->signal_to_noise, "Conversion signal-to-noise ratio %f dB should be no less than %f dB.",
  941. signal_to_noise, spec->signal_to_noise);
  942. SDLTest_AssertCheck(max_error <= spec->max_error, "Maximum conversion error %f should be no more than %f.",
  943. max_error, spec->max_error);
  944. }
  945. return TEST_COMPLETED;
  946. }
  947. /* ================= Test Case References ================== */
  948. /* Audio test cases */
  949. static const SDLTest_TestCaseReference audioTest1 = {
  950. (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevices, "audio_enumerateAndNameAudioDevices", "Enumerate and name available audio devices (output and capture)", TEST_ENABLED
  951. };
  952. static const SDLTest_TestCaseReference audioTest2 = {
  953. (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevicesNegativeTests, "audio_enumerateAndNameAudioDevicesNegativeTests", "Negative tests around enumeration and naming of audio devices.", TEST_ENABLED
  954. };
  955. static const SDLTest_TestCaseReference audioTest3 = {
  956. (SDLTest_TestCaseFp)audio_printAudioDrivers, "audio_printAudioDrivers", "Checks available audio driver names.", TEST_ENABLED
  957. };
  958. static const SDLTest_TestCaseReference audioTest4 = {
  959. (SDLTest_TestCaseFp)audio_printCurrentAudioDriver, "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED
  960. };
  961. static const SDLTest_TestCaseReference audioTest5 = {
  962. (SDLTest_TestCaseFp)audio_buildAudioCVT, "audio_buildAudioCVT", "Builds various audio conversion structures.", TEST_ENABLED
  963. };
  964. static const SDLTest_TestCaseReference audioTest6 = {
  965. (SDLTest_TestCaseFp)audio_buildAudioCVTNegative, "audio_buildAudioCVTNegative", "Checks calls with invalid input to SDL_BuildAudioCVT", TEST_ENABLED
  966. };
  967. static const SDLTest_TestCaseReference audioTest7 = {
  968. (SDLTest_TestCaseFp)audio_getAudioStatus, "audio_getAudioStatus", "Checks current audio status.", TEST_ENABLED
  969. };
  970. static const SDLTest_TestCaseReference audioTest8 = {
  971. (SDLTest_TestCaseFp)audio_openCloseAndGetAudioStatus, "audio_openCloseAndGetAudioStatus", "Opens and closes audio device and get audio status.", TEST_ENABLED
  972. };
  973. static const SDLTest_TestCaseReference audioTest9 = {
  974. (SDLTest_TestCaseFp)audio_lockUnlockOpenAudioDevice, "audio_lockUnlockOpenAudioDevice", "Locks and unlocks an open audio device.", TEST_ENABLED
  975. };
  976. /* TODO: enable test when SDL_ConvertAudio segfaults on cygwin have been fixed. */
  977. /* For debugging, test case can be run manually using --filter audio_convertAudio */
  978. static const SDLTest_TestCaseReference audioTest10 = {
  979. (SDLTest_TestCaseFp)audio_convertAudio, "audio_convertAudio", "Convert audio using available formats.", TEST_DISABLED
  980. };
  981. /* TODO: enable test when SDL_AudioDeviceConnected has been implemented. */
  982. static const SDLTest_TestCaseReference audioTest11 = {
  983. (SDLTest_TestCaseFp)audio_openCloseAudioDeviceConnected, "audio_openCloseAudioDeviceConnected", "Opens and closes audio device and get connected status.", TEST_DISABLED
  984. };
  985. static const SDLTest_TestCaseReference audioTest12 = {
  986. (SDLTest_TestCaseFp)audio_quitInitAudioSubSystem, "audio_quitInitAudioSubSystem", "Quit and re-init audio subsystem.", TEST_ENABLED
  987. };
  988. static const SDLTest_TestCaseReference audioTest13 = {
  989. (SDLTest_TestCaseFp)audio_initQuitAudio, "audio_initQuitAudio", "Init and quit audio drivers directly.", TEST_ENABLED
  990. };
  991. static const SDLTest_TestCaseReference audioTest14 = {
  992. (SDLTest_TestCaseFp)audio_initOpenCloseQuitAudio, "audio_initOpenCloseQuitAudio", "Cycle through init, open, close and quit with various audio specs.", TEST_ENABLED
  993. };
  994. static const SDLTest_TestCaseReference audioTest15 = {
  995. (SDLTest_TestCaseFp)audio_pauseUnpauseAudio, "audio_pauseUnpauseAudio", "Pause and Unpause audio for various audio specs while testing callback.", TEST_ENABLED
  996. };
  997. static const SDLTest_TestCaseReference audioTest16 = {
  998. (SDLTest_TestCaseFp)audio_resampleLoss, "audio_resampleLoss", "Check signal-to-noise ratio and maximum error of audio resampling.", TEST_ENABLED
  999. };
  1000. /* Sequence of Audio test cases */
  1001. static const SDLTest_TestCaseReference *audioTests[] = {
  1002. &audioTest1, &audioTest2, &audioTest3, &audioTest4, &audioTest5, &audioTest6,
  1003. &audioTest7, &audioTest8, &audioTest9, &audioTest10, &audioTest11,
  1004. &audioTest12, &audioTest13, &audioTest14, &audioTest15, &audioTest16, NULL
  1005. };
  1006. /* Audio test suite (global) */
  1007. SDLTest_TestSuiteReference audioTestSuite = {
  1008. "Audio",
  1009. _audioSetUp,
  1010. audioTests,
  1011. _audioTearDown
  1012. };