🛠️🐜 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.

1040 lines
38 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 <stdio.h>
  10. #include <string.h>
  11. #include "SDL.h"
  12. #include "SDL_test.h"
  13. /* ================= Test Case Implementation ================== */
  14. /* Fixture */
  15. void
  16. _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
  27. _audioTearDown(void *arg)
  28. {
  29. /* Remove a possibly created file from SDL disk writer audio driver; ignore errors */
  30. remove("sdlaudio.raw");
  31. SDLTest_AssertPass("Cleanup of test files completed");
  32. }
  33. /* Global counter for callback invocation */
  34. int _audio_testCallbackCounter;
  35. /* Global accumulator for total callback length */
  36. int _audio_testCallbackLength;
  37. /* Test callback function */
  38. void SDLCALL _audio_testCallback(void *userdata, Uint8 *stream, int len)
  39. {
  40. /* track that callback was called */
  41. _audio_testCallbackCounter++;
  42. _audio_testCallbackLength += len;
  43. }
  44. /* Test case functions */
  45. /**
  46. * \brief Stop and restart audio subsystem
  47. *
  48. * \sa https://wiki.libsdl.org/SDL_QuitSubSystem
  49. * \sa https://wiki.libsdl.org/SDL_InitSubSystem
  50. */
  51. int audio_quitInitAudioSubSystem()
  52. {
  53. /* Stop SDL audio subsystem */
  54. SDL_QuitSubSystem( SDL_INIT_AUDIO );
  55. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  56. /* Restart audio again */
  57. _audioSetUp(NULL);
  58. return TEST_COMPLETED;
  59. }
  60. /**
  61. * \brief Start and stop audio directly
  62. *
  63. * \sa https://wiki.libsdl.org/SDL_InitAudio
  64. * \sa https://wiki.libsdl.org/SDL_QuitAudio
  65. */
  66. int audio_initQuitAudio()
  67. {
  68. int result;
  69. int i, iMax;
  70. const char* audioDriver;
  71. /* Stop SDL audio subsystem */
  72. SDL_QuitSubSystem( SDL_INIT_AUDIO );
  73. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  74. /* Loop over all available audio drivers */
  75. iMax = SDL_GetNumAudioDrivers();
  76. SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
  77. SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
  78. for (i = 0; i < iMax; i++) {
  79. audioDriver = SDL_GetAudioDriver(i);
  80. SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
  81. SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
  82. SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
  83. /* Call Init */
  84. result = SDL_AudioInit(audioDriver);
  85. SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
  86. SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
  87. /* Call Quit */
  88. SDL_AudioQuit();
  89. SDLTest_AssertPass("Call to SDL_AudioQuit()");
  90. }
  91. /* NULL driver specification */
  92. audioDriver = NULL;
  93. /* Call Init */
  94. result = SDL_AudioInit(audioDriver);
  95. SDLTest_AssertPass("Call to SDL_AudioInit(NULL)");
  96. SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
  97. /* Call Quit */
  98. SDL_AudioQuit();
  99. SDLTest_AssertPass("Call to SDL_AudioQuit()");
  100. /* Restart audio again */
  101. _audioSetUp(NULL);
  102. return TEST_COMPLETED;
  103. }
  104. /**
  105. * \brief Start, open, close and stop audio
  106. *
  107. * \sa https://wiki.libsdl.org/SDL_InitAudio
  108. * \sa https://wiki.libsdl.org/SDL_OpenAudio
  109. * \sa https://wiki.libsdl.org/SDL_CloseAudio
  110. * \sa https://wiki.libsdl.org/SDL_QuitAudio
  111. */
  112. int audio_initOpenCloseQuitAudio()
  113. {
  114. int result, expectedResult;
  115. int i, iMax, j, k;
  116. const char* audioDriver;
  117. SDL_AudioSpec desired;
  118. /* Stop SDL audio subsystem */
  119. SDL_QuitSubSystem( SDL_INIT_AUDIO );
  120. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  121. /* Loop over all available audio drivers */
  122. iMax = SDL_GetNumAudioDrivers();
  123. SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
  124. SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
  125. for (i = 0; i < iMax; i++) {
  126. audioDriver = SDL_GetAudioDriver(i);
  127. SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
  128. SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
  129. SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
  130. /* Change specs */
  131. for (j = 0; j < 2; j++) {
  132. /* Call Init */
  133. result = SDL_AudioInit(audioDriver);
  134. SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
  135. SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
  136. /* Set spec */
  137. SDL_memset(&desired, 0, sizeof(desired));
  138. switch (j) {
  139. case 0:
  140. /* Set standard desired spec */
  141. desired.freq = 22050;
  142. desired.format = AUDIO_S16SYS;
  143. desired.channels = 2;
  144. desired.samples = 4096;
  145. desired.callback = _audio_testCallback;
  146. desired.userdata = NULL;
  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_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
  205. SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
  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. case 1:
  224. /* Set custom desired spec */
  225. desired.freq = 48000;
  226. desired.format = AUDIO_F32SYS;
  227. desired.channels = 2;
  228. desired.samples = 2048;
  229. desired.callback = _audio_testCallback;
  230. desired.userdata = NULL;
  231. break;
  232. }
  233. /* Call Open */
  234. result = SDL_OpenAudio(&desired, NULL);
  235. SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL)", j);
  236. SDLTest_AssertCheck(result == 0, "Verify return value; expected: 0 got: %d", result);
  237. /* Start and stop audio multiple times */
  238. for (l=0; l<3; l++) {
  239. SDLTest_Log("Pause/Unpause iteration: %d", l+1);
  240. /* Reset callback counters */
  241. _audio_testCallbackCounter = 0;
  242. _audio_testCallbackLength = 0;
  243. /* Un-pause audio to start playing (maybe multiple times) */
  244. pause_on = 0;
  245. for (k=0; k <= j; k++) {
  246. SDL_PauseAudio(pause_on);
  247. SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k+1);
  248. }
  249. /* Wait for callback */
  250. totalDelay = 0;
  251. do {
  252. SDL_Delay(10);
  253. totalDelay += 10;
  254. }
  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. {
  390. for (i=0; i<n; i++) {
  391. name = SDL_GetAudioDriver(i);
  392. SDLTest_AssertPass("Call to SDL_GetAudioDriver(%i)", i);
  393. SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
  394. if (name != NULL) {
  395. SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
  396. }
  397. }
  398. }
  399. return TEST_COMPLETED;
  400. }
  401. /**
  402. * \brief Checks current audio driver name with initialized audio.
  403. *
  404. * \sa https://wiki.libsdl.org/SDL_GetCurrentAudioDriver
  405. */
  406. int audio_printCurrentAudioDriver()
  407. {
  408. /* Check current audio driver */
  409. const char *name = SDL_GetCurrentAudioDriver();
  410. SDLTest_AssertPass("Call to SDL_GetCurrentAudioDriver()");
  411. SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
  412. if (name != NULL) {
  413. SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
  414. }
  415. return TEST_COMPLETED;
  416. }
  417. /* Definition of all formats, channels, and frequencies used to test audio conversions */
  418. const int _numAudioFormats = 18;
  419. SDL_AudioFormat _audioFormats[] = { AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S16SYS, AUDIO_S16, AUDIO_U16LSB,
  420. AUDIO_U16MSB, AUDIO_U16SYS, AUDIO_U16, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S32SYS, AUDIO_S32,
  421. AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_F32SYS, AUDIO_F32 };
  422. const char *_audioFormatsVerbose[] = { "AUDIO_S8", "AUDIO_U8", "AUDIO_S16LSB", "AUDIO_S16MSB", "AUDIO_S16SYS", "AUDIO_S16", "AUDIO_U16LSB",
  423. "AUDIO_U16MSB", "AUDIO_U16SYS", "AUDIO_U16", "AUDIO_S32LSB", "AUDIO_S32MSB", "AUDIO_S32SYS", "AUDIO_S32",
  424. "AUDIO_F32LSB", "AUDIO_F32MSB", "AUDIO_F32SYS", "AUDIO_F32" };
  425. const int _numAudioChannels = 4;
  426. Uint8 _audioChannels[] = { 1, 2, 4, 6 };
  427. const int _numAudioFrequencies = 4;
  428. int _audioFrequencies[] = { 11025, 22050, 44100, 48000 };
  429. /**
  430. * \brief Builds various audio conversion structures
  431. *
  432. * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
  433. */
  434. int audio_buildAudioCVT()
  435. {
  436. int result;
  437. SDL_AudioCVT cvt;
  438. SDL_AudioSpec spec1;
  439. SDL_AudioSpec spec2;
  440. int i, ii, j, jj, k, kk;
  441. /* No conversion needed */
  442. spec1.format = AUDIO_S16LSB;
  443. spec1.channels = 2;
  444. spec1.freq = 22050;
  445. result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
  446. spec1.format, spec1.channels, spec1.freq);
  447. SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec1)");
  448. SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0, got: %i", result);
  449. /* Typical conversion */
  450. spec1.format = AUDIO_S8;
  451. spec1.channels = 1;
  452. spec1.freq = 22050;
  453. spec2.format = AUDIO_S16LSB;
  454. spec2.channels = 2;
  455. spec2.freq = 44100;
  456. result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
  457. spec2.format, spec2.channels, spec2.freq);
  458. SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
  459. SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
  460. /* All source conversions with random conversion targets, allow 'null' conversions */
  461. for (i = 0; i < _numAudioFormats; i++) {
  462. for (j = 0; j < _numAudioChannels; j++) {
  463. for (k = 0; k < _numAudioFrequencies; k++) {
  464. spec1.format = _audioFormats[i];
  465. spec1.channels = _audioChannels[j];
  466. spec1.freq = _audioFrequencies[k];
  467. ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
  468. jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
  469. kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
  470. spec2.format = _audioFormats[ii];
  471. spec2.channels = _audioChannels[jj];
  472. spec2.freq = _audioFrequencies[kk];
  473. result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
  474. spec2.format, spec2.channels, spec2.freq);
  475. 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)",
  476. i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
  477. SDLTest_AssertCheck(result == 0 || result == 1, "Verify result value; expected: 0 or 1, got: %i", result);
  478. if (result<0) {
  479. SDLTest_LogError("%s", SDL_GetError());
  480. } else {
  481. SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
  482. }
  483. }
  484. }
  485. }
  486. return TEST_COMPLETED;
  487. }
  488. /**
  489. * \brief Checkes calls with invalid input to SDL_BuildAudioCVT
  490. *
  491. * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
  492. */
  493. int audio_buildAudioCVTNegative()
  494. {
  495. const char *expectedError = "Parameter 'cvt' is invalid";
  496. const char *error;
  497. int result;
  498. SDL_AudioCVT cvt;
  499. SDL_AudioSpec spec1;
  500. SDL_AudioSpec spec2;
  501. int i;
  502. char message[256];
  503. /* Valid format */
  504. spec1.format = AUDIO_S8;
  505. spec1.channels = 1;
  506. spec1.freq = 22050;
  507. spec2.format = AUDIO_S16LSB;
  508. spec2.channels = 2;
  509. spec2.freq = 44100;
  510. SDL_ClearError();
  511. SDLTest_AssertPass("Call to SDL_ClearError()");
  512. /* NULL input for CVT buffer */
  513. result = SDL_BuildAudioCVT((SDL_AudioCVT *)NULL, spec1.format, spec1.channels, spec1.freq,
  514. spec2.format, spec2.channels, spec2.freq);
  515. SDLTest_AssertPass("Call to SDL_BuildAudioCVT(NULL,...)");
  516. SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
  517. error = SDL_GetError();
  518. SDLTest_AssertPass("Call to SDL_GetError()");
  519. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  520. if (error != NULL) {
  521. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  522. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  523. }
  524. /* Invalid conversions */
  525. for (i = 1; i < 64; i++) {
  526. /* Valid format to start with */
  527. spec1.format = AUDIO_S8;
  528. spec1.channels = 1;
  529. spec1.freq = 22050;
  530. spec2.format = AUDIO_S16LSB;
  531. spec2.channels = 2;
  532. spec2.freq = 44100;
  533. SDL_ClearError();
  534. SDLTest_AssertPass("Call to SDL_ClearError()");
  535. /* Set various invalid format inputs */
  536. SDL_strlcpy(message, "Invalid: ", 256);
  537. if (i & 1) {
  538. SDL_strlcat(message, " spec1.format", 256);
  539. spec1.format = 0;
  540. }
  541. if (i & 2) {
  542. SDL_strlcat(message, " spec1.channels", 256);
  543. spec1.channels = 0;
  544. }
  545. if (i & 4) {
  546. SDL_strlcat(message, " spec1.freq", 256);
  547. spec1.freq = 0;
  548. }
  549. if (i & 8) {
  550. SDL_strlcat(message, " spec2.format", 256);
  551. spec2.format = 0;
  552. }
  553. if (i & 16) {
  554. SDL_strlcat(message, " spec2.channels", 256);
  555. spec2.channels = 0;
  556. }
  557. if (i & 32) {
  558. SDL_strlcat(message, " spec2.freq", 256);
  559. spec2.freq = 0;
  560. }
  561. SDLTest_Log("%s", message);
  562. result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
  563. spec2.format, spec2.channels, spec2.freq);
  564. SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
  565. SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
  566. error = SDL_GetError();
  567. SDLTest_AssertPass("Call to SDL_GetError()");
  568. SDLTest_AssertCheck(error != NULL && error[0] != '\0', "Validate that error message was not NULL or empty");
  569. }
  570. SDL_ClearError();
  571. SDLTest_AssertPass("Call to SDL_ClearError()");
  572. return TEST_COMPLETED;
  573. }
  574. /**
  575. * \brief Checks current audio status.
  576. *
  577. * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
  578. */
  579. int audio_getAudioStatus()
  580. {
  581. SDL_AudioStatus result;
  582. /* Check current audio status */
  583. result = SDL_GetAudioStatus();
  584. SDLTest_AssertPass("Call to SDL_GetAudioStatus()");
  585. SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
  586. "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
  587. SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
  588. return TEST_COMPLETED;
  589. }
  590. /**
  591. * \brief Opens, checks current audio status, and closes a device.
  592. *
  593. * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
  594. */
  595. int audio_openCloseAndGetAudioStatus()
  596. {
  597. SDL_AudioStatus result;
  598. int i;
  599. int count;
  600. const char *device;
  601. SDL_AudioDeviceID id;
  602. SDL_AudioSpec desired, obtained;
  603. /* Get number of devices. */
  604. count = SDL_GetNumAudioDevices(0);
  605. SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
  606. if (count > 0) {
  607. for (i = 0; i < count; i++) {
  608. /* Get device name */
  609. device = SDL_GetAudioDeviceName(i, 0);
  610. SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
  611. SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
  612. if (device == NULL) return TEST_ABORTED;
  613. /* Set standard desired spec */
  614. desired.freq=22050;
  615. desired.format=AUDIO_S16SYS;
  616. desired.channels=2;
  617. desired.samples=4096;
  618. desired.callback=_audio_testCallback;
  619. desired.userdata=NULL;
  620. /* Open device */
  621. id = SDL_OpenAudioDevice(device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
  622. SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
  623. SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %" SDL_PRIu32, id);
  624. if (id > 1) {
  625. /* Check device audio status */
  626. result = SDL_GetAudioDeviceStatus(id);
  627. SDLTest_AssertPass("Call to SDL_GetAudioDeviceStatus()");
  628. SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
  629. "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
  630. SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
  631. /* Close device again */
  632. SDL_CloseAudioDevice(id);
  633. SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
  634. }
  635. }
  636. } else {
  637. SDLTest_Log("No devices to test with");
  638. }
  639. return TEST_COMPLETED;
  640. }
  641. /**
  642. * \brief Locks and unlocks open audio device.
  643. *
  644. * \sa https://wiki.libsdl.org/SDL_LockAudioDevice
  645. * \sa https://wiki.libsdl.org/SDL_UnlockAudioDevice
  646. */
  647. int audio_lockUnlockOpenAudioDevice()
  648. {
  649. int i;
  650. int count;
  651. const char *device;
  652. SDL_AudioDeviceID id;
  653. SDL_AudioSpec desired, obtained;
  654. /* Get number of devices. */
  655. count = SDL_GetNumAudioDevices(0);
  656. SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
  657. if (count > 0) {
  658. for (i = 0; i < count; i++) {
  659. /* Get device name */
  660. device = SDL_GetAudioDeviceName(i, 0);
  661. SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
  662. SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
  663. if (device == NULL) return TEST_ABORTED;
  664. /* Set standard desired spec */
  665. desired.freq=22050;
  666. desired.format=AUDIO_S16SYS;
  667. desired.channels=2;
  668. desired.samples=4096;
  669. desired.callback=_audio_testCallback;
  670. desired.userdata=NULL;
  671. /* Open device */
  672. id = SDL_OpenAudioDevice(device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
  673. SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
  674. SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %" SDL_PRIu32, id);
  675. if (id > 1) {
  676. /* Lock to protect callback */
  677. SDL_LockAudioDevice(id);
  678. SDLTest_AssertPass("SDL_LockAudioDevice(%" SDL_PRIu32 ")", id);
  679. /* Simulate callback processing */
  680. SDL_Delay(10);
  681. SDLTest_Log("Simulate callback processing - delay");
  682. /* Unlock again */
  683. SDL_UnlockAudioDevice(id);
  684. SDLTest_AssertPass("SDL_UnlockAudioDevice(%" SDL_PRIu32 ")", id);
  685. /* Close device again */
  686. SDL_CloseAudioDevice(id);
  687. SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
  688. }
  689. }
  690. } else {
  691. SDLTest_Log("No devices to test with");
  692. }
  693. return TEST_COMPLETED;
  694. }
  695. /**
  696. * \brief Convert audio using various conversion structures
  697. *
  698. * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
  699. * \sa https://wiki.libsdl.org/SDL_ConvertAudio
  700. */
  701. int audio_convertAudio()
  702. {
  703. int result;
  704. SDL_AudioCVT cvt;
  705. SDL_AudioSpec spec1;
  706. SDL_AudioSpec spec2;
  707. int c;
  708. char message[128];
  709. int i, ii, j, jj, k, kk, l, ll;
  710. /* Iterate over bitmask that determines which parameters are modified in the conversion */
  711. for (c = 1; c < 8; c++) {
  712. SDL_strlcpy(message, "Changing:", 128);
  713. if (c & 1) {
  714. SDL_strlcat(message, " Format", 128);
  715. }
  716. if (c & 2) {
  717. SDL_strlcat(message, " Channels", 128);
  718. }
  719. if (c & 4) {
  720. SDL_strlcat(message, " Frequencies", 128);
  721. }
  722. SDLTest_Log("%s", message);
  723. /* All source conversions with random conversion targets */
  724. for (i = 0; i < _numAudioFormats; i++) {
  725. for (j = 0; j < _numAudioChannels; j++) {
  726. for (k = 0; k < _numAudioFrequencies; k++) {
  727. spec1.format = _audioFormats[i];
  728. spec1.channels = _audioChannels[j];
  729. spec1.freq = _audioFrequencies[k];
  730. /* Ensure we have a different target format */
  731. do {
  732. if (c & 1) {
  733. ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
  734. } else {
  735. ii = 1;
  736. }
  737. if (c & 2) {
  738. jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
  739. } else {
  740. jj= j;
  741. }
  742. if (c & 4) {
  743. kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
  744. } else {
  745. kk = k;
  746. }
  747. } while ((i == ii) && (j == jj) && (k == kk));
  748. spec2.format = _audioFormats[ii];
  749. spec2.channels = _audioChannels[jj];
  750. spec2.freq = _audioFrequencies[kk];
  751. result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
  752. spec2.format, spec2.channels, spec2.freq);
  753. 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)",
  754. i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
  755. SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
  756. if (result != 1) {
  757. SDLTest_LogError("%s", SDL_GetError());
  758. } else {
  759. SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
  760. if (cvt.len_mult < 1) return TEST_ABORTED;
  761. /* Create some random data to convert */
  762. l = 64;
  763. ll = l * cvt.len_mult;
  764. SDLTest_Log("Creating dummy sample buffer of %i length (%i bytes)", l, ll);
  765. cvt.len = l;
  766. cvt.buf = (Uint8 *)SDL_malloc(ll);
  767. SDLTest_AssertCheck(cvt.buf != NULL, "Check data buffer to convert is not NULL");
  768. if (cvt.buf == NULL) return TEST_ABORTED;
  769. /* Convert the data */
  770. result = SDL_ConvertAudio(&cvt);
  771. SDLTest_AssertPass("Call to SDL_ConvertAudio()");
  772. SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0; got: %i", result);
  773. SDLTest_AssertCheck(cvt.buf != NULL, "Verify conversion buffer is not NULL");
  774. SDLTest_AssertCheck(cvt.len_ratio > 0.0, "Verify conversion length ratio; expected: >0; got: %f", cvt.len_ratio);
  775. /* Free converted buffer */
  776. SDL_free(cvt.buf);
  777. cvt.buf = NULL;
  778. }
  779. }
  780. }
  781. }
  782. }
  783. return TEST_COMPLETED;
  784. }
  785. /**
  786. * \brief Opens, checks current connected status, and closes a device.
  787. *
  788. * \sa https://wiki.libsdl.org/SDL_AudioDeviceConnected
  789. */
  790. int audio_openCloseAudioDeviceConnected()
  791. {
  792. int result = -1;
  793. int i;
  794. int count;
  795. const char *device;
  796. SDL_AudioDeviceID id;
  797. SDL_AudioSpec desired, obtained;
  798. /* Get number of devices. */
  799. count = SDL_GetNumAudioDevices(0);
  800. SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
  801. if (count > 0) {
  802. for (i = 0; i < count; i++) {
  803. /* Get device name */
  804. device = SDL_GetAudioDeviceName(i, 0);
  805. SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
  806. SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
  807. if (device == NULL) return TEST_ABORTED;
  808. /* Set standard desired spec */
  809. desired.freq=22050;
  810. desired.format=AUDIO_S16SYS;
  811. desired.channels=2;
  812. desired.samples=4096;
  813. desired.callback=_audio_testCallback;
  814. desired.userdata=NULL;
  815. /* Open device */
  816. id = SDL_OpenAudioDevice(device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
  817. SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
  818. SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >1, got: %" SDL_PRIu32, id);
  819. if (id > 1) {
  820. /* TODO: enable test code when function is available in SDL2 */
  821. #ifdef AUDIODEVICECONNECTED_DEFINED
  822. /* Get connected status */
  823. result = SDL_AudioDeviceConnected(id);
  824. SDLTest_AssertPass("Call to SDL_AudioDeviceConnected()");
  825. #endif
  826. SDLTest_AssertCheck(result == 1, "Verify returned value; expected: 1; got: %i", result);
  827. /* Close device again */
  828. SDL_CloseAudioDevice(id);
  829. SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
  830. }
  831. }
  832. } else {
  833. SDLTest_Log("No devices to test with");
  834. }
  835. return TEST_COMPLETED;
  836. }
  837. /* ================= Test Case References ================== */
  838. /* Audio test cases */
  839. static const SDLTest_TestCaseReference audioTest1 =
  840. { (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevices, "audio_enumerateAndNameAudioDevices", "Enumerate and name available audio devices (output and capture)", TEST_ENABLED };
  841. static const SDLTest_TestCaseReference audioTest2 =
  842. { (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevicesNegativeTests, "audio_enumerateAndNameAudioDevicesNegativeTests", "Negative tests around enumeration and naming of audio devices.", TEST_ENABLED };
  843. static const SDLTest_TestCaseReference audioTest3 =
  844. { (SDLTest_TestCaseFp)audio_printAudioDrivers, "audio_printAudioDrivers", "Checks available audio driver names.", TEST_ENABLED };
  845. static const SDLTest_TestCaseReference audioTest4 =
  846. { (SDLTest_TestCaseFp)audio_printCurrentAudioDriver, "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED };
  847. static const SDLTest_TestCaseReference audioTest5 =
  848. { (SDLTest_TestCaseFp)audio_buildAudioCVT, "audio_buildAudioCVT", "Builds various audio conversion structures.", TEST_ENABLED };
  849. static const SDLTest_TestCaseReference audioTest6 =
  850. { (SDLTest_TestCaseFp)audio_buildAudioCVTNegative, "audio_buildAudioCVTNegative", "Checks calls with invalid input to SDL_BuildAudioCVT", TEST_ENABLED };
  851. static const SDLTest_TestCaseReference audioTest7 =
  852. { (SDLTest_TestCaseFp)audio_getAudioStatus, "audio_getAudioStatus", "Checks current audio status.", TEST_ENABLED };
  853. static const SDLTest_TestCaseReference audioTest8 =
  854. { (SDLTest_TestCaseFp)audio_openCloseAndGetAudioStatus, "audio_openCloseAndGetAudioStatus", "Opens and closes audio device and get audio status.", TEST_ENABLED };
  855. static const SDLTest_TestCaseReference audioTest9 =
  856. { (SDLTest_TestCaseFp)audio_lockUnlockOpenAudioDevice, "audio_lockUnlockOpenAudioDevice", "Locks and unlocks an open audio device.", TEST_ENABLED };
  857. /* TODO: enable test when SDL_ConvertAudio segfaults on cygwin have been fixed. */
  858. /* For debugging, test case can be run manually using --filter audio_convertAudio */
  859. static const SDLTest_TestCaseReference audioTest10 =
  860. { (SDLTest_TestCaseFp)audio_convertAudio, "audio_convertAudio", "Convert audio using available formats.", TEST_DISABLED };
  861. /* TODO: enable test when SDL_AudioDeviceConnected has been implemented. */
  862. static const SDLTest_TestCaseReference audioTest11 =
  863. { (SDLTest_TestCaseFp)audio_openCloseAudioDeviceConnected, "audio_openCloseAudioDeviceConnected", "Opens and closes audio device and get connected status.", TEST_DISABLED };
  864. static const SDLTest_TestCaseReference audioTest12 =
  865. { (SDLTest_TestCaseFp)audio_quitInitAudioSubSystem, "audio_quitInitAudioSubSystem", "Quit and re-init audio subsystem.", TEST_ENABLED };
  866. static const SDLTest_TestCaseReference audioTest13 =
  867. { (SDLTest_TestCaseFp)audio_initQuitAudio, "audio_initQuitAudio", "Init and quit audio drivers directly.", TEST_ENABLED };
  868. static const SDLTest_TestCaseReference audioTest14 =
  869. { (SDLTest_TestCaseFp)audio_initOpenCloseQuitAudio, "audio_initOpenCloseQuitAudio", "Cycle through init, open, close and quit with various audio specs.", TEST_ENABLED };
  870. static const SDLTest_TestCaseReference audioTest15 =
  871. { (SDLTest_TestCaseFp)audio_pauseUnpauseAudio, "audio_pauseUnpauseAudio", "Pause and Unpause audio for various audio specs while testing callback.", TEST_ENABLED };
  872. /* Sequence of Audio test cases */
  873. static const SDLTest_TestCaseReference *audioTests[] = {
  874. &audioTest1, &audioTest2, &audioTest3, &audioTest4, &audioTest5, &audioTest6,
  875. &audioTest7, &audioTest8, &audioTest9, &audioTest10, &audioTest11,
  876. &audioTest12, &audioTest13, &audioTest14, &audioTest15, NULL
  877. };
  878. /* Audio test suite (global) */
  879. SDLTest_TestSuiteReference audioTestSuite = {
  880. "Audio",
  881. _audioSetUp,
  882. audioTests,
  883. _audioTearDown
  884. };