实时播放 python 中的原始音频文件
play raw audio file in python in realtime
我在 python 有一个 udp
服务器,它连续接收来自客户端的原始格式字节数组的语音数据包。如何实时播放服务器端的语音?有什么推荐的库或方法吗?
如果需要的话,这是我非常简单的服务器代码(我对此表示怀疑)
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()
有一种使用回调方法的方法可能会更好。
我在 python 有一个 udp
服务器,它连续接收来自客户端的原始格式字节数组的语音数据包。如何实时播放服务器端的语音?有什么推荐的库或方法吗?
如果需要的话,这是我非常简单的服务器代码(我对此表示怀疑)
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()
有一种使用回调方法的方法可能会更好。