testautomation_audio.c 43 KB

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