Wednesday, March 6, 2013

Let your software speak! (Lazarus+Freepascal+eSpeak tutorial)


TTS or Text-To-Speech is a technology which allows the speech synthesis or artificial speaking of computers. Another way to explain it is that it says what you write. You can write whatever text you want and it will say it! It can read poems or essays or even say bad words to someone if you wish! It is a technology which, well, speaks for itself! ;-)

eSpeak is a command line TTS tool which is almost useless when used alone. But it seems like a powerful and flexible TTS engine when used in your software through command line calls. eSpeak is open source and a better option than Festival (according to me). eSpeak supports Windows/Linux/Mac platforms. The windows version uses Microsoft SAPI5.

Step 1: Download and Install Lazarus

We will use Lazarus as our IDE (Integrated Development Environment) which is powered by Free Pascal. If you haven't installed it yet then you can download it for free from here:
http://sourceforge.net/projects/lazarus/files/

(Choose Windows 32bit if you are not sure about your processor architecture.)

Step 2: Download and install eSpeak (we'll use as stand alone later)

Download and install eSpeak from here:
http://espeak.sourceforge.net/download.html
The file should be named something like: espeak-1.46.02-win.zip

Create a directory where you will save the source code for the program. For example, D:\LazarusCodes\TTS-eSpeak-Test

Assuming that you installed eSpeak in the default installation directory C:\Program Files\eSpeak , now copy the installation folder to D:\LazarusCodes\TTS-eSpeak-Test. Thus you will have a D:\LazarusCodes\TTS-eSpeak-Test\eSpeak\command_line directory.

You may now uninstall eSpeak (we will use --path parameter to specify espeak-data directory which will allow us to use eSpeak in the stand alone mode, which will allow the user to use eSpeak even when it is not installed! It is a rather portable option. Cool, right?!)

Step 3: Code in Lazarus

1. Open Lazarus.
2. Create a new Project, type of Application.
3. Toggle to form design mode (F12). Drop a TMemo and a TButton in the form.
4. Also go to System tab and add a TProcess in the form. Set the Options to [poUsePipes] (Click the arrow/plus on the left and set poUsePipes to true). Also set Show Window to swoHIDE. (swoHide option hides the console window when running the command line tool.)
5. Write the following code:

procedure TForm1.Button1Click(Sender: TObject);
begin

  // We will call the eSpeak command line exe
  Process1.CommandLine:= ExtractFilePath(Application.ExeName) + 'eSpeak\command_line\espeak.exe' +
                          ' --path=eSpeak ' +
                          ' "' + Memo1.Text + '"';
  Process1.Active:=True;

end;  

6. Click File -> Save All and save your project in the directory D:\LazarusCodes\TTS-eSpeak-Test.

Step 4: Run and test

Press F9 to compile and see the program how it works. Write something in the Memo and press the button. You will hear your words spoken. You may customize the interface (add images, speedbuttons, change captions etc.) and Run (F9) again.




When you want to distribute the code don't forget to Strip and upx the exe to reduce its size. You will need to distribute the exe and the eSpeak directory to show off your skills to your friends or your keen users!

Step 5: Enhance the code (Optional)


Sometimes when the user enters large text often there is need to stop the speaking. You can drop another TButton (or TSpeedButton or TBitButton .... ) and double click it. Then write the command:

Process1.Terminate(0);

You can also drop a TEdit on the form and use it to specify further parameters to modify speed, pitch, volume, word gap and many other options of the spoken text. Now the Button1 command may change like this (or anything else, come on you are the programer!):

  // We will call the eSpeak command line exe
  Process1.CommandLine:= ExtractFilePath(Application.ExeName) + 'eSpeak\command_line\espeak.exe' +
                          ' --path=eSpeak ' + Edit1.Text + ' ' +
                          ' "' + Memo1.Text + '"';

Command line parameters and explanations are available here:
http://espeak.sourceforge.net/commands.html

Reference:
http://www.rolfware.de/delphi/espeak_example.html
http://www.freepascal.org/docs-html/rtl/sysutils/executeprocess.html
http://www.mail-archive.com/lazarus@lists.lazarus.freepascal.org/msg26498.html

0 comments:

Post a Comment

Random Posts

Visitors

free counters

Add Us in Your Circles

  © Ink of life 2017 | Privacy Policy

ProBlogger Template by Ourblogtemplates.com

Back to TOP