Python:使用PyAudio(或其他工具)进行实时音频流传输? [英] Python: realtime audio streaming with PyAudio (or something else)?
问题描述
当前,我正在使用NumPy从NumPy数组生成WAV文件.我想知道是否有可能在实际将NumPy数组写入硬盘之前实时播放它.我发现使用PyAudio的所有示例都依赖于首先将NumPy数组写入WAV文件,但是我想拥有一个预览功能,该功能只是将NumPy数组吐出到音频输出.
Currently I'm using NumPy to generate the WAV file from a NumPy array. I wonder if it's possible to play the NumPy array in realtime before it's actually written to the hard drive. All examples I found using PyAudio rely on writing the NumPy array to a WAV file first, but I'd like to have a preview function that just spits out the NumPy array to the audio output.
也应该是跨平台的.我正在使用Python 3(Anaconda发行版).
Should be cross-platform, too. I'm using Python 3 (Anaconda distribution).
推荐答案
这有效!感谢您的帮助!
This has worked! Thanks for help!
def generate_sample(self, ob, preview):
print("* Generating sample...")
tone_out = array(ob, dtype=int16)
if preview:
print("* Previewing audio file...")
bytestream = tone_out.tobytes()
pya = pyaudio.PyAudio()
stream = pya.open(format=pya.get_format_from_width(width=2), channels=1, rate=OUTPUT_SAMPLE_RATE, output=True)
stream.write(bytestream)
stream.stop_stream()
stream.close()
pya.terminate()
print("* Preview completed!")
else:
write('sound.wav', SAMPLE_RATE, tone_out)
print("* Wrote audio file!")
现在看来是如此简单,但是当您不太了解Python时,它看起来就像地狱.
Seems so simple now, but when you don't know Python very well, it seems like hell.
这篇关于Python:使用PyAudio(或其他工具)进行实时音频流传输?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!