问题描述
我不知道如何解决这个问题.请帮帮我:)
I have no idea how to solve this problem. Please help me :)
我想将一台 PC 录制的声音数据发送到另一台 PC 并播放.(通过 UDP)
I would like to send sound data, recorded by one PC, to the other PC and play it. (by UDP)
程序可能正常运行,但声音包含(?)不舒服的噪音.当我试图记录 &在一个程序序列中播放声音,它工作正常.没有噪音.如果即使在一台 PC 上使用 UDP,使用 IP 127.0.0.1,就会出现噪音.起初,我认为这是因为播放的声音在另一台 PC 中出现了,我通过制作缓冲区来修复它.它解决了一点噪音,但几乎所有的噪音仍然存在.
The program might work correctly, but the sound contain(?) uncomfortable noise. when I tried to record & play sound in one program sequence, it worked correctly. There was no noise. In case of using UDP even in one PC, use IP 127.0.0.1, the noise appeared. At first, I thought the factor is because played sound is out in the other PC and I fixed it by making buffer. It solved little noise, but almost all the noise is still remaining.
就是下面的代码
客户
import pyaudio
import socket
from threading import Thread
frames = []
def udpStream():
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
if len(frames) > 0:
udp.sendto(frames.pop(0), ("127.0.0.1", 12345))
udp.close()
def record(stream, CHUNK):
while True:
frames.append(stream.read(CHUNK))
if __name__ == "__main__":
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = CHUNK,
)
Tr = Thread(target = record, args = (stream, CHUNK,))
Ts = Thread(target = udpStream)
Tr.setDaemon(True)
Ts.setDaemon(True)
Tr.start()
Ts.start()
Tr.join()
Ts.join()
服务器
import pyaudio
import socket
from threading import Thread
frames = []
def udpStream(CHUNK):
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp.bind(("127.0.0.1", 12345))
while True:
soundData, addr = udp.recvfrom(CHUNK)
frames.append(soundData)
udp.close()
def play(stream, CHUNK):
BUFFER = 10
while True:
if len(frames) == BUFFER:
while True:
stream.write(frames.pop(0), CHUNK)
if __name__ == "__main__":
FORMAT = pyaudio.paInt16
CHUNK = 1024
CHANNELS = 2
RATE = 44100
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels = CHANNELS,
rate = RATE,
output = True,
frames_per_buffer = CHUNK,
)
Ts = Thread(target = udpStream, args=(CHUNK,))
Tp = Thread(target = play, args=(stream, CHUNK,))
Ts.setDaemon(True)
Tp.setDaemon(True)
Ts.start()
Tp.start()
Ts.join()
Tp.join()
抱歉,源代码太长了.随意玩这个程序.
sorry for long source code. Feel free to play this program.
推荐答案
我已经搜索了这个噪音的原因.终于我知道为什么会这样了.
I have searched for the reason of this noise. Finally I could detect why this happened.
其实这个程序的UDP传输并没有造成丢包.
Actually, This program UDP transfer did not cause packet loss.
即使有,声音也没有那么大的噪音.
Even if it did, the sound do not have such a serious noise.
本程序正确发送数据,几乎没有丢包,但receive"方法无法正确接收数据.
This program sent data correctly, and there are almost no packet loss, but the "receive" method could not receive data correctly.
在服务器程序中
In server program
def udpStream(CHUNK):
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp.bind(("127.0.0.1", 12345))
while True:
soundData, addr = udp.recvfrom(CHUNK)
frames.append(soundData)
udp.close()
这个程序只能提供25%"的数据.(我检查了数据量)
This program could data only "25%". (I checked the amount of data)
所以,我尝试接收数据乘法(CHANNELS * 2)
So, I tried to receive the data multiply (CHANNELS * 2)
soundData, addr = udp.recvfrom(CHUNK * CHANNELS * 2)
这导致声音数据可以100%完全接收.
This results in the sound data can be received 100% completely.
最后,一台电脑录制的声音在另一台电脑上播放,没有噪音.
Finally, the sound recorded by one PC is played in the other PC without noise.
这篇关于UDP声音传输:播放的声音有很大的噪音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!