testautomation_audio.c 37 KB

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