pygame:在两个线程中同时使用pygame.mixer失败
我正在尝试用pygame和numpy制作一个小的声音生成器。下面的代码可以正确播放我numpy数组中的数据,但当我试图把这段代码放到一个模块中,并从不同的线程或进程中访问它,以同时播放两个声音时,其中一个声音却是在另一个声音之前播放,而不是两个声音同时播放。
def _play_array(array, ms, vol):
sound = pygame.sndarray.make_sound(_intern._as_int16(array))
channel = sound.play(-1)
channel.set_volume(vol)
if ms > 50:
pygame.time.delay(ms-50)
channel.fadeout(50)
else:
pygame.time.delay(ms)
sound.stop()
更新:
我尝试安装了audiere,但结果是出现了一个非常长的错误信息,看来安装程序遇到的问题是:/Developer/SDKs/MacOSX10.6.sdk/usr/include/stdarg.h:4:25: error: stdarg.h: No such file or directory
(编辑) 现在我发现它在Mac OS X上不工作。
更新2:
尝试使用snack/tkSnack时出现了错误:RuntimeError: Tk not intialized or not registered with Snack
更新3:
我尝试安装wxPython来在写入文件后播放声音,但import wx
时失败了:/usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.7/site-packages/wx-2.8-mac-unicode/wx/_core_.so: no matching architecture in universal wrapper
。这个问题让我有点烦了……
更新4:
见帖子
2 个回答
要完全解决你的问题,你可以使用py audiere来制作声音。这样你就可以同时播放两个不同的音调。
根据你的代码,我觉得你不能同时播放两个音调。以下是我频率生成器的一小段代码:
speakers = audiere.open_device()
tone = speakers.create_square(start_freq *2)
tone.pan = 1
tone.stop()
tone.play()
只需要有两个声音,而不是一个。
抱歉这段代码没有用到你的代码,但希望这对你有帮助。:)
这让我感到很烦。于是我决定使用系统自带的音乐播放器来播放,因为其他方法似乎都不管用。
os.system("open " + path)
在Mac上运行得很好。