Making Processing talk
Since Processing has no speaking capabilities out of the box, our first task is adding an external library using the new Processing Library Manager. We will use the ttslib package, which is a wrapper library around the FreeTTS library.
We will also create a short, speaking Processing sketch to check the installation.
Engage Thrusters
Processing can be extended by contributed libraries. Most of these additional libraries can be installed by navigating to Sketch | Import Library… | Add Library..., as shown in the following screenshot:
In the Library Manager dialog, enter
ttslib
in the search field to filter the list of libraries.Click on the ttslib entry and then on the Install button, as shown in the following screenshot, to download and install the library:
To use the new library, we need to import it to our sketch. We do this by clicking on the Sketch menu and choosing Import Library... and then ttslib.
We will now add the
setup()
anddraw()
methods to our sketch. We will leave thedraw()
method empty for now and instantiate aTTS
object in thesetup()
method. Your sketch should look like the following code snippet:import guru.ttslib.*; TTS tts; void setup() { tts = new TTS(); } void draw() { }
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Now we will add a
mousePressed()
method to our sketch, which will get called if someone clicks on our sketch window. In this method, we are calling thespeak()
method of theTTS
object we created in thesetup()
method.void mousePressed() { tts.speak("Hello, I am a Computer"); }
Click on the Run button to start the Processing sketch. A little gray window should appear.
Turn on your speakers or put on your headphones, and click on the gray window. If nothing went wrong, a friendly male computer voice named
kevin16
should greet you now.
Objective Complete - Mini Debriefing
In steps 1 to 3, we installed an additional library to Processing. The ttslib is a wrapper library around the FreeTTS text-to-speech engine.
Then we created a simple Processing sketch that imports the installed library and creates an instance of the TTS
class. The TTS
objects match the speakers we need in our sketches. In this case, we created only one speaker and added a mousePressed()
method that calls the speak()
method of our tts
object.