【问题标题】:How do I send PyAudio data over the internet using Socket with Python?如何使用 Socket 和 Python 通过 Internet 发送 PyAudio 数据?
【发布时间】:2021-07-04 02:45:23
【问题描述】:

所以......我关于 Stack Overflow 的第一个问题,哦,天哪,这是一个令人震惊的问题。我使用 Python 进行日常编程,我最近的项目是一个语音聊天应用程序。我需要用 PyAudio 来记录我要发送的数据,而且我过去用 PyAudio 成功地记录了一些东西,所以这不是问题。下一部分是。 我不知道如何使用 Socket!

有人可以尝试给我一些代码来帮助我解决问题吗?在你说“去阅读教程!”之前,我已经尝试这样做了,我的大脑快要爆炸了。那么有人可以帮忙吗?

为了表明我并非完全无助,这里有一些代码可以录制 10 秒的音频并将其作为 .wav 文件保存到与保存 .py while 相同的位置:


import pyaudio
import wave

chunk = 1024  # Record in chunks of 1024 samples
sample_format = pyaudio.paInt16  # 16 bits per sample
channels = 1
fs = 44100  # Record at 44100 samples per second
seconds = 10
filename = "output.wav"

p = pyaudio.PyAudio()  # Create an interface to PortAudio

print('Recording')

stream = p.open(format=sample_format,
                channels=channels,
                rate=fs,
                frames_per_buffer=chunk,
                input=True)

frames = []  # Initialize array to store frames

# Store data in chunks for 10 seconds
for i in range(0, int(fs / chunk * seconds)):
    data = stream.read(chunk)
    frames.append(data)

# Stop and close the stream 
stream.stop_stream()
stream.close()
# Terminate the PortAudio interface
p.terminate()

print('Finished recording')

# Save the recorded data as a WAV file
wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)

就是这样……祝你有美好的一天!

PS:要更改上述脚本记录的秒数,请将变量“秒”从 10 更改为您想要的任何值。

【问题讨论】:

    标签: python


    【解决方案1】:

    因为听起来您的主要困难是通过某种网络协议传输文件/数据,所以我将使用 ZeroMQ 来说明这个概念,因为它的开销较小,因此我们可以专注于重要的事情。您可以使用pip install zmq 安装它。

    首先,确保您正在编写的文件已关闭:

    wf.close()
    

    之后就可以使用以下sn-p发送文件内容了:

    import zmq
             
    PORT = 12345  # must match the receiver port
    context = zmq.Context()
    socket = context.socket(zmq.PAIR)
    socket.connect(f'tcp://localhost:{PORT}')
                                                                                          
    with open('output.wav', 'rb') as in_file:
        file_content = in_file.read()
                                                                                          
    print(f'Sending {len(file_content)} bytes.')                                        
    socket.send(file_content) 
    

    接收器部分可以这样实现:

    import zmq
                                                                                      
    PORT = 12345  # must match the sender port
    context = zmq.Context()
    socket = context.socket(zmq.PAIR)
    socket.bind(f'tcp://*:{PORT}')
    
    msg = socket.recv()                                                            
    print(f'Received {len(msg)} bytes.')
    

    如果您现在在两个单独的终端中运行发送方和接收方,您将(理想情况下)看到发送方发送的数据被接收方接收。您可以将它们写入文件和/或播放它们。 您可以对 vanilla socket 执行相同的操作,但需要更多的开销来设置和收集数据,并将其拆分为更小的块。 无论哪种方式,您都必须决定如何处理双向通信。您将不得不在无限循环中进行发送和接收。上面的代码只显示了一次数据传输。

    【讨论】:

    • 请不要被我的措辞弄糊涂了。如果您在 Linux 下工作,您将打开两个终端(或更准确地说:终端仿真器),然后分别启动您的发送方和接收方应用程序。无论您使用什么操作系统,只需启动这两个应用程序,它们就可以进行通信。
    猜你喜欢
    • 2014-10-27
    • 2021-03-27
    • 2015-07-07
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2019-10-13
    相关资源
    最近更新 更多