testautomation_audio.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /**
  2. * Original code: automated SDL audio test written by Edgar Simo "bobbens"
  3. * New/updated tests: aschiffler at ferzkopp dot net
  4. */
  5. /* quiet windows compiler warnings */
  6. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  7. #define _CRT_SECURE_NO_WARNINGS
  8. #endif
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <SDL3/SDL.h>
  12. #include <SDL3/SDL_test.h>
  13. #include "testautomation_suites.h"
  14. /* ================= Test Case Implementation ================== */
  15. /* Fixture */
  16. static void audioSetUp(void *arg)
  17. {
  18. /* Start SDL audio subsystem */
  19. int ret = SDL_InitSubSystem(SDL_INIT_AUDIO);
  20. SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO)");
  21. SDLTest_AssertCheck(ret == 0, "Check result from SDL_InitSubSystem(SDL_INIT_AUDIO)");
  22. if (ret != 0) {
  23. SDLTest_LogError("%s", SDL_GetError());
  24. }
  25. }
  26. static void audioTearDown(void *arg)
  27. {
  28. /* Remove a possibly created file from SDL disk writer audio driver; ignore errors */
  29. (void)remove("sdlaudio.raw");
  30. SDLTest_AssertPass("Cleanup of test files completed");
  31. }
  32. /* Global counter for callback invocation */
  33. static int g_audio_testCallbackCounter;
  34. /* Global accumulator for total callback length */
  35. static int g_audio_testCallbackLength;
  36. /* Test callback function */
  37. static void SDLCALL audio_testCallback(void *userdata, Uint8 *stream, int len)
  38. {
  39. /* track that callback was called */
  40. g_audio_testCallbackCounter++;
  41. g_audio_testCallbackLength += len;
  42. }
  43. static SDL_AudioDeviceID g_audio_id = -1;
  44. /* Test case functions */
  45. /**
  46. * \brief Stop and restart audio subsystem
  47. *
  48. * \sa SDL_QuitSubSystem
  49. * \sa SDL_InitSubSystem
  50. */
  51. static int audio_quitInitAudioSubSystem(void *arg)
  52. {
  53. /* Stop SDL audio subsystem */
  54. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  55. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  56. /* Restart audio again */
  57. audioSetUp(NULL);
  58. return TEST_COMPLETED;
  59. }
  60. /**
  61. * \brief Start and stop audio directly
  62. *
  63. * \sa SDL_InitAudio
  64. * \sa SDL_QuitAudio
  65. */
  66. static int audio_initQuitAudio(void *arg)
  67. {
  68. int result;
  69. int i, iMax;
  70. const char *audioDriver;
  71. /* Stop SDL audio subsystem */
  72. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  73. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  74. /* Loop over all available audio drivers */
  75. iMax = SDL_GetNumAudioDrivers();
  76. SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
  77. SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
  78. for (i = 0; i < iMax; i++) {
  79. audioDriver = SDL_GetAudioDriver(i);
  80. SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
  81. SDLTest_Assert(audioDriver != NULL, "Audio driver name is not NULL");
  82. SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver); /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */
  83. /* Call Init */
  84. SDL_SetHint("SDL_AUDIO_DRIVER", audioDriver);
  85. result = SDL_InitSubSystem(SDL_INIT_AUDIO);
  86. SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO) with driver='%s'", audioDriver);
  87. SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
  88. /* Call Quit */
  89. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  90. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  91. }
  92. /* NULL driver specification */
  93. audioDriver = NULL;
  94. /* Call Init */
  95. SDL_SetHint("SDL_AUDIO_DRIVER", audioDriver);
  96. result = SDL_InitSubSystem(SDL_INIT_AUDIO);
  97. SDLTest_AssertPass("Call to SDL_AudioInit(NULL)");
  98. SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
  99. /* Call Quit */
  100. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  101. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  102. /* Restart audio again */
  103. audioSetUp(NULL);
  104. return TEST_COMPLETED;
  105. }
  106. /**
  107. * \brief Start, open, close and stop audio
  108. *
  109. * \sa SDL_InitAudio
  110. * \sa SDL_OpenAudioDevice
  111. * \sa SDL_CloseAudioDevice
  112. * \sa SDL_QuitAudio
  113. */
  114. static int audio_initOpenCloseQuitAudio(void *arg)
  115. {
  116. int result;
  117. int i, iMax, j, k;
  118. const char *audioDriver;
  119. SDL_AudioSpec desired;
  120. /* Stop SDL audio subsystem */
  121. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  122. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  123. /* Loop over all available audio drivers */
  124. iMax = SDL_GetNumAudioDrivers();
  125. SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
  126. SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
  127. for (i = 0; i < iMax; i++) {
  128. audioDriver = SDL_GetAudioDriver(i);
  129. SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
  130. SDLTest_Assert(audioDriver != NULL, "Audio driver name is not NULL");
  131. SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver); /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */
  132. /* Change specs */
  133. for (j = 0; j < 2; j++) {
  134. /* Call Init */
  135. SDL_SetHint("SDL_AUDIO_DRIVER", audioDriver);
  136. result = SDL_InitSubSystem(SDL_INIT_AUDIO);
  137. SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO) with driver='%s'", audioDriver);
  138. SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
  139. /* Set spec */
  140. SDL_memset(&desired, 0, sizeof(desired));
  141. switch (j) {
  142. case 0:
  143. /* Set standard desired spec */
  144. desired.freq = 22050;
  145. desired.format = SDL_AUDIO_S16SYS;
  146. desired.channels = 2;
  147. case 1:
  148. /* Set custom desired spec */
  149. desired.freq = 48000;
  150. desired.format = SDL_AUDIO_F32SYS;
  151. desired.channels = 2;
  152. break;
  153. }
  154. /* Call Open (maybe multiple times) */
  155. for (k = 0; k <= j; k++) {
  156. result = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, &desired);
  157. if (k == 0) {
  158. g_audio_id = result;
  159. }
  160. SDLTest_AssertPass("Call to SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, desired_spec_%d), call %d", j, k + 1);
  161. SDLTest_AssertCheck(result > 0, "Verify return value; expected: > 0, got: %d", result);
  162. }
  163. /* Call Close (maybe multiple times) */
  164. for (k = 0; k <= j; k++) {
  165. SDL_CloseAudioDevice(g_audio_id);
  166. SDLTest_AssertPass("Call to SDL_CloseAudioDevice(), call %d", k + 1);
  167. }
  168. /* Call Quit (maybe multiple times) */
  169. for (k = 0; k <= j; k++) {
  170. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  171. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO), 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 SDL_PauseAudioDevice
  183. * \sa SDL_PlayAudioDevice
  184. */
  185. static int audio_pauseUnpauseAudio(void *arg)
  186. {
  187. int result;
  188. int i, iMax, j, k, l;
  189. int totalDelay;
  190. int pause_on;
  191. int originalCounter;
  192. const char *audioDriver;
  193. SDL_AudioSpec desired;
  194. /* Stop SDL audio subsystem */
  195. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  196. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  197. /* Loop over all available audio drivers */
  198. iMax = SDL_GetNumAudioDrivers();
  199. SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
  200. SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
  201. for (i = 0; i < iMax; i++) {
  202. audioDriver = SDL_GetAudioDriver(i);
  203. SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
  204. SDLTest_Assert(audioDriver != NULL, "Audio driver name is not NULL");
  205. SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver); /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */
  206. /* Change specs */
  207. for (j = 0; j < 2; j++) {
  208. /* Call Init */
  209. SDL_SetHint("SDL_AUDIO_DRIVER", audioDriver);
  210. result = SDL_InitSubSystem(SDL_INIT_AUDIO);
  211. SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO) with driver='%s'", audioDriver);
  212. SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
  213. /* Set spec */
  214. SDL_memset(&desired, 0, sizeof(desired));
  215. switch (j) {
  216. case 0:
  217. /* Set standard desired spec */
  218. desired.freq = 22050;
  219. desired.format = SDL_AUDIO_S16SYS;
  220. desired.channels = 2;
  221. break;
  222. case 1:
  223. /* Set custom desired spec */
  224. desired.freq = 48000;
  225. desired.format = SDL_AUDIO_F32SYS;
  226. desired.channels = 2;
  227. break;
  228. }
  229. /* Call Open */
  230. g_audio_id = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, &desired);
  231. result = g_audio_id;
  232. SDLTest_AssertPass("Call to SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, desired_spec_%d)", j);
  233. SDLTest_AssertCheck(result > 0, "Verify return value; expected > 0 got: %d", result);
  234. #if 0 /* !!! FIXME: maybe update this? */
  235. /* Start and stop audio multiple times */
  236. for (l = 0; l < 3; l++) {
  237. SDLTest_Log("Pause/Unpause iteration: %d", l + 1);
  238. /* Reset callback counters */
  239. g_audio_testCallbackCounter = 0;
  240. g_audio_testCallbackLength = 0;
  241. /* Un-pause audio to start playing (maybe multiple times) */
  242. pause_on = 0;
  243. for (k = 0; k <= j; k++) {
  244. SDL_PlayAudioDevice(g_audio_id);
  245. SDLTest_AssertPass("Call to SDL_PlayAudioDevice(g_audio_id), call %d", k + 1);
  246. }
  247. /* Wait for callback */
  248. totalDelay = 0;
  249. do {
  250. SDL_Delay(10);
  251. totalDelay += 10;
  252. } while (g_audio_testCallbackCounter == 0 && totalDelay < 1000);
  253. SDLTest_AssertCheck(g_audio_testCallbackCounter > 0, "Verify callback counter; expected: >0 got: %d", g_audio_testCallbackCounter);
  254. SDLTest_AssertCheck(g_audio_testCallbackLength > 0, "Verify callback length; expected: >0 got: %d", g_audio_testCallbackLength);
  255. /* Pause audio to stop playing (maybe multiple times) */
  256. for (k = 0; k <= j; k++) {
  257. pause_on = (k == 0) ? 1 : SDLTest_RandomIntegerInRange(99, 9999);
  258. if (pause_on) {
  259. SDL_PauseAudioDevice(g_audio_id);
  260. SDLTest_AssertPass("Call to SDL_PauseAudioDevice(g_audio_id), call %d", k + 1);
  261. } else {
  262. SDL_PlayAudioDevice(g_audio_id);
  263. SDLTest_AssertPass("Call to SDL_PlayAudioDevice(g_audio_id), call %d", k + 1);
  264. }
  265. }
  266. /* Ensure callback is not called again */
  267. originalCounter = g_audio_testCallbackCounter;
  268. SDL_Delay(totalDelay + 10);
  269. SDLTest_AssertCheck(originalCounter == g_audio_testCallbackCounter, "Verify callback counter; expected: %d, got: %d", originalCounter, g_audio_testCallbackCounter);
  270. }
  271. #endif
  272. /* Call Close */
  273. SDL_CloseAudioDevice(g_audio_id);
  274. SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
  275. /* Call Quit */
  276. SDL_QuitSubSystem(SDL_INIT_AUDIO);
  277. SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
  278. } /* spec loop */
  279. } /* driver loop */
  280. /* Restart audio again */
  281. audioSetUp(NULL);
  282. return TEST_COMPLETED;
  283. }
  284. /**
  285. * \brief Enumerate and name available audio devices (output and capture).
  286. *
  287. * \sa SDL_GetNumAudioDevices
  288. * \sa SDL_GetAudioDeviceName
  289. */
  290. static int audio_enumerateAndNameAudioDevices(void *arg)
  291. {
  292. int t, tt;
  293. int i, n, nn;
  294. char *name, *nameAgain;
  295. SDL_AudioDeviceID *devices = NULL;
  296. /* Iterate over types: t=0 output device, t=1 input/capture device */
  297. for (t = 0; t < 2; t++) {
  298. /* Get number of devices. */
  299. devices = (t) ? SDL_GetAudioCaptureDevices(&n) : SDL_GetAudioOutputDevices(&n);
  300. SDLTest_AssertPass("Call to SDL_GetAudio%sDevices(%i)", (t) ? "Capture" : "Output", t);
  301. SDLTest_Log("Number of %s devices < 0, reported as %i", (t) ? "capture" : "output", n);
  302. SDLTest_AssertCheck(n >= 0, "Validate result is >= 0, got: %i", n);
  303. /* List devices. */
  304. if (n > 0) {
  305. SDLTest_AssertCheck(devices != NULL, "Validate devices is not NULL if n > 0");
  306. for (i = 0; i < n; i++) {
  307. name = SDL_GetAudioDeviceName(devices[i]);
  308. SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i)", i);
  309. SDLTest_AssertCheck(name != NULL, "Verify result from SDL_GetAudioDeviceName(%i) is not NULL", i);
  310. if (name != NULL) {
  311. SDLTest_AssertCheck(name[0] != '\0', "verify result from SDL_GetAudioDeviceName(%i) is not empty, got: '%s'", i, name);
  312. SDL_free(name);
  313. }
  314. }
  315. }
  316. SDL_free(devices);
  317. }
  318. return TEST_COMPLETED;
  319. }
  320. /**
  321. * \brief Negative tests around enumeration and naming of audio devices.
  322. *
  323. * \sa SDL_GetNumAudioDevices
  324. * \sa SDL_GetAudioDeviceName
  325. */
  326. static int audio_enumerateAndNameAudioDevicesNegativeTests(void *arg)
  327. {
  328. return TEST_COMPLETED; /* nothing in here atm since these interfaces changed in SDL3. */
  329. }
  330. /**
  331. * \brief Checks available audio driver names.
  332. *
  333. * \sa SDL_GetNumAudioDrivers
  334. * \sa SDL_GetAudioDriver
  335. */
  336. static int audio_printAudioDrivers(void *arg)
  337. {
  338. int i, n;
  339. const char *name;
  340. /* Get number of drivers */
  341. n = SDL_GetNumAudioDrivers();
  342. SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
  343. SDLTest_AssertCheck(n >= 0, "Verify number of audio drivers >= 0, got: %i", n);
  344. /* List drivers. */
  345. if (n > 0) {
  346. for (i = 0; i < n; i++) {
  347. name = SDL_GetAudioDriver(i);
  348. SDLTest_AssertPass("Call to SDL_GetAudioDriver(%i)", i);
  349. SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
  350. if (name != NULL) {
  351. SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
  352. }
  353. }
  354. }
  355. return TEST_COMPLETED;
  356. }
  357. /**
  358. * \brief Checks current audio driver name with initialized audio.
  359. *
  360. * \sa SDL_GetCurrentAudioDriver
  361. */
  362. static int audio_printCurrentAudioDriver(void *arg)
  363. {
  364. /* Check current audio driver */
  365. const char *name = SDL_GetCurrentAudioDriver();
  366. SDLTest_AssertPass("Call to SDL_GetCurrentAudioDriver()");
  367. SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
  368. if (name != NULL) {
  369. SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
  370. }
  371. return TEST_COMPLETED;
  372. }
  373. /* Definition of all formats, channels, and frequencies used to test audio conversions */
  374. static SDL_AudioFormat g_audioFormats[] = { SDL_AUDIO_S8, SDL_AUDIO_U8, SDL_AUDIO_S16LSB, SDL_AUDIO_S16MSB, SDL_AUDIO_S16SYS, SDL_AUDIO_S16,
  375. SDL_AUDIO_S32LSB, SDL_AUDIO_S32MSB, SDL_AUDIO_S32SYS, SDL_AUDIO_S32,
  376. SDL_AUDIO_F32LSB, SDL_AUDIO_F32MSB, SDL_AUDIO_F32SYS, SDL_AUDIO_F32 };
  377. static const char *g_audioFormatsVerbose[] = { "SDL_AUDIO_S8", "SDL_AUDIO_U8", "SDL_AUDIO_S16LSB", "SDL_AUDIO_S16MSB", "SDL_AUDIO_S16SYS", "SDL_AUDIO_S16",
  378. "SDL_AUDIO_S32LSB", "SDL_AUDIO_S32MSB", "SDL_AUDIO_S32SYS", "SDL_AUDIO_S32",
  379. "SDL_AUDIO_F32LSB", "SDL_AUDIO_F32MSB", "SDL_AUDIO_F32SYS", "SDL_AUDIO_F32" };
  380. static const int g_numAudioFormats = SDL_arraysize(g_audioFormats);
  381. static Uint8 g_audioChannels[] = { 1, 2, 4, 6 };
  382. static const int g_numAudioChannels = SDL_arraysize(g_audioChannels);
  383. static int g_audioFrequencies[] = { 11025, 22050, 44100, 48000 };
  384. static const int g_numAudioFrequencies = SDL_arraysize(g_audioFrequencies);
  385. /**
  386. * \brief Builds various audio conversion structures
  387. *
  388. * \sa SDL_CreateAudioStream
  389. */
  390. static int audio_buildAudioStream(void *arg)
  391. {
  392. SDL_AudioStream *stream;
  393. SDL_AudioSpec spec1;
  394. SDL_AudioSpec spec2;
  395. int i, ii, j, jj, k, kk;
  396. /* No conversion needed */
  397. spec1.format = SDL_AUDIO_S16LSB;
  398. spec1.channels = 2;
  399. spec1.freq = 22050;
  400. stream = SDL_CreateAudioStream(&spec1, &spec1);
  401. SDLTest_AssertPass("Call to SDL_CreateAudioStream(spec1 ==> spec1)");
  402. SDLTest_AssertCheck(stream != NULL, "Verify stream value; expected: != NULL, got: %p", (void *)stream);
  403. SDL_DestroyAudioStream(stream);
  404. /* Typical conversion */
  405. spec1.format = SDL_AUDIO_S8;
  406. spec1.channels = 1;
  407. spec1.freq = 22050;
  408. spec2.format = SDL_AUDIO_S16LSB;
  409. spec2.channels = 2;
  410. spec2.freq = 44100;
  411. stream = SDL_CreateAudioStream(&spec1, &spec2);
  412. SDLTest_AssertPass("Call to SDL_CreateAudioStream(spec1 ==> spec2)");
  413. SDLTest_AssertCheck(stream != NULL, "Verify stream value; expected: != NULL, got: %p", (void *)stream);
  414. SDL_DestroyAudioStream(stream);
  415. /* All source conversions with random conversion targets, allow 'null' conversions */
  416. for (i = 0; i < g_numAudioFormats; i++) {
  417. for (j = 0; j < g_numAudioChannels; j++) {
  418. for (k = 0; k < g_numAudioFrequencies; k++) {
  419. spec1.format = g_audioFormats[i];
  420. spec1.channels = g_audioChannels[j];
  421. spec1.freq = g_audioFrequencies[k];
  422. ii = SDLTest_RandomIntegerInRange(0, g_numAudioFormats - 1);
  423. jj = SDLTest_RandomIntegerInRange(0, g_numAudioChannels - 1);
  424. kk = SDLTest_RandomIntegerInRange(0, g_numAudioFrequencies - 1);
  425. spec2.format = g_audioFormats[ii];
  426. spec2.channels = g_audioChannels[jj];
  427. spec2.freq = g_audioFrequencies[kk];
  428. stream = SDL_CreateAudioStream(&spec1, &spec2);
  429. SDLTest_AssertPass("Call to SDL_CreateAudioStream(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)",
  430. i, g_audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, g_audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
  431. SDLTest_AssertCheck(stream != NULL, "Verify stream value; expected: != NULL, got: %p", (void *)stream);
  432. if (stream == NULL) {
  433. SDLTest_LogError("%s", SDL_GetError());
  434. }
  435. SDL_DestroyAudioStream(stream);
  436. }
  437. }
  438. }
  439. return TEST_COMPLETED;
  440. }
  441. /**
  442. * \brief Checks calls with invalid input to SDL_CreateAudioStream
  443. *
  444. * \sa SDL_CreateAudioStream
  445. */
  446. static int audio_buildAudioStreamNegative(void *arg)
  447. {
  448. const char *error;
  449. SDL_AudioStream *stream;
  450. SDL_AudioSpec spec1;
  451. SDL_AudioSpec spec2;
  452. int i;
  453. char message[256];
  454. /* Valid format */
  455. spec1.format = SDL_AUDIO_S8;
  456. spec1.channels = 1;
  457. spec1.freq = 22050;
  458. spec2.format = SDL_AUDIO_S16LSB;
  459. spec2.channels = 2;
  460. spec2.freq = 44100;
  461. SDL_ClearError();
  462. SDLTest_AssertPass("Call to SDL_ClearError()");
  463. /* Invalid conversions */
  464. for (i = 1; i < 64; i++) {
  465. /* Valid format to start with */
  466. spec1.format = SDL_AUDIO_S8;
  467. spec1.channels = 1;
  468. spec1.freq = 22050;
  469. spec2.format = SDL_AUDIO_S16LSB;
  470. spec2.channels = 2;
  471. spec2.freq = 44100;
  472. SDL_ClearError();
  473. SDLTest_AssertPass("Call to SDL_ClearError()");
  474. /* Set various invalid format inputs */
  475. SDL_strlcpy(message, "Invalid: ", 256);
  476. if (i & 1) {
  477. SDL_strlcat(message, " spec1.format", 256);
  478. spec1.format = 0;
  479. }
  480. if (i & 2) {
  481. SDL_strlcat(message, " spec1.channels", 256);
  482. spec1.channels = 0;
  483. }
  484. if (i & 4) {
  485. SDL_strlcat(message, " spec1.freq", 256);
  486. spec1.freq = 0;
  487. }
  488. if (i & 8) {
  489. SDL_strlcat(message, " spec2.format", 256);
  490. spec2.format = 0;
  491. }
  492. if (i & 16) {
  493. SDL_strlcat(message, " spec2.channels", 256);
  494. spec2.channels = 0;
  495. }
  496. if (i & 32) {
  497. SDL_strlcat(message, " spec2.freq", 256);
  498. spec2.freq = 0;
  499. }
  500. SDLTest_Log("%s", message);
  501. stream = SDL_CreateAudioStream(&spec1, &spec2);
  502. SDLTest_AssertPass("Call to SDL_CreateAudioStream(spec1 ==> spec2)");
  503. SDLTest_AssertCheck(stream == NULL, "Verify stream value; expected: NULL, got: %p", (void *)stream);
  504. error = SDL_GetError();
  505. SDLTest_AssertPass("Call to SDL_GetError()");
  506. SDLTest_AssertCheck(error != NULL && error[0] != '\0', "Validate that error message was not NULL or empty");
  507. SDL_DestroyAudioStream(stream);
  508. }
  509. SDL_ClearError();
  510. SDLTest_AssertPass("Call to SDL_ClearError()");
  511. return TEST_COMPLETED;
  512. }
  513. /**
  514. * \brief Checks current audio status.
  515. *
  516. * \sa SDL_GetAudioDeviceStatus
  517. */
  518. static int audio_getAudioStatus(void *arg)
  519. {
  520. return TEST_COMPLETED; /* no longer a thing in SDL3. */
  521. }
  522. /**
  523. * \brief Opens, checks current audio status, and closes a device.
  524. *
  525. * \sa SDL_GetAudioStatus
  526. */
  527. static int audio_openCloseAndGetAudioStatus(void *arg)
  528. {
  529. return TEST_COMPLETED; /* not a thing in SDL3. */
  530. }
  531. /**
  532. * \brief Locks and unlocks open audio device.
  533. *
  534. * \sa SDL_LockAudioDevice
  535. * \sa SDL_UnlockAudioDevice
  536. */
  537. static int audio_lockUnlockOpenAudioDevice(void *arg)
  538. {
  539. return TEST_COMPLETED; /* not a thing in SDL3 */
  540. }
  541. /**
  542. * \brief Convert audio using various conversion structures
  543. *
  544. * \sa SDL_CreateAudioStream
  545. */
  546. static int audio_convertAudio(void *arg)
  547. {
  548. SDL_AudioStream *stream;
  549. SDL_AudioSpec spec1;
  550. SDL_AudioSpec spec2;
  551. int c;
  552. char message[128];
  553. int i, ii, j, jj, k, kk;
  554. /* Iterate over bitmask that determines which parameters are modified in the conversion */
  555. for (c = 1; c < 8; c++) {
  556. SDL_strlcpy(message, "Changing:", 128);
  557. if (c & 1) {
  558. SDL_strlcat(message, " Format", 128);
  559. }
  560. if (c & 2) {
  561. SDL_strlcat(message, " Channels", 128);
  562. }
  563. if (c & 4) {
  564. SDL_strlcat(message, " Frequencies", 128);
  565. }
  566. SDLTest_Log("%s", message);
  567. /* All source conversions with random conversion targets */
  568. for (i = 0; i < g_numAudioFormats; i++) {
  569. for (j = 0; j < g_numAudioChannels; j++) {
  570. for (k = 0; k < g_numAudioFrequencies; k++) {
  571. spec1.format = g_audioFormats[i];
  572. spec1.channels = g_audioChannels[j];
  573. spec1.freq = g_audioFrequencies[k];
  574. /* Ensure we have a different target format */
  575. do {
  576. if (c & 1) {
  577. ii = SDLTest_RandomIntegerInRange(0, g_numAudioFormats - 1);
  578. } else {
  579. ii = 1;
  580. }
  581. if (c & 2) {
  582. jj = SDLTest_RandomIntegerInRange(0, g_numAudioChannels - 1);
  583. } else {
  584. jj = j;
  585. }
  586. if (c & 4) {
  587. kk = SDLTest_RandomIntegerInRange(0, g_numAudioFrequencies - 1);
  588. } else {
  589. kk = k;
  590. }
  591. } while ((i == ii) && (j == jj) && (k == kk));
  592. spec2.format = g_audioFormats[ii];
  593. spec2.channels = g_audioChannels[jj];
  594. spec2.freq = g_audioFrequencies[kk];
  595. stream = SDL_CreateAudioStream(&spec1, &spec2);
  596. SDLTest_AssertPass("Call to SDL_CreateAudioStream(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)",
  597. i, g_audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, g_audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
  598. SDLTest_AssertCheck(stream != NULL, "Verify stream value; expected: != NULL, got: %p", (void *)stream);
  599. if (stream == NULL) {
  600. SDLTest_LogError("%s", SDL_GetError());
  601. } else {
  602. Uint8 *dst_buf = NULL, *src_buf = NULL;
  603. int dst_len = 0, src_len = 0, real_dst_len = 0;
  604. int l = 64;
  605. int src_samplesize, dst_samplesize;
  606. src_samplesize = (SDL_AUDIO_BITSIZE(spec1.format) / 8) * spec1.channels;
  607. dst_samplesize = (SDL_AUDIO_BITSIZE(spec2.format) / 8) * spec2.channels;
  608. /* Create some random data to convert */
  609. src_len = l * src_samplesize;
  610. SDLTest_Log("Creating dummy sample buffer of %i length (%i bytes)", l, src_len);
  611. src_buf = (Uint8 *)SDL_malloc(src_len);
  612. SDLTest_AssertCheck(dst_buf != NULL, "Check src data buffer to convert is not NULL");
  613. if (src_buf == NULL) {
  614. return TEST_ABORTED;
  615. }
  616. src_len = src_len & ~(src_samplesize - 1);
  617. dst_len = dst_samplesize * (src_len / src_samplesize);
  618. if (spec1.freq < spec2.freq) {
  619. const double mult = ((double)spec2.freq) / ((double)spec1.freq);
  620. dst_len *= (int) SDL_ceil(mult);
  621. }
  622. dst_len = dst_len & ~(dst_samplesize - 1);
  623. dst_buf = (Uint8 *)SDL_calloc(1, dst_len);
  624. SDLTest_AssertCheck(dst_buf != NULL, "Check dst data buffer to convert is not NULL");
  625. if (dst_buf == NULL) {
  626. return TEST_ABORTED;
  627. }
  628. /* Run the audio converter */
  629. if (SDL_PutAudioStreamData(stream, src_buf, src_len) < 0 ||
  630. SDL_FlushAudioStream(stream) < 0) {
  631. return TEST_ABORTED;
  632. }
  633. real_dst_len = SDL_GetAudioStreamData(stream, dst_buf, dst_len);
  634. SDLTest_AssertCheck(real_dst_len > 0, "Verify result value; expected: > 0; got: %i", real_dst_len);
  635. if (real_dst_len < 0) {
  636. return TEST_ABORTED;
  637. }
  638. SDL_DestroyAudioStream(stream);
  639. /* Free converted buffer */
  640. SDL_free(src_buf);
  641. SDL_free(dst_buf);
  642. }
  643. }
  644. }
  645. }
  646. }
  647. return TEST_COMPLETED;
  648. }
  649. /**
  650. * \brief Opens, checks current connected status, and closes a device.
  651. *
  652. * \sa SDL_AudioDeviceConnected
  653. */
  654. static int audio_openCloseAudioDeviceConnected(void *arg)
  655. {
  656. return TEST_COMPLETED; /* not a thing in SDL3. */
  657. }
  658. static double sine_wave_sample(const Sint64 idx, const Sint64 rate, const Sint64 freq, const double phase)
  659. {
  660. /* Using integer modulo to avoid precision loss caused by large floating
  661. * point numbers. Sint64 is needed for the large integer multiplication.
  662. * The integers are assumed to be non-negative so that modulo is always
  663. * non-negative.
  664. * sin(i / rate * freq * 2 * PI + phase)
  665. * = sin(mod(i / rate * freq, 1) * 2 * PI + phase)
  666. * = sin(mod(i * freq, rate) / rate * 2 * PI + phase) */
  667. return SDL_sin(((double)(idx * freq % rate)) / ((double)rate) * (SDL_PI_D * 2) + phase);
  668. }
  669. /**
  670. * \brief Check signal-to-noise ratio and maximum error of audio resampling.
  671. *
  672. * \sa https://wiki.libsdl.org/SDL_CreateAudioStream
  673. * \sa https://wiki.libsdl.org/SDL_DestroyAudioStream
  674. * \sa https://wiki.libsdl.org/SDL_PutAudioStreamData
  675. * \sa https://wiki.libsdl.org/SDL_FlushAudioStream
  676. * \sa https://wiki.libsdl.org/SDL_GetAudioStreamData
  677. */
  678. static int audio_resampleLoss(void *arg)
  679. {
  680. /* Note: always test long input time (>= 5s from experience) in some test
  681. * cases because an improper implementation may suffer from low resampling
  682. * precision with long input due to e.g. doing subtraction with large floats. */
  683. struct test_spec_t {
  684. int time;
  685. int freq;
  686. double phase;
  687. int rate_in;
  688. int rate_out;
  689. double signal_to_noise;
  690. double max_error;
  691. } test_specs[] = {
  692. { 50, 440, 0, 44100, 48000, 60, 0.0025 },
  693. { 50, 5000, SDL_PI_D / 2, 20000, 10000, 65, 0.0010 },
  694. { 0 }
  695. };
  696. int spec_idx = 0;
  697. for (spec_idx = 0; test_specs[spec_idx].time > 0; ++spec_idx) {
  698. const struct test_spec_t *spec = &test_specs[spec_idx];
  699. const int frames_in = spec->time * spec->rate_in;
  700. const int frames_target = spec->time * spec->rate_out;
  701. const int len_in = frames_in * (int)sizeof(float);
  702. const int len_target = frames_target * (int)sizeof(float);
  703. SDL_AudioSpec tmpspec1, tmpspec2;
  704. Uint64 tick_beg = 0;
  705. Uint64 tick_end = 0;
  706. int i = 0;
  707. int ret = 0;
  708. SDL_AudioStream *stream = NULL;
  709. float *buf_in = NULL;
  710. float *buf_out = NULL;
  711. int len_out = 0;
  712. double max_error = 0;
  713. double sum_squared_error = 0;
  714. double sum_squared_value = 0;
  715. double signal_to_noise = 0;
  716. SDLTest_AssertPass("Test resampling of %i s %i Hz %f phase sine wave from sampling rate of %i Hz to %i Hz",
  717. spec->time, spec->freq, spec->phase, spec->rate_in, spec->rate_out);
  718. tmpspec1.format = SDL_AUDIO_F32;
  719. tmpspec1.channels = 1;
  720. tmpspec1.freq = spec->rate_in;
  721. tmpspec2.format = SDL_AUDIO_F32;
  722. tmpspec2.channels = 1;
  723. tmpspec2.freq = spec->rate_out;
  724. stream = SDL_CreateAudioStream(&tmpspec1, &tmpspec2);
  725. SDLTest_AssertPass("Call to SDL_CreateAudioStream(SDL_AUDIO_F32, 1, %i, SDL_AUDIO_F32, 1, %i)", spec->rate_in, spec->rate_out);
  726. SDLTest_AssertCheck(stream != NULL, "Expected SDL_CreateAudioStream to succeed.");
  727. if (stream == NULL) {
  728. return TEST_ABORTED;
  729. }
  730. buf_in = (float *)SDL_malloc(len_in);
  731. SDLTest_AssertCheck(buf_in != NULL, "Expected input buffer to be created.");
  732. if (buf_in == NULL) {
  733. SDL_DestroyAudioStream(stream);
  734. return TEST_ABORTED;
  735. }
  736. for (i = 0; i < frames_in; ++i) {
  737. *(buf_in + i) = (float)sine_wave_sample(i, spec->rate_in, spec->freq, spec->phase);
  738. }
  739. tick_beg = SDL_GetPerformanceCounter();
  740. ret = SDL_PutAudioStreamData(stream, buf_in, len_in);
  741. SDLTest_AssertPass("Call to SDL_PutAudioStreamData(stream, buf_in, %i)", len_in);
  742. SDLTest_AssertCheck(ret == 0, "Expected SDL_PutAudioStreamData to succeed.");
  743. SDL_free(buf_in);
  744. if (ret != 0) {
  745. SDL_DestroyAudioStream(stream);
  746. return TEST_ABORTED;
  747. }
  748. ret = SDL_FlushAudioStream(stream);
  749. SDLTest_AssertPass("Call to SDL_FlushAudioStream(stream)");
  750. SDLTest_AssertCheck(ret == 0, "Expected SDL_FlushAudioStream to succeed");
  751. if (ret != 0) {
  752. SDL_DestroyAudioStream(stream);
  753. return TEST_ABORTED;
  754. }
  755. buf_out = (float *)SDL_malloc(len_target);
  756. SDLTest_AssertCheck(buf_out != NULL, "Expected output buffer to be created.");
  757. if (buf_out == NULL) {
  758. SDL_DestroyAudioStream(stream);
  759. return TEST_ABORTED;
  760. }
  761. len_out = SDL_GetAudioStreamData(stream, buf_out, len_target);
  762. SDLTest_AssertPass("Call to SDL_GetAudioStreamData(stream, buf_out, %i)", len_target);
  763. /** !!! FIXME: SDL_AudioStream does not return output of the same length as
  764. ** !!! FIXME: the input even if SDL_FlushAudioStream is called. */
  765. SDLTest_AssertCheck(len_out <= len_target, "Expected output length to be no larger than %i, got %i.",
  766. len_target, len_out);
  767. SDL_DestroyAudioStream(stream);
  768. if (len_out > len_target) {
  769. SDL_free(buf_out);
  770. return TEST_ABORTED;
  771. }
  772. tick_end = SDL_GetPerformanceCounter();
  773. SDLTest_Log("Resampling used %f seconds.", ((double)(tick_end - tick_beg)) / SDL_GetPerformanceFrequency());
  774. for (i = 0; i < len_out / (int)sizeof(float); ++i) {
  775. const float output = *(buf_out + i);
  776. const double target = sine_wave_sample(i, spec->rate_out, spec->freq, spec->phase);
  777. const double error = SDL_fabs(target - output);
  778. max_error = SDL_max(max_error, error);
  779. sum_squared_error += error * error;
  780. sum_squared_value += target * target;
  781. }
  782. SDL_free(buf_out);
  783. signal_to_noise = 10 * SDL_log10(sum_squared_value / sum_squared_error); /* decibel */
  784. SDLTest_AssertCheck(isfinite(sum_squared_value), "Sum of squared target should be finite.");
  785. SDLTest_AssertCheck(isfinite(sum_squared_error), "Sum of squared error should be finite.");
  786. /* Infinity is theoretically possible when there is very little to no noise */
  787. SDLTest_AssertCheck(!isnan(signal_to_noise), "Signal-to-noise ratio should not be NaN.");
  788. SDLTest_AssertCheck(isfinite(max_error), "Maximum conversion error should be finite.");
  789. SDLTest_AssertCheck(signal_to_noise >= spec->signal_to_noise, "Conversion signal-to-noise ratio %f dB should be no less than %f dB.",
  790. signal_to_noise, spec->signal_to_noise);
  791. SDLTest_AssertCheck(max_error <= spec->max_error, "Maximum conversion error %f should be no more than %f.",
  792. max_error, spec->max_error);
  793. }
  794. return TEST_COMPLETED;
  795. }
  796. /* ================= Test Case References ================== */
  797. /* Audio test cases */
  798. static const SDLTest_TestCaseReference audioTest1 = {
  799. audio_enumerateAndNameAudioDevices, "audio_enumerateAndNameAudioDevices", "Enumerate and name available audio devices (output and capture)", TEST_ENABLED
  800. };
  801. static const SDLTest_TestCaseReference audioTest2 = {
  802. audio_enumerateAndNameAudioDevicesNegativeTests, "audio_enumerateAndNameAudioDevicesNegativeTests", "Negative tests around enumeration and naming of audio devices.", TEST_ENABLED
  803. };
  804. static const SDLTest_TestCaseReference audioTest3 = {
  805. audio_printAudioDrivers, "audio_printAudioDrivers", "Checks available audio driver names.", TEST_ENABLED
  806. };
  807. static const SDLTest_TestCaseReference audioTest4 = {
  808. audio_printCurrentAudioDriver, "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED
  809. };
  810. static const SDLTest_TestCaseReference audioTest5 = {
  811. audio_buildAudioStream, "audio_buildAudioStream", "Builds various audio conversion structures.", TEST_ENABLED
  812. };
  813. static const SDLTest_TestCaseReference audioTest6 = {
  814. audio_buildAudioStreamNegative, "audio_buildAudioStreamNegative", "Checks calls with invalid input to SDL_CreateAudioStream", TEST_ENABLED
  815. };
  816. static const SDLTest_TestCaseReference audioTest7 = {
  817. audio_getAudioStatus, "audio_getAudioStatus", "Checks current audio status.", TEST_ENABLED
  818. };
  819. static const SDLTest_TestCaseReference audioTest8 = {
  820. audio_openCloseAndGetAudioStatus, "audio_openCloseAndGetAudioStatus", "Opens and closes audio device and get audio status.", TEST_ENABLED
  821. };
  822. static const SDLTest_TestCaseReference audioTest9 = {
  823. audio_lockUnlockOpenAudioDevice, "audio_lockUnlockOpenAudioDevice", "Locks and unlocks an open audio device.", TEST_ENABLED
  824. };
  825. /* TODO: enable test when SDL_ConvertAudio segfaults on cygwin have been fixed.
  826. * TODO: re-check, since this was changer to AudioStream */
  827. /* For debugging, test case can be run manually using --filter audio_convertAudio */
  828. static const SDLTest_TestCaseReference audioTest10 = {
  829. audio_convertAudio, "audio_convertAudio", "Convert audio using available formats.", TEST_DISABLED
  830. };
  831. /* TODO: enable test when SDL_AudioDeviceConnected has been implemented. */
  832. static const SDLTest_TestCaseReference audioTest11 = {
  833. audio_openCloseAudioDeviceConnected, "audio_openCloseAudioDeviceConnected", "Opens and closes audio device and get connected status.", TEST_DISABLED
  834. };
  835. static const SDLTest_TestCaseReference audioTest12 = {
  836. audio_quitInitAudioSubSystem, "audio_quitInitAudioSubSystem", "Quit and re-init audio subsystem.", TEST_ENABLED
  837. };
  838. static const SDLTest_TestCaseReference audioTest13 = {
  839. audio_initQuitAudio, "audio_initQuitAudio", "Init and quit audio drivers directly.", TEST_ENABLED
  840. };
  841. static const SDLTest_TestCaseReference audioTest14 = {
  842. audio_initOpenCloseQuitAudio, "audio_initOpenCloseQuitAudio", "Cycle through init, open, close and quit with various audio specs.", TEST_ENABLED
  843. };
  844. static const SDLTest_TestCaseReference audioTest15 = {
  845. audio_pauseUnpauseAudio, "audio_pauseUnpauseAudio", "Pause and Unpause audio for various audio specs while testing callback.", TEST_ENABLED
  846. };
  847. static const SDLTest_TestCaseReference audioTest16 = {
  848. audio_resampleLoss, "audio_resampleLoss", "Check signal-to-noise ratio and maximum error of audio resampling.", TEST_ENABLED
  849. };
  850. /* Sequence of Audio test cases */
  851. static const SDLTest_TestCaseReference *audioTests[] = {
  852. &audioTest1, &audioTest2, &audioTest3, &audioTest4, &audioTest5, &audioTest6,
  853. &audioTest7, &audioTest8, &audioTest9, &audioTest10, &audioTest11,
  854. &audioTest12, &audioTest13, &audioTest14, &audioTest15, &audioTest16, NULL
  855. };
  856. /* Audio test suite (global) */
  857. SDLTest_TestSuiteReference audioTestSuite = {
  858. "Audio",
  859. audioSetUp,
  860. audioTests,
  861. audioTearDown
  862. };