testautomation_audio.c 47 KB

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