Skip to chapter content
navan✳THE BUILD LABPYTHON / GUIDE
0/26 read
CHAPTER 24 / 26 · TIME AND MEDIA

Speech synthesis and recognition.

Turn text into audio and audio into uncertain text with clear boundaries.

BY NAVAN SAMI5 MIN READTHEORY FIRST
01 / THE BASICS

Start here.

Speech synthesis turns text into audio; speech recognition turns an audio signal into guessed text. The two directions have different inputs and uncertainties. In synthesis, a program chooses words and a voice engine produces sound. In recognition, microphones capture sound and a model estimates words. Noise, accents, names, and microphone quality can affect the estimate, so the recognized text must be treated as uncertain external input.

pyttsx3 is a local text-to-speech library. The example creates an engine, queues the sentence with say, and plays queued speech with runAndWait. Queuing and playing are separate steps: the first describes what should be spoken, and the second performs the side effect. A working output also depends on the operating system's voice backend and audio device. A text version of important information should remain available if sound cannot be heard.

A recognition workflow needs an audio source, a chosen language or model, a result, and a way to respond when confidence is low or the service is unavailable. For consequential commands, such as sending a message, the program should show or repeat what it understood before acting. The chapter's model is not that speech is magically exact; it is that audio and text cross a boundary where meaning must be checked.

02 / THE IDEA

Build the model.

Text-to-speech turns written words into spoken audio; speech recognition estimates words from a recording. They solve different problems. A voice assistant often chains them, but the recogniser's text is only a hypothesis about what was said. Background noise, accents, names, and network conditions can change the result.

Design the spoken interaction around short, confirmable steps. If a program hears a command that could delete a file or send a message, repeat the interpreted action and ask for confirmation. If a recogniser cannot understand, ask for a retry or offer typed input. A transcript should be shown to the user when possible so errors are visible.

03 / UNDER THE SURFACE

How it works.

Speech synthesis can be local or cloud-based. Local engines may have limited voices but avoid sending text to a service; cloud engines may offer higher quality but need credentials and network access. Keep a text version of important output for accessibility and review. A script should not rely on sound alone to report an error.

Speech recognition starts with an audio source, captures a sample, and sends it to a recogniser or local model. Choose an appropriate language, sample duration, and timeout. Handle the two distinct failures of unintelligible audio and an unavailable service. Treat the transcript as external input and validate it just as you would text typed into a form.

For a reliable application, test with recordings from real conditions rather than only a quiet microphone. Record consent and privacy expectations before retaining audio. Measure recognition on the words that matter to your task, such as names or command verbs, and provide an alternative path when speech fails.

04 / A WORKED EXAMPLE

Read it line by line.

example.py
import pyttsx3
engine = pyttsx3.init()
engine.say('The report is ready')
engine.runAndWait()

pyttsx3 initialises a local speech engine, queues a sentence, and plays it when runAndWait executes. The example depends on the optional pyttsx3 package and a supported operating-system speech backend. Playback is a side effect, while the sentence remains ordinary text until the engine speaks it.

05 / ANOTHER EXAMPLE

See the rule again.

example-2.py
transcript = 'show report'
allowed = {'show report', 'show help'}
command = transcript.strip().lower()
if command in allowed:
    print('Ready:', command)
else:
    print('Please repeat')

The output is Ready: show report. An allowlist limits the actions an uncertain transcript can trigger. A real speech workflow should show or repeat the interpreted command before any side effect.

06 / REMEMBER THIS

A mistake to avoid.

Never treat a recognised voice command as certain identity or certain intent. Recognition errors and recordings can trigger the wrong action; confirm consequential commands through another step.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗Python · Understand the idea, then write the code.