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

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