testautomation_audio.c 45 KB

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