Jump to content

Recommended Posts

Posted (edited)

Trying to write a script that lists the available TTS voices and speaks some sample text for each voice. What I have here works, except that I'm having trouble trapping the COM error that some voices trigger. After a few such errors, the script hangs. I confess that, for the error trapping, I'm just following the example scripts for the ObjEvent() function, recipe style, without fully understanding why it should (or should not) work here. Pointers will be gratefully accepted.

If Not $CmdLine[0] Then
    $sSay = "The quick brown fox jumped over the lazy dog."
Else
    $sSay = $CmdLine[1]
EndIf

Global $__g_oTemplateCOMErrorHandler = 0

$oVox = ObjCreate("SAPI.spVoice")
If @error Then Exit MsgBox(0, @ScriptName, "Error creating SAPI object")

$oVox.Volume = 100
$oVox.Rate = 0
$oVoices = $oVox.GetVoices()
For $vName in $oVoices
    $oVox.Voice = $vName
    ConsoleWrite($vName.GetDescription() & @CRLF)
    Template_COMErrorRegister()
    $oVox.Speak($vName.GetDescription())
    If @error Then
        ConsoleWrite("COM error" & @CRLF)
        Sleep(500)
        ContinueLoop
    EndIf
    $oVox.Speak($sSay)
    If @error Then
        ConsoleWrite("COM error" & @CRLF)
        Sleep(500)
        ContinueLoop
    EndIf
    Template_COMErrorUnregister()
Next
Exit

Func Template_COMErrorRegister()
    $__g_oTemplateCOMErrorHandler = ObjEvent("AutoIt.Error", "Template_COMErrFunc")
EndFunc   ;==>Template_COMErrorRegister

Func Template_COMErrorUnregister()
    $__g_oTemplateCOMErrorHandler = 0
EndFunc   ;==>Template_COMErrorUnregister

Func Template_COMErrFunc()
    Sleep(500)
EndFunc   ;==>Template_COMErrFunc

 

Edited by CarlD
clarity
Posted (edited)
  On 6/28/2020 at 11:38 PM, Danp2 said:

Try moving the register/unregister function calls outside your For / Next loop.

Expand  

Works! (Of course.) Thank you very much. I will now have to figure out why.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ListVoices.exe
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
If Not $CmdLine[0] Then
    $sSay = "The quick brown fox jumped over the lazy dog."
Else
    $sSay = $CmdLine[1]
EndIf

Global $__g_oTemplateCOMErrorHandler = 0, $iErrors = 0

$oVox = ObjCreate("SAPI.spVoice")
If @error Then Exit MsgBox(0, @ScriptName, "Error creating SAPI object")

$oVox.Volume = 100
$oVox.Rate = 0
$oVoices = $oVox.GetVoices()
Template_COMErrorRegister()
ConsoleWrite("Ctrl-C quits..." & @CRLF&@CRLF)
For $vName in $oVoices
    $i = 0
    $oVox.Voice = $vName
    ConsoleWrite($vName.GetDescription() & @CRLF)
    $oVox.Speak($vName.GetDescription() & @CRLF & $sSay)
    If COMError(@error) Then $iErrors += 1
Next
Template_COMErrorUnRegister()
Exit $iErrors

Func COMError($bErr)
    If $bErr Then
        ConsoleWrite("SAPI error" & @CRLF)
        Sleep(500)
    EndIf
    Return $bErr
EndFunc  ;==>TestForCOMError

Func Template_COMErrorRegister()
    $__g_oTemplateCOMErrorHandler = ObjEvent("AutoIt.Error", "Template_COMErrFunc")
EndFunc   ;==>Template_COMErrorRegister

Func Template_COMErrorUnregister()
    $__g_oTemplateCOMErrorHandler = 0
EndFunc   ;==>Template_COMErrorUnregister

Func Template_COMErrFunc()
    Sleep(500)
EndFunc   ;==>Template_COMErrFunc

 

Edited by CarlD
Posted

Did you try this:

or this:

 

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • 2 weeks later...
Posted (edited)
  On 6/29/2020 at 10:57 AM, mLipok said:

Did you try this:

or this:

 

Thanks, I'll check these out. Meanwhile, my code now looks like this:

; ListVoices.au3 -- CLD rev. 2020-07-09
;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ListVoices.exe
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Global $__g_oTemplateCOMErrorHandler = 0
Local $iErrors = 0
Local $sSay = "The quick brown fox jumped over the lazy dog."
Local $sVnm = "", $bShh = 0
If StringinStr($CmdLineRaw, "/q") Or StringinStr($CmdLineRaw, "-q") Then $bShh = 1

