Python:使用 PyAudio(或其他东西)进行实时音频流传输? [英] Python: Realtime audio streaming with PyAudio (or something else)?

查看:139
本文介绍了Python:使用 PyAudio(或其他东西)进行实时音频流传输?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用 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屋!

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