Skip to content

Commit d7abcbb

Browse files
authored
Merge pull request #2741 from pygame-community/ankith26-mixer-get-driver
Add `mixer.get_driver`
2 parents e3e205c + e29df14 commit d7abcbb

File tree

6 files changed

+111
-29
lines changed

6 files changed

+111
-29
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: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""Debug functionality that allows for more useful issue reporting
22
"""
33

4+
import platform
45
import sys
56
import traceback
67
import importlib
78
from typing import Tuple, Optional, Callable
89

10+
from pygame.version import ver
11+
912
ImportResult = Tuple[str, bool, Optional[Callable]]
1013

1114

@@ -53,6 +56,24 @@ def attempt_import(module, function_name, output_str=""):
5356
return (output_str, success, i)
5457

5558

59+
def _get_platform_info():
60+
"""
61+
Internal helper to get platform information
62+
"""
63+
ret = f"Platform:\t\t{platform.platform()}\n"
64+
ret += f"System:\t\t\t{platform.system()}\n"
65+
ret += f"System Version:\t\t{platform.version()}\n"
66+
ret += f"Processor:\t\t{platform.processor()}\n"
67+
ret += (
68+
f"Architecture:\t\tBits: {platform.architecture()[0]}\t"
69+
f"Linkage: {platform.architecture()[1]}\n\n"
70+
)
71+
72+
ret += f"Python:\t\t\t{platform.python_implementation()} {sys.version}\n"
73+
ret += f"pygame version:\t\t{ver}\n"
74+
return ret
75+
76+
5677
def print_debug_info(filename=None):
5778
"""Gets debug information for reporting bugs. Prints to console
5879
if filename is not specified, otherwise writes to that file
@@ -69,7 +90,14 @@ def default_return(linked=True):
6990
# pylint: disable=unused-argument
7091
return (-1, -1, -1)
7192

72-
from pygame.display import get_driver, get_init as display_init
93+
from pygame.display import (
94+
get_driver as get_display_driver,
95+
get_init as display_init,
96+
)
97+
from pygame.mixer import (
98+
get_driver as get_mixer_driver,
99+
get_init as mixer_init,
100+
)
73101
from pygame.base import get_sdl_version
74102

75103
debug_str, *mixer = attempt_import(
@@ -100,33 +128,7 @@ def default_return(linked=True):
100128
else:
101129
ft_version = freetype[1]
102130

103-
from pygame.version import ver
104-
105-
import platform
106-
107-
debug_str += f"Platform:\t\t{platform.platform()}\n"
108-
109-
debug_str += f"System:\t\t\t{platform.system()}\n"
110-
111-
debug_str += f"System Version:\t\t{platform.version()}\n"
112-
113-
debug_str += f"Processor:\t\t{platform.processor()}\n"
114-
115-
debug_str += (
116-
f"Architecture:\t\tBits: {platform.architecture()[0]}\t"
117-
f"Linkage: {platform.architecture()[1]}\n"
118-
)
119-
120-
if display_init():
121-
debug_str += f"Driver:\t\t\t{get_driver()}\n\n"
122-
else:
123-
debug_str += "Driver:\t\t\tDisplay Not Initialized\n\n"
124-
125-
debug_str += f"Python:\t\t\t{platform.python_implementation()}\n"
126-
127-
debug_str += f"pygame version:\t\t{ver}\n"
128-
129-
debug_str += f"python version:\t\t{str_from_tuple(sys.version_info[0:3])}\n\n"
131+
debug_str += _get_platform_info()
130132

131133
debug_str += (
132134
f"SDL versions:\t\tLinked: {str_from_tuple(get_sdl_version())}\t"
@@ -150,9 +152,19 @@ def default_return(linked=True):
150152

151153
debug_str += (
152154
f"Freetype versions:\tLinked: {str_from_tuple(ft_version())}\t"
153-
f"Compiled: {str_from_tuple(ft_version(linked = False))}"
155+
f"Compiled: {str_from_tuple(ft_version(linked = False))}\n\n"
154156
)
155157

158+
if display_init():
159+
debug_str += f"Display Driver:\t\t{get_display_driver()}\n"
160+
else:
161+
debug_str += "Display Driver:\t\tDisplay Not Initialized\n"
162+
163+
if mixer_init():
164+
debug_str += f"Mixer Driver:\t\t{get_mixer_driver()}"
165+
else:
166+
debug_str += "Mixer Driver:\t\tMixer Not Initialized"
167+
156168
if filename is None:
157169
print(debug_str)
158170

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)