@@ -20,6 +20,7 @@ def with_sdl_audio():
2020 if original_driver :
2121 os .environ ["SDL_AUDIODRIVER" ] = original_driver
2222 # Initialize SDL2 with video and audio subsystems
23+ sdl2 .SDL_Quit ()
2324 sdl2 .SDL_ClearError ()
2425 ret = sdl2 .SDL_Init (sdl2 .SDL_INIT_VIDEO | sdl2 .SDL_INIT_AUDIO )
2526 assert sdl2 .SDL_GetError () == b""
@@ -31,6 +32,23 @@ def with_sdl_audio():
3132 if original_driver :
3233 os .environ ["SDL_AUDIODRIVER" ] = original_driver
3334
35+ @pytest .fixture
36+ def with_default_driver (with_sdl_audio ):
37+ driver = sdl2 .SDL_GetCurrentAudioDriver ()
38+ if driver == None or sdl2 .SDL_GetNumAudioDevices (False ) == 0 :
39+ sdl2 .SDL_QuitSubSystem (SDL_INIT_AUDIO )
40+ os .environ ["SDL_AUDIODRIVER" ] = b'dummy'
41+ sdl2 .SDL_InitSubSystem (SDL_INIT_AUDIO )
42+ driver = sdl2 .SDL_GetCurrentAudioDriver ()
43+ yield driver
44+
45+ def _get_audio_drivers ():
46+ drivers = []
47+ for index in range (sdl2 .SDL_GetNumAudioDrivers ()):
48+ name = sdl2 .SDL_GetAudioDriver (index )
49+ drivers .append (name .decode ('utf-8' ))
50+ return drivers
51+
3452
3553# Test macro functions
3654
@@ -227,11 +245,10 @@ def test_SDL_GetAudioDeviceName(with_sdl_audio):
227245 # Reset audio subsystem
228246 SDL_Quit ()
229247 SDL_Init (0 )
230- for index in range ( sdl2 . SDL_GetNumAudioDrivers () ):
248+ for drivername in _get_audio_drivers ( ):
231249 # Get input/output device names for each audio driver
232- drivername = sdl2 .SDL_GetAudioDriver (index )
233- backends .append (drivername .decode ("utf-8" ))
234- os .environ ["SDL_AUDIODRIVER" ] = drivername .decode ("utf-8" )
250+ backends .append (drivername )
251+ os .environ ["SDL_AUDIODRIVER" ] = drivername
235252 # Need to reinitialize subsystem for each driver
236253 SDL_InitSubSystem (SDL_INIT_AUDIO )
237254 driver = sdl2 .SDL_GetCurrentAudioDriver ()
@@ -258,24 +275,13 @@ def test_SDL_GetAudioDeviceName(with_sdl_audio):
258275 print (" - output: {0}" .format (str (devices [driver ]['output' ])))
259276
260277@pytest .mark .skipif (sdl2 .dll .version < 2016 , reason = "not available" )
261- def test_SDL_GetAudioDeviceSpec (with_sdl_audio ):
262- # Reset audio subsystem
263- SDL_Quit ()
264- SDL_Init (0 )
265- # Find an audio driver with at least one output
266- SDL_InitSubSystem (SDL_INIT_AUDIO )
267- driver = sdl2 .SDL_GetCurrentAudioDriver ()
268- if driver == None or sdl2 .SDL_GetNumAudioDevices (False ) == 0 :
269- SDL_QuitSubSystem (SDL_INIT_AUDIO )
270- os .environ ["SDL_AUDIODRIVER" ] = b'dummy'
271- SDL_InitSubSystem (SDL_INIT_AUDIO )
272- driver = sdl2 .SDL_GetCurrentAudioDriver ()
278+ def test_SDL_GetAudioDeviceSpec (with_default_driver ):
279+ driver = with_default_driver
273280 drivername = driver .decode ('utf-8' )
274281 # Get name and spec of first output device
275282 outspec = sdl2 .SDL_AudioSpec (0 , 0 , 0 , 0 )
276283 outname = sdl2 .SDL_GetAudioDeviceName (0 , False ).decode ('utf-8' )
277284 ret = sdl2 .SDL_GetAudioDeviceSpec (0 , False , ctypes .byref (outspec ))
278- SDL_QuitSubSystem (SDL_INIT_AUDIO )
279285 assert ret == 0
280286 # Validate frequency and channel count were set
281287 hz = outspec .freq
@@ -291,6 +297,32 @@ def test_SDL_GetAudioDeviceSpec(with_sdl_audio):
291297 print (msg .format (outname , drivername ))
292298 print (msg2 .format (hz , chans , fmt , bufsize ))
293299
300+ @pytest .mark .skipif (sdl2 .dll .version < 2240 , reason = "not available" )
301+ def test_SDL_GetDefaultAudioInfo (with_default_driver ):
302+ driver = with_default_driver
303+ drivername = driver .decode ('utf-8' )
304+ # Get name and spec of first output device
305+ outspec = sdl2 .SDL_AudioSpec (0 , 0 , 0 , 0 )
306+ outname = ctypes .c_char_p ()
307+ ret = sdl2 .SDL_GetDefaultAudioInfo (ctypes .byref (outname ), ctypes .byref (outspec ), 0 )
308+ # If method isn't implemented for the current back end, just skip
309+ if ret < 0 and b"not supported" in sdl2 .SDL_GetError ():
310+ pytest .skip ("not supported by driver" )
311+ assert ret == 0
312+ # Validate frequency and channel count were set
313+ hz = outspec .freq
314+ fmt = FORMAT_NAME_MAP [outspec .format ] if outspec .format > 0 else 'unknown'
315+ chans = outspec .channels
316+ bufsize = outspec .samples if outspec .samples > 0 else 'unknown'
317+ assert hz > 0
318+ assert chans > 0
319+ # Print out device spec info
320+ outname = outname .value .decode ('utf-8' )
321+ msg = "Default audio spec for {0} with '{1}' driver:"
322+ msg2 = "{0} Hz, {1} channels, {2} format, {3} sample buffer size"
323+ print (msg .format (outname , drivername ))
324+ print (msg2 .format (hz , chans , fmt , bufsize ))
325+
294326def test_SDL_OpenCloseAudioDevice (with_sdl_audio ):
295327 #TODO: Add tests for callback
296328 fmt = sdl2 .AUDIO_F32 if sys .platform == "darwin" else sdl2 .AUDIO_U16
0 commit comments