Skip to content

Commit ca584d8

Browse files
committed
Add mixer.get_driver
1 parent be55232 commit ca584d8

File tree

6 files changed

+85
-3
lines changed

6 files changed

+85
-3
lines changed

buildconfig/stubs/pygame/mixer.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def pre_init(
2828
) -> None: ...
2929
def quit() -> None: ...
3030
def get_init() -> Tuple[int, int, int]: ...
31+
def get_driver() -> str: ...
3132
def stop() -> None: ...
3233
def pause() -> None: ...
3334
def unpause() -> None: ...

docs/reST/ref/mixer.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ The following file formats are supported
165165

166166
.. ## pygame.mixer.get_init ##
167167
168+
.. function:: get_driver
169+
170+
| :sl:`get the name of the current audio backend driver`
171+
| :sg:`get_driver() -> str`
172+
173+
Pygame chooses one of many available audio backend drivers when it is
174+
initialized. This returns the internal name used for the backend. This
175+
function is intended to be used for getting diagnostic/debugging information.
176+
This can be controlled with ``SDL_AUDIODRIVER`` environment variable.
177+
178+
.. versionadded:: 2.5.0
179+
168180
.. function:: stop
169181

170182
| :sl:`stop playback of all sound channels`

src_c/doc/mixer_doc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define DOC_MIXER_PREINIT "pre_init(frequency=44100, size=-16, channels=2, buffer=512, devicename=None, allowedchanges=AUDIO_ALLOW_FREQUENCY_CHANGE | AUDIO_ALLOW_CHANNELS_CHANGE) -> None\npreset the mixer init arguments"
55
#define DOC_MIXER_QUIT "quit() -> None\nuninitialize the mixer"
66
#define DOC_MIXER_GETINIT "get_init() -> (frequency, format, channels)\ntest if the mixer is initialized"
7+
#define DOC_MIXER_GETDRIVER "get_driver() -> str\nget the name of the current audio backend driver"
78
#define DOC_MIXER_STOP "stop() -> None\nstop playback of all sound channels"
89
#define DOC_MIXER_PAUSE "pause() -> None\ntemporarily stop playback of all sound channels"
910
#define DOC_MIXER_UNPAUSE "unpause() -> None\nresume paused playback of sound channels"

src_c/mixer.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,18 @@ pg_mixer_get_init(PyObject *self, PyObject *_null)
582582
return Py_BuildValue("(iii)", freq, realform, channels);
583583
}
584584

585+
static PyObject *
586+
pg_mixer_get_driver(PyObject *self, PyObject *_null)
587+
{
588+
const char *name = NULL;
589+
MIXER_INIT_CHECK();
590+
name = SDL_GetCurrentAudioDriver();
591+
if (!name) {
592+
name = "unknown";
593+
}
594+
return PyUnicode_FromString(name);
595+
}
596+
585597
static PyObject *
586598
pre_init(PyObject *self, PyObject *args, PyObject *keywds)
587599
{
@@ -1941,6 +1953,8 @@ static PyMethodDef _mixer_methods[] = {
19411953
{"quit", (PyCFunction)mixer_quit, METH_NOARGS, DOC_MIXER_QUIT},
19421954
{"get_init", (PyCFunction)pg_mixer_get_init, METH_NOARGS,
19431955
DOC_MIXER_GETINIT},
1956+
{"get_driver", (PyCFunction)pg_mixer_get_driver, METH_NOARGS,
1957+
DOC_MIXER_GETDRIVER},
19441958
{"pre_init", (PyCFunction)pre_init, METH_VARARGS | METH_KEYWORDS,
19451959
DOC_MIXER_PREINIT},
19461960
{"get_num_channels", (PyCFunction)get_num_channels, METH_NOARGS,

src_py/_debug.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ def default_return(linked=True):
6969
# pylint: disable=unused-argument
7070
return (-1, -1, -1)
7171

72-
from pygame.display import get_driver, get_init as display_init
72+
from pygame.display import (
73+
get_driver as get_display_driver,
74+
get_init as display_init,
75+
)
76+
from pygame.mixer import (
77+
get_driver as get_mixer_driver,
78+
get_init as mixer_init,
79+
)
7380
from pygame.base import get_sdl_version
7481

7582
debug_str, *mixer = attempt_import(
@@ -118,9 +125,14 @@ def default_return(linked=True):
118125
)
119126

120127
if display_init():
121-
debug_str += f"Driver:\t\t\t{get_driver()}\n\n"
128+
debug_str += f"Display Driver:\t\t\t{get_display_driver()}\n\n"
129+
else:
130+
debug_str += "Display Driver:\t\t\tDisplay Not Initialized\n\n"
131+
132+
if mixer_init():
133+
debug_str += f"Mixer Driver:\t\t\t{get_mixer_driver()}\n\n"
122134
else:
123-
debug_str += "Driver:\t\t\tDisplay Not Initialized\n\n"
135+
debug_str += "Mixer Driver:\t\t\tMixer Not Initialized\n\n"
124136

125137
debug_str += f"Python:\t\t\t{platform.python_implementation()}\n"
126138

test/mixer_test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,48 @@ def tearDown(self):
4848
mixer.quit()
4949
mixer.pre_init(0, 0, 0, 0)
5050

51+
def test_get_driver(self):
52+
mixer.init()
53+
drivers = [
54+
pygame.NULL_VIDEODRIVER,
55+
"pipewire",
56+
"pulseaudio",
57+
"alsa",
58+
"jack",
59+
"sndio",
60+
"netbsd",
61+
"dsp",
62+
"qsa",
63+
"audio",
64+
"arts",
65+
"esd",
66+
"nacl",
67+
"nas",
68+
"wasapi",
69+
"directsound",
70+
"winmm",
71+
"paud",
72+
"haiku",
73+
"coreaudio",
74+
"disk",
75+
"fusionsound",
76+
"AAudio",
77+
"openslES",
78+
"android",
79+
"ps2",
80+
"psp",
81+
"vita",
82+
"n3ds",
83+
"emscripten",
84+
"DART",
85+
]
86+
driver = mixer.get_driver()
87+
self.assertIn(driver, drivers)
88+
89+
mixer.quit()
90+
with self.assertRaises(pygame.error):
91+
mixer.get_driver()
92+
5193
def test_init__keyword_args(self):
5294
# note: this test used to loop over all CONFIGS, but it's very slow..
5395
mixer.init(**CONFIG)

0 commit comments

Comments
 (0)