testautomation_audio.c 45 KB

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