UDP sound transmission: the sound signal has a lot of noise

I do not know how to solve this problem. Please help me:)

I would like to send audio data recorded by one PC to another computer and play it back. (over UDP)

The program may work correctly, but the sound contains (?) Uncomfortable noise. when I tried to record and play sound in the same sequence of programs, it worked correctly. There was no noise. In the case of using UDP, even on one PC, use IP 127.0.0.1, noise has appeared. At first I thought that this factor was due to the fact that the reproduced sound is not available on another PC, and I fixed it by creating a buffer. He decided to make a little noise, but almost all the noise still remains.

following code:

Client

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() 

Server

 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 the long source code. Feel free to play this program.

+8
python udp networking audio pyaudio
source share
2 answers

I was looking for the cause of this noise. Finally, I was able to determine why this happened.

In fact, this transfer of the UDP program did not cause packet loss.

Even so, the sound does not have such serious noise.


This program sends data correctly, and packet loss practically does not occur, but the receive method cannot correctly receive data.


In the 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() 

This program can only have "25%" data. (I checked the amount of data)

So, I tried to get the data multiply (CHANNELS * 2)

  soundData, addr = udp.recvfrom(CHUNK * CHANNELS * 2) 

As a result, sound data can be obtained 100% completely.

Finally, sound recorded on one PC is reproduced on another PC without noise.

+8
source share

I ran into the same problem, but your solution did not help me. I found that using

 stream.write(frames.pop(0)) 

instead

 stream.write(frames.pop(0), CHUNK) 

Removes all noise in the received signal.

0
source share

All Articles