使用SoundDevice库对windows环境下音频流的wire

对于可以路由的设备(输入输出兼容设备,比如远程输入和输出)的直接用官方源码,其他跨设备的输入输出串流可以用以下代码自行读取写入数据:

import sounddevice as sd
import numpy as np

nextbuf=None

def callback(indata, frames, time, status):
    global nextbuf
    if status:
        print(status)
    if nextbuf is None:
        nextbuf=indata.copy()
    elif len(nextbuf<60000): #buff限制长度,读取太慢造成溢出则不再写入
        nextbuf=np.append(nextbuf,indata,axis=0)
    
def callbackout(outdata, frames, time, status):
    global nextbuf
    if status:
        print(status)
    if nextbuf is not None and len(nextbuf)>len(outdata):
        outdata[:]=nextbuf[:len(outdata)]
        nextbuf=nextbuf[len(outdata):]
        
   
try:
    #print(sd.query_devices()) #显示所有设备数据
    sdinput=sd.InputStream(channels=2,device=0,callback=callback) #输入设备,device是编号
    sdoutput=sd.OutputStream(channels=2,device=3,callback=callbackout) #输出设备,device是编号
    sdinput.start()
    sdoutput.start()
    print("press any key to quit...")
    print('#' * 80)
    input()
finally:
    sdinput.stop()
    sdoutput.stop()
    sdinput.close()
    sdoutput.close()
    pass
posted @ 2024-02-07 14:06  oglib  阅读(104)  评论(0)    收藏  举报