IoT Cookbook > Raspberry Pi > Speech Synthesis

Pi Speech Synthesis

Output (English) sentences from your Pi which are sent as text via WAMP.

Pi with speaker

The example uses the flite text-to-speech engine to convert english sentences to voice which is then output via the Pi's audio output.

Try it out

The code for the example consists of an adapter written in Python and AutobahnPython using Twisted. The adapter runs on the Pi and connects to running on a network accessible from the Pi.

Included is a frontend running in the browser. The frontend is written in JavaScript using AutobahnJS and connects to the same router instance as the adapter connects to. Consequently, the frontend is able to invoke the procedures exposed on the Pi and subscribe to events generated from there.

Preparations on the Pi

Enable audio output on the 3.5mm plug:

sudo amixer cset numid=3 1

Install the flite text-to-speech processor:

sudo apt-get install -y flite

Test the text-to-speech engine:

flite -voice slt "Hi, my name is Susan. How can I help you?"

The volume may be a bit low. To increase that to (near) the maximum, do

sudo amixer set PCM -- -100

to set this to near maximun (and try larger values instead of -100 to decrease the volume a bit again).

Install AutobahnPython

sudo pip install autobahn[twisted]

Running the Speech Synthesis

The code for this can be found in the crossbarexamples GitHub repository under iotcookbook. You need to clone this (or download it as a ZIP file).

You need a instance for the Speech Synthesis adapter on the Pi and the browser frontend to connect to.

The simplest way is to navigate to iotcookbook/device/pi/speechsynt in your local crossbarexamples repo, and do

crossbar start

This will also serve the browser frontend under http://localhost:8080/speechsynth_frontend.html.

Get onto the Pi, e.g. via scp

scp pi@<IP of your Pi>:~/

and then start it, passing the URL of and the realm to connect to, e.g.

python \
    --router 'ws://' \
    --realm 'iot_cookbook'

Then use the browser frontend to send a text of your choice. (Be aware that the full text you send is rendered to audio before playback starts. Long texts can take a while to start - so better to chunk them into smaller bits.)


The adapter exposes these procedures

and publishes event to these topics

Using it

No real difference to the trying out - run '' on the Pi, connect to a WAMP router, call from any WAMP component using the above API.