testautomation_audio.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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(src_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, 80, 0.0009 },
  692. { 50, 5000, SDL_PI_D / 2, 20000, 10000, 999, 0.0001 },
  693. { 50, 440, 0, 22050, 96000, 79, 0.0120 },
  694. { 50, 440, 0, 96000, 22050, 80, 0.0002 },
  695. { 0 }
  696. };
  697. int spec_idx = 0;
  698. int min_channels = 1;
  699. int max_channels = 1 /*8*/;
  700. int num_channels = min_channels;
  701. for (spec_idx = 0; test_specs[spec_idx].time > 0; ++num_channels) {
  702. const struct test_spec_t *spec = &test_specs[spec_idx];
  703. const int frames_in = spec->time * spec->rate_in;
  704. const int frames_target = spec->time * spec->rate_out;
  705. const int len_in = (frames_in * num_channels) * (int)sizeof(float);
  706. const int len_target = (frames_target * num_channels) * (int)sizeof(float);
  707. SDL_AudioSpec tmpspec1, tmpspec2;
  708. Uint64 tick_beg = 0;
  709. Uint64 tick_end = 0;
  710. int i = 0;
  711. int j = 0;
  712. int ret = 0;
  713. SDL_AudioStream *stream = NULL;
  714. float *buf_in = NULL;
  715. float *buf_out = NULL;
  716. int len_out = 0;
  717. double max_error = 0;
  718. double sum_squared_error = 0;
  719. double sum_squared_value = 0;
  720. double signal_to_noise = 0;
  721. if (num_channels > max_channels) {
  722. num_channels = 1;
  723. ++spec_idx;
  724. }
  725. SDLTest_AssertPass("Test resampling of %i s %i Hz %f phase sine wave from sampling rate of %i Hz to %i Hz",
  726. spec->time, spec->freq, spec->phase, spec->rate_in, spec->rate_out);
  727. tmpspec1.format = SDL_AUDIO_F32;
  728. tmpspec1.channels = num_channels;
  729. tmpspec1.freq = spec->rate_in;
  730. tmpspec2.format = SDL_AUDIO_F32;
  731. tmpspec2.channels = num_channels;
  732. tmpspec2.freq = spec->rate_out;
  733. stream = SDL_CreateAudioStream(&tmpspec1, &tmpspec2);
  734. SDLTest_AssertPass("Call to SDL_CreateAudioStream(SDL_AUDIO_F32, 1, %i, SDL_AUDIO_F32, 1, %i)", spec->rate_in, spec->rate_out);
  735. SDLTest_AssertCheck(stream != NULL, "Expected SDL_CreateAudioStream to succeed.");
  736. if (stream == NULL) {
  737. return TEST_ABORTED;
  738. }
  739. buf_in = (float *)SDL_malloc(len_in);
  740. SDLTest_AssertCheck(buf_in != NULL, "Expected input buffer to be created.");
  741. if (buf_in == NULL) {
  742. SDL_DestroyAudioStream(stream);
  743. return TEST_ABORTED;
  744. }
  745. for (i = 0; i < frames_in; ++i) {
  746. float f = (float)sine_wave_sample(i, spec->rate_in, spec->freq, spec->phase);
  747. for (j = 0; j < num_channels; ++j) {
  748. *(buf_in + (i * num_channels) + j) = f;
  749. }
  750. }
  751. tick_beg = SDL_GetPerformanceCounter();
  752. ret = SDL_PutAudioStreamData(stream, buf_in, len_in);
  753. SDLTest_AssertPass("Call to SDL_PutAudioStreamData(stream, buf_in, %i)", len_in);
  754. SDLTest_AssertCheck(ret == 0, "Expected SDL_PutAudioStreamData to succeed.");
  755. SDL_free(buf_in);
  756. if (ret != 0) {
  757. SDL_DestroyAudioStream(stream);
  758. return TEST_ABORTED;
  759. }
  760. ret = SDL_FlushAudioStream(stream);
  761. SDLTest_AssertPass("Call to SDL_FlushAudioStream(stream)");
  762. SDLTest_AssertCheck(ret == 0, "Expected SDL_FlushAudioStream to succeed");
  763. if (ret != 0) {
  764. SDL_DestroyAudioStream(stream);
  765. return TEST_ABORTED;
  766. }
  767. buf_out = (float *)SDL_malloc(len_target);
  768. SDLTest_AssertCheck(buf_out != NULL, "Expected output buffer to be created.");
  769. if (buf_out == NULL) {
  770. SDL_DestroyAudioStream(stream);
  771. return TEST_ABORTED;
  772. }
  773. len_out = SDL_GetAudioStreamData(stream, buf_out, len_target);
  774. SDLTest_AssertPass("Call to SDL_GetAudioStreamData(stream, buf_out, %i)", len_target);
  775. SDLTest_AssertCheck(len_out == len_target, "Expected output length to be no larger than %i, got %i.",
  776. len_target, len_out);
  777. SDL_DestroyAudioStream(stream);
  778. if (len_out > len_target) {
  779. SDL_free(buf_out);
  780. return TEST_ABORTED;
  781. }
  782. tick_end = SDL_GetPerformanceCounter();
  783. SDLTest_Log("Resampling used %f seconds.", ((double)(tick_end - tick_beg)) / SDL_GetPerformanceFrequency());
  784. for (i = 0; i < frames_target; ++i) {
  785. const double target = sine_wave_sample(i, spec->rate_out, spec->freq, spec->phase);
  786. for (j = 0; j < num_channels; ++j) {
  787. const float output = *(buf_out + (i * num_channels) + j);
  788. const double error = SDL_fabs(target - output);
  789. max_error = SDL_max(max_error, error);
  790. sum_squared_error += error * error;
  791. sum_squared_value += target * target;
  792. }
  793. }
  794. SDL_free(buf_out);
  795. signal_to_noise = 10 * SDL_log10(sum_squared_value / sum_squared_error); /* decibel */
  796. SDLTest_AssertCheck(isfinite(sum_squared_value), "Sum of squared target should be finite.");
  797. SDLTest_AssertCheck(isfinite(sum_squared_error), "Sum of squared error should be finite.");
  798. /* Infinity is theoretically possible when there is very little to no noise */
  799. SDLTest_AssertCheck(!isnan(signal_to_noise), "Signal-to-noise ratio should not be NaN.");
  800. SDLTest_AssertCheck(isfinite(max_error), "Maximum conversion error should be finite.");
  801. SDLTest_AssertCheck(signal_to_noise >= spec->signal_to_noise, "Conversion signal-to-noise ratio %f dB should be no less than %f dB.",
  802. signal_to_noise, spec->signal_to_noise);
  803. SDLTest_AssertCheck(max_error <= spec->max_error, "Maximum conversion error %f should be no more than %f.",
  804. max_error, spec->max_error);
  805. }
  806. return TEST_COMPLETED;
  807. }
  808. /**
  809. * \brief Check accuracy converting between audio formats.
  810. *
  811. * \sa SDL_ConvertAudioSamples
  812. */
  813. static int audio_convertAccuracy(void *arg)
  814. {
  815. static SDL_AudioFormat formats[] = { SDL_AUDIO_S8, SDL_AUDIO_U8, SDL_AUDIO_S16SYS, SDL_AUDIO_S32SYS };
  816. static const char* format_names[] = { "S8", "U8", "S16", "S32" };
  817. int src_num = 65537 + 2048 + 48 + 256 + 100000;
  818. int src_len = src_num * sizeof(float);
  819. float* src_data = SDL_malloc(src_len);
  820. int i, j;
  821. SDLTest_AssertCheck(src_data != NULL, "Expected source buffer to be created.");
  822. if (src_data == NULL) {
  823. return TEST_ABORTED;
  824. }
  825. j = 0;
  826. /* Generate a uniform range of floats between [-1.0, 1.0] */
  827. for (i = 0; i < 65537; ++i) {
  828. src_data[j++] = ((float)i - 32768.0f) / 32768.0f;
  829. }
  830. /* Generate floats close to 1.0 */
  831. const float max_val = 16777216.0f;
  832. for (i = 0; i < 1024; ++i) {
  833. float f = (max_val + (float)(512 - i)) / max_val;
  834. src_data[j++] = f;
  835. src_data[j++] = -f;
  836. }
  837. for (i = 0; i < 24; ++i) {
  838. float f = (max_val + (float)(3u << i)) / max_val;
  839. src_data[j++] = f;
  840. src_data[j++] = -f;
  841. }
  842. /* Generate floats far outside the [-1.0, 1.0] range */
  843. for (i = 0; i < 128; ++i) {
  844. float f = 2.0f + (float) i;
  845. src_data[j++] = f;
  846. src_data[j++] = -f;
  847. }
  848. /* Fill the rest with random floats between [-1.0, 1.0] */
  849. for (i = 0; i < 100000; ++i) {
  850. src_data[j++] = SDLTest_RandomSint32() / 2147483648.0f;
  851. }
  852. for (i = 0; i < SDL_arraysize(formats); ++i) {
  853. SDL_AudioSpec src_spec, tmp_spec;
  854. Uint64 convert_begin, convert_end;
  855. Uint8 *tmp_data, *dst_data;
  856. int tmp_len, dst_len;
  857. int ret;
  858. SDL_AudioFormat format = formats[i];
  859. const char* format_name = format_names[i];
  860. /* Formats with > 23 bits can represent every value exactly */
  861. float min_delta = 1.0f;
  862. float max_delta = -1.0f;
  863. /* Subtract 1 bit to account for sign */
  864. int bits = SDL_AUDIO_BITSIZE(format) - 1;
  865. float target_max_delta = (bits > 23) ? 0.0f : (1.0f / (float)(1 << bits));
  866. float target_min_delta = -target_max_delta;
  867. src_spec.format = SDL_AUDIO_F32;
  868. src_spec.channels = 1;
  869. src_spec.freq = 44800;
  870. tmp_spec.format = format;
  871. tmp_spec.channels = 1;
  872. tmp_spec.freq = 44800;
  873. convert_begin = SDL_GetPerformanceCounter();
  874. tmp_data = NULL;
  875. tmp_len = 0;
  876. ret = SDL_ConvertAudioSamples(&src_spec, (const Uint8*) src_data, src_len, &tmp_spec, &tmp_data, &tmp_len);
  877. SDLTest_AssertCheck(ret == 0, "Expected SDL_ConvertAudioSamples(F32->%s) to succeed", format_name);
  878. if (ret != 0) {
  879. SDL_free(src_data);
  880. return TEST_ABORTED;
  881. }
  882. dst_data = NULL;
  883. dst_len = 0;
  884. ret = SDL_ConvertAudioSamples(&tmp_spec, tmp_data, tmp_len, &src_spec, &dst_data, &dst_len);
  885. SDLTest_AssertCheck(ret == 0, "Expected SDL_ConvertAudioSamples(%s->F32) to succeed", format_name);
  886. if (ret != 0) {
  887. SDL_free(tmp_data);
  888. SDL_free(src_data);
  889. return TEST_ABORTED;
  890. }
  891. convert_end = SDL_GetPerformanceCounter();
  892. SDLTest_Log("Conversion via %s took %f seconds.", format_name, ((double)(convert_end - convert_begin)) / SDL_GetPerformanceFrequency());
  893. SDL_free(tmp_data);
  894. for (j = 0; j < src_num; ++j) {
  895. float x = src_data[j];
  896. float y = ((float*)dst_data)[j];
  897. float d = SDL_clamp(x, -1.0f, 1.0f) - y;
  898. min_delta = SDL_min(min_delta, d);
  899. max_delta = SDL_max(max_delta, d);
  900. }
  901. SDLTest_AssertCheck(min_delta >= target_min_delta, "%s has min delta of %+f, should be >= %+f", format_name, min_delta, target_min_delta);
  902. SDLTest_AssertCheck(max_delta <= target_max_delta, "%s has max delta of %+f, should be <= %+f", format_name, max_delta, target_max_delta);
  903. SDL_free(dst_data);
  904. }
  905. SDL_free(src_data);
  906. return TEST_COMPLETED;
  907. }
  908. /* ================= Test Case References ================== */
  909. /* Audio test cases */
  910. static const SDLTest_TestCaseReference audioTest1 = {
  911. audio_enumerateAndNameAudioDevices, "audio_enumerateAndNameAudioDevices", "Enumerate and name available audio devices (output and capture)", TEST_ENABLED
  912. };
  913. static const SDLTest_TestCaseReference audioTest2 = {
  914. audio_enumerateAndNameAudioDevicesNegativeTests, "audio_enumerateAndNameAudioDevicesNegativeTests", "Negative tests around enumeration and naming of audio devices.", TEST_ENABLED
  915. };
  916. static const SDLTest_TestCaseReference audioTest3 = {
  917. audio_printAudioDrivers, "audio_printAudioDrivers", "Checks available audio driver names.", TEST_ENABLED
  918. };
  919. static const SDLTest_TestCaseReference audioTest4 = {
  920. audio_printCurrentAudioDriver, "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED
  921. };
  922. static const SDLTest_TestCaseReference audioTest5 = {
  923. audio_buildAudioStream, "audio_buildAudioStream", "Builds various audio conversion structures.", TEST_ENABLED
  924. };
  925. static const SDLTest_TestCaseReference audioTest6 = {
  926. audio_buildAudioStreamNegative, "audio_buildAudioStreamNegative", "Checks calls with invalid input to SDL_CreateAudioStream", TEST_ENABLED
  927. };
  928. static const SDLTest_TestCaseReference audioTest7 = {
  929. audio_getAudioStatus, "audio_getAudioStatus", "Checks current audio status.", TEST_ENABLED
  930. };
  931. static const SDLTest_TestCaseReference audioTest8 = {
  932. audio_openCloseAndGetAudioStatus, "audio_openCloseAndGetAudioStatus", "Opens and closes audio device and get audio status.", TEST_ENABLED
  933. };
  934. static const SDLTest_TestCaseReference audioTest9 = {
  935. audio_lockUnlockOpenAudioDevice, "audio_lockUnlockOpenAudioDevice", "Locks and unlocks an open audio device.", TEST_ENABLED
  936. };
  937. /* TODO: enable test when SDL_ConvertAudio segfaults on cygwin have been fixed.
  938. * TODO: re-check, since this was changer to AudioStream */
  939. /* For debugging, test case can be run manually using --filter audio_convertAudio */
  940. static const SDLTest_TestCaseReference audioTest10 = {
  941. audio_convertAudio, "audio_convertAudio", "Convert audio using available formats.", TEST_DISABLED
  942. };
  943. /* TODO: enable test when SDL_AudioDeviceConnected has been implemented. */
  944. static const SDLTest_TestCaseReference audioTest11 = {
  945. audio_openCloseAudioDeviceConnected, "audio_openCloseAudioDeviceConnected", "Opens and closes audio device and get connected status.", TEST_DISABLED
  946. };
  947. static const SDLTest_TestCaseReference audioTest12 = {
  948. audio_quitInitAudioSubSystem, "audio_quitInitAudioSubSystem", "Quit and re-init audio subsystem.", TEST_ENABLED
  949. };
  950. static const SDLTest_TestCaseReference audioTest13 = {
  951. audio_initQuitAudio, "audio_initQuitAudio", "Init and quit audio drivers directly.", TEST_ENABLED
  952. };
  953. static const SDLTest_TestCaseReference audioTest14 = {
  954. audio_initOpenCloseQuitAudio, "audio_initOpenCloseQuitAudio", "Cycle through init, open, close and quit with various audio specs.", TEST_ENABLED
  955. };
  956. static const SDLTest_TestCaseReference audioTest15 = {
  957. audio_pauseUnpauseAudio, "audio_pauseUnpauseAudio", "Pause and Unpause audio for various audio specs while testing callback.", TEST_ENABLED
  958. };
  959. static const SDLTest_TestCaseReference audioTest16 = {
  960. audio_resampleLoss, "audio_resampleLoss", "Check signal-to-noise ratio and maximum error of audio resampling.", TEST_ENABLED
  961. };
  962. static const SDLTest_TestCaseReference audioTest17 = {
  963. audio_convertAccuracy, "audio_convertAccuracy", "Check accuracy converting between audio formats.", TEST_ENABLED
  964. };
  965. /* Sequence of Audio test cases */
  966. static const SDLTest_TestCaseReference *audioTests[] = {
  967. &audioTest1, &audioTest2, &audioTest3, &audioTest4, &audioTest5, &audioTest6,
  968. &audioTest7, &audioTest8, &audioTest9, &audioTest10, &audioTest11,
  969. &audioTest12, &audioTest13, &audioTest14, &audioTest15, &audioTest16,
  970. &audioTest17, NULL
  971. };
  972. /* Audio test suite (global) */
  973. SDLTest_TestSuiteReference audioTestSuite = {
  974. "Audio",
  975. audioSetUp,
  976. audioTests,
  977. audioTearDown
  978. };