summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Geier <Matthias.Geier@gmail.com>2017-02-05 12:50:18 +0100
committerMatthias Geier <Matthias.Geier@gmail.com>2017-02-05 12:50:18 +0100
commite04dc1cc994f211b02b1f216f180343f2e2e85f7 (patch)
treeabe022fe512719c7c353100bdb688287e9848f54
parentd26c8008d2a52f54426a1bdd965cfac037dfd482 (diff)
First try 'utf-8' then 'mbcs' on MME and DS device names
Closes #72.
-rw-r--r--sounddevice.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/sounddevice.py b/sounddevice.py
index 5781d22..975cb2c 100644
--- a/sounddevice.py
+++ b/sounddevice.py
@@ -723,10 +723,23 @@ def query_devices(device=None, kind=None):
if not info:
raise PortAudioError('Error querying device {0}'.format(device))
assert info.structVersion == 2
+ name_bytes = _ffi.string(info.name)
+ try:
+ # We don't know beforehand if DirectSound and MME device names use
+ # 'utf-8' or 'mbcs' encoding. Let's try 'utf-8' first, because it more
+ # likely raises an exception on 'mbcs' data than vice versa, see also
+ # https://github.com/spatialaudio/python-sounddevice/issues/72.
+ # All other host APIs use 'utf-8' anyway.
+ name = name_bytes.decode('utf-8')
+ except UnicodeDecodeError:
+ if info.hostApi in (
+ _lib.Pa_HostApiTypeIdToHostApiIndex(_lib.paDirectSound),
+ _lib.Pa_HostApiTypeIdToHostApiIndex(_lib.paMME)):
+ name = name_bytes.decode('mbcs')
+ else:
+ raise
device_dict = {
- 'name': _ffi.string(info.name).decode('mbcs' if info.hostApi in (
- _lib.Pa_HostApiTypeIdToHostApiIndex(_lib.paDirectSound),
- _lib.Pa_HostApiTypeIdToHostApiIndex(_lib.paMME)) else 'utf-8'),
+ 'name': name,
'hostapi': info.hostApi,
'max_input_channels': info.maxInputChannels,
'max_output_channels': info.maxOutputChannels,