Debian Codegraph @

test/testautomation_audio.c

raw
1 /**
2 * Original code: automated SDL audio test written by Edgar Simo "bobbens"
3 * New/updated tests: aschiffler at ferzkopp dot net
4 */
5
6 /* quiet windows compiler warnings */
7 #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
8 #define _CRT_SECURE_NO_WARNINGS
9 #endif
10
11 #include <math.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #include "SDL.h"
16 #include "SDL_test.h"
17
18 /* ================= Test Case Implementation ================== */
19
20 /* Fixture */
21
22 void _audioSetUp(void *arg)
23 {
24 /* Start SDL audio subsystem */
25 int ret = SDL_InitSubSystem(SDL_INIT_AUDIO);
26 SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO)");
27 SDLTest_AssertCheck(ret == 0, "Check result from SDL_InitSubSystem(SDL_INIT_AUDIO)");
28 if (ret != 0) {
29 SDLTest_LogError("%s", SDL_GetError());
30 }
31 }
32
33 void _audioTearDown(void *arg)
34 {
35 /* Remove a possibly created file from SDL disk writer audio driver; ignore errors */
36 (void)remove("sdlaudio.raw");
37
38 SDLTest_AssertPass("Cleanup of test files completed");
39 }
40
41 /* Global counter for callback invocation */
42 int _audio_testCallbackCounter;
43
44 /* Global accumulator for total callback length */
45 int _audio_testCallbackLength;
46
47 /* Test callback function */
48 void SDLCALL _audio_testCallback(void *userdata, Uint8 *stream, int len)
49 {
50 /* track that callback was called */
51 _audio_testCallbackCounter++;
52 _audio_testCallbackLength += len;
53 }
54
55 #if defined(__linux__)
56 /* Linux builds can include many audio drivers, but some are very
57 * obscure and typically unsupported on modern systems. They will
58 * be skipped in tests that run against all included drivers, as
59 * they are basically guaranteed to fail.
60 */
61 static SDL_bool DriverIsProblematic(const char *driver)
62 {
63 static const char *driverList[] = {
64 /* Omnipresent in Linux builds, but deprecated since 2002,
65 * very rarely used on Linux nowadays, and is almost certainly
66 * guaranteed to fail.
67 */
68 "dsp",
69
70 /* Jack isn't always configured properly on end user systems */
71 "jack",
72
73 /* OpenBSD sound API. Can be used on Linux, but very rare. */
74 "sndio",
75
76 /* Always fails on initialization and/or opening a device.
77 * Does anyone or anything actually use this?
78 */
79 "nas"
80 };
81
82 int i;
83
84 for (i = 0; i < SDL_arraysize(driverList); ++i) {
85 if (SDL_strcmp(driver, driverList[i]) == 0) {
86 return SDL_TRUE;
87 }
88 }
89
90 return SDL_FALSE;
91 }
92 #endif
93
94 /* Test case functions */
95
96 /**
97 * \brief Stop and restart audio subsystem
98 *
99 * \sa https://wiki.libsdl.org/SDL_QuitSubSystem
100 * \sa https://wiki.libsdl.org/SDL_InitSubSystem
101 */
102 int audio_quitInitAudioSubSystem(void)
103 {
104 /* Stop SDL audio subsystem */
105 SDL_QuitSubSystem(SDL_INIT_AUDIO);
106 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
107
108 /* Restart audio again */
109 _audioSetUp(NULL);
110
111 return TEST_COMPLETED;
112 }
113
114 /**
115 * \brief Start and stop audio directly
116 *
117 * \sa https://wiki.libsdl.org/SDL_InitAudio
118 * \sa https://wiki.libsdl.org/SDL_QuitAudio
119 */
120 int audio_initQuitAudio(void)
121 {
122 int result;
123 int i, iMax;
124 const char *audioDriver;
125 const char *hint = SDL_GetHint(SDL_HINT_AUDIODRIVER);
126
127 /* Stop SDL audio subsystem */
128 SDL_QuitSubSystem(SDL_INIT_AUDIO);
129 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
130
131 /* Was a specific driver requested? */
132 audioDriver = SDL_GetHint(SDL_HINT_AUDIODRIVER);
133
134 if (audioDriver == NULL) {
135 /* Loop over all available audio drivers */
136 iMax = SDL_GetNumAudioDrivers();
137 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
138 SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
139 } else {
140 /* A specific driver was requested for testing */
141 iMax = 1;
142 }
143 for (i = 0; i < iMax; i++) {
144 if (audioDriver == NULL) {
145 audioDriver = SDL_GetAudioDriver(i);
146 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
147 SDLTest_Assert(audioDriver != NULL, "Audio driver name is not NULL");
148 SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver); /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */
149
150 #if defined(__linux__)
151 if (DriverIsProblematic(audioDriver)) {
152 SDLTest_Log("Audio driver '%s' flagged as problematic: skipping init/quit test (set SDL_AUDIODRIVER=%s to force)", audioDriver, audioDriver);
153 audioDriver = NULL;
154 continue;
155 }
156 #endif
157 }
158
159 if (hint && SDL_strcmp(audioDriver, hint) != 0) {
160 continue;
161 }
162
163 /* Call Init */
164 result = SDL_AudioInit(audioDriver);
165 SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
166 SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
167
168 /* Call Quit */
169 SDL_AudioQuit();
170 SDLTest_AssertPass("Call to SDL_AudioQuit()");
171
172 audioDriver = NULL;
173 }
174
175 /* NULL driver specification */
176 audioDriver = NULL;
177
178 /* Call Init */
179 result = SDL_AudioInit(audioDriver);
180 SDLTest_AssertPass("Call to SDL_AudioInit(NULL)");
181 SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
182
183 /* Call Quit */
184 SDL_AudioQuit();
185 SDLTest_AssertPass("Call to SDL_AudioQuit()");
186
187 /* Restart audio again */
188 _audioSetUp(NULL);
189
190 return TEST_COMPLETED;
191 }
192
193 /**
194 * \brief Start, open, close and stop audio
195 *
196 * \sa https://wiki.libsdl.org/SDL_InitAudio
197 * \sa https://wiki.libsdl.org/SDL_OpenAudio
198 * \sa https://wiki.libsdl.org/SDL_CloseAudio
199 * \sa https://wiki.libsdl.org/SDL_QuitAudio
200 */
201 int audio_initOpenCloseQuitAudio(void)
202 {
203 int result, expectedResult;
204 int i, iMax, j, k;
205 const char *audioDriver;
206 SDL_AudioSpec desired;
207 const char *hint = SDL_GetHint(SDL_HINT_AUDIODRIVER);
208
209 /* Stop SDL audio subsystem */
210 SDL_QuitSubSystem(SDL_INIT_AUDIO);
211 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
212
213 /* Was a specific driver requested? */
214 audioDriver = SDL_GetHint(SDL_HINT_AUDIODRIVER);
215
216 if (audioDriver == NULL) {
217 /* Loop over all available audio drivers */
218 iMax = SDL_GetNumAudioDrivers();
219 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
220 SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
221 } else {
222 /* A specific driver was requested for testing */
223 iMax = 1;
224 }
225 for (i = 0; i < iMax; i++) {
226 if (audioDriver == NULL) {
227 audioDriver = SDL_GetAudioDriver(i);
228 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
229 SDLTest_Assert(audioDriver != NULL, "Audio driver name is not NULL");
230 SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver); /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */
231
232 #if defined(__linux__)
233 if (DriverIsProblematic(audioDriver)) {
234 SDLTest_Log("Audio driver '%s' flagged as problematic: skipping device open/close test (set SDL_AUDIODRIVER=%s to force)", audioDriver, audioDriver);
235 audioDriver = NULL;
236 continue;
237 }
238 #endif
239 }
240
241 if (hint && SDL_strcmp(audioDriver, hint) != 0) {
242 continue;
243 }
244
245 /* Change specs */
246 for (j = 0; j < 2; j++) {
247
248 /* Call Init */
249 result = SDL_AudioInit(audioDriver);
250 SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
251 SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
252
253 /* Check for output devices */
254 result = SDL_GetNumAudioDevices(SDL_FALSE);
255 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(SDL_FALSE)");
256 SDLTest_AssertCheck(result >= 0, "Validate result value; expected: >=0 got: %d", result);
257 if (result <= 0) {
258 SDLTest_Log("No output devices for '%s': skipping device open/close test", audioDriver);
259 SDL_AudioQuit();
260 SDLTest_AssertPass("Call to SDL_AudioQuit()");
261 break;
262 }
263
264 /* Set spec */
265 SDL_memset(&desired, 0, sizeof(desired));
266 switch (j) {
267 case 0:
268 /* Set standard desired spec */
269 desired.freq = 22050;
270 desired.format = AUDIO_S16SYS;
271 desired.channels = 2;
272 desired.samples = 4096;
273 desired.callback = _audio_testCallback;
274 desired.userdata = NULL;
275 break;
276 case 1:
277 /* Set custom desired spec */
278 desired.freq = 48000;
279 desired.format = AUDIO_F32SYS;
280 desired.channels = 2;
281 desired.samples = 2048;
282 desired.callback = _audio_testCallback;
283 desired.userdata = NULL;
284 break;
285 }
286
287 /* Call Open (maybe multiple times) */
288 for (k = 0; k <= j; k++) {
289 result = SDL_OpenAudio(&desired, NULL);
290 SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL), call %d", j, k + 1);
291 expectedResult = (k == 0) ? 0 : -1;
292 SDLTest_AssertCheck(result == expectedResult, "Verify return value; expected: %d, got: %d", expectedResult, result);
293 }
294
295 /* Call Close (maybe multiple times) */
296 for (k = 0; k <= j; k++) {
297 SDL_CloseAudio();
298 SDLTest_AssertPass("Call to SDL_CloseAudio(), call %d", k + 1);
299 }
300
301 /* Call Quit (maybe multiple times) */
302 for (k = 0; k <= j; k++) {
303 SDL_AudioQuit();
304 SDLTest_AssertPass("Call to SDL_AudioQuit(), call %d", k + 1);
305 }
306
307 } /* spec loop */
308
309 audioDriver = NULL;
310
311 } /* driver loop */
312
313 /* Restart audio again */
314 _audioSetUp(NULL);
315
316 return TEST_COMPLETED;
317 }
318
319 /**
320 * \brief Pause and unpause audio
321 *
322 * \sa https://wiki.libsdl.org/SDL_PauseAudio
323 */
324 int audio_pauseUnpauseAudio(void)
325 {
326 int result;
327 int i, iMax, j, k, l;
328 int totalDelay;
329 int pause_on;
330 int originalCounter;
331 const char *audioDriver;
332 SDL_AudioSpec desired;
333 const char *hint = SDL_GetHint(SDL_HINT_AUDIODRIVER);
334
335 /* Stop SDL audio subsystem */
336 SDL_QuitSubSystem(SDL_INIT_AUDIO);
337 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
338
339 /* Was a specific driver requested? */
340 audioDriver = SDL_GetHint(SDL_HINT_AUDIODRIVER);
341
342 if (audioDriver == NULL) {
343 /* Loop over all available audio drivers */
344 iMax = SDL_GetNumAudioDrivers();
345 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
346 SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
347 } else {
348 /* A specific driver was requested for testing */
349 iMax = 1;
350 }
351 for (i = 0; i < iMax; i++) {
352 if (audioDriver == NULL) {
353 audioDriver = SDL_GetAudioDriver(i);
354 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
355 SDLTest_Assert(audioDriver != NULL, "Audio driver name is not NULL");
356 SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver); /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */
357
358 #if defined(__linux__)
359 if (DriverIsProblematic(audioDriver)) {
360 SDLTest_Log("Audio driver '%s' flagged as problematic: skipping pause/unpause test (set SDL_AUDIODRIVER=%s to force)", audioDriver, audioDriver);
361 audioDriver = NULL;
362 continue;
363 }
364 #endif
365 }
366
367 if (hint && SDL_strcmp(audioDriver, hint) != 0) {
368 continue;
369 }
370
371 /* Change specs */
372 for (j = 0; j < 2; j++) {
373
374 /* Call Init */
375 result = SDL_AudioInit(audioDriver);
376 SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
377 SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
378
379 result = SDL_GetNumAudioDevices(SDL_FALSE);
380 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(SDL_FALSE)");
381 SDLTest_AssertCheck(result >= 0, "Validate result value; expected: >=0 got: %d", result);
382 if (result <= 0) {
383 SDLTest_Log("No output devices for '%s': skipping pause/unpause test", audioDriver);
384 SDL_AudioQuit();
385 SDLTest_AssertPass("Call to SDL_AudioQuit()");
386 break;
387 }
388
389 /* Set spec */
390 SDL_memset(&desired, 0, sizeof(desired));
391 switch (j) {
392 case 0:
393 /* Set standard desired spec */
394 desired.freq = 22050;
395 desired.format = AUDIO_S16SYS;
396 desired.channels = 2;
397 desired.samples = 4096;
398 desired.callback = _audio_testCallback;
399 desired.userdata = NULL;
400 break;
401 case 1:
402 /* Set custom desired spec */
403 desired.freq = 48000;
404 desired.format = AUDIO_F32SYS;
405 desired.channels = 2;
406 desired.samples = 2048;
407 desired.callback = _audio_testCallback;
408 desired.userdata = NULL;
409 break;
410 }
411
412 /* Call Open */
413 result = SDL_OpenAudio(&desired, NULL);
414 SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL)", j);
415 SDLTest_AssertCheck(result == 0, "Verify return value; expected: 0 got: %d", result);
416
417 /* Start and stop audio multiple times */
418 for (l = 0; l < 3; l++) {
419 SDLTest_Log("Pause/Unpause iteration: %d", l + 1);
420
421 /* Reset callback counters */
422 _audio_testCallbackCounter = 0;
423 _audio_testCallbackLength = 0;
424
425 /* Un-pause audio to start playing (maybe multiple times) */
426 pause_on = 0;
427 for (k = 0; k <= j; k++) {
428 SDL_PauseAudio(pause_on);
429 SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k + 1);
430 }
431
432 /* Wait for callback */
433 totalDelay = 0;
434 do {
435 SDL_Delay(10);
436 totalDelay += 10;
437 } while (_audio_testCallbackCounter == 0 && totalDelay < 1000);
438 SDLTest_AssertCheck(_audio_testCallbackCounter > 0, "Verify callback counter; expected: >0 got: %d", _audio_testCallbackCounter);
439 SDLTest_AssertCheck(_audio_testCallbackLength > 0, "Verify callback length; expected: >0 got: %d", _audio_testCallbackLength);
440
441 /* Pause audio to stop playing (maybe multiple times) */
442 for (k = 0; k <= j; k++) {
443 pause_on = (k == 0) ? 1 : SDLTest_RandomIntegerInRange(99, 9999);
444 SDL_PauseAudio(pause_on);
445 SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k + 1);
446 }
447
448 /* Ensure callback is not called again */
449 originalCounter = _audio_testCallbackCounter;
450 SDL_Delay(totalDelay + 10);
451 SDLTest_AssertCheck(originalCounter == _audio_testCallbackCounter, "Verify callback counter; expected: %d, got: %d", originalCounter, _audio_testCallbackCounter);
452 }
453
454 /* Call Close */
455 SDL_CloseAudio();
456 SDLTest_AssertPass("Call to SDL_CloseAudio()");
457
458 /* Call Quit */
459 SDL_AudioQuit();
460 SDLTest_AssertPass("Call to SDL_AudioQuit()");
461
462 } /* spec loop */
463
464 audioDriver = NULL;
465
466 } /* driver loop */
467
468 /* Restart audio again */
469 _audioSetUp(NULL);
470
471 return TEST_COMPLETED;
472 }
473
474 /**
475 * \brief Enumerate and name available audio devices (output and capture).
476 *
477 * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
478 * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
479 */
480 int audio_enumerateAndNameAudioDevices(void)
481 {
482 int t, tt;
483 int i, n, nn;
484 const char *name, *nameAgain;
485
486 /* Iterate over types: t=0 output device, t=1 input/capture device */
487 for (t = 0; t < 2; t++) {
488
489 /* Get number of devices. */
490 n = SDL_GetNumAudioDevices(t);
491 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(%i)", t);
492 SDLTest_Log("Number of %s devices < 0, reported as %i", (t) ? "capture" : "output", n);
493 SDLTest_AssertCheck(n >= 0, "Validate result is >= 0, got: %i", n);
494
495 /* Variation of non-zero type */
496 if (t == 1) {
497 tt = t + SDLTest_RandomIntegerInRange(1, 10);
498 nn = SDL_GetNumAudioDevices(tt);
499 SDLTest_AssertCheck(n == nn, "Verify result from SDL_GetNumAudioDevices(%i), expected same number of audio devices %i, got %i", tt, n, nn);
500 nn = SDL_GetNumAudioDevices(-tt);
501 SDLTest_AssertCheck(n == nn, "Verify result from SDL_GetNumAudioDevices(%i), expected same number of audio devices %i, got %i", -tt, n, nn);
502 }
503
504 /* List devices. */
505 if (n > 0) {
506 for (i = 0; i < n; i++) {
507 name = SDL_GetAudioDeviceName(i, t);
508 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
509 SDLTest_AssertCheck(name != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, t);
510 if (name != NULL) {
511 SDLTest_AssertCheck(name[0] != '\0', "verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, t, name);
512 if (t == 1) {
513 /* Also try non-zero type */
514 tt = t + SDLTest_RandomIntegerInRange(1, 10);
515 nameAgain = SDL_GetAudioDeviceName(i, tt);
516 SDLTest_AssertCheck(nameAgain != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, tt);
517 if (nameAgain != NULL) {
518 SDLTest_AssertCheck(nameAgain[0] != '\0', "Verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, tt, nameAgain);
519 SDLTest_AssertCheck(SDL_strcmp(name, nameAgain) == 0,
520 "Verify SDL_GetAudioDeviceName(%i, %i) and SDL_GetAudioDeviceName(%i %i) return the same string",
521 i, t, i, tt);
522 }
523 }
524 }
525 }
526 }
527 }
528
529 return TEST_COMPLETED;
530 }
531
532 /**
533 * \brief Negative tests around enumeration and naming of audio devices.
534 *
535 * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
536 * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
537 */
538 int audio_enumerateAndNameAudioDevicesNegativeTests(void)
539 {
540 int t;
541 int i, j, no, nc;
542 const char *name;
543
544 /* Get number of devices. */
545 no = SDL_GetNumAudioDevices(0);
546 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
547 nc = SDL_GetNumAudioDevices(1);
548 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(1)");
549
550 /* Invalid device index when getting name */
551 for (t = 0; t < 2; t++) {
552 /* Negative device index */
553 i = SDLTest_RandomIntegerInRange(-10, -1);
554 name = SDL_GetAudioDeviceName(i, t);
555 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
556 SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result NULL, expected NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
557
558 /* Device index past range */
559 for (j = 0; j < 3; j++) {
560 i = (t) ? nc + j : no + j;
561 name = SDL_GetAudioDeviceName(i, t);
562 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
563 SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result, expected: NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
564 }
565
566 /* Capture index past capture range but within output range */
567 if ((no > 0) && (no > nc) && (t == 1)) {
568 i = no - 1;
569 name = SDL_GetAudioDeviceName(i, t);
570 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
571 SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result, expected: NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
572 }
573 }
574
575 return TEST_COMPLETED;
576 }
577
578 /**
579 * \brief Checks available audio driver names.
580 *
581 * \sa https://wiki.libsdl.org/SDL_GetNumAudioDrivers
582 * \sa https://wiki.libsdl.org/SDL_GetAudioDriver
583 */
584 int audio_printAudioDrivers(void)
585 {
586 int i, n;
587 const char *name;
588
589 /* Get number of drivers */
590 n = SDL_GetNumAudioDrivers();
591 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
592 SDLTest_AssertCheck(n >= 0, "Verify number of audio drivers >= 0, got: %i", n);
593
594 /* List drivers. */
595 if (n > 0) {
596 for (i = 0; i < n; i++) {
597 name = SDL_GetAudioDriver(i);
598 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%i)", i);
599 SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
600 if (name != NULL) {
601 SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
602 }
603 }
604 }
605
606 return TEST_COMPLETED;
607 }
608
609 /**
610 * \brief Checks current audio driver name with initialized audio.
611 *
612 * \sa https://wiki.libsdl.org/SDL_GetCurrentAudioDriver
613 */
614 int audio_printCurrentAudioDriver(void)
615 {
616 /* Check current audio driver */
617 const char *name = SDL_GetCurrentAudioDriver();
618 SDLTest_AssertPass("Call to SDL_GetCurrentAudioDriver()");
619 SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
620 if (name != NULL) {
621 SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
622 }
623
624 return TEST_COMPLETED;
625 }
626
627 /* Definition of all formats, channels, and frequencies used to test audio conversions */
628 const int _numAudioFormats = 18;
629 SDL_AudioFormat _audioFormats[] = { AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S16SYS, AUDIO_S16, AUDIO_U16LSB,
630 AUDIO_U16MSB, AUDIO_U16SYS, AUDIO_U16, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S32SYS, AUDIO_S32,
631 AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_F32SYS, AUDIO_F32 };
632 const char *_audioFormatsVerbose[] = { "AUDIO_S8", "AUDIO_U8", "AUDIO_S16LSB", "AUDIO_S16MSB", "AUDIO_S16SYS", "AUDIO_S16", "AUDIO_U16LSB",
633 "AUDIO_U16MSB", "AUDIO_U16SYS", "AUDIO_U16", "AUDIO_S32LSB", "AUDIO_S32MSB", "AUDIO_S32SYS", "AUDIO_S32",
634 "AUDIO_F32LSB", "AUDIO_F32MSB", "AUDIO_F32SYS", "AUDIO_F32" };
635 const int _numAudioChannels = 4;
636 Uint8 _audioChannels[] = { 1, 2, 4, 6 };
637 const int _numAudioFrequencies = 4;
638 int _audioFrequencies[] = { 11025, 22050, 44100, 48000 };
639
640 /**
641 * \brief Builds various audio conversion structures
642 *
643 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
644 */
645 int audio_buildAudioCVT(void)
646 {
647 int result;
648 SDL_AudioCVT cvt;
649 SDL_AudioSpec spec1;
650 SDL_AudioSpec spec2;
651 int i, ii, j, jj, k, kk;
652
653 /* No conversion needed */
654 spec1.format = AUDIO_S16LSB;
655 spec1.channels = 2;
656 spec1.freq = 22050;
657 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
658 spec1.format, spec1.channels, spec1.freq);
659 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec1)");
660 SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0, got: %i", result);
661
662 /* Typical conversion */
663 spec1.format = AUDIO_S8;
664 spec1.channels = 1;
665 spec1.freq = 22050;
666 spec2.format = AUDIO_S16LSB;
667 spec2.channels = 2;
668 spec2.freq = 44100;
669 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
670 spec2.format, spec2.channels, spec2.freq);
671 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
672 SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
673
674 /* All source conversions with random conversion targets, allow 'null' conversions */
675 for (i = 0; i < _numAudioFormats; i++) {
676 for (j = 0; j < _numAudioChannels; j++) {
677 for (k = 0; k < _numAudioFrequencies; k++) {
678 spec1.format = _audioFormats[i];
679 spec1.channels = _audioChannels[j];
680 spec1.freq = _audioFrequencies[k];
681 ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
682 jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
683 kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
684 spec2.format = _audioFormats[ii];
685 spec2.channels = _audioChannels[jj];
686 spec2.freq = _audioFrequencies[kk];
687 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
688 spec2.format, spec2.channels, spec2.freq);
689 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)",
690 i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
691 SDLTest_AssertCheck(result == 0 || result == 1, "Verify result value; expected: 0 or 1, got: %i", result);
692 if (result < 0) {
693 SDLTest_LogError("%s", SDL_GetError());
694 } else {
695 SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
696 }
697 }
698 }
699 }
700
701 return TEST_COMPLETED;
702 }
703
704 /**
705 * \brief Checkes calls with invalid input to SDL_BuildAudioCVT
706 *
707 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
708 */
709 int audio_buildAudioCVTNegative(void)
710 {
711 const char *expectedError = "Parameter 'cvt' is invalid";
712 const char *error;
713 int result;
714 SDL_AudioCVT cvt;
715 SDL_AudioSpec spec1;
716 SDL_AudioSpec spec2;
717 int i;
718 char message[256];
719
720 /* Valid format */
721 spec1.format = AUDIO_S8;
722 spec1.channels = 1;
723 spec1.freq = 22050;
724 spec2.format = AUDIO_S16LSB;
725 spec2.channels = 2;
726 spec2.freq = 44100;
727
728 SDL_ClearError();
729 SDLTest_AssertPass("Call to SDL_ClearError()");
730
731 /* NULL input for CVT buffer */
732 result = SDL_BuildAudioCVT((SDL_AudioCVT *)NULL, spec1.format, spec1.channels, spec1.freq,
733 spec2.format, spec2.channels, spec2.freq);
734 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(NULL,...)");
735 SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
736 error = SDL_GetError();
737 SDLTest_AssertPass("Call to SDL_GetError()");
738 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
739 if (error != NULL) {
740 SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
741 "Validate error message, expected: '%s', got: '%s'", expectedError, error);
742 }
743
744 /* Invalid conversions */
745 for (i = 1; i < 64; i++) {
746 /* Valid format to start with */
747 spec1.format = AUDIO_S8;
748 spec1.channels = 1;
749 spec1.freq = 22050;
750 spec2.format = AUDIO_S16LSB;
751 spec2.channels = 2;
752 spec2.freq = 44100;
753
754 SDL_ClearError();
755 SDLTest_AssertPass("Call to SDL_ClearError()");
756
757 /* Set various invalid format inputs */
758 SDL_strlcpy(message, "Invalid: ", 256);
759 if (i & 1) {
760 SDL_strlcat(message, " spec1.format", 256);
761 spec1.format = 0;
762 }
763 if (i & 2) {
764 SDL_strlcat(message, " spec1.channels", 256);
765 spec1.channels = 0;
766 }
767 if (i & 4) {
768 SDL_strlcat(message, " spec1.freq", 256);
769 spec1.freq = 0;
770 }
771 if (i & 8) {
772 SDL_strlcat(message, " spec2.format", 256);
773 spec2.format = 0;
774 }
775 if (i & 16) {
776 SDL_strlcat(message, " spec2.channels", 256);
777 spec2.channels = 0;
778 }
779 if (i & 32) {
780 SDL_strlcat(message, " spec2.freq", 256);
781 spec2.freq = 0;
782 }
783 SDLTest_Log("%s", message);
784 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
785 spec2.format, spec2.channels, spec2.freq);
786 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
787 SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
788 error = SDL_GetError();
789 SDLTest_AssertPass("Call to SDL_GetError()");
790 SDLTest_AssertCheck(error != NULL && error[0] != '\0', "Validate that error message was not NULL or empty");
791 }
792
793 SDL_ClearError();
794 SDLTest_AssertPass("Call to SDL_ClearError()");
795
796 return TEST_COMPLETED;
797 }
798
799 /**
800 * \brief Checks current audio status.
801 *
802 * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
803 */
804 int audio_getAudioStatus(void)
805 {
806 SDL_AudioStatus result;
807
808 /* Check current audio status */
809 result = SDL_GetAudioStatus();
810 SDLTest_AssertPass("Call to SDL_GetAudioStatus()");
811 SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
812 "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
813 SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
814
815 return TEST_COMPLETED;
816 }
817
818 /**
819 * \brief Opens, checks current audio status, and closes a device.
820 *
821 * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
822 */
823 int audio_openCloseAndGetAudioStatus(void)
824 {
825 SDL_AudioStatus result;
826 int i;
827 int count;
828 const char *device;
829 SDL_AudioDeviceID id;
830 SDL_AudioSpec desired, obtained;
831
832 /* Get number of devices. */
833 count = SDL_GetNumAudioDevices(0);
834 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
835 if (count > 0) {
836 for (i = 0; i < count; i++) {
837 /* Get device name */
838 device = SDL_GetAudioDeviceName(i, 0);
839 SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
840 SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
841 if (device == NULL) {
842 return TEST_ABORTED;
843 }
844
845 /* Set standard desired spec */
846 desired.freq = 22050;
847 desired.format = AUDIO_S16SYS;
848 desired.channels = 2;
849 desired.samples = 4096;
850 desired.callback = _audio_testCallback;
851 desired.userdata = NULL;
852
853 /* Open device */
854 id = SDL_OpenAudioDevice(device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
855 SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
856 SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %" SDL_PRIu32, id);
857 if (id > 1) {
858
859 /* Check device audio status */
860 result = SDL_GetAudioDeviceStatus(id);
861 SDLTest_AssertPass("Call to SDL_GetAudioDeviceStatus()");
862 SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
863 "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
864 SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
865
866 /* Close device again */
867 SDL_CloseAudioDevice(id);
868 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
869 }
870 }
871 } else {
872 SDLTest_Log("No devices to test with");
873 }
874
875 return TEST_COMPLETED;
876 }
877
878 /**
879 * \brief Locks and unlocks open audio device.
880 *
881 * \sa https://wiki.libsdl.org/SDL_LockAudioDevice
882 * \sa https://wiki.libsdl.org/SDL_UnlockAudioDevice
883 */
884 int audio_lockUnlockOpenAudioDevice(void)
885 {
886 int i;
887 int count;
888 const char *device;
889 SDL_AudioDeviceID id;
890 SDL_AudioSpec desired, obtained;
891
892 /* Get number of devices. */
893 count = SDL_GetNumAudioDevices(0);
894 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
895 if (count > 0) {
896 for (i = 0; i < count; i++) {
897 /* Get device name */
898 device = SDL_GetAudioDeviceName(i, 0);
899 SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
900 SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
901 if (device == NULL) {
902 return TEST_ABORTED;
903 }
904
905 /* Set standard desired spec */
906 desired.freq = 22050;
907 desired.format = AUDIO_S16SYS;
908 desired.channels = 2;
909 desired.samples = 4096;
910 desired.callback = _audio_testCallback;
911 desired.userdata = NULL;
912
913 /* Open device */
914 id = SDL_OpenAudioDevice(device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
915 SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
916 SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %" SDL_PRIu32, id);
917 if (id > 1) {
918 /* Lock to protect callback */
919 SDL_LockAudioDevice(id);
920 SDLTest_AssertPass("SDL_LockAudioDevice(%" SDL_PRIu32 ")", id);
921
922 /* Simulate callback processing */
923 SDL_Delay(10);
924 SDLTest_Log("Simulate callback processing - delay");
925
926 /* Unlock again */
927 SDL_UnlockAudioDevice(id);
928 SDLTest_AssertPass("SDL_UnlockAudioDevice(%" SDL_PRIu32 ")", id);
929
930 /* Close device again */
931 SDL_CloseAudioDevice(id);
932 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
933 }
934 }
935 } else {
936 SDLTest_Log("No devices to test with");
937 }
938
939 return TEST_COMPLETED;
940 }
941
942 /**
943 * \brief Convert audio using various conversion structures
944 *
945 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
946 * \sa https://wiki.libsdl.org/SDL_ConvertAudio
947 */
948 int audio_convertAudio(void)
949 {
950 int result;
951 SDL_AudioCVT cvt;
952 SDL_AudioSpec spec1;
953 SDL_AudioSpec spec2;
954 int c;
955 char message[128];
956 int i, ii, j, jj, k, kk, l, ll;
957
958 /* Iterate over bitmask that determines which parameters are modified in the conversion */
959 for (c = 1; c < 8; c++) {
960 SDL_strlcpy(message, "Changing:", 128);
961 if (c & 1) {
962 SDL_strlcat(message, " Format", 128);
963 }
964 if (c & 2) {
965 SDL_strlcat(message, " Channels", 128);
966 }
967 if (c & 4) {
968 SDL_strlcat(message, " Frequencies", 128);
969 }
970 SDLTest_Log("%s", message);
971 /* All source conversions with random conversion targets */
972 for (i = 0; i < _numAudioFormats; i++) {
973 for (j = 0; j < _numAudioChannels; j++) {
974 for (k = 0; k < _numAudioFrequencies; k++) {
975 spec1.format = _audioFormats[i];
976 spec1.channels = _audioChannels[j];
977 spec1.freq = _audioFrequencies[k];
978
979 /* Ensure we have a different target format */
980 do {
981 if (c & 1) {
982 ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
983 } else {
984 ii = 1;
985 }
986 if (c & 2) {
987 jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
988 } else {
989 jj = j;
990 }
991 if (c & 4) {
992 kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
993 } else {
994 kk = k;
995 }
996 } while ((i == ii) && (j == jj) && (k == kk));
997 spec2.format = _audioFormats[ii];
998 spec2.channels = _audioChannels[jj];
999 spec2.freq = _audioFrequencies[kk];
1000
1001 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
1002 spec2.format, spec2.channels, spec2.freq);
1003 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)",
1004 i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
1005 SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
1006 if (result != 1) {
1007 SDLTest_LogError("%s", SDL_GetError());
1008 } else {
1009 SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
1010 if (cvt.len_mult < 1) {
1011 return TEST_ABORTED;
1012 }
1013
1014 /* Create some random data to convert */
1015 l = 64;
1016 ll = l * cvt.len_mult;
1017 SDLTest_Log("Creating dummy sample buffer of %i length (%i bytes)", l, ll);
1018 cvt.len = l;
1019 cvt.buf = (Uint8 *)SDL_malloc(ll);
1020 SDLTest_AssertCheck(cvt.buf != NULL, "Check data buffer to convert is not NULL");
1021 if (cvt.buf == NULL) {
1022 return TEST_ABORTED;
1023 }
1024
1025 /* Convert the data */
1026 result = SDL_ConvertAudio(&cvt);
1027 SDLTest_AssertPass("Call to SDL_ConvertAudio()");
1028 SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0; got: %i", result);
1029 SDLTest_AssertCheck(cvt.buf != NULL, "Verify conversion buffer is not NULL");
1030 SDLTest_AssertCheck(cvt.len_ratio > 0.0, "Verify conversion length ratio; expected: >0; got: %f", cvt.len_ratio);
1031
1032 /* Free converted buffer */
1033 SDL_free(cvt.buf);
1034 cvt.buf = NULL;
1035 }
1036 }
1037 }
1038 }
1039 }
1040
1041 return TEST_COMPLETED;
1042 }
1043
1044 /**
1045 * \brief Opens, checks current connected status, and closes a device.
1046 *
1047 * \sa https://wiki.libsdl.org/SDL_AudioDeviceConnected
1048 */
1049 int audio_openCloseAudioDeviceConnected(void)
1050 {
1051 int result = -1;
1052 int i;
1053 int count;
1054 const char *device;
1055 SDL_AudioDeviceID id;
1056 SDL_AudioSpec desired, obtained;
1057
1058 /* Get number of devices. */
1059 count = SDL_GetNumAudioDevices(0);
1060 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
1061 if (count > 0) {
1062 for (i = 0; i < count; i++) {
1063 /* Get device name */
1064 device = SDL_GetAudioDeviceName(i, 0);
1065 SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
1066 SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
1067 if (device == NULL) {
1068 return TEST_ABORTED;
1069 }
1070
1071 /* Set standard desired spec */
1072 desired.freq = 22050;
1073 desired.format = AUDIO_S16SYS;
1074 desired.channels = 2;
1075 desired.samples = 4096;
1076 desired.callback = _audio_testCallback;
1077 desired.userdata = NULL;
1078
1079 /* Open device */
1080 id = SDL_OpenAudioDevice(device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
1081 SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
1082 SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >1, got: %" SDL_PRIu32, id);
1083 if (id > 1) {
1084
1085 /* TODO: enable test code when function is available in SDL2 */
1086
1087 #ifdef AUDIODEVICECONNECTED_DEFINED
1088 /* Get connected status */
1089 result = SDL_AudioDeviceConnected(id);
1090 SDLTest_AssertPass("Call to SDL_AudioDeviceConnected()");
1091 #endif
1092 SDLTest_AssertCheck(result == 1, "Verify returned value; expected: 1; got: %i", result);
1093
1094 /* Close device again */
1095 SDL_CloseAudioDevice(id);
1096 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
1097 }
1098 }
1099 } else {
1100 SDLTest_Log("No devices to test with");
1101 }
1102
1103 return TEST_COMPLETED;
1104 }
1105
1106 static double sine_wave_sample(const Sint64 idx, const Sint64 rate, const Sint64 freq, const double phase)
1107 {
1108 /* Using integer modulo to avoid precision loss caused by large floating
1109 * point numbers. Sint64 is needed for the large integer multiplication.
1110 * The integers are assumed to be non-negative so that modulo is always
1111 * non-negative.
1112 * sin(i / rate * freq * 2 * M_PI + phase)
1113 * = sin(mod(i / rate * freq, 1) * 2 * M_PI + phase)
1114 * = sin(mod(i * freq, rate) / rate * 2 * M_PI + phase) */
1115 return SDL_sin(((double) (idx * freq % rate)) / ((double) rate) * (M_PI * 2) + phase);
1116 }
1117
1118 /**
1119 * \brief Check signal-to-noise ratio and maximum error of audio resampling.
1120 *
1121 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
1122 * \sa https://wiki.libsdl.org/SDL_ConvertAudio
1123 */
1124 int audio_resampleLoss(void)
1125 {
1126 /* Note: always test long input time (>= 5s from experience) in some test
1127 * cases because an improper implementation may suffer from low resampling
1128 * precision with long input due to e.g. doing subtraction with large floats. */
1129 struct test_spec_t {
1130 int time;
1131 int freq;
1132 double phase;
1133 int rate_in;
1134 int rate_out;
1135 double signal_to_noise;
1136 double max_error;
1137 } test_specs[] = {
1138 { 50, 440, 0, 44100, 48000, 60, 0.0025 },
1139 { 50, 5000, M_PI / 2, 20000, 10000, 65, 0.0010 },
1140 { 0 }
1141 };
1142
1143 int spec_idx = 0;
1144
1145 for (spec_idx = 0; test_specs[spec_idx].time > 0; ++spec_idx) {
1146 const struct test_spec_t *spec = &test_specs[spec_idx];
1147 const int frames_in = spec->time * spec->rate_in;
1148 const int frames_target = spec->time * spec->rate_out;
1149 const int len_in = frames_in * (int)sizeof(float);
1150 const int len_target = frames_target * (int)sizeof(float);
1151
1152 Uint64 tick_beg = 0;
1153 Uint64 tick_end = 0;
1154 SDL_AudioCVT cvt;
1155 int i = 0;
1156 int ret = 0;
1157 double max_error = 0;
1158 double sum_squared_error = 0;
1159 double sum_squared_value = 0;
1160 double signal_to_noise = 0;
1161
1162 SDLTest_AssertPass("Test resampling of %i s %i Hz %f phase sine wave from sampling rate of %i Hz to %i Hz",
1163 spec->time, spec->freq, spec->phase, spec->rate_in, spec->rate_out);
1164
1165 ret = SDL_BuildAudioCVT(&cvt, AUDIO_F32SYS, 1, spec->rate_in, AUDIO_F32SYS, 1, spec->rate_out);
1166 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(&cvt, AUDIO_F32SYS, 1, %i, AUDIO_F32SYS, 1, %i)", spec->rate_in, spec->rate_out);
1167 SDLTest_AssertCheck(ret == 1, "Expected SDL_BuildAudioCVT to succeed and conversion to be needed.");
1168 if (ret != 1) {
1169 return TEST_ABORTED;
1170 }
1171
1172 cvt.buf = (Uint8 *)SDL_malloc(len_in * cvt.len_mult);
1173 SDLTest_AssertCheck(cvt.buf != NULL, "Expected input buffer to be created.");
1174 if (cvt.buf == NULL) {
1175 return TEST_ABORTED;
1176 }
1177
1178 cvt.len = len_in;
1179 for (i = 0; i < frames_in; ++i) {
1180 *(((float *) cvt.buf) + i) = (float)sine_wave_sample(i, spec->rate_in, spec->freq, spec->phase);
1181 }
1182
1183 tick_beg = SDL_GetPerformanceCounter();
1184 ret = SDL_ConvertAudio(&cvt);
1185 tick_end = SDL_GetPerformanceCounter();
1186 SDLTest_AssertPass("Call to SDL_ConvertAudio(&cvt)");
1187 SDLTest_AssertCheck(ret == 0, "Expected SDL_ConvertAudio to succeed.");
1188 SDLTest_AssertCheck(cvt.len_cvt == len_target, "Expected output length %i, got %i.", len_target, cvt.len_cvt);
1189 if (ret != 0 || cvt.len_cvt != len_target) {
1190 SDL_free(cvt.buf);
1191 return TEST_ABORTED;
1192 }
1193 SDLTest_Log("Resampling used %f seconds.", ((double) (tick_end - tick_beg)) / SDL_GetPerformanceFrequency());
1194
1195 for (i = 0; i < frames_target; ++i) {
1196 const float output = *(((float *) cvt.buf) + i);
1197 const double target = sine_wave_sample(i, spec->rate_out, spec->freq, spec->phase);
1198 const double error = SDL_fabs(target - output);
1199 max_error = SDL_max(max_error, error);
1200 sum_squared_error += error * error;
1201 sum_squared_value += target * target;
1202 }
1203 SDL_free(cvt.buf);
1204 signal_to_noise = 10 * SDL_log10(sum_squared_value / sum_squared_error); /* decibel */
1205 SDLTest_AssertCheck(isfinite(sum_squared_value), "Sum of squared target should be finite.");
1206 SDLTest_AssertCheck(isfinite(sum_squared_error), "Sum of squared error should be finite.");
1207 /* Infinity is theoretically possible when there is very little to no noise */
1208 SDLTest_AssertCheck(!isnan(signal_to_noise), "Signal-to-noise ratio should not be NaN.");
1209 SDLTest_AssertCheck(isfinite(max_error), "Maximum conversion error should be finite.");
1210 SDLTest_AssertCheck(signal_to_noise >= spec->signal_to_noise, "Conversion signal-to-noise ratio %f dB should be no less than %f dB.",
1211 signal_to_noise, spec->signal_to_noise);
1212 SDLTest_AssertCheck(max_error <= spec->max_error, "Maximum conversion error %f should be no more than %f.",
1213 max_error, spec->max_error);
1214 }
1215
1216 return TEST_COMPLETED;
1217 }
1218
1219 /* ================= Test Case References ================== */
1220
1221 /* Audio test cases */
1222 static const SDLTest_TestCaseReference audioTest1 = {
1223 (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevices, "audio_enumerateAndNameAudioDevices", "Enumerate and name available audio devices (output and capture)", TEST_ENABLED
1224 };
1225
1226 static const SDLTest_TestCaseReference audioTest2 = {
1227 (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevicesNegativeTests, "audio_enumerateAndNameAudioDevicesNegativeTests", "Negative tests around enumeration and naming of audio devices.", TEST_ENABLED
1228 };
1229
1230 static const SDLTest_TestCaseReference audioTest3 = {
1231 (SDLTest_TestCaseFp)audio_printAudioDrivers, "audio_printAudioDrivers", "Checks available audio driver names.", TEST_ENABLED
1232 };
1233
1234 static const SDLTest_TestCaseReference audioTest4 = {
1235 (SDLTest_TestCaseFp)audio_printCurrentAudioDriver, "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED
1236 };
1237
1238 static const SDLTest_TestCaseReference audioTest5 = {
1239 (SDLTest_TestCaseFp)audio_buildAudioCVT, "audio_buildAudioCVT", "Builds various audio conversion structures.", TEST_ENABLED
1240 };
1241
1242 static const SDLTest_TestCaseReference audioTest6 = {
1243 (SDLTest_TestCaseFp)audio_buildAudioCVTNegative, "audio_buildAudioCVTNegative", "Checks calls with invalid input to SDL_BuildAudioCVT", TEST_ENABLED
1244 };
1245
1246 static const SDLTest_TestCaseReference audioTest7 = {
1247 (SDLTest_TestCaseFp)audio_getAudioStatus, "audio_getAudioStatus", "Checks current audio status.", TEST_ENABLED
1248 };
1249
1250 static const SDLTest_TestCaseReference audioTest8 = {
1251 (SDLTest_TestCaseFp)audio_openCloseAndGetAudioStatus, "audio_openCloseAndGetAudioStatus", "Opens and closes audio device and get audio status.", TEST_ENABLED
1252 };
1253
1254 static const SDLTest_TestCaseReference audioTest9 = {
1255 (SDLTest_TestCaseFp)audio_lockUnlockOpenAudioDevice, "audio_lockUnlockOpenAudioDevice", "Locks and unlocks an open audio device.", TEST_ENABLED
1256 };
1257
1258 /* TODO: enable test when SDL_ConvertAudio segfaults on cygwin have been fixed. */
1259 /* For debugging, test case can be run manually using --filter audio_convertAudio */
1260
1261 static const SDLTest_TestCaseReference audioTest10 = {
1262 (SDLTest_TestCaseFp)audio_convertAudio, "audio_convertAudio", "Convert audio using available formats.", TEST_DISABLED
1263 };
1264
1265 /* TODO: enable test when SDL_AudioDeviceConnected has been implemented. */
1266
1267 static const SDLTest_TestCaseReference audioTest11 = {
1268 (SDLTest_TestCaseFp)audio_openCloseAudioDeviceConnected, "audio_openCloseAudioDeviceConnected", "Opens and closes audio device and get connected status.", TEST_DISABLED
1269 };
1270
1271 static const SDLTest_TestCaseReference audioTest12 = {
1272 (SDLTest_TestCaseFp)audio_quitInitAudioSubSystem, "audio_quitInitAudioSubSystem", "Quit and re-init audio subsystem.", TEST_ENABLED
1273 };
1274
1275 static const SDLTest_TestCaseReference audioTest13 = {
1276 (SDLTest_TestCaseFp)audio_initQuitAudio, "audio_initQuitAudio", "Init and quit audio drivers directly.", TEST_ENABLED
1277 };
1278
1279 static const SDLTest_TestCaseReference audioTest14 = {
1280 (SDLTest_TestCaseFp)audio_initOpenCloseQuitAudio, "audio_initOpenCloseQuitAudio", "Cycle through init, open, close and quit with various audio specs.", TEST_ENABLED
1281 };
1282
1283 static const SDLTest_TestCaseReference audioTest15 = {
1284 (SDLTest_TestCaseFp)audio_pauseUnpauseAudio, "audio_pauseUnpauseAudio", "Pause and Unpause audio for various audio specs while testing callback.", TEST_ENABLED
1285 };
1286
1287 static const SDLTest_TestCaseReference audioTest16 = {
1288 (SDLTest_TestCaseFp)audio_resampleLoss, "audio_resampleLoss", "Check signal-to-noise ratio and maximum error of audio resampling.", TEST_ENABLED
1289 };
1290
1291 /* Sequence of Audio test cases */
1292 static const SDLTest_TestCaseReference *audioTests[] = {
1293 &audioTest1, &audioTest2, &audioTest3, &audioTest4, &audioTest5, &audioTest6,
1294 &audioTest7, &audioTest8, &audioTest9, &audioTest10, &audioTest11,
1295 &audioTest12, &audioTest13, &audioTest14, &audioTest15, &audioTest16, NULL
1296 };
1297
1298 /* Audio test suite (global) */
1299 SDLTest_TestSuiteReference audioTestSuite = {
1300 "Audio",
1301 _audioSetUp,
1302 audioTests,
1303 _audioTearDown
1304 };