Integration With Python

Below we provide two examples of generating speech with the festival program. 

  1. Saying Star Wars quotes based on button presses.
  2. Speaking Temperature using the MCP9808 sensor.

CircuitPython Library Support

If you are already running the latest Raspian distrobution you will need to install these two packages to run the CircuitPython examples in this guide. 

Download: file
sudo pip3 install adafruit-blinka
Download: file
sudo pip3 install adafruit-circuitpython-mcp9808

Saying Sounds and Using Buttons

Adafruit has a great tutorial on hooking buttons to your Raspberry Pi and using Python to output sounds: Playing sounds and using buttons with Raspberry Pi. That tutorial uses the omxplayer program to play sounds.  We'll change the code to use festival and say programmed sentences:

import time
import os
import board
import digitalio

button1 = digitalio.DigitalInOut(board.D23)
button1.direction = digitalio.Direction.INPUT
button1.pull = digitalio.Pull.UP

button2 = digitalio.DigitalInOut(board.D24)
button2.direction = digitalio.Direction.INPUT
button2.pull = digitalio.Pull.UP

button3 = digitalio.DigitalInOut(board.D25)
button3.direction = digitalio.Direction.INPUT
button3.pull = digitalio.Pull.UP

print("press a button!")

while True:
    if not button1.value:
        os.system('echo "Use the force Luke!" | festival --tts')

    if not button2.value:
        os.system('echo "Some rescue!" | festival --tts')

    if not button3.value:
        os.system('echo "I find your lack of faith disturbing." | festival --tts')

    time.sleep(0.1)

We can easily copy this code onto our Pi using the 'wget' command and run it using the following python syntax.

Download: file
wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Speech_Synthesis_on_the_Raspberry_Pi/saying_sounds_using_buttons.py
sudo python3 ./saying_sounds_using_buttons.py

Speaking Temperature

You can also programmatically change the sentences depending on your code. We'll modify the Adafruit tutorial MCP9808 Temperature Sensor Python Library. The tutorial shows wiring the sensor to your Pi and printing out values. Instead of printing the values, suppose we have Flite read the value to you.  The code would change to:

import os
import time
import board
import busio
import adafruit_mcp9808

# This example shows how to get the temperature from a MCP9808 board
i2c_bus = busio.I2C(board.SCL, board.SDA)
mcp = adafruit_mcp9808.MCP9808(i2c_bus)

while True:
    # print precise temperature values to console
    tempC = mcp.temperature
    tempF = tempC * 9 / 5 + 32
    print('Temperature: {} C {} F '.format(tempC, tempF))

    # drop decimal points and convert to string for speech
    tempC = str(int(tempC))
    tempF = str(int(tempF))
    os.system("echo 'The temperature is " + tempF + " degrees' | festival --tts")

    time.sleep(60.0)

The sky is the limit as far as having sentences change depending on sensors, time of day, the weather, and more.

We can easily copy this code onto our Pi using the 'wget' command and run it using the following python syntax.

Download: file
wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Speech_Synthesis_on_the_Raspberry_Pi/speaking_temperature.py
sudo python3 ./speaking_temperature.py
This guide was first published on Feb 01, 2016. It was last updated on Feb 01, 2016. This page (Integration With Python) was last updated on Oct 21, 2019.