提问人:gtzack 提问时间:10/13/2023 更新时间:10/13/2023 访问量:27
如何使用 python 中的回调函数使用 pyaudio 播放和录制 2 个通道?
How to play and record 2 channels with pyaudio using callback functions in python?
问:
有没有人运气使用回调函数使用 pyaudio 同时播放音频和录制两个通道?我整天都在搞砸这个问题,我似乎想不通。任何帮助将不胜感激。这就是我目前所拥有的。
谢谢
扎克
'def playandrec(self, insound, playch, rec_samp_len, recch):
# check to see if pg is active and stop it
if self.__pg != 0:
if self.pgOpen == 1:
self.delPage(self.pg)
# zero pad unused chanels
self.__wf = np.zeros((insound.shape[0] + self.miscSampDelays, max(playch)), dtype=np.float32)
# fill in sound
if insound.ndim == 1:
for i in range(insound.size):
self.__wf[i][playch[0] - 1] = insound[i]
elif insound.ndim == 2:
for j in range(insound.shape[1]):
for i in range(insound.shape[0]):
self.__wf[i][playch[j] - 1] = insound[i][j]
else:
print('error goes here too may dims ')
# make a stream
pg = self.p.open(
rate=self.fs,
channels=max(recch),
format=self.s.paFloat32,
input=True,
output=True,
input_device_index=self.outdev,
output_device_index=self.indev,
frames_per_buffer=self.chunksize,
stream_callback=self.__playandreccallback
)
# set var to pass
self.__pgOpen = 1
self.__playandrecCalled = 1
self.recch = recch
self.reccount = rec_samp_len
self.n = 0
self.__pg = pg
nmax = math.ceil(((rec_samp_len + self.miscSampDelays) / self.chunksize))
self.recsignal = np.zeros((self.chunksize * nmax, max(recch)), dtype=np.float32)
# start stream
pg.start_stream()
return pg`
def __playandreccallback(self, in_data, frame_count, time_info, status):
"""
:param in_data:
:param frame_count:
:param time_info:
:param status:
:return:
"""
chunk = self.__wf[(frame_count * self.n):((self.n + 1) * frame_count)]
data = np.fromstring(in_data, dtype=np.float32)
# breakpoint()
for idx in range(max(self.recch)):
self.recsignal[self.n * frame_count:(self.n+1) * frame_count, idx] = data[idx::max(self.recch)]
# ch1 = data[0::2]
# ch2 = data[1::2]
# breakpoint()
self.n = self.n + 1
if self.n * frame_count > self.reccount + self.miscSampDelays:
# compleat
tobeornottobe = pyaudio.paComplete
else:
tobeornottobe = pyaudio.paContinue
return in_data, tobeornottobe
如果我将in_data传递给输出,则呈指数增长。如果我传递除in_data之外的任何内容,则只会调用一次回调。
答: 暂无答案
评论