6

当使用带有 ASIO+DirectSound 支持的 PyAudio(Portaudio 绑定)时,此代码:

import pyaudio

p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
    print p.get_device_info_by_index(i)

...产生此错误:

UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 1: invalid continuation byte

我们如何解决这个问题?


问题可能来自第 990 行的“pyaudio.py”,因为 utf8 解码不成功:

           return {'index' : index,
                    'structVersion' : device_info.structVersion,
                    'name' : device_info.name,

这个答案在这里音频设备名称中的特殊字符:Pyaudio(“不要使用 PyAudio”)并不令人满意。


追溯

...
{'defaultSampleRate': 44100.0, 'defaultLowOutputLatency': 0.0, 'defaultLowInputLatency': 0.12, 'maxInputChannels': 2L, 'structVersion': 2L, 'hostApi': 1L, 'index': 8, 'defaultHighOutputLatency': 0.0, 'maxOutputChannels': 0L, 'name': u'Microphone interne (Conexant 20672 SmartAudio HD)', 'defaultHighInputLatency': 0.24}
Traceback (most recent call last):
  File "D:\test\test.py", line 5, in <module>
    print p.get_device_info_by_index(i)
  File "C:\ProgramData\Anaconda\lib\site-packages\pyaudio.py", line 977, in get_device_info_by_index
    pa.get_device_info(device_index)
  File "C:\ProgramData\Anaconda\lib\site-packages\pyaudio.py", line 990, in _make_device_info_dictionary
    'name' : device_info.name,
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 1: invalid continuation byte
4

4 回答 4

5

错误“无效的继续字节”让我认为该特定索引的文本已损坏。

If you're able to modify the pyaudio.py file (or get the pyaudio.py file to return just the name), you might be able to try handle the UTF-8 decoding yourself by using 'Unicode Dammit'. It pretty much takes a best guess at what the encoding can be. Here's a link to their tutorial (http://www.crummy.com/software/BeautifulSoup/bs4/doc/#unicode-dammit)

I think the code would look just like the tutorial:

from bs4 import UnicodeDammit

dammit = UnicodeDammit(audiodevicename)
print(dammit.unicode_markup) ## Wéird Device Name!
于 2014-02-08T06:40:58.060 回答
1

I've forked pyAudio and modified https://github.com/joelewis/PyAudio/blob/master/src/_portaudiomodule.c code to use

PyUnicode_DecodeFSDefault

instead of

 PyUnicode_FromString

which likely might solve the unicode issue. See if you could find it helpful.

fork: https://github.com/joelewis/PyAudio/

于 2014-02-14T13:25:06.590 回答
1

The only successful solution found is :

Many thanks to @cgohlke for having built new ready-to-use installers : http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio

于 2014-02-18T08:44:17.213 回答
0

I think the clue here is

UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 1: invalid continuation byte

For whatever reason something returned by get_device_info_by_index() (probably the name field) contains the byte 0xe9 which, if you are interpreting the string of bytes as UTF8, signifies a "continuation byte". This means that it expects some valid bytes to follow the 0xe9. valid bytes means some sequence of bytes that constitutes a legitimate UTF8 character. E.g.

http://hexutf8.com/?q=e981a8

uses 0xe9 with some valid continuation bytes.

于 2016-09-08T15:14:06.610 回答