我在线程上播放声音设备时遇到了一些问题。我在开始时将声音设备导入为 sd。然后在运行期间,我想使用 ASIO 声卡在线程上播放音调。我需要在线程上进行的所有配置都运行良好。但是,当我想播放音调时,出现以下错误:
sounddevice.PortAudioError: Error opening OutputStream: Unexpected host API 2 error 0: u'Failed to load ASIO driver'
如果我每次需要时都在线程上初始化导入,它就可以工作。但我当然不想那样做。有什么想法可以解决吗?
谢谢!
这里有一个简单的代码示例:
from threading import Thread
import numpy as np
import sounddevice as sd
class Test(Thread):
def __init__(self):
Thread.__init__(self)
#-- Configuration of the Tone to be played
self.fs = 44100 # sampling rate, in Hz, 44100 or 48000
duration = 1.05 # in seconds, may be float
f = 200.0 # sine frequency, Hz, may be float
self.tone_data = (np.sin(2*np.pi*np.arange(self.fs*duration)*f/self.fs)).astype(np.float32)
def run(self):
#-- Configuration of the ASIO sound card
#import sounddevice as sd
sd.default.channels = 2
sd.default.device = 14
print sd.query_devices(sd.default.device)['name']
#sd.default.latency = ('low','low')
#asio_out = sd.AsioSettings(channel_selectors=[1, 2])
#sd.default.extra_settings = asio_out
sd.default.samplerate = self.fs
sd.play(self.tone_data)
sd.wait()
w = Test()
w.start()