A batch file speaking!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rafhack
Posts: 19
Joined: 23 Feb 2014 15:02

A batch file speaking!

#1 Post by Rafhack » 30 May 2014 18:34

Code: Select all

@echo off
call:speak "Hello %username%"
:begin
set /p "spk=Speak: "
call:speak "%spk%"
goto begin
:Speak
(
echo/Dim ProSpeak
echo/Set ProSpeak = WScript.CreateObject^("SAPI.SpVoice"^)
echo/ProSpeak.Speak "%~1"
) > proSpeak.vbs
echo/%~1
cscript //nologo proSpeak.vbs&del proSpeak.vbs
cls

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: A batch file speaking!

#2 Post by dbenham » 30 May 2014 18:59

I'm pretty sure I've seen this before, perhaps even on this site.

There is no need for a temporary file if you use JScript instead of VBS.

Code: Select all

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment

::------------ Bactch Script ----------------------------
@echo off
call :speak "Hello %username%! Type Q to quit."
:begin
set /p "spk=Speak: "
if /i "%spk:~0,1%" equ "Q" exit /b
call :speak "%spk%"
goto begin

:Speak
cscript //E:JScript //nologo "%~f0" %1
cls
exit /b

-------------- JScript ----------------------------------*/
var obj = new ActiveXObject("SAPI.SpVoice");
obj.Speak(WScript.Arguments.Unnamed(0));


Dave Benham

Rafhack
Posts: 19
Joined: 23 Feb 2014 15:02

Re: A batch file speaking!

#3 Post by Rafhack » 30 May 2014 19:05

dbenham wrote:I'm pretty sure I've seen this before, perhaps even on this site.

There is no need for a temporary file if you use JScript instead of VBS.

Code: Select all

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment

::------------ Bactch Script ----------------------------
@echo off
call :speak "Hello %username%! Type Q to quit."
:begin
set /p "spk=Speak: "
if /i "%spk:~0,1%" equ "Q" exit /b
call :speak "%spk%"
goto begin

:Speak
cscript //E:JScript //nologo "%~f0" %1
cls
exit /b

-------------- JScript ----------------------------------*/
var obj = new ActiveXObject("SAPI.SpVoice");
obj.Speak(WScript.Arguments.Unnamed(0));


Dave Benham



Cool! I've never used JScript before.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: A batch file speaking!

#4 Post by Squashman » 30 May 2014 19:11

dbenham wrote:I'm pretty sure I've seen this before, perhaps even on this site.

Yep. Seven previous threads dating back to 2011!

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: A batch file speaking!

#5 Post by Yury » 02 Jun 2014 03:09

One more method:


Code: Select all

@echo off
call :speak "Hello_%username%!_Type_Q_to_quit."
echo Quotation marks in the beginning of the text not allowed!
echo.

:begin
set /p "spk=Speak: "
if /i "%spk:~0,1%" equ "Q" exit /b
set "spk=%spk: =_%"
set "spk=%spk:"=_%"
call :speak "%spk%"
goto begin

:speak
mshta "vbscript:CreateObject("SAPI.SpVoice").Speak(replace(%1,"_",chr(32)))&close()"
cls
exit /b



.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: A batch file speaking!

#6 Post by Yury » 03 Jun 2014 00:40

With "javascript":


Code: Select all

@echo off
call :speak "Hello %username%! Type Q to quit."
echo Quotation marks in the beginning of the text not allowed!
echo.

:begin
set /p "spk=Speak: "
if /i "%spk:~0,1%" equ "Q" exit /b
call :speak "%spk%"
goto begin

:speak
mshta "javascript:new ActiveXObject("SAPI.SpVoice").Speak('%~1');close();"
cls
exit /b



.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: A batch file speaking!

#7 Post by foxidrive » 03 Jun 2014 01:37

Yury, here's an different option for exiting and avoiding some quote problems. Poison character can affect it but alphanumeric are ok.

I was surprised how good the voice was...

Code: Select all

@echo off
call :speak Hello %username%! Press Enter alone to quit.

:begin
set "spk="
set /p "spk=Speak: "
if not defined spk exit
call :speak %spk%
goto begin

:speak
mshta "javascript:new ActiveXObject("SAPI.SpVoice").Speak('%*');close();"
cls
exit /b

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: A batch file speaking!

#8 Post by Dragokas » 03 Jun 2014 12:05

foxidrive wrote:I was surprised how good the voice was...


It seems you use Windows >= Vista.
Windows XP has more scary default-voice :twisted: try it :mrgreen:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: A batch file speaking!

#9 Post by foxidrive » 04 Jun 2014 03:49

Yes, you're right on both counts.

Windows 8.1 has a female voice and XP was male and not as good.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: A batch file speaking!

#10 Post by penpen » 04 Jun 2014 10:34

I've found this: http://msdn.microsoft.com/en-us/library/ms717077(v=vs.85).aspx

To use it as input, i've modified the batch a little bit:

Code: Select all

@echo off
set "spk=Hello %username%! Press Enter alone to quit."
call :speak spk

:begin
set "spk="
set /p "spk=Speak: "
if not defined spk exit
call :speak spk
goto begin

:speak
setlocal enableDelayedExpansion
mshta "javascript:new ActiveXObject("SAPI.SpVoice").Speak('!%~1!');close();"
endlocal
cls
exit /b
Now you could use ths as input:

Code: Select all

<rate speed="-10"> This is rate speed -10. </rate>
<rate speed="0"> This is rate speed 0. </rate>
<rate speed="10"> This is rate speed 10. </rate>

<volume level="20" > This is volume 20. </volume>
<volume level="100"> This is volume 100. </volume>

<emph> This is emphasize. </emph>

<spell> These is spell. </spell>

<pitch absmiddle="-10"> This is abs middle pitch -10 </pitch>
<pitch absmiddle="0"> This is abs middle pitch 0 </pitch>
<pitch absmiddle="10"> This is abs middle pitch 10 </pitch>

<pitch middle="-10"> This is middle pitch -10 </pitch>
<pitch middle="0"> This is middle pitch 0 </pitch>
<pitch middle="10"> This is middle pitch 10 </pitch>

<volume level="75"> 2 seconds <silence msec="2000"/> of silence. </volume>
Notes (only tested these on Win XP home 32 bit):
The "silence" tag only works within the other tags above this list, and
the "pron" tag will hang the vbs/batch.

penpen

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: A batch file speaking!

#11 Post by Yury » 04 Jun 2014 15:12

foxidrive, penpen, I added one more line of code to further improve:


Code: Select all

@echo off
set "spk=Hello %username%! Press Enter alone to quit."
call :speak spk

:begin
set "spk="
set /p "spk=Speak: "
if not defined spk exit
set "spk=%spk:"=%"
call :speak spk
goto begin

:speak
setlocal enableDelayedExpansion
mshta "javascript:new ActiveXObject("SAPI.SpVoice").Speak('!%~1!');close();"
endlocal
cls
exit /b



.

Post Reply