testautomation_audio.c 47 KB

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