音频设备名称中的特殊字符:Pyaudio [英] Special characters in audio devices name : Pyaudio
问题描述
我目前正面临一个难题.我需要在法语Windows环境中使用Pyaudio,默认情况下,音频设备的名称包含é
或è
.
I'm currently facing a hard problem. I need to use Pyaudio on a french windows environnement and the name of the audio devices contains é
or è
by default.
这是出现特殊字符时出现的错误:
This is the error I get when a special character is present:
u=self.p.get_device_info_by_index(e)
File "C:\Python27\lib\site-packages\pyaudio.py", line 977, in get_device_info_
by_index
pa.get_device_info(device_index)
File "C:\Python27\lib\site-packages\pyaudio.py", line 987, in _make_device_inf
o_dictionary
print device_info.name
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 13: invalid
continuation byte
如果我可以访问代码,这将不是问题(我想我需要在字符串链的前面添加u"...").
This wouldn't be a problem if I could access the code (I would need to add a u"..." in front of the string chain I guess).
问题是我查看了Pyaudio代码,导致该错误的方法在pyd文件(_portaudio.pyd)中定义,因此,我无法对其进行修改!
The problem is that I looked inside the Pyaudio code and the method causing the bug is defined in an pyd file (_portaudio.pyd), therefor, I can't modify it!
我尝试下载_portaudio自己进行编译,但是我发现的发行版是用C编写的,而且代码繁重(我不了解C的第一件事).也许我可以在那里做些什么,但我不知道确切的位置和方式.
I tried to download _portaudio to compile it myself, but the distribution I found is coded in C and quite heavy (I don't know the first thing about C). Maybe I could do something there but I don't know exactly where and how.
我也可以通过注释获得音频设备名称的行来解决该问题,但是要识别没有名称显示给用户的特定音频输入要困难得多.
I could also handle the problem by just commenting the line getting the name of the audio devices, but it's much harder to identify a specific audio input without its name to show to the user.
这是整个过程: 我从pyaudio调用函数:
Here is the overall process : I call the function from pyaudio :
import pyaudio
self.p= pyaudio.PyAudio()
i=self.p.get_device_count()
for e in range(i):
u=self.p.get_device_info_by_index(e)
这将带我进入pyaudio模块,该模块调用方法:
This will lead me into the pyaudio module which calls the method :
device_info.name
device_info是_portaudio.pyd中定义的对象. 由于特定音频设备的名称包含é"或è"(谢谢您的窗口),因此_portaudio.pyd并未进行编码以处理这些字符.它返回错误:
device_info being an object defined in _portaudio.pyd. Since the name of particular audio devices contain "é" or "è" (Thank you windows), and the _portaudio.pyd is not encoded to handle those characters. It returns the error :
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 13: invalid
continuation byte
推荐答案
我正面临着完全相同的问题.就我而言,音频设备的名称中有中文字符,当我print device_info.name
I'm facing exactly the same problem. In my case, there are Chinese charactors in the name of audio device, and UnicodeDecodeError occured when I print device_info.name
这是我的解决方法.
使用 pymedia 代替pyaudio.试试这个:
Use pymedia instead of pyaudio. Try this:
>>> import pymedia
>>> dict_list = pymedia.audio.sound.getIDevices()
>>> print dict_list
({'name': '\xc2\xf3\xbf\xcb\xb7\xe7 (Realtek High Definition', 'channels': 2, 'manufId': '1', 'formats': 1048575, 'id': 0, 'productId': '65'},)
>>> dict_list = pymedia.audio.sound.getODevices()
>>> print dict_list
({'name': '\xd1\xef\xc9\xf9\xc6\xf7 (MV USB AUDIO)', 'channels': 2, 'manufId': 'ffff', 'formats': 1048575, 'id': 0, 'productId': 'ffff'}, {'name': '\xd1\xef\xc9\xf9\xc6\xf7 (Realtek High Definition', 'channels': 2, 'manufId': '1', 'formats': 1048575, 'id': 1, 'productId': '64'})
我正在使用python 2.7.
I'm using python 2.7 .
这篇关于音频设备名称中的特殊字符:Pyaudio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!