Computers that speak…
There is something magical about making a computer speak. I remember just getting started with them and my friends and I would attempt to convince the computer to say all manner of, ahem, interesting things. None the less. Getting early starters in on the fun is a great way to keep them interested.
Flight is a small and easy-to-install text-to-speech utility… that happens to work great on a Raspberry PI. To get started install it using apt:
sudo apt-get install -y flite
Your first use of the program is easy. Just put the text you’d like to run after the -t argument like this:
flite -t "Hello computer master. Lots of fun to be had now!"
Now try replacing that text with your own. I’ll wait…
Try it with code…
Running the program on the command line is a great first step. The next is to make it dynamic with a little code, but first. We will take a look at how to use Python to run programs that are available to the command line. To do that we will need:
from subprocess import call call(["ls","-l"])
Call is a special function we are going to give ourselves access to. We need to ask for it from the subprocess library. A library is like a catalog of lots and lots of programs either provided by the language “python” or by 3rd party developers. Its another way software developers help each other out. When we create something useful we have the option of giving it to other developers so they can re-use it. Lots of engineers do this for fame, notoriety, or simply because its good for humanity.
In the above code, call means call another program and start it as a thing separate from what our code is doing. Lets now modify that example to start speaking for us:
from subprocess import call call(["flite","-t","Hello world!"])
Now run it!
Now something a little more interesting…
So, you’ve done the hello world! Time to make it say something a little more dynamic. Try making the computer say it many times:
from subprocess import call for x in range(0,3): call(["flite","-t","Hello world!"])
This code will cause it to say Hello World three times!
So that was fun. Now lets throw something dynamic into there:
from subprocess import call for x in range(0,3): call(["flite", "-t", "Hello world! Number " + str(x)]);
Now lets get it to say something crazy…
Lets make it say your name! Remember from Lesson 3 to take in input you need to use:
name = raw_input("What is your name?")
So lets have it say the prompt requesting it. Then take the input on the command line for the name.
from subprocess import call call(["flite", "-t",'What is your name? Enter it using the keyboard.']); name = raw_input("What is your name? "); call(["flite","-t",'Hello ' + str(name)]);
Finally… just make it say whatever…
Now we’ll use something called a while loop. That’s a common construct that lets you create programs that should continue until some condition is no longer true. For example this will run forever because True is always, well, true:
while True: value = raw_input("What should I say? "); call(["flite","-t", value]);
To kill that program hit the CTRL-C, which means using the “control” key and the “c” key at the same time. Which will kill the python program.
If you’d like to make it a little more elegant, we can use a combination of a While with an If statement. Like this:
keepGoing = True while keepGoing: value = raw_input("What should I say? "); call(["flite","-t", value]); if value == "done": keepGoing = False
A little thanks…
The inspiration for this lesson came from Adafruit’s Mike Barela. Thanks Mike!