0

I'm reading the output of a command that generates a WAV file and I play the result piping these two processes.

#Generate WAV stream on output
process = Popen(['larynx', '-v', environ["VOICE"], "--raw-stream", "\"{}\"".format(message)], stdout=PIPE)
#Reads WAV input stream and plays it
process2 = Popen(["aplay", "-r", "22050", "-c", "1", "-f", "S16_LE"], shell=True, stdin=PIPE, bufsize=4000)

while True:
     data = process.stdout.read(4000)
     if(len(data) == 0): break
     process2.stdin.write(data)

It just plays a corrupted version of what it should play. Reading aplay stderr reveals that it detects a wrong bitrate and sample rate (8bit ad 8KHz instead of 16bit and 16KHz) Meanwhile if I just use the command:

larynx -v en --raw-stream my_message | aplay -r 22050 -c 1 -f S16_LE

It plays the result correctly. I think stdout.read is losing my WAV file shape. Any idea on how to solve this?

fra98_
  • 31
  • 3
  • 1
    `shell=True` is causing `aplay` to be run with no arguments. It has nothing to do with `stdout.read()`. – Charles Duffy Nov 17 '21 at 17:21
  • @CharlesDuffy You're right! I don't even know why I placed shell=True, thank you so much! – fra98_ Nov 17 '21 at 17:27
  • BTW, you might think of directly connecting the two processes together instead of having Python sit in the middle; see https://docs.python.org/3/library/subprocess.html#replacing-shell-pipeline (particularly the examples where you set process2 to have `stdin=process.stdout`). And if you _do_ keep Python in the middle, it'll be a bit more efficient if you do your reads and writes in page-aligned chunks -- so do 8192 bytes at a time instead of 4000. – Charles Duffy Nov 17 '21 at 23:12
  • @CharlesDuffy I had to simplify because of the question. In my real system case I have a client with the aplay process and a docker container with the larynx process. I know I can just pipe the two processes, but im my case they're actually running on separate machines. While testing they were actually just the two commands piped in one shell command, I had to separate them later. You're right though, better with pre-aligned chunks – fra98_ Nov 19 '21 at 00:51

0 Answers0