我在線程上播放聲音設備時遇到了一些問題。我在開始時將音頻設備作爲sd導入。然後在運行期間,我想用ASIO聲卡在線程上播放音調。我需要在線程上完成的所有配置都運行良好。然而,當我想玩的語氣我得到了以下錯誤:線程上的Python Sounddevice.play()
sounddevice.PortAudioError:錯誤打開的OutputStream:未預料到的主機API 2錯誤0:u'Failed加載ASIO驅動」
如果我在線程中每次需要時初始化導入,它就可以工作。但我當然不想這樣做。任何想法很難解決它?
謝謝!
這裏一個簡單的代碼示例:
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()
請提供[MCVE](http://stackoverflow.com/help/mcve)。 – Matthias
@Matthias我加了,示例代碼 – Laura
出於好奇,你使用多線程的用例是什麼? – Matthias