SAPI parameters, rate, volume and async speech?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

SAPI parameters, rate, volume and async speech?

26 Jun 2017, 07:33

Hi, I'm using AHK and SAPI to speak inaccessible apps and all is working well, apart from I want to be able to adjust/include further sAPI parameters (rate, volume and async speech). As a newby, I'm struggling including these the correct way. Has anyone got basic examples showing how to do this? Here's a test few lines with an example of what I'm using, with the ComObj as a global:

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Recommended for catching common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
CoordMode, Mouse, Window
oVoice:=ComObjCreate("SAPI.SpVoice")

^1::
oVoice.Speak("Hello, this is just a test to see how I can manipulate SAPI")
Return


So, not sure at all where to include/use any other parameters like SAPI.Rate and SVSFlagsAsync.. Help much appreciated. THX Steve.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: SAPI parameters, rate, volume and async speech?

26 Jun 2017, 08:39

SpVoice GetVoices method (SAPI 5.3)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
SpVoice Interface (SAPI 5.3)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx

[see also links in the first post]
[function] Easy Text to speech - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/5342 ... to-speech/

TTS or SAPI read highlighted text - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/1230 ... hted-text/

I believe there must have some AHK link that helped me, that I can't find, that contained this line:
Name:=SpVoice.GetAudioOutputs.Item(A_Index-1).GetDescription

On my XP machine I used to have: Microsoft Sam, LH Michelle, LH Michael.
But now I only have Microsoft Anna, on Windows 7. [insert missing cry emoticon here]

Code: Select all

q:: ;SAPI
;vText := "hello world"
;vText := "AutoHotkey for the win"
vText := "Auto Hot key, for the win"
;vText := "The quick brown fox jumps over the lazy dog."
;vText := "You have selected Microsoft Sam as the computer's default voice."
;vText := "antidisestablishmentarianism"
;vText := "anteedisestablishmentarianism" ;incorrect spelling, better pronunciation
;vText := "supercalifragilisticexpialidocious"
;vText := "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch"
;vText := "Jennifer, Alison, Phillipa, Sue, Deborah, Annabel, too. I wrote this song for you."

oSpVoice := ComObjCreate("SAPI.SpVoice")

;speak
oSpVoice.Speak(vText)

;list voices
vOutput := ""
Loop % oSpVoice.GetVoices.Count
	vOutput .= oSpVoice.GetVoices.Item(A_Index-1).GetAttribute("Name") "`r`n"
MsgBox, % SubStr(vOutput, 1, -2)

;get current voice
MsgBox, % oSpVoice.Voice.GetAttribute("Name")

;set voice
;vName := "Microsoft Sam"
;vName := "LH Michelle"
;vName := "LH Michael"
vName := "Microsoft Anna"
oSpVoice.Voice := oSpVoice.GetVoices("Name=" vName).Item(0)
vText := "I am " vName ". Where do you want to go today?"

;get volume
MsgBox % oSpVoice.Volume

;set volume (range: 0 to 100)
oSpVoice.Volume := 50
MsgBox % oSpVoice.Volume
oSpVoice.Speak(vText)
oSpVoice.Volume := 100

;get speed
MsgBox % oSpVoice.Rate

;set speed (range: -10 to 10)
oSpVoice.Rate := 8
oSpVoice.Speak(vText)
oSpVoice.Rate := 0

;set pitch (range: -10 to 10)
;vPitch := -10
;vPitch := 10
vList := "-10,10"
;SVSFPersistXML := 0x20 ;SVSFIsXML := 0x8 ;SVSFlagsAsync := 0x1
Loop, Parse, vList, % ","
{
	vPitch := A_LoopField
	oSpVoice.Speak("<pitch absmiddle=""" vPitch """/>", 0x29)
	oSpVoice.Speak(vText)
}

oSpVoice := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

Re: SAPI parameters, rate, volume and async speech?

26 Jun 2017, 10:15

Hi Geesws and thanks a lot for the code, I'll give that a shot later tonight and see how it all works out. The code/scripts I'm doing are for 3rd party VST plugins which are inaccessible, so the user is already using a screenreader. Idealy, I'd like just a 1/few lines to set the SAPI speech for these scripts, as the user wouldn't really be interested in configuring it. I just need it speeding up a bit and I want it to speak asynchronously, so it doesn't slow down the use of the script. If you know how to put the rate/ asynnchronous speech into the default SAPI output, it would be exactly what's needed, but this may just do the job, so I appreciate you posting. So, I basically want something like(not written correctly :)):
oVoice:=ComObjCreate("SAPI.SpVoice"), SAPI.Rate:7, AsynchronousSpeech:1

THX Steve.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: SAPI parameters, rate, volume and async speech?

26 Jun 2017, 11:03

Like this?

Code: Select all

q:: ;SAPI asynchronous
;SpVoice Speak method (SAPI 5.3)
;https://msdn.microsoft.com/en-us/library/ms723609(v=vs.85).aspx
;The Speak method can be called synchronously or asynchronously. When called synchronously, the method does not return until the text has been spoken; when called asynchronously, it returns immediately, and the voice speaks as a background process.

oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 7
vText := ""
Loop, 100
	vText .= A_Index " "
oSpVoice.Speak(vText, 1) ;SVSFlagsAsync := 0x1
Sleep 4000
oSPVoice := ""
MsgBox, % "ended prematurely"
return
It seems that you can't play multiple voices at the same time. Even with multiple scripts running.

Btw I'm not sure how to change the settings permanently, which even if that's not what you want, would be interesting to know about.
[EDIT:]
C:\Windows\System32\Speech\SpeechUX\sapi.cpl
You have selected Microsoft Anna - English (United States) as the computer's default voice.
HKEY_LOCAL_MACHINE\Software\Microsoft\Speech\Voices\Tokens

Hopefully it's possible to add more voices ... hello Google.
[EDIT:]
Microsoft Sam for Windows 7: Is this possible, or do I need to - Microsoft Community
https://answers.microsoft.com/en-us/win ... 487?page=2
Speakonia? Copy files from Windows XP? Anyway I don't want to mess with this PC right now, so I'll have to wait till I have a spare PC again. Unless anyone has had success with this. Basically I want Microsoft Sam, LH Michelle and LH Michael back again, and maybe more voices! (I hate installing software, it's rarely a good idea. Often it's quicker to write it yourself. Portable ftw.)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

Re: SAPI parameters, rate, volume and async speech?

26 Jun 2017, 15:03

Hey Jeeswg, that worked beautifully! thanks so much! I'm just using the below, but may extend it now I have more to go on...thanks again!
oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 7

^1::
oSpVoice.Speak("hello", 1) ;SVSFlagsAsync := 0x1
Return
stevesax
Posts: 22
Joined: 12 Jun 2017, 15:25

Re: SAPI parameters, rate, volume and async speech?

27 Jun 2017, 10:36

Hi, just as a slight update, I added the ability to speed up/slow down the speech, which is simple, but fits my needs...hopefully it might be useful for others? :)
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Recommended for catching common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
CoordMode, Mouse, Window
;oVoice:=ComObjCreate("SAPI.SpVoice")
oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 7

^1::
oSpVoice.Speak("hello this is a test", 1)
Return

^2::
; decrease speaking rate
nDec:=oSpVoice.Rate
nDec := nDec - 1
oSpVoice.Rate := nDec
oSpVoice.Speak("Slower", 1)
Return

^3::
; increase speaking rate
nInc:=oSpVoice.Rate
nInc := nInc + 1
oSpVoice.Rate := nInc
oSpVoice.Speak("Faster", 1)
Return
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: SAPI parameters, rate, volume and async speech?

04 Aug 2018, 02:41

would you know how to get it to reach each letter separately?

For example instead of saying hello it should say H E L L O
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: SAPI parameters, rate, volume and async speech?

04 Aug 2018, 13:12

I found this useful for improved sound quality (from the old forum):

Code: Select all

SetOutputVoiceFormat(SAPI, 39)								; 39 = 48kHz16BitStereo , 4 = 8kHz8BitMono.

SetOutputVoiceFormat(SAPI, AudioOutputStreamFormatType)
{
	SAPI.AllowAudioOutputFormatChangesOnNextSet:=0
	SAPI.AudioOutputStream.Format.Type:=AudioOutputStreamFormatType
	SAPI.AudioOutputStream:=SAPI.AudioOutputStream
	SAPI.AllowAudioOutputFormatChangesOnNextSet:=1
}
HTH
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: SAPI parameters, rate, volume and async speech?

10 Aug 2018, 18:52

jeeswg wrote:Like this?

Code: Select all

q:: ;SAPI asynchronous
;SpVoice Speak method (SAPI 5.3)
;https://msdn.microsoft.com/en-us/library/ms723609(v=vs.85).aspx
;The Speak method can be called synchronously or asynchronously. When called synchronously, the method does not return until the text has been spoken; when called asynchronously, it returns immediately, and the voice speaks as a background process.

oSPVoice := ComObjCreate("SAPI.SpVoice")
oSpVoice.Rate := 7
vText := ""
Loop, 100
	vText .= A_Index " "
oSpVoice.Speak(vText, 1) ;SVSFlagsAsync := 0x1
Sleep 4000
oSPVoice := ""
MsgBox, % "ended prematurely"
return
It seems that you can't play multiple voices at the same time. Even with multiple scripts running.

Btw I'm not sure how to change the settings permanently, which even if that's not what you want, would be interesting to know about.
[EDIT:]
C:\Windows\System32\Speech\SpeechUX\sapi.cpl
You have selected Microsoft Anna - English (United States) as the computer's default voice.
HKEY_LOCAL_MACHINE\Software\Microsoft\Speech\Voices\Tokens

Hopefully it's possible to add more voices ... hello Google.
[EDIT:]
Microsoft Sam for Windows 7: Is this possible, or do I need to - Microsoft Community
https://answers.microsoft.com/en-us/win ... 487?page=2
Speakonia? Copy files from Windows XP? Anyway I don't want to mess with this PC right now, so I'll have to wait till I have a spare PC again. Unless anyone has had success with this. Basically I want Microsoft Sam, LH Michelle and LH Michael back again, and maybe more voices! (I hate installing software, it's rarely a good idea. Often it's quicker to write it yourself. Portable ftw.)
jeeswg, will the voice index numbers always be 0 1 2

For example, a machine that has one only, will its index be 0 or is it possible it will be 1?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: SAPI parameters, rate, volume and async speech?

10 Aug 2018, 18:59

- I don't have any hard facts. My expectations would be the same as yours, first index is 0, last index is n-1, where the number of voices available = n.
- You can use checks and error messages to handle unexpected results. E.g. try/catch, and .GetAttribute("Name").
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: SAPI parameters, rate, volume and async speech?

10 Aug 2018, 19:09

jeeswg wrote:- I don't have any hard facts. My expectations would be the same as yours, first index is 0, last index is n-1, where the number of voices available = n.
- You can use checks and error messages to handle unexpected results. E.g. try/catch, and .GetAttribute("Name").
thank you

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], imstupidpleshelp, Rohwedder, ShatterCoder and 184 guests