testautomation_audio.c 45 KB

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