本文介绍了在 python 中实时播放原始音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 python 中有一个 udp
服务器,它以原始格式,字节数组连续接收来自客户端的语音数据包.如何在服务器端实时播放语音?任何推荐的库或方法?
I have a udp
server in python that continuously receives voice packets from a client in raw format, array of bytes. How can I play the voice on the server side in real time? Any recommended libraries or ways to do it?
如果需要,这是我非常简单的服务器代码(我对此表示怀疑)
Here is my very simple server code if needed (which I doubt)
import socket
UDP_IP = "192.168.1.105"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
#what to do to stream the incoming voice packets?
推荐答案
PyAudio https://people.csail.mit.edu/hubert/pyaudio/
import pyaudio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=44100,
output=True)
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
while data != '':
stream.write(data)
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
stream.stop_stream()
stream.close()
p.terminate()
有一种使用回调方法的方法可能会更好.
There is a way of using a callback method which might be better.
这篇关于在 python 中实时播放原始音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!