GUIDE

Pygame text to speech with natural voices

Pygame is where many people write their first game, and sooner or later a character needs to talk. pyttsx3 speaks with the robotic system voices, and cloud APIs need a key and an internet connection, which a school computer or a game jam laptop may not have.

The SuperClonica engine is a Python package, so a Pygame project can voice text itself, offline, with natural voices in 31 languages, and play the result through pygame.mixer without touching a file. This guide shows the code, how to keep the game smooth while a line is voiced, and when to ship files instead.

Two ways to make a Pygame game talk

Use run-time speech for prototypes, tools and games you run from source; ship files for a finished game.

  • At run time: the game voices any text, such as the player’s name, and plays it at once.
  • Pre-voiced files: lines voiced during development, loaded with pygame.mixer.Sound like any sound effect.
  • Both use the same engine, voices and languages; Chinese is not supported yet.
  • Everything runs on an ordinary processor; no graphics card is needed.

Pick voices free online

On the page of any voice, type a line of your character, 30 to 100 characters, choose the language and press Voice it. In a few seconds you hear whether the voice fits, with no account.

Voice a line at run time

Loading the engine takes a few seconds, so create it once at start-up, not for every line.

  • Install both packages: python -m pip install superclonica-cpu pygame.
  • Set the mixer to the engine’s format before pygame.init(): pygame.mixer.init(frequency=44100, size=-16, channels=1).
  • Create the engine once, tts = Superclonica(), then voice and play a line: audio, rate = tts.synthesize("Welcome, hero! The village needs you.", "M2", "en"); sound = pygame.mixer.Sound(array=(audio * 32767).astype(np.int16)); sound.play().
  • The engine returns float samples at 44.1 kHz; scaling them to 16-bit integers is all pygame needs.

Keep the game smooth

A short line takes about half a second on a desktop processor, and a frozen frame is noticeable. Voice lines in a background thread and put the finished Sound into a queue that the main loop plays, or voice the lines of a level while its loading screen is shown. Keep a dictionary from text to Sound, so a line that repeats is voiced only once.

Shipping the game

Run-time speech needs the engine, about 385 MB of model and Python on the player’s computer, which is fine for a jam entry run from source but heavy for a download. For a released game, voice all lines in advance with tts.save('line.ogg', audio) and ship the OGG files. Use the ten stock voices of the model, M1 to M5 and F1 to F5, or your own voice trained from a speaker who agreed; fan-made character voices are for fan games and parody.