Lazarus eSpeak TTS Example - Let your software speak! lazarus / 8-Other ---------------------------------------------------------------------- nsunny · 2013-03-06 14:33 I love Lazarus. But sometimes I find that resources for Lazarus is very scarce. Also the weird fact is there are many programers who know many things but are not interested to share that in the form of tutorials or sample codes. That's why we have to use code from Delphi sites (which is sad). I am still a newbie even after over 1 year of exposure to Lazarus (due to busyness I guess). But I want to share this little piece of knowledge to this community for any benefit that may come. QuoteA more detailed tutorial (for newbies) can be found here: http://inkoflife.blogspot.com/2013/03/let-your-software-speak.html (EDIT: Further info and command line parameter details can be found here: http://wiki.lazarus.freepascal.org/espeak ) Step 1: 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 2: Code in Lazarus 1. Open Lazarus. 2. Create a new Project. 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: Code: [Select]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. File -> Save All and save your project in the directory D:\LazarusCodes\TTS-eSpeak-Test. Step 3: 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. Step 4: 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: Code: [Select]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!): Code: [Select]  // 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 are available here: http://espeak.sourceforge.net/commands.html Reference / Assistance: 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 ---------------------------------------------------------------------- BigChimp · 2013-03-06 14:39 Thanks - that looks nice. However, could you please post tutorials on the wiki http://wiki.lazarus.freepascal.org/espeak ... that makes it much easier to find them as well as edit/update them. On the wiki you will also find an article for using SAPI (Windows Speech API) and IIRC also festival TTS. Thanks ---------------------------------------------------------------------- theo · 2013-03-06 14:50 The SAPI page is a starting point for TTS : http://wiki.freepascal.org/SAPI ---------------------------------------------------------------------- nsunny · 2013-03-07 07:02 Thanks BigChimp and theo. I have edited both the wikis. ---------------------------------------------------------------------- BigChimp · 2013-03-07 07:16 Thanks nsunny, that was a great contribution! I took the liberty of removing the duplicate code on the SAPI page - and mention it is cross-platform Linux+OSX as well! Thanks a lot ---------------------------------------------------------------------- theo · 2013-03-07 11:13 You can also import SAPI Voice as an Activex now (FPC trunk?) I'll probably write a wiki about it when it is ready in FPC stable. Looks like: Code: [Select]  var   SOToken: ISpeechObjectToken;   SOTokens: ISpeechObjectTokens; begin   fSpVoice := TAxcSpVoice.Create(nil);   fSpVoice.OnEndStream:=@fSpVoiceEndStream;   fSpVoice.OleServer.Set_Rate(-3);   SOTokens := fSpVoice.OleServer.GetVoices('', ''); etc... ---------------------------------------------------------------------- BigChimp · 2013-03-07 11:54 Quote from: theo on March 07, 2013, 12:13:08 pmYou can also import SAPI Voice as an Activex now (FPC trunk?) I'll probably write a wiki about it when it is ready in FPC stable. Go ahead - it is in FPC stable (2.6.2): http://wiki.lazarus.freepascal.org/LazActiveX#Installing ---------------------------------------------------------------------- Fred vS · 2013-05-19 18:05 Hello  I have translated the original C header into Pascal. It load dynamically the library and have reference counter. I do not have test it on Windows. If you need to install Portaudio, you may use the dynamic loader of portaudio too. PS : In a near future, gonna add some example how to use it. PS2 : Bye, bye ATK, AT-SPI and friends, i gonna do my way.  PS3 : @ Avra : many, many thanks for the tip, i feel free now to build a real assistive toolkit for LCL, fpGUI and Console app. ---------------------------------------------------------------------- BigChimp · 2013-05-20 05:39 Quote from: Fred vS on May 19, 2013, 08:05:15 pmPS2 : Bye, bye ATK, AT-SPI and friends, i gonna do my way.  What if GTK users have festival installed or some commercial text to speech engine? Presumably ATK/AT-SPI is low level enough to use multiple engines at the user's choice. See e.g. https://developer.gnome.org/accessibility-devel-guide/stable/gad-how- it-works.html.en ---------------------------------------------------------------------- User137 · 2013-05-20 10:25 It's a good start, but best sample that sourceforge gave was this http://espeak.sourceforge.net/samples/raven.ogg If you can tweak the parameters to make it sound better, be my guest. But make sure to upload it to the samples to give espeak proper credit. ---------------------------------------------------------------------- Fred vS · 2013-05-20 11:45 Chose promise, chose due.  Here examples with code and binaries how to use espeak.pas. https://sites.google.com/site/fiensprototyping/ATfpGUI_LCL.tar.gz Those demos are for Linux 64. You do not need to install anything, the binaries (must) work "out of the box". PS : It is a try for assistive access, so you may use the keyboard to test it. @ BigChimp. You are the one i have the most respect here in that forum. Thanks for your help but AT-SPI is too difficult to understand. If is working a different way with Linux, Windows or Mac OS. Lot of work to do it (not) work, no help, strange bad reactions. I prefer to develop my own accessibilty tool, accessible for everybody, simple and equal for each system. PS : Everybody is welcome to follow me in that exiting travel. ---------------------------------------------------------------------- esvignolo · 2013-10-25 01:30 Hi Fred vS! the link for download isn't working, can you upload again? Many thanks! ---------------------------------------------------------------------- Fred vS · 2013-10-25 12:26 @ esvignolo : Hello. Take a look here :http://forum.lazarus.freepascal.org/index.php/topic,21174.0.html sak GitHub sources : http://fredvs.github.io/sak/ sak binaries demos : https://sites.google.com/site/fiensprototyping/sk_bin_demos.zip PS : You may use espeak.dll (and sak_dll) only if your program is open-source. Otherwise you may use espeak.exe (and sak).