简体   繁体   English

使用 PyAudio 在 Python 中进行环回(“你听到的”)录音

[英]Loopback ('What u hear') recording in Python using PyAudio

Good day,再会,

I'm trying to record my speaker output with Python using PyAudio.我正在尝试使用 PyAudio 用 Python 录制我的扬声器输出。 Currently, I'm able to record my microphone input and send this over to the 'listener'.目前,我能够记录我的麦克风输入并将其发送给“听众”。 What I'm trying to do now is create a loopback, so it will record the output from my speakers.我现在要做的是创建一个环回,这样它就会记录我的扬声器的输出。 I was able to do this with the 'Stereo Mix' from windows, but since this needs to be cross platform, there should be another way to do this.我能够使用 Windows 中的“立体声混音”来做到这一点,但由于这需要跨平台,因此应该有另一种方法来做到这一点。

Does anyone has some advice on how I could achieve this?有没有人对我如何实现这一目标有一些建议?

Here is my current code for recording an input stream.这是我当前用于记录输入流的代码。

import socket
import pyaudio
import wave

#record
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 40

HOST = '192.168.0.122'    # The remote host
PORT = 50007              # The same port as used by the server

recording = True

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

p = pyaudio.PyAudio()

for i in range(0, p.get_device_count()):
    print(i, p.get_device_info_by_index(i)['name'])

device_index = int(input('Device index: '))

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK,
                input_device_index=device_index)

print("*recording")

frames = []

while recording:
    data  = stream.read(CHUNK)
    frames.append(data)
    s.sendall(data)

print("*done recording")

stream.stop_stream()
stream.close()
p.terminate()
s.close()

print("*closed")

Any help would be greatly appreciated!任何帮助将不胜感激!

EDIT: I didn't see the cross-platform thing.编辑:我没有看到跨平台的东西。 Leaving this as a reference to the loopback device for Windows.将此作为对 Windows 环回设备的参考。

Install a virtual loopback device like [1] and select the adapter as your input device.安装一个像 [1] 的虚拟环回设备并选择适配器作为您的输入设备。 That works for me very well.这对我很有效。

You can check that with pyAudio like so:您可以使用 pyAudio 进行检查,如下所示:

>>> print p.get_device_count()
8

>>> print p.get_device_info_by_index(1)["name"]
Line 1 (Virtual Audio Cable)

So, I use 1 as my device_index.所以,我使用 1 作为我的 device_index。

[1] http://virtual-audio-cable.en.softonic.com/ [1] http://virtual-audio-cable.en.softonic.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM