Text-to-speech: give the robot a voice
Time: 10:15 AM to 10:55 AM
espeak text-to-speech engine works, build a program to explore different voices, and then combine speech with driving controls.
Test espeak from the terminal
Before writing any code, verify the speaker works:2>/dev/null suppresses audio system warnings — the speech still plays normally.
espeak parameters
espeak takes several flags that change how the speech sounds:
Try these in the terminal:
Program 1: TTS explorer
Build a menu-driven program that lets you try all the espeak options interactively.Step 1 — The speak function
This function wraps espeak so you can call it from Python. It pipes audio through PulseAudio with a direct-espeak fallback:os.system() runs a shell command. The --stdout flag makes espeak output audio data instead of playing it directly, and paplay plays it through PulseAudio to the correct speaker. The safe variable escapes single quotes so phrases like “I’m a robot” do not break the command.
Step 2 — Demo functions
Each demo explores one parameter. Here is the speed demo:Step 3 — Menu
Wire the demos into a menu:Run it
This script does not need
sudo because it does not use motors or servos — it only plays audio.Final code: tts_demo.py
Click to see the complete program
Click to see the complete program
Program 2: talking driver
Now combine speech with movement. The robot announces each action before performing it. Speech runs in a background thread so the robot keeps driving while it talks.Step 1 — Background speech
The key difference fromtts_demo.py: speech must not block driving. Use threading to run espeak in the background:
threading.Thread(target=_say, daemon=True).start() launches _say in a separate thread. The main loop immediately continues to the next keypress without waiting for espeak to finish. daemon=True means the thread automatically stops when the program exits.
Step 2 — The control loop
Same WASD pattern as the keyboard driver, but each action callsspeak() first:
Run it
Final code: talking_driver.py
Click to see the complete program
Click to see the complete program
Challenge: customize the talking driver
1
Custom startup message
Change the
speak("Ready to drive") line to your own greeting. The robot will say it every time the script starts.2
Add speed announcements
Add
elif blocks for + and - keys that announce the new speed. For example: speak(f"Speed {speed} percent").3
Custom voice
Change the voice in the
speak() function. Try espeak -v en+f3 for a female voice, or -v en+m7 for a whisper.After this section, take a 10-minute break (10:55 AM to 11:05 AM).

