使用Python实时播放麦克风输入到PC [英] Using Python to playback mic input to PC in real-time

查看:89
本文介绍了使用Python实时播放麦克风输入到PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 进行麦克风监控",即除了来自 PC 的任何其他输出信号外,还通过耳机实时播放麦克风信号.

I'm trying to use Python to 'mic-monitor', i.e., to playback the mic signal through the headphones in real-time, in addition to any other output signal from the PC.

我可以通过修改我的 PC 的播放设置来实现这一点,但我想用 Python 来实现,这样我就可以对 Raspberry Pi 进行编程,以便对我的 PS4 廉价耳机进行麦克风监控.

I can accomplish this by amending my PC's playback settings, but I want to do it with Python, so that I can program a Raspberry Pi to mic-monitor my cheap headset for the PS4.

我可以几乎使用 PyAudio 或 sounddevice 来完成此操作,但有一个小而显着的延迟.所以:

I can almost accomplish this with PyAudio or sounddevice, but there is a small but significant delay. So:

  • 有没有办法用 Python 消除这种延迟,例如通过某种方式更直接地访问我的 PC 输入?
  • 否则,为什么不能使用 Python?
  • 我能否以与我的 PC 相同的方式将 Raspberry Pi 配置为麦克风监视器?

Sounddevice 代码如下供参考:

Sounddevice code is shown below for reference:

import sounddevice as sd
duration = 5.5  # seconds

def callback(indata, outdata, frames, time, status):
    if status:
        print(status)
    outdata[:] = indata

with sd.Stream(channels=2, callback=callback):
    sd.sleep(int(duration * 1000))

推荐答案

中间总是存在计算机延迟.专业音频设备通常是为最小延迟而定制的(或者只是模拟).为了减少延迟,您需要一次记录较小的块,然后再将它们发送到输出,这会引入更多的处理开销.使用较小的块有时还会在信号中引入更多的抖动,因为帧间延迟可能跟不上采样率.PortAudio 可能可以配置为具有更少的延迟,但您可能也会从操作系统和音频驱动程序中获得很多延迟.这是一个讨论如何优化操作系统和音频驱动程序以实现最小延迟的页面在树莓派上.PortAudio(大多数 python 音频库背后的驱动程序),还有一个关于音频驱动程序的讨论延迟取决于您的操作系统.

There will always be latency with a computer in-between. Professional audio gear is usually custom built for minimal latency (or it's just analog). To reduce latency you need to record smaller chunks at a time before sending them to the output which does introduce more processing overhead. Using smaller chunks can also at some point introduce more jitter in the signal because the inter-frame latency might not keep up with the sample rate. PortAudio is probably likely able to be configured to have a bit less latency, but you're probably getting a lot of the delay from the OS and audio drivers as well. Here's a page discussing how you can optimize the OS and audio drivers for minimal latency on a Raspberry Pi. PortAudio (the driver behind most python audio libraries), also has a discussion on audio driver latency based on your operating system.

查看文档 对于sd.Stream,看起来即使你指定一个更小的blocksize,由于实现,它可能会使延迟更糟.

looking at the documentation for sd.Stream, it looks like even if you specify a smaller blocksize, due to the implementation, it may make latency even worse.

然而,有一个选项可以指定确切的延迟(如果需要特定的延迟)或尽可能快地"实现最大努力;通过指定 latency = "low" 这会尝试考虑您正在使用的特定硬件,并尽可能快地运行.

There is however an option to specify an exact latency (if a particular delay is desirable) or to achieve a best effort "as fast as possible" by specifying latency = "low" This attempts to take into account the specific hardware you're using, and go as fast as possible.

这篇关于使用Python实时播放麦克风输入到PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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