testautomation_audio.c 37 KB

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