Skip to content

Commit e04dc1c

Browse files
committed
First try 'utf-8' then 'mbcs' on MME and DS device names
Closes #72.
1 parent d26c800 commit e04dc1c

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

sounddevice.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,23 @@ def query_devices(device=None, kind=None):
723723
if not info:
724724
raise PortAudioError('Error querying device {0}'.format(device))
725725
assert info.structVersion == 2
726+
name_bytes = _ffi.string(info.name)
727+
try:
728+
# We don't know beforehand if DirectSound and MME device names use
729+
# 'utf-8' or 'mbcs' encoding. Let's try 'utf-8' first, because it more
730+
# likely raises an exception on 'mbcs' data than vice versa, see also
731+
# https://github.com/spatialaudio/python-sounddevice/issues/72.
732+
# All other host APIs use 'utf-8' anyway.
733+
name = name_bytes.decode('utf-8')
734+
except UnicodeDecodeError:
735+
if info.hostApi in (
736+
_lib.Pa_HostApiTypeIdToHostApiIndex(_lib.paDirectSound),
737+
_lib.Pa_HostApiTypeIdToHostApiIndex(_lib.paMME)):
738+
name = name_bytes.decode('mbcs')
739+
else:
740+
raise
726741
device_dict = {
727-
'name': _ffi.string(info.name).decode('mbcs' if info.hostApi in (
728-
_lib.Pa_HostApiTypeIdToHostApiIndex(_lib.paDirectSound),
729-
_lib.Pa_HostApiTypeIdToHostApiIndex(_lib.paMME)) else 'utf-8'),
742+
'name': name,
730743
'hostapi': info.hostApi,
731744
'max_input_channels': info.maxInputChannels,
732745
'max_output_channels': info.maxOutputChannels,

0 commit comments

Comments
 (0)