如何解决如何在 Raspbeery Pi 上同时录制来自两个声卡的音频
我在 Raspberry Pi 中插入了两个单独的 USB 声卡,我想同时记录来自它们的输入。稍后我想将这两个音频加入一个文件。 现在我似乎无法在 SO 上找到解决方案。 我正在使用 PyAudio,我可以成功地为这两个声卡打开流,如下所示:
stream = audio.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,input_device_index=index,frames_per_buffer=CHUNK)
其中 index 代表我已经知道的设备索引。 PyAudio 文档说要读取流我需要做这样的事情:
for i in range(0,int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
Recordframes.append(data)
所以我的问题是我如何设法同时从两个流中读取? 任何帮助表示赞赏:)
更新: 我试过这个代码:
import numpy as np
import pyaudio
import wave
import threading
import time
import subprocess
CHUNK = 512
FORMAT = pyaudio.paInt16
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "/home/pi/recordedFile1.wav"
p = pyaudio.PyAudio()
for i in range(0,p.get_device_count()):
print(i,p.get_device_info_by_index(i)['name'])
class Recorder:
def __init__(self):
self.going = False
self.process = None
def record(self,filename):
try:
if self.process.is_alive():
self.going = False
except AttributeError:
print("test")
self.process = threading.Thread(target=self._record)
self.process.start()
self.filename = filename
def _record(self):
p = pyaudio.PyAudio()
stream = p.open(
format=FORMAT,channels=1,frames_per_buffer=CHUNK,input_device_index=2)
stream2 = p.open(
format=FORMAT,input_device_index=3)
frames = []
frames2 = []
self.going = True
while self.going:
data = stream.read(CHUNK,exception_on_overflow=False)
data2 = stream2.read(CHUNK,exception_on_overflow=False)
frames.append(data)
frames2.append(data2)
frames = b''.join(frames);
frames2 = b''.join(frames2);
Sdecoded = np.frombuffer(frames,'int16')
Mdecoded = np.frombuffer(frames2,'int16')
mix = (Sdecoded + Mdecoded)
signal = np.clip(mix,-32767,32766)
encodecoded = wave.struct.pack("%dh" % (len(signal)),*list(signal))
# print("* done recording")
stream.stop_stream()
stream.close()
stream2.stop_stream()
stream2.close()
p.terminate()
self.going = False
wf = wave.open(WAVE_OUTPUT_FILENAME,'wb')
wf.setnchannels(1)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(encodecoded)
wf.close()
def stop_recording(self):
self.going = False
print('started')
recoder = Recorder()
print('recorded created')
recoder.record("/home/pi/recordedFile1.wav")
print('done recording')
但它无限运行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。