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.
python udp networking audio pyaudio
ami_GS
source share