testautomation_audio.c 49 KB

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