Pregunta

I was wondering if there was a way to access espeak as you might in the command line:

laptop:~$espeak
say this line first
say this line second
...

Right now, the only ways I can do it in python is process = subprocess.Popen(['espeak'], stdin=subprocess.PIPE ), followed either by

process.communicate(expression)

or

process.stdin.write(expression)
process.stdin.close()

The former blocks the rest of the program until espeak is finished, not desirable behavior, while the latter doesn't block but allows for the possibility of overlap, say if my program calls espeak twice too quickly. I was wondering if there was a way to call espeak like in the command line, where I could quickly input multiple things to speak, but it would only say the second thing after the first completed, for example.

In particular, both of the examples above shut down espeak after a single expression. Is there a way to avoid this?

Edit: The answer, given by ed., is to write with newlines at the end then flush

process = subprocess.Popen(['espeak'], stdin=subprocess.PIPE )
process.stdin.write("say this line first\n")
process.stdin.flush()
process.stdin.write("say this line second\n")
process.stdin.flush()

process.stdin.close()
¿Fue útil?

Solución

You could try calling process.stdin.write(expression) where expression has a newline at the end, and don't call process.stdin.close() until you're done with espeak.

If that doesn't work then add a process.stdin.flush() call after the write.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top