如何将numpy数组流式传输到pyaudio流中? [英] howto stream numpy array into pyaudio stream?

查看:109
本文介绍了如何将numpy数组流式传输到pyaudio流中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码,该代码应该根据用户的操作向用户提供一些音频输出,并且我想生成声音,而不是要播放固定数量的 wav 文件.现在,我要做的是生成numpy格式的信号,将数据存储在 wav 文件中,然后将同一文件读入 pyaudio .我认为这是多余的,但是,我找不到办法.我的问题是,我可以直接将numpy数组(或常规列表)流式传输到pyaudio中进行播放吗?

I'm writing a code that supposed to give some audio output to the user based on his action, and I want to generate the sound rather than having a fixed number of wav files to play. Now, what I'm doing is to generate the signal in numpy format, store the data in a wav file and then read the same file into pyaudio. I think this is redundant, however, I couldn't find a way to do that. My question is, can I stream a numpy array (or a regular list) directly into my the pyaudio to play?

推荐答案

如果只是播放而无需同步到任何内容,则可以执行以下操作:

If its just playback and does not need to be synchronised to anything then you can just do the following:

# Open stream with correct settings
stream = self.p.open(format=pyaudio.paFloat32,
                         channels=CHANNELS,
                         rate=48000,
                         output=True,
                         output_device_index=1
                         )
# Assuming you have a numpy array called samples
data = samples.astype(np.float32).tostring()
stream.write(data)

我使用这种方法,对我来说效果很好.如果您需要同时录制,则此操作将无效.

I use this method and it works fine for me. If you need to record at the same time then this won't work.

这篇关于如何将numpy数组流式传输到pyaudio流中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