UDP sound transfer : played sound have big noise

ami_GS picture ami_GS · Jan 16, 2014 · Viewed 8k times · Source

I have no idea how to solve this problem. Please help me :)

I would like to send sound data, recorded by one PC, to the other PC and play it. (by UDP)

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.

the following code is it

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

Answer

ami_GS picture ami_GS · Feb 15, 2014

I have searched for the reason of this noise. Finally I could detect why this happened.

Actually, This program UDP transfer did not cause packet loss.

Even if it did, the sound do not have such a serious noise.


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

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

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

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

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.