0

I want to write a game using pygame that is able to produce text-to-speech output.

I found the package pyttsx3, which works fine on its own, e.g. like this:

import pyttsx3

engine = pyttsx3.init()
engine.say("abc")
engine.runAndWait()

pygame on its own works fine as well, including audio. However, I can't get pyttsx3 to work together with pygame:

import pygame
import pyttsx3

pygame.init()
engine = pyttsx3.init()
engine.say("abc")
engine.runAndWait()

When I run the script, I get no audio, but the following text output:

$ python example.py
pygame 2.1.0 (SDL 2.0.16, Python 3.9.7)
Hello from the pygame community. https://www.pygame.org/contribute.html
ALSA lib pcm_dmix.c:1035:(snd_pcm_dmix_open) unable to open slave
aplay: main:831: audio open error: Device or resource busy

This behaviour is independent of the import or init sequence. I'm running Arch Linux.

Tonechas
  • 13,398
  • 16
  • 46
  • 80
  • do you have the same problem when you run `PyGame` without `tts`? Maybe problem is different. – furas Nov 14 '21 at 23:36
  • code works for me on Linux Mint 20 (based on Ubuntu 20.04) pygame 2.0.1 (SDL 2.0.14, Python 3.8.10) – furas Nov 14 '21 at 23:38
  • @furas: No, pygame (including audio) works fine without pyttsx3. – brotherlove Nov 15 '21 at 22:14
  • it is strange. The only idea: save to file `engine.save_to_file("abc", "audio.wav")` and play in `PyGame` - `s = pygame.mixer.Sound("audio.wav")` `s.play()` `while pygame.mixer.get_busy(): pass` – furas Nov 15 '21 at 22:34
  • @furas: Tnx for the workaround. An `engine.runAndWait()` is necessary after the `engine.save_to_file("abc", "audio.wav")`, but then it works. Yet, it's only a workaround for the time being. – brotherlove Nov 17 '21 at 13:49
  • I would dig in source code to see if you could use `io.BytesIO` (instead of file on disk) to save data in memory and later read from memory. – furas Nov 17 '21 at 17:11

0 Answers0