diff --git a/buildconfig/stubs/pygame/_sdl2/audio.pyi b/buildconfig/stubs/pygame/_sdl2/audio.pyi index fffb94e55f..0068b73021 100644 --- a/buildconfig/stubs/pygame/_sdl2/audio.pyi +++ b/buildconfig/stubs/pygame/_sdl2/audio.pyi @@ -1,4 +1,5 @@ from collections.abc import Callable +from typing import Optional AUDIO_U8: int AUDIO_S8: int @@ -25,7 +26,7 @@ def get_audio_device_names(iscapture: bool = False) -> list[str]: ... class AudioDevice: def __init__( self, - devicename: str, + devicename: Optional[str], iscapture: bool, frequency: int, audioformat: int, @@ -39,7 +40,7 @@ class AudioDevice: @property def deviceid(self) -> int: ... @property - def devicename(self) -> str: ... + def devicename(self) -> Optional[str]: ... @property def callback(self) -> Callable[[AudioDevice, memoryview], None]: ... @property diff --git a/src_c/cython/pygame/_sdl2/audio.pyx b/src_c/cython/pygame/_sdl2/audio.pyx index 97c6504a08..96e49c82f6 100644 --- a/src_c/cython/pygame/_sdl2/audio.pyx +++ b/src_c/cython/pygame/_sdl2/audio.pyx @@ -131,8 +131,8 @@ cdef class AudioDevice: memset(&self.desired, 0, sizeof(SDL_AudioSpec)) self._iscapture = iscapture self._callback = callback - if not isinstance(devicename, str): - raise TypeError("devicename must be a string") + if devicename is not None and not isinstance(devicename, str): + raise TypeError("devicename must be a string or None") self._devicename = devicename self.desired.freq = frequency; @@ -142,8 +142,17 @@ cdef class AudioDevice: self.desired.callback = recording_cb; self.desired.userdata = self + cdef bytes devicename_bytes + cdef const char* devicename_ptr + + if self._devicename is None: + devicename_ptr = NULL + else: + devicename_bytes = self._devicename.encode("utf-8") + devicename_ptr = devicename_bytes + self._deviceid = SDL_OpenAudioDevice( - self._devicename.encode("utf-8"), + devicename_ptr, self._iscapture, &self.desired, &self.obtained,