python - PyAudio:如何以回调/非阻塞模式访问 stream.read() 数据

标签 python python-3.x audio pyaudio

不懂编程,但在遵循文档网站上的连线和连线回调示例时,我正在努力弄清楚如何以回调模式访问 stream.read(CHUNK) 数据以由 audioop 库中的 audioop.rms() 处理。

下面是两个稍微修改过的例子。前者是 blocking 模式下的 wire 方法,被 rms 函数成功处理。后者是 non-blocking 模式下的 wire 方法,我不知道如何访问相同的数据。

"""
PyAudio Example: Make a wire between input and output (i.e., record a
few samples and play them back immediately).
"""

import pyaudio
import audioop  # new

CHUNK = 1024
WIDTH = 2
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5

p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                frames_per_buffer=CHUNK)

print("* recording")

while True:  # <-----------------------------------------
    data = stream.read(CHUNK)
    stream.write(data, CHUNK)
    rms = audioop.rms(data, WIDTH)  # new
    print(rms)  # computer audio power

print("* done")

stream.stop_stream()
stream.close()

p.terminate()

非阻塞

"""
PyAudio Example: Make a wire between input and output (i.e., record a
few samples and play them back immediately).

This is the callback (non-blocking) version.
"""

import pyaudio
import time
import audioop  # new

WIDTH = 2
CHANNELS = 2
RATE = 44100

p = pyaudio.PyAudio()


def callback(in_data, frame_count, time_info, status):
    return in_data, pyaudio.paContinue


stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                stream_callback=callback)

stream.start_stream()

while stream.is_active():  # <--------------------------------------------
    time.sleep(0.1)
    # data = stream.read(1024)    # the docs say not to call this
    data = stream.get_read_available()  # not sure what to do
    print(data)     # new
    rms = audioop.rms(data, WIDTH)  # compute audio power
    print(rms)  # new

stream.stop_stream()
stream.close()

p.terminate()

最佳答案

我看到这是将近一年前的事,但它可能对其他人有帮助。在使用非阻塞模式和回调函数时,您不需要(事实上,正如您发现的那样,不能使用)stream.read()。你不需要。当您打开的流有数据要从默认源输入或输出时,回调函数将在不同的线程中自动调用。

由于您想对每个 block 执行某些操作(计算 rms 值),因此您需要在处理每个 block 的回调函数中执行此操作。但是,如果您尝试做的太多,它就无法在下一个 block 准备好之前处理所有内容,并且您会丢失数据。因此,文件写入和打印语句不是您应该在回调中执行的操作。 (把它想象成一个中断处理程序:只做必要的事情)。所以在下面的工作代码中,我只是在回调函数中计算 rms,并将其设为全局变量,以便您可以从主程序访问它以进行打印。

我没有尝试为您传递给回调的每个 block 计算音频时间,因此通过将 time.sleep() 值保持在 0.1,下面的代码可能缺少中间 rms 值。

"""
PyAudio Example: Make a wire between input and output (i.e., record a
few samples and play them back immediately).

This is the callback (non-blocking) version.
"""

import pyaudio
import time
import audioop  # new

WIDTH = 2
CHANNELS = 2
RATE = 44100

p = pyaudio.PyAudio()

rms = None
def callback(in_data, frame_count, time_info, status):
    # print(in_data)     # takes too long in callback
    global rms
    rms = audioop.rms(in_data, WIDTH)  # compute audio power
    # print(rms)  # new # takes too long in callback
    return in_data, pyaudio.paContinue


stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                stream_callback=callback)

stream.start_stream()

while stream.is_active():  # <--------------------------------------------
    print(rms)    # may be losing some values if sleeping too long, didn't check
    time.sleep(0.1)

stream.stop_stream()
stream.close()

p.terminate()

关于python - PyAudio:如何以回调/非阻塞模式访问 stream.read() 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62618934/

相关文章:

python - 如何跟踪当前在 Jupyter 笔记本中运行的单元格?

python - Groupby、计数并计算 Pandas 中的中位数

python - 根据另一个数组对齐 numpy 数组

python:是否可以要求函数的参数都是关键字?

android - Android MediaMuxer音频问题

ios - 使用AudioConverter将PCM转换为AAC并使用AVAssetWriter写入.mp4文件时,音频失真

python - 从列表项向字典添加新键时出错

python-3.x - 如何在 matplotlib 中创建具有垂直阴影的绘图

Django迁移错误: Column does not exist

audio - 具有极长音频的 FFMPEG 视频