Saturday, April 6, 2019

WinBatch - Fun Text to Speech Simulation


Here's something fun I wrote years ago... it's a brief WinBatch program that accesses the Windows Sapi.spVoice() object to allow text-to-speech translations. The text you enter in the edit box will be read back to you by the computer voice installed on your Windows machine. You can find your voices in the Speech Properties section of Control Panel (Navigation depends on Windows version).


The two voices I have installed are David (Male) & Zira (Female) - I'll incorporate these in my Speech Utility program - allowing to select either with Radio Buttons.


The page is simple (built using the WinBatch GUI Dialog Builder*). Enter some text, select the voice, rate & volume then click "Talk to Me!" to hear the robotic voice read your text back to you. This is a great utility if you have laryngitus or simply want to annoy your wife & kids.


*Note - WinBatch includes a Windows GUI Dialog Builder - I mentioned it here in the Numeric-to-Literal Post (March 14, 2019).

TDTALK.WBT - Source Code:

;**********************************************************************
;*                                                                    *
;*       MODULE: TDTALK.WBT                                           *
;*       AUTHOR: TONY DELIA.                                          *
;*         DATE: 05/18/2004.                                          *
;*         DESC: TD WINBATCH SPEECH EMULATION.                        *
;*      VERSION: WINBATCH 2004F.                                      *
;*                                                                    *
;**********************************************************************

;**********************************************************************
;*    EXTENDERS:                                                      *
;**********************************************************************
;*                                                                    *
;*    NO EXTENDERS REQUIRED.                                          *
;*                                                                    *
;**********************************************************************

;**********************************************************************
;*    REVISIONS:                                                      *
;**********************************************************************
;*                                                                    *
;*   DATE     PROGRAMMER      DESCRIPTION                             *
;* ---------- --------------- --------------------------------------- *
;*                                                                    *
;* 05/18/2004 DELIA,TONY      ORIGINAL CODING.                        *
;*                                                                    *
;**********************************************************************

;**********************************************************************
;*       UDF/SUBROUTINES                                              *
;**********************************************************************

#include "TDFUN.WBC"

;**********************************************************************
;*       PROCESS MAIN                                                 *
;**********************************************************************

TDFUN_UserProps()

myText                  = "Hello "

if LDAP_name           <> ""

   myText               = StrCat(myText, LDAP_name, ". ")

endif

SAPI                    = ObjectOpen("Sapi.SpVoice")
sVOL                    = SAPI.volume
sVOI                    = SAPI.voice
sVOI_Desc               = sVOI.getdescription

SAPI.voice              = SAPI.GetVoices.Item(0)
SAPI.speak(myText)

gosub Define_Dialog_SPK

sw                      = @YES

while sw               == @YES

   ButtonPushed         = Dialog("SPK")

   Select ButtonPushed

      Case 1

         if myText     == ""
            myText      = "Enter something you moron"
         endif

         SAPI.voice     = SAPI.GetVoices.Item(0)
         SAPI.rate      =   0   
         SAPI.volume    = 100

         if myVoice    == "2"
            SAPI.voice  = SAPI.GetVoices.Item(1)
         endif
         if myRate     <> ""
            SAPI.rate   = myRate
         endif
         if myVolume   <> ""
            SAPI.volume = myVolume
         endif

         SAPI.speak(myText)
        
         break

      Case 2

         sw             = @NO
         break

   EndSelect

endwhile

SAPI.speak("Goodbye %LDAP_name%")

ObjectClose(SAPI)

Exit

;**********************************************************************
;*      Define Dialog SPK                                              *
;**********************************************************************

:Define_Dialog_SPK

SPKFormat=`WWWDLGED,6.1`

SPKCaption=`TDTALK - Text to Speech Synthesizer`
SPKX=002
SPKY=061
SPKWidth=366
SPKHeight=246
SPKNumControls=016
SPKProcedure=`DEFAULT`
SPKFont=`Microsoft Sans Serif|6656|40|34`
SPKTextColor=`0|0|0`
SPKBackground=`DEFAULT,DEFAULT`
SPKConfig=0

SPK001=`023,013,296,024,STATICTEXT,DEFAULT,"TDTALK - Text to Speech Utility",DEFAULT,1,512,"Microsoft Sans Serif|16384|70|34","0|0|0",DEFAULT`
SPK002=`021,043,172,012,STATICTEXT,DEFAULT,"Enter Speech Text  (Please keep it clean):",DEFAULT,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SPK003=`021,055,174,090,MULTILINEBOX,MyText,"Hello...",DEFAULT,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SPK004=`021,209,078,022,PUSHBUTTON,DEFAULT,"Talk to Me!",1,11,32,DEFAULT,DEFAULT,DEFAULT`
SPK005=`115,211,078,020,PUSHBUTTON,DEFAULT,"Exit",2,12,0,DEFAULT,DEFAULT,DEFAULT`
SPK006=`209,057,134,126,PICTURE,DEFAULT,"TDTALK",DEFAULT,13,DEFAULT,DEFAULT,DEFAULT,"C:\wbt\TDTALK.BMP"`
SPK007=`209,195,134,012,STATICTEXT,DEFAULT,"Developed by Tony DeLia",DEFAULT,14,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SPK008=`209,207,134,012,STATICTEXT,DEFAULT,"NSS/Advanced Publications",DEFAULT,15,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SPK009=`209,219,134,012,STATICTEXT,DEFAULT,"November '2004",DEFAULT,16,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SPK010=`027,165,044,012,RADIOBUTTON,MyVoice,"Male",1,5,DEFAULT,"Microsoft Sans Serif|6656|40|34","0|0|0",DEFAULT`
SPK011=`027,179,044,012,RADIOBUTTON,MyVoice,"Female",2,6,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SPK012=`021,153,070,048,GROUPBOX,DEFAULT,"Voice",DEFAULT,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SPK013=`155,157,036,012,EDITBOX,MyRate,DEFAULT,DEFAULT,8,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SPK014=`095,157,052,012,STATICTEXT,DEFAULT,"Rate (-10 to 10):",DEFAULT,7,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SPK015=`155,175,036,012,EDITBOX,MyVolume,DEFAULT,DEFAULT,10,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SPK016=`095,175,052,012,STATICTEXT,DEFAULT,"Volume (0 - 100):",DEFAULT,9,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

Return

;**********************************************************************
;*       END OF PROGRAM                                               *
;**********************************************************************


The included library TDFUN.WBC and the function TDFUN_UserProps() are not required for the speech functionality & can be commented out or removed. I use it to automatically extract my LDAP name assigned to my userid then start off with a personal greeting - "Hello Tony DeLia" as well as a final "Goodbye Tony DeLia".

The Male and Female Voice Options are specific to this computer (which was the impetus in the Male/Female Radio Button Design). It could also be designed as a drop-down list with all the installed voices... This was a fun little application to create & mess around with.