play raw audio file in python in realtime
Asked Answered
H

1

7

I have a udp server in python that continuously receives voice packets from a client in raw format, array of bytes. How can I play the voice on the server side in real time? Any recommended libraries or ways to do it?

Here is my very simple server code if needed (which I doubt)

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?
Henryk answered 2/5, 2017 at 11:44 Comment(0)
B
14

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

There is a way of using a callback method which might be better.

Bronchopneumonia answered 2/5, 2017 at 12:32 Comment(4)
This just receives the incoming stream and saves it to a PyAudio object, right? Or does it play it as well?Henryk
It plays the raw data through the speakers. You have to use input=True instead of output=True to capture audio data from a microphone.Bronchopneumonia
You will have to change your data format, number of channels, and your sample rate to match what your audio data is. Also make sure you are streaming fast enough. If data, addr = sock.recvfrom(1024) takes too long to return it'll probably cause skips in your audio data.Bronchopneumonia
This solution works for me. I was working on pymumble and need to play the sound captured from the mumble server. The format of the captured sound is "16 bits signed mono little-endian 48000Hz". I modified stream as follows stream = p.open(format=pyaudio.paInt16, channels=1, rate=48000, output=True) and the sound from server is played.Martian

© 2022 - 2024 — McMap. All rights reserved.