testautomation_audio.c 49 KB

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