Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions buildconfig/stubs/pygame/_sdl2/audio.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Callable
from typing import Optional

AUDIO_U8: int
AUDIO_S8: int
Expand All @@ -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,
Expand All @@ -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
Expand Down
15 changes: 12 additions & 3 deletions src_c/cython/pygame/_sdl2/audio.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -142,8 +142,17 @@ cdef class AudioDevice:
self.desired.callback = <SDL_AudioCallback>recording_cb;
self.desired.userdata = <void*>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,
Expand Down
Loading