简体   繁体   中英

Python piping received tcp/udp pcm data from network socket into subprocess (aplay)

I can't for the life of me figure this out.. I'm trying to take data received on a network connection, and then pipe it to a subprocess, which will stream the data to my soundcard via aplay.

I've managed to do this, but it pauses briefly while it receives the data.

while True: 
    data = sock.recv(1024)
    p1.stdin.write(data)

What is the best way to setup a pipe that will stream from a network connection indefinitely? Setting up multiprocessing or threading to do this?

Thanks!

Playing back sound is especially tricky - human ears are sensitive to any kind of break in sound playback and the perceived quality would be bad. Further, you can never predict the delays/jitter/latency in the network. Hence, the following model is the best:

  1. Multi-threading - One thread receives from the network and submits to a queue. Another thread reads from the queue and submits audio data to sound card.
  2. Have a jitter buffer that can hold 1-2 seconds worth of data. (Depends on your application. If you are writing a voice chat app, not more than 150 msec of data should be buffered).
  3. If you run out of data in this buffer, re-buffer before starting playback. A temporary pause in playback is better than constant breaks in audio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM