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?