Switch $CmdLine[0]
    Case 0
        Sleep(10)
    Case 1
        If $CmdLine[1] = "/?" Or $CmdLine[1] = "-?" Then
            ConsoleWrite(@ScriptName & ": List|Sample text-to-speech voices" & @CRLF & "Usages:" & @CRLF & "1) List all installed voices and hear a test phrase:" & @CRLF & "   " & @ScriptName & " [""test phrase""]|/Q" & @CRLF & "   Default test phrase is:" & @CRLF & "     ""The quick brown fox jumped over the lazy dog.""" & @CRLF & "   /Q[uiet] = list only, no spoken output" & @CRLF & "2) List voice(s) matching|-excluding a voice name or description:" & @CRLF & "   " & @ScriptName & " /V [-][""]voice[""] [""test phrase""]|/Q" & @CRLF & "   [""]voice[""] may be a substring of the name or description; for example:" & @CRLF & "     " & @ScriptName & " /V George [/Q] ==> all voices named ""George""" & @CRLF & "     " & @ScriptName & " /V Canada [/Q] ==> all voices INcluding ""Canada""" & @CRLF & "     " & @ScriptName & " /V -English [/Q] ==> all voices EXcluding ""English""" & @CRLF & "3) View this help:" & @CRLF & "   " & @ScriptName & " /?" & @CRLF)
            Exit
        Else
            $sSay = $CmdLine[1]
        EndIf
    Case 2
        If $CmdLine[1] = "/v" Or $CmdLine[1] = "-v" Then
            $sVnm = $CmdLine[2]
        Else
            If $bShh Then
                $sSay = $CmdLine[1]
            Else
                $sSay = $CmdLine[1] & " " & $CmdLine[2]
            EndIf
        EndIf
    Case Else
        If $CmdLine[1] = "/v" Or $CmdLine[1] = "-v" Then
            $sVnm = $CmdLine[2]
            $sSay = $CmdLine[3]
        Else
            $sSay = $CmdLine[1] & " " & $CmdLine[2] & " " & $CmdLine[3]
            If $bShh Then
                $sSay = $CmdLine[1]
            Else
                $sSay = $CmdLine[1] & " " & $CmdLine[2] & " " & $CmdLine[3]
            EndIf
        EndIf
EndSwitch

$oVox = ObjCreate("SAPI.spVoice")
If @error Then Exit MsgBox(0, @ScriptName, "Error creating SAPI object")

$iCount = 0
$oVox.Volume = 100
$oVox.Rate = 0
$oVoices = $oVox.GetVoices()

Template_COMErrorRegister()
$bIn = 1
If $sVnm Then
    If StringInStr($sVnm, "-") = 1 Then
        $bIn = 0
        $sVnm = StringTrimLeft($sVnm, 1)
    EndIf
    For $vName in $oVoices
        $oVox.Voice = $vName
        If _StringInOrOutStr($vName.GetDescription(), $sVnm, $bIn) Then
            $iCount +=1
            $oVox.Voice = $vName
            ConsoleWrite($vName.GetDescription() & @CRLF)
            If Not $bShh Then $oVox.Speak($vName.GetDescription() & @CRLF & $sSay)
        EndIf
        If COMError(@error) Then $iErrors += 1
    Next
    If $iCount < 1 Then Consolewrite("No matching voice """ & $sVnm & """" & @CRLF)
    Exit
Else
    ConsoleWrite("Ctrl-C quits" & @CRLF & @ScriptName & " /? gives usage details" & @CRLF&@CRLF)
    For $vName in $oVoices
        $iCount += 1
        $oVox.Voice = $vName
        ConsoleWrite($iCount & ". " & $vName.GetDescription() & @CRLF)
        If Not $bShh Then $oVox.Speak($vName.GetDescription() & @CRLF & $sSay)
        If COMError(@error) Then $iErrors += 1
    Next
    Exit $iErrors
EndIf
Template_COMErrorUnRegister()

; Function definitions
; --------------------
Func _StringInOrOutStr($sString, $sSubString, $bIn = 1, $iCaseSense = 0, $iOccurrence = 1, $iStart = 1, $iCount = StringLen($sString))
#cs
------------------------------------------------------------------------
    Test for inclusion OR exclusion of substring in string
        If $bIn = 1 (test for inclusion), function and return value is
      same as StringInStr()
    If $bIn = 0 (test for exclusion), function returns 0 if
      StringInStr() > 0, else returns 1
------------------------------------------------------------------------
#ce
    $iReturn = StringInStr($sString, $sSubString, $iCaseSense, $iOccurrence, $iStart, $iCount)
    If Not $bIn Then
        If $iReturn Then
            $iReturn = 0
        Else
            $iReturn = 1
        EndIf
    EndIf
    Return $iReturn
EndFunc  ;==>_StringInOrOutStr

Func COMError($bErr)
    If $bErr Then
        ConsoleWrite("SAPI error" & @CRLF)
        Sleep(500)
    EndIf
    Return $bErr
EndFunc  ;==>TestForCOMError

Func Template_COMErrorRegister()
    $__g_oTemplateCOMErrorHandler = ObjEvent("AutoIt.Error", "Template_COMErrFunc")
EndFunc   ;==>Template_COMErrorRegister

Func Template_COMErrorUnregister()
    $__g_oTemplateCOMErrorHandler = 0
EndFunc   ;==>Template_COMErrorUnregister

Func Template_COMErrFunc()
    Sleep(500)
EndFunc   ;==>Template_COMErrFunc
; --------------------

 

Expand  

 

Edited by CarlD
Updated script

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...