2 * Original code: automated SDL audio test written by Edgar Simo "bobbens"
3 * New/updated tests: aschiffler at ferzkopp dot net
6 /* quiet windows compiler warnings */
7 #define _CRT_SECURE_NO_WARNINGS
15 /* ================= Test Case Implementation ================== */
20 _audioSetUp(void *arg)
22 /* Start SDL audio subsystem */
23 int ret = SDL_InitSubSystem( SDL_INIT_AUDIO );
24 SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO)");
25 SDLTest_AssertCheck(ret==0, "Check result from SDL_InitSubSystem(SDL_INIT_AUDIO)");
27 SDLTest_LogError("%s", SDL_GetError());
32 _audioTearDown(void *arg)
34 /* Remove a possibly created file from SDL disk writer audio driver; ignore errors */
35 remove("sdlaudio.raw");
37 SDLTest_AssertPass("Cleanup of test files completed");
41 /* Global counter for callback invocation */
42 int _audio_testCallbackCounter;
44 /* Global accumulator for total callback length */
45 int _audio_testCallbackLength;
48 /* Test callback function */
49 void _audio_testCallback(void *userdata, Uint8 *stream, int len)
51 /* track that callback was called */
52 _audio_testCallbackCounter++;
53 _audio_testCallbackLength += len;
57 /* Test case functions */
60 * \brief Stop and restart audio subsystem
62 * \sa https://wiki.libsdl.org/SDL_QuitSubSystem
63 * \sa https://wiki.libsdl.org/SDL_InitSubSystem
65 int audio_quitInitAudioSubSystem()
67 /* Stop SDL audio subsystem */
68 SDL_QuitSubSystem( SDL_INIT_AUDIO );
69 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
71 /* Restart audio again */
74 return TEST_COMPLETED;
78 * \brief Start and stop audio directly
80 * \sa https://wiki.libsdl.org/SDL_InitAudio
81 * \sa https://wiki.libsdl.org/SDL_QuitAudio
83 int audio_initQuitAudio()
87 const char* audioDriver;
89 /* Stop SDL audio subsystem */
90 SDL_QuitSubSystem( SDL_INIT_AUDIO );
91 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
93 /* Loop over all available audio drivers */
94 iMax = SDL_GetNumAudioDrivers();
95 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
96 SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
97 for (i = 0; i < iMax; i++) {
98 audioDriver = SDL_GetAudioDriver(i);
99 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
100 SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
101 SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
104 result = SDL_AudioInit(audioDriver);
105 SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
106 SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
110 SDLTest_AssertPass("Call to SDL_AudioQuit()");
113 /* NULL driver specification */
117 result = SDL_AudioInit(audioDriver);
118 SDLTest_AssertPass("Call to SDL_AudioInit(NULL)");
119 SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
123 SDLTest_AssertPass("Call to SDL_AudioQuit()");
125 /* Restart audio again */
128 return TEST_COMPLETED;
132 * \brief Start, open, close and stop audio
134 * \sa https://wiki.libsdl.org/SDL_InitAudio
135 * \sa https://wiki.libsdl.org/SDL_OpenAudio
136 * \sa https://wiki.libsdl.org/SDL_CloseAudio
137 * \sa https://wiki.libsdl.org/SDL_QuitAudio
139 int audio_initOpenCloseQuitAudio()
141 int result, expectedResult;
143 const char* audioDriver;
144 SDL_AudioSpec desired;
146 /* Stop SDL audio subsystem */
147 SDL_QuitSubSystem( SDL_INIT_AUDIO );
148 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
150 /* Loop over all available audio drivers */
151 iMax = SDL_GetNumAudioDrivers();
152 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
153 SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
154 for (i = 0; i < iMax; i++) {
155 audioDriver = SDL_GetAudioDriver(i);
156 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
157 SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
158 SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
161 for (j = 0; j < 2; j++) {
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);
169 SDL_memset(&desired, 0, sizeof(desired));
172 /* Set standard desired spec */
173 desired.freq = 22050;
174 desired.format = AUDIO_S16SYS;
175 desired.channels = 2;
176 desired.samples = 4096;
177 desired.callback = _audio_testCallback;
178 desired.userdata = NULL;
181 /* Set custom desired spec */
182 desired.freq = 48000;
183 desired.format = AUDIO_F32SYS;
184 desired.channels = 2;
185 desired.samples = 2048;
186 desired.callback = _audio_testCallback;
187 desired.userdata = NULL;
191 /* Call Open (maybe multiple times) */
192 for (k=0; k <= j; k++) {
193 result = SDL_OpenAudio(&desired, NULL);
194 SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL), call %d", j, k+1);
195 expectedResult = (k==0) ? 0 : -1;
196 SDLTest_AssertCheck(result == expectedResult, "Verify return value; expected: %d, got: %d", expectedResult, result);
199 /* Call Close (maybe multiple times) */
200 for (k=0; k <= j; k++) {
202 SDLTest_AssertPass("Call to SDL_CloseAudio(), call %d", k+1);
205 /* Call Quit (maybe multiple times) */
206 for (k=0; k <= j; k++) {
208 SDLTest_AssertPass("Call to SDL_AudioQuit(), call %d", k+1);
214 /* Restart audio again */
217 return TEST_COMPLETED;
221 * \brief Pause and unpause audio
223 * \sa https://wiki.libsdl.org/SDL_PauseAudio
225 int audio_pauseUnpauseAudio()
228 int i, iMax, j, k, l;
232 const char* audioDriver;
233 SDL_AudioSpec desired;
235 /* Stop SDL audio subsystem */
236 SDL_QuitSubSystem( SDL_INIT_AUDIO );
237 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
239 /* Loop over all available audio drivers */
240 iMax = SDL_GetNumAudioDrivers();
241 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
242 SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
243 for (i = 0; i < iMax; i++) {
244 audioDriver = SDL_GetAudioDriver(i);
245 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
246 SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
247 SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
250 for (j = 0; j < 2; j++) {
253 result = SDL_AudioInit(audioDriver);
254 SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
255 SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
258 SDL_memset(&desired, 0, sizeof(desired));
261 /* Set standard desired spec */
262 desired.freq = 22050;
263 desired.format = AUDIO_S16SYS;
264 desired.channels = 2;
265 desired.samples = 4096;
266 desired.callback = _audio_testCallback;
267 desired.userdata = NULL;
270 /* Set custom desired spec */
271 desired.freq = 48000;
272 desired.format = AUDIO_F32SYS;
273 desired.channels = 2;
274 desired.samples = 2048;
275 desired.callback = _audio_testCallback;
276 desired.userdata = NULL;
281 result = SDL_OpenAudio(&desired, NULL);
282 SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL)", j);
283 SDLTest_AssertCheck(result == 0, "Verify return value; expected: 0 got: %d", result);
285 /* Start and stop audio multiple times */
286 for (l=0; l<3; l++) {
287 SDLTest_Log("Pause/Unpause iteration: %d", l+1);
289 /* Reset callback counters */
290 _audio_testCallbackCounter = 0;
291 _audio_testCallbackLength = 0;
293 /* Un-pause audio to start playing (maybe multiple times) */
295 for (k=0; k <= j; k++) {
296 SDL_PauseAudio(pause_on);
297 SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k+1);
300 /* Wait for callback */
306 while (_audio_testCallbackCounter == 0 && totalDelay < 1000);
307 SDLTest_AssertCheck(_audio_testCallbackCounter > 0, "Verify callback counter; expected: >0 got: %d", _audio_testCallbackCounter);
308 SDLTest_AssertCheck(_audio_testCallbackLength > 0, "Verify callback length; expected: >0 got: %d", _audio_testCallbackLength);
310 /* Pause audio to stop playing (maybe multiple times) */
311 for (k=0; k <= j; k++) {
312 pause_on = (k==0) ? 1 : SDLTest_RandomIntegerInRange(99, 9999);
313 SDL_PauseAudio(pause_on);
314 SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k+1);
317 /* Ensure callback is not called again */
318 originalCounter = _audio_testCallbackCounter;
319 SDL_Delay(totalDelay + 10);
320 SDLTest_AssertCheck(originalCounter == _audio_testCallbackCounter, "Verify callback counter; expected: %d, got: %d", originalCounter, _audio_testCallbackCounter);
325 SDLTest_AssertPass("Call to SDL_CloseAudio()");
329 SDLTest_AssertPass("Call to SDL_AudioQuit()");
334 /* Restart audio again */
337 return TEST_COMPLETED;
341 * \brief Enumerate and name available audio devices (output and capture).
343 * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
344 * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
346 int audio_enumerateAndNameAudioDevices()
350 const char *name, *nameAgain;
352 /* Iterate over types: t=0 output device, t=1 input/capture device */
353 for (t=0; t<2; t++) {
355 /* Get number of devices. */
356 n = SDL_GetNumAudioDevices(t);
357 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(%i)", t);
358 SDLTest_Log("Number of %s devices < 0, reported as %i", (t) ? "capture" : "output", n);
359 SDLTest_AssertCheck(n >= 0, "Validate result is >= 0, got: %i", n);
361 /* Variation of non-zero type */
363 tt = t + SDLTest_RandomIntegerInRange(1,10);
364 nn = SDL_GetNumAudioDevices(tt);
365 SDLTest_AssertCheck(n==nn, "Verify result from SDL_GetNumAudioDevices(%i), expected same number of audio devices %i, got %i", tt, n, nn);
366 nn = SDL_GetNumAudioDevices(-tt);
367 SDLTest_AssertCheck(n==nn, "Verify result from SDL_GetNumAudioDevices(%i), expected same number of audio devices %i, got %i", -tt, n, nn);
372 for (i=0; i<n; i++) {
373 name = SDL_GetAudioDeviceName(i, t);
374 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
375 SDLTest_AssertCheck(name != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, t);
377 SDLTest_AssertCheck(name[0] != '\0', "verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, t, name);
379 /* Also try non-zero type */
380 tt = t + SDLTest_RandomIntegerInRange(1,10);
381 nameAgain = SDL_GetAudioDeviceName(i, tt);
382 SDLTest_AssertCheck(nameAgain != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, tt);
383 if (nameAgain != NULL) {
384 SDLTest_AssertCheck(nameAgain[0] != '\0', "Verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, tt, nameAgain);
385 SDLTest_AssertCheck(SDL_strcmp(name, nameAgain)==0,
386 "Verify SDL_GetAudioDeviceName(%i, %i) and SDL_GetAudioDeviceName(%i %i) return the same string",
395 return TEST_COMPLETED;
399 * \brief Negative tests around enumeration and naming of audio devices.
401 * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
402 * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
404 int audio_enumerateAndNameAudioDevicesNegativeTests()
410 /* Get number of devices. */
411 no = SDL_GetNumAudioDevices(0);
412 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
413 nc = SDL_GetNumAudioDevices(1);
414 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(1)");
416 /* Invalid device index when getting name */
417 for (t=0; t<2; t++) {
418 /* Negative device index */
419 i = SDLTest_RandomIntegerInRange(-10,-1);
420 name = SDL_GetAudioDeviceName(i, t);
421 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
422 SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result NULL, expected NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
424 /* Device index past range */
425 for (j=0; j<3; j++) {
426 i = (t) ? nc+j : no+j;
427 name = SDL_GetAudioDeviceName(i, t);
428 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
429 SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result, expected: NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
432 /* Capture index past capture range but within output range */
433 if ((no>0) && (no>nc) && (t==1)) {
435 name = SDL_GetAudioDeviceName(i, t);
436 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
437 SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result, expected: NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
441 return TEST_COMPLETED;
446 * \brief Checks available audio driver names.
448 * \sa https://wiki.libsdl.org/SDL_GetNumAudioDrivers
449 * \sa https://wiki.libsdl.org/SDL_GetAudioDriver
451 int audio_printAudioDrivers()
456 /* Get number of drivers */
457 n = SDL_GetNumAudioDrivers();
458 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
459 SDLTest_AssertCheck(n>=0, "Verify number of audio drivers >= 0, got: %i", n);
464 for (i=0; i<n; i++) {
465 name = SDL_GetAudioDriver(i);
466 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%i)", i);
467 SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
469 SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
474 return TEST_COMPLETED;
479 * \brief Checks current audio driver name with initialized audio.
481 * \sa https://wiki.libsdl.org/SDL_GetCurrentAudioDriver
483 int audio_printCurrentAudioDriver()
485 /* Check current audio driver */
486 const char *name = SDL_GetCurrentAudioDriver();
487 SDLTest_AssertPass("Call to SDL_GetCurrentAudioDriver()");
488 SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
490 SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
493 return TEST_COMPLETED;
496 /* Definition of all formats, channels, and frequencies used to test audio conversions */
497 const int _numAudioFormats = 18;
498 SDL_AudioFormat _audioFormats[] = { AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S16SYS, AUDIO_S16, AUDIO_U16LSB,
499 AUDIO_U16MSB, AUDIO_U16SYS, AUDIO_U16, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S32SYS, AUDIO_S32,
500 AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_F32SYS, AUDIO_F32 };
501 char *_audioFormatsVerbose[] = { "AUDIO_S8", "AUDIO_U8", "AUDIO_S16LSB", "AUDIO_S16MSB", "AUDIO_S16SYS", "AUDIO_S16", "AUDIO_U16LSB",
502 "AUDIO_U16MSB", "AUDIO_U16SYS", "AUDIO_U16", "AUDIO_S32LSB", "AUDIO_S32MSB", "AUDIO_S32SYS", "AUDIO_S32",
503 "AUDIO_F32LSB", "AUDIO_F32MSB", "AUDIO_F32SYS", "AUDIO_F32" };
504 const int _numAudioChannels = 4;
505 Uint8 _audioChannels[] = { 1, 2, 4, 6 };
506 const int _numAudioFrequencies = 4;
507 int _audioFrequencies[] = { 11025, 22050, 44100, 48000 };
511 * \brief Builds various audio conversion structures
513 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
515 int audio_buildAudioCVT()
521 int i, ii, j, jj, k, kk;
523 /* No conversion needed */
524 spec1.format = AUDIO_S16LSB;
527 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
528 spec1.format, spec1.channels, spec1.freq);
529 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec1)");
530 SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0, got: %i", result);
532 /* Typical conversion */
533 spec1.format = AUDIO_S8;
536 spec2.format = AUDIO_S16LSB;
539 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
540 spec2.format, spec2.channels, spec2.freq);
541 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
542 SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
544 /* All source conversions with random conversion targets, allow 'null' conversions */
545 for (i = 0; i < _numAudioFormats; i++) {
546 for (j = 0; j < _numAudioChannels; j++) {
547 for (k = 0; k < _numAudioFrequencies; k++) {
548 spec1.format = _audioFormats[i];
549 spec1.channels = _audioChannels[j];
550 spec1.freq = _audioFrequencies[k];
551 ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
552 jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
553 kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
554 spec2.format = _audioFormats[ii];
555 spec2.channels = _audioChannels[jj];
556 spec2.freq = _audioFrequencies[kk];
557 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
558 spec2.format, spec2.channels, spec2.freq);
559 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)",
560 i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
561 SDLTest_AssertCheck(result == 0 || result == 1, "Verify result value; expected: 0 or 1, got: %i", result);
563 SDLTest_LogError("%s", SDL_GetError());
565 SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
571 return TEST_COMPLETED;
575 * \brief Checkes calls with invalid input to SDL_BuildAudioCVT
577 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
579 int audio_buildAudioCVTNegative()
581 const char *expectedError = "Parameter 'cvt' is invalid";
591 spec1.format = AUDIO_S8;
594 spec2.format = AUDIO_S16LSB;
599 SDLTest_AssertPass("Call to SDL_ClearError()");
601 /* NULL input for CVT buffer */
602 result = SDL_BuildAudioCVT((SDL_AudioCVT *)NULL, spec1.format, spec1.channels, spec1.freq,
603 spec2.format, spec2.channels, spec2.freq);
604 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(NULL,...)");
605 SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
606 error = SDL_GetError();
607 SDLTest_AssertPass("Call to SDL_GetError()");
608 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
610 SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
611 "Validate error message, expected: '%s', got: '%s'", expectedError, error);
614 /* Invalid conversions */
615 for (i = 1; i < 64; i++) {
616 /* Valid format to start with */
617 spec1.format = AUDIO_S8;
620 spec2.format = AUDIO_S16LSB;
625 SDLTest_AssertPass("Call to SDL_ClearError()");
627 /* Set various invalid format inputs */
628 SDL_strlcpy(message, "Invalid: ", 256);
630 SDL_strlcat(message, " spec1.format", 256);
634 SDL_strlcat(message, " spec1.channels", 256);
638 SDL_strlcat(message, " spec1.freq", 256);
642 SDL_strlcat(message, " spec2.format", 256);
646 SDL_strlcat(message, " spec2.channels", 256);
650 SDL_strlcat(message, " spec2.freq", 256);
653 SDLTest_Log("%s", message);
654 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
655 spec2.format, spec2.channels, spec2.freq);
656 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
657 SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
658 error = SDL_GetError();
659 SDLTest_AssertPass("Call to SDL_GetError()");
660 SDLTest_AssertCheck(error != NULL && error[0] != '\0', "Validate that error message was not NULL or empty");
664 SDLTest_AssertPass("Call to SDL_ClearError()");
666 return TEST_COMPLETED;
670 * \brief Checks current audio status.
672 * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
674 int audio_getAudioStatus()
676 SDL_AudioStatus result;
678 /* Check current audio status */
679 result = SDL_GetAudioStatus();
680 SDLTest_AssertPass("Call to SDL_GetAudioStatus()");
681 SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
682 "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
683 SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
685 return TEST_COMPLETED;
691 * \brief Opens, checks current audio status, and closes a device.
693 * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
695 int audio_openCloseAndGetAudioStatus()
697 SDL_AudioStatus result;
701 SDL_AudioDeviceID id;
702 SDL_AudioSpec desired, obtained;
704 /* Get number of devices. */
705 count = SDL_GetNumAudioDevices(0);
706 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
708 for (i = 0; i < count; i++) {
709 /* Get device name */
710 device = (char *)SDL_GetAudioDeviceName(i, 0);
711 SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
712 SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
713 if (device == NULL) return TEST_ABORTED;
715 /* Set standard desired spec */
717 desired.format=AUDIO_S16SYS;
719 desired.samples=4096;
720 desired.callback=_audio_testCallback;
721 desired.userdata=NULL;
724 id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
725 SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
726 SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %i", id);
729 /* Check device audio status */
730 result = SDL_GetAudioDeviceStatus(id);
731 SDLTest_AssertPass("Call to SDL_GetAudioDeviceStatus()");
732 SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
733 "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
734 SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
736 /* Close device again */
737 SDL_CloseAudioDevice(id);
738 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
742 SDLTest_Log("No devices to test with");
745 return TEST_COMPLETED;
749 * \brief Locks and unlocks open audio device.
751 * \sa https://wiki.libsdl.org/SDL_LockAudioDevice
752 * \sa https://wiki.libsdl.org/SDL_UnlockAudioDevice
754 int audio_lockUnlockOpenAudioDevice()
759 SDL_AudioDeviceID id;
760 SDL_AudioSpec desired, obtained;
762 /* Get number of devices. */
763 count = SDL_GetNumAudioDevices(0);
764 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
766 for (i = 0; i < count; i++) {
767 /* Get device name */
768 device = (char *)SDL_GetAudioDeviceName(i, 0);
769 SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
770 SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
771 if (device == NULL) return TEST_ABORTED;
773 /* Set standard desired spec */
775 desired.format=AUDIO_S16SYS;
777 desired.samples=4096;
778 desired.callback=_audio_testCallback;
779 desired.userdata=NULL;
782 id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
783 SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
784 SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %i", id);
786 /* Lock to protect callback */
787 SDL_LockAudioDevice(id);
788 SDLTest_AssertPass("SDL_LockAudioDevice(%i)", id);
790 /* Simulate callback processing */
792 SDLTest_Log("Simulate callback processing - delay");
795 SDL_UnlockAudioDevice(id);
796 SDLTest_AssertPass("SDL_UnlockAudioDevice(%i)", id);
798 /* Close device again */
799 SDL_CloseAudioDevice(id);
800 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
804 SDLTest_Log("No devices to test with");
807 return TEST_COMPLETED;
812 * \brief Convert audio using various conversion structures
814 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
815 * \sa https://wiki.libsdl.org/SDL_ConvertAudio
817 int audio_convertAudio()
825 int i, ii, j, jj, k, kk, l, ll;
827 /* Iterate over bitmask that determines which parameters are modified in the conversion */
828 for (c = 1; c < 8; c++) {
829 SDL_strlcpy(message, "Changing:", 128);
831 SDL_strlcat(message, " Format", 128);
834 SDL_strlcat(message, " Channels", 128);
837 SDL_strlcat(message, " Frequencies", 128);
839 SDLTest_Log("%s", message);
840 /* All source conversions with random conversion targets */
841 for (i = 0; i < _numAudioFormats; i++) {
842 for (j = 0; j < _numAudioChannels; j++) {
843 for (k = 0; k < _numAudioFrequencies; k++) {
844 spec1.format = _audioFormats[i];
845 spec1.channels = _audioChannels[j];
846 spec1.freq = _audioFrequencies[k];
848 /* Ensure we have a different target format */
851 ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
856 jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
861 kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
865 } while ((i == ii) && (j == jj) && (k == kk));
866 spec2.format = _audioFormats[ii];
867 spec2.channels = _audioChannels[jj];
868 spec2.freq = _audioFrequencies[kk];
870 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
871 spec2.format, spec2.channels, spec2.freq);
872 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)",
873 i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
874 SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
876 SDLTest_LogError("%s", SDL_GetError());
878 SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
879 if (cvt.len_mult < 1) return TEST_ABORTED;
881 /* Create some random data to convert */
883 ll = l * cvt.len_mult;
884 SDLTest_Log("Creating dummy sample buffer of %i length (%i bytes)", l, ll);
886 cvt.buf = (Uint8 *)SDL_malloc(ll);
887 SDLTest_AssertCheck(cvt.buf != NULL, "Check data buffer to convert is not NULL");
888 if (cvt.buf == NULL) return TEST_ABORTED;
890 /* Convert the data */
891 result = SDL_ConvertAudio(&cvt);
892 SDLTest_AssertPass("Call to SDL_ConvertAudio()");
893 SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0; got: %i", result);
894 SDLTest_AssertCheck(cvt.buf != NULL, "Verify conversion buffer is not NULL");
895 SDLTest_AssertCheck(cvt.len_ratio > 0.0, "Verify conversion length ratio; expected: >0; got: %f", cvt.len_ratio);
897 /* Free converted buffer */
906 return TEST_COMPLETED;
911 * \brief Opens, checks current connected status, and closes a device.
913 * \sa https://wiki.libsdl.org/SDL_AudioDeviceConnected
915 int audio_openCloseAudioDeviceConnected()
921 SDL_AudioDeviceID id;
922 SDL_AudioSpec desired, obtained;
924 /* Get number of devices. */
925 count = SDL_GetNumAudioDevices(0);
926 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
928 for (i = 0; i < count; i++) {
929 /* Get device name */
930 device = (char *)SDL_GetAudioDeviceName(i, 0);
931 SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
932 SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
933 if (device == NULL) return TEST_ABORTED;
935 /* Set standard desired spec */
937 desired.format=AUDIO_S16SYS;
939 desired.samples=4096;
940 desired.callback=_audio_testCallback;
941 desired.userdata=NULL;
944 id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
945 SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
946 SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >1, got: %i", id);
949 /* TODO: enable test code when function is available in SDL2 */
951 #ifdef AUDIODEVICECONNECTED_DEFINED
952 /* Get connected status */
953 result = SDL_AudioDeviceConnected(id);
954 SDLTest_AssertPass("Call to SDL_AudioDeviceConnected()");
956 SDLTest_AssertCheck(result == 1, "Verify returned value; expected: 1; got: %i", result);
958 /* Close device again */
959 SDL_CloseAudioDevice(id);
960 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
964 SDLTest_Log("No devices to test with");
967 return TEST_COMPLETED;
972 /* ================= Test Case References ================== */
974 /* Audio test cases */
975 static const SDLTest_TestCaseReference audioTest1 =
976 { (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevices, "audio_enumerateAndNameAudioDevices", "Enumerate and name available audio devices (output and capture)", TEST_ENABLED };
978 static const SDLTest_TestCaseReference audioTest2 =
979 { (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevicesNegativeTests, "audio_enumerateAndNameAudioDevicesNegativeTests", "Negative tests around enumeration and naming of audio devices.", TEST_ENABLED };
981 static const SDLTest_TestCaseReference audioTest3 =
982 { (SDLTest_TestCaseFp)audio_printAudioDrivers, "audio_printAudioDrivers", "Checks available audio driver names.", TEST_ENABLED };
984 static const SDLTest_TestCaseReference audioTest4 =
985 { (SDLTest_TestCaseFp)audio_printCurrentAudioDriver, "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED };
987 static const SDLTest_TestCaseReference audioTest5 =
988 { (SDLTest_TestCaseFp)audio_buildAudioCVT, "audio_buildAudioCVT", "Builds various audio conversion structures.", TEST_ENABLED };
990 static const SDLTest_TestCaseReference audioTest6 =
991 { (SDLTest_TestCaseFp)audio_buildAudioCVTNegative, "audio_buildAudioCVTNegative", "Checks calls with invalid input to SDL_BuildAudioCVT", TEST_ENABLED };
993 static const SDLTest_TestCaseReference audioTest7 =
994 { (SDLTest_TestCaseFp)audio_getAudioStatus, "audio_getAudioStatus", "Checks current audio status.", TEST_ENABLED };
996 static const SDLTest_TestCaseReference audioTest8 =
997 { (SDLTest_TestCaseFp)audio_openCloseAndGetAudioStatus, "audio_openCloseAndGetAudioStatus", "Opens and closes audio device and get audio status.", TEST_ENABLED };
999 static const SDLTest_TestCaseReference audioTest9 =
1000 { (SDLTest_TestCaseFp)audio_lockUnlockOpenAudioDevice, "audio_lockUnlockOpenAudioDevice", "Locks and unlocks an open audio device.", TEST_ENABLED };
1002 /* TODO: enable test when SDL_ConvertAudio segfaults on cygwin have been fixed. */
1003 /* For debugging, test case can be run manually using --filter audio_convertAudio */
1005 static const SDLTest_TestCaseReference audioTest10 =
1006 { (SDLTest_TestCaseFp)audio_convertAudio, "audio_convertAudio", "Convert audio using available formats.", TEST_DISABLED };
1008 /* TODO: enable test when SDL_AudioDeviceConnected has been implemented. */
1010 static const SDLTest_TestCaseReference audioTest11 =
1011 { (SDLTest_TestCaseFp)audio_openCloseAudioDeviceConnected, "audio_openCloseAudioDeviceConnected", "Opens and closes audio device and get connected status.", TEST_DISABLED };
1013 static const SDLTest_TestCaseReference audioTest12 =
1014 { (SDLTest_TestCaseFp)audio_quitInitAudioSubSystem, "audio_quitInitAudioSubSystem", "Quit and re-init audio subsystem.", TEST_ENABLED };
1016 static const SDLTest_TestCaseReference audioTest13 =
1017 { (SDLTest_TestCaseFp)audio_initQuitAudio, "audio_initQuitAudio", "Init and quit audio drivers directly.", TEST_ENABLED };
1019 static const SDLTest_TestCaseReference audioTest14 =
1020 { (SDLTest_TestCaseFp)audio_initOpenCloseQuitAudio, "audio_initOpenCloseQuitAudio", "Cycle through init, open, close and quit with various audio specs.", TEST_ENABLED };
1022 static const SDLTest_TestCaseReference audioTest15 =
1023 { (SDLTest_TestCaseFp)audio_pauseUnpauseAudio, "audio_pauseUnpauseAudio", "Pause and Unpause audio for various audio specs while testing callback.", TEST_ENABLED };
1025 /* Sequence of Audio test cases */
1026 static const SDLTest_TestCaseReference *audioTests[] = {
1027 &audioTest1, &audioTest2, &audioTest3, &audioTest4, &audioTest5, &audioTest6,
1028 &audioTest7, &audioTest8, &audioTest9, &audioTest10, &audioTest11,
1029 &audioTest12, &audioTest13, &audioTest14, &audioTest15, NULL
1032 /* Audio test suite (global) */
1033 SDLTest_TestSuiteReference audioTestSuite = {