Last mod: 2024.12.22

Raspberry Pi - Speech synthesizers

Minicomputers like the Raspberry Pi are an excellent fit for IoT applications due to their compact size, versatility, and affordability. With the ability to run a full Linux-based operating system, they offer an easy way to integrate with sensors, actuators, and other hardware components. One of the cool things about these devices is how easily they can interact with human users, especially when it comes to speech synthesis.

Imagine setting up a Raspberry Pi to read out alerts, provide status updates, or even engage in conversations for applications like smart home devices or assistive technology. With tools like espeak or Festival, you can easily turn text into speech. These text-to-speech engines work on minimal hardware, making them perfect for smaller devices.

Hardware

We need two devices:

  1. Raspberry Pi, almost any version, important thing is that it has audio output. Raspbian OS or other standard distribution based on Debian.
  2. Output speakers with mini jack connector

Raspberry and speakres

Today we already have many speech synthesis libraries. Some examples:

eSpeak

Install:

sudo apt-get update
sudo apt-get install espeak

Let's test it:

espeak -a 75 -g 4 -p 10 -s 150 "Hello World!"

Where based on the manual:

  • a - Amplitude, 0 to 200, default is 100
  • g - Word gap. Pause between words, units of 10mS at the default speed
  • p - Pitch adjustment, 0 to 99, default is 50
  • s - Speed in approximate words per minute. The default is 175

Full parameter list is avaialbe on command:

espeak -h

Pico TTS

Install:

sudo apt update
sudo apt install libttspico-utils

Verify Installation:

pico2wave --help

Generate speech audio and play it:

pico2wave -w output.wav "<TEXT>"
aplay output.wav

pico2wave

We can either prepare ready-made WAV files with messages for the IoT device or generate, play and delete them. Here, for example, a script picoSpeak.sh may be helpful:

#!/bin/bash
text="$1"
pico2wave -w temp.wav "$text"
aplay temp.wav
rm temp.wav

Save it and make it executable:

chmod +x picoSpeak.sh

Run it with:

./picoSpeak.sh "Hello World!"

Volume control

To control the volume from bash commands, we can use the amixer command:

amixer cset numid=1 70%

Where:

  • numid - output mixer, we can list by command:
amixer controls
  • 70% - volume as percentage

Command to display status:

amixer

Example effect:

amixer