【问题标题】:Playing soundcard input to soundcard output in Python在 Python 中播放声卡输入到声卡输出
【发布时间】:2018-02-04 22:13:27
【问题描述】:

有谁知道是否有办法使用 Python 将某个声卡输入播放到同一个声卡中的输出? (或任何其他语言)

如果是这样,是否可以编写一个脚本来读取声卡的输入并将其反弹到同一张卡上的输出,如果此输入静默超过十分钟,则将其重新路由到输出?

提前致谢

编辑:欢迎任何操作系统的答案。首选 Windows 或 linux 发行版,OSX 也可以接受。

【问题讨论】:

  • 我认为这将取决于您的操作系统,因此您应该编辑您的问题并包括您正在运行的问题
  • @notorious.no 可以

标签: python linux audio pyaudio portaudio


【解决方案1】:

pyaudio 有一个称为非阻塞回调的方法,其中输入(例如麦克风)按原样直接路由到输出(扬声器/耳机),这意味着无需对传入流进行任何操作。
不确定这是否是您要找的。
如果您需要在写入(或发送到 putput)之前操作流,那么您有 @rewolf 的答案。

源代码来自site

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

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)

stream.stop_stream()
stream.close()

p.terminate()

【讨论】:

    【解决方案2】:

    拣货设备

    您可以通过 PyAudio API 获取支持的设备列表:

    p = pyaudio.PyAudio()
    host_info = p.get_host_api_info_by_index(0)    
    device_count = host_info.get('deviceCount')
    
    # can iterate through devices and check their info:
    p.get_device_info_by_host_api_device_index(0, device_index)
    
    {'defaultSampleRate': 44100.0, 'defaultLowOutputLatency': 0.01, 'defaultLowInputLatency': 0.0029024943310657597, 'maxInputChannels': 2L, 'structVersion': 2L, 'hostApi': 0L, 'index': 0L, 'defaultHighOutputLatency': 0.1, 'maxOutputChannels': 0L, 'name': u'Built-in Microphone', 'defaultHighInputLatency': 0.013061224489795919}
    

    实时录制和播放

    可以通过打开和读取输入流来完成录制,并且可以同时通过输出流播放。 可以使用input_device_indexoutput_device_index选择设备

    例如,这会立即录制和播放:

    import pyaudio
    
    BUFFER_SIZE = 4096
    DURATION = 5
    SAMPLE_RATE = 44100
    
    p = pyaudio.PyAudio()
    
    input_stream = p.open(
        format=pyaudio.paInt16,
        channels=2,
        rate=SAMPLE_RATE,
        input=True,
        frames_per_buffer=BUFFER_SIZE,
        input_device_index=0
        )
    
    output_stream = p.open(
        format=pyaudio.paInt16,
        channels=2,
        rate=SAMPLE_RATE,
        output=True
        )
    
    for i in xrange(int(SAMPLE_RATE / BUFFER_SIZE * DURATION)):
        data = input_stream.read(BUFFER_SIZE)
        output_stream.write(data)
    
    input_stream.stop_stream()
    input_stream.close()
    output_stream.stop_stream()
    output_stream.close()
    p.terminate()
    

    我并没有完全关注您关于在卡之间弹跳音频等的确切问题。但我相信您可以从我上面提到的内容中弄清楚。

    希望对你有帮助

    【讨论】:

    • 这很有帮助,谢谢!我的问题具体是如何将给定的输入直接路由到输出。模拟模拟本质上是拿一把电吉他并切换它通过的放大器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 2012-01-16
    • 1970-01-01
    相关资源
    最近更新 更多