Python:使用 PyAudio(或其他东西)进行实时音频流?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31674416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:23:47  来源:igfitidea点击:

Python: realtime audio streaming with PyAudio (or something else)?

pythonaudionumpyaudio-streaming

提问by Mario Kru?elj

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.

目前我正在使用 NumPy 从 NumPy 数组生成 WAV 文件。我想知道是否可以在实际写入硬盘之前实时播放 NumPy 数组。我发现的所有使用 PyAudio 的示例都依赖于首先将 NumPy 数组写入 WAV 文件,但我希望有一个预览功能,它只是将 NumPy 数组输出到音频输出。

Should be cross-platform, too. I'm using Python 3 (Anaconda distribution).

也应该是跨平台的。我正在使用 Python 3(Anaconda 发行版)。

采纳答案by Mario Kru?elj

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!")

Seems so simple now, but when you don't know Python very well, it seems like hell.

现在看起来很简单,但是当你不太了解 Python 时,它看起来就像地狱。

回答by Roland Smith

As you can see in the examples, pyaudio just reads data from the WAV file and writes that to the stream.

正如您在示例中所见,pyaudio 只是从 WAV 文件中读取数据并将其写入流。

It is not necessary to write a WAV file first, you just need a stream of data in the right format.

没有必要先写一个 WAV 文件,你只需要一个正确格式的数据流

I'm adding the example below in case the link ever goes dead (note that I didn't write this code):

我正在添加下面的示例,以防链接失效(请注意,我没有编写此代码):

"""PyAudio Example: Play a WAVE file."""

import pyaudio
import wave
import sys

CHUNK = 1024

if len(sys.argv) < 2:
    print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
    sys.exit(-1)

wf = wave.open(sys.argv[1], 'rb')

p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)

data = wf.readframes(CHUNK)

while data != '':
    stream.write(data)
    data = wf.readframes(CHUNK)

stream.stop_stream()
stream.close()

p.terminate()

回答by Matthias

This is really simple with python-sounddevice:

这对于python-sounddevice来说非常简单:

import sounddevice as sd
sd.play(myarray, 44100)