Python 在多进程调用pyaudio 或者 sounddevice报错,PaMacCore (AUHAL) msg=Audio Hardware: Unknown Property

提示报错:
||PaMacCore (AUHAL)|| Warning on line 525: err=’‘who?’’, msg=Audio Hardware: Unknown Property
解决办法:对于pyaudio 需要把pyaudio.PyAudio()写在Process的函数里边
对于 sounddevice 需要把 imoprt sounddevice 写在Process的函数里边
​​在这里插入图片描述

def play_worker_pyaudio(
    wav_path: str,
    chunk: int,
    output_device: int,
):
    import pyaudio
    import wave
    f = wave.open(wav_path,'rb')
    #open stream
    p = pyaudio.PyAudio()
    stream = p.open(
        format = p.get_format_from_width(f.getsampwidth()),
        channels = f.getnchannels(),
        rate = f.getframerate(),
        output = True,
        output_device_index=output_device,
        )
def record_worker_sounddevice():
    import sounddevice as sd
    fs = 44100 # Hz
    length = 1 # s
    recording = sd.rec(frames=int(fs * length), samplerate=fs, blocking=True, channels=1)
    return recording

正常调用即可

process_play_1 = Process(target=play_worker_pyaudio, kwargs=dict(
        wav_path = '../Downloads/take.wav',
        chunk = 1024,
        output_device = 5,
    ))
    process_play_1.start()

你可能感兴趣的:(那些年的坑)