Jump to content

Text-to-Speech UDF


Beege
 Share

Recommended Posts

Here is a small UDF and example for using Microsoft's Text-to-Speech interface. Please leave some feedback. Let me know if you have any problems or don't understand something. Also the example uses all three microsoft voices. If Mike and Mary are not installed they can be downloaded here

UDF:

; #FUNCTION# ====================================================================================================================
; Name...........: _StartTTS
; Description ...: Creates a object to be used with Text-to-Speak Functions.
; Syntax.........: _StartTTS()
; Parameters ....:
; Return values .: Success - Returns a object
; Author ........: bchris01

; Example .......: Yes
; ===============================================================================================================================
Func _StartTTS()
    Return ObjCreate("SAPI.SpVoice")
EndFunc   ;==>_StartTTS

; #FUNCTION# ====================================================================================================================
; Name...........: _SetRate
; Description ...: Sets the rendering rate of the voice. (How fast the voice talks.)
; Syntax.........: _SetRate(ByRef $Object, $iRate)
; Parameters ....: $Object        - Object returned from _StartTTS().
;                  $iRate         - Value specifying the speaking rate of the voice. Supported values range from -10 to 10
; Return values .:  None
; Author ........: bchris01
; Example .......: Yes
; ===============================================================================================================================
Func _SetRate(ByRef $Object, $iRate); Rates can be from -10 to 10
    $Object.Rate = $iRate
EndFunc   ;==>_SetRate

; #FUNCTION# ====================================================================================================================
; Name...........: _SetVolume
; Description ...: Sets the volume of the voice.
; Syntax.........: _SetVolume(ByRef $Object, $iVolume)
; Parameters ....: $Object        - Object returned from _StartTTS().
;                  $iVolume       - Value specifying the volume of the voice. Supported values range from 0-100. Default = 100
; Return values .:  None
; Author ........: bchris01
; Example .......: Yes
; ===============================================================================================================================
Func _SetVolume(ByRef $Object, $iVolume);Volume
    $Object.Volume = $iVolume
EndFunc   ;==>_SetVolume

; #FUNCTION# ====================================================================================================================
; Name...........: _SetVoice
; Description ...: Sets the identity of the voice used for text synthesis.
; Syntax.........: _SetVoice(ByRef $Object, $sVoiceName)
; Parameters ....: $Object        - Object returned from _StartTTS().
;                  $sVoiceName    - String matching one of the voices installed.
; Return values .:  Success - Sets object to voice.
;                   Failure - Sets @error to 1
; Author ........: bchris01
; Example .......: Yes
; ===============================================================================================================================
Func _SetVoice(ByRef $Object, $sVoiceName)
    Local $VoiceNames, $VoiceGroup = $Object.GetVoices
    For $VoiceNames In $VoiceGroup
        If $VoiceNames.GetDescription() = $sVoiceName Then
            $Object.Voice = $VoiceNames
            Return
        EndIf
    Next
    Return SetError(1)
EndFunc   ;==>_SetVoice

; #FUNCTION# ====================================================================================================================
; Name...........: _GetVoices
; Description ...: Retrives the currently installed voice identitys.
; Syntax.........: _GetVoices(ByRef $Object[, $Return = True])
; Parameters ....: $Object        - Object returned from _StartTTS().
;                  $bReturn       - String of text you want spoken.
;                  |If $bReturn = True then a 0-based array is returned.
;                  |If $bReturn = False then a string seperated by delimiter "|" is returned.
; Return values .:  Success - Returns an array or string containing installed voice identitys.
; Author ........: bchris01
; Example .......: Yes
; ===============================================================================================================================
Func _GetVoices(ByRef $Object, $bReturn = True)
    Local $sVoices, $VoiceGroup = $Object.GetVoices
    For $Voices In $VoiceGroup
        $sVoices &= $Voices.GetDescription() & '|'
    Next
    If $bReturn Then Return StringSplit(StringTrimRight($sVoices, 1), '|', 2)
    Return StringTrimRight($sVoices, 1)
EndFunc   ;==>_GetVoices

; #FUNCTION# ====================================================================================================================
; Name...........: _Speak
; Description ...: Speaks the contents of the text string.
; Syntax.........: _Speak(ByRef $Object, $sText)
; Parameters ....: $Object        - Object returned from _StartTTS().
;                  $sText         - String of text you want spoken.
; Return values .:  Success - Speaks the text.
; Author ........: bchris01
; Example .......: Yes
; ===============================================================================================================================
Func _Speak(ByRef $Object, $sText)
    $Object.Speak($sText)
EndFunc   ;==>_Speak
TTS.au3

Example:

#include <TTS.au3>

$Default = _StartTTS()
If Not IsObj($Default) Then
    MsgBox(0, 'Error', 'Failed create object')
    Exit
EndIf
_Speak($Default, 'Hello my name is Sam. I am the default voice. Here are the other voices you have to choose from.')
MsgBox(0, 'Voices installed', StringReplace(_GetVoices($Default, False), '|', @CRLF))

$aVoiceSelection = _GetVoices($Default)

$Mike = _StartTTS()
_SetVoice($Mike, 'Microsoft Mike')
If @error Then
    _Speak($Default, 'Mike is Not installed.')
    $Mike = False
EndIf

$Mary = _StartTTS()
_SetVoice($Mary, $aVoiceSelection[1])
If @error Then
    _Speak($Default, 'Mary is Not installed.')
    $Mary = False
EndIf

If IsObj($Mike) Then _Speak($Mike, 'Hello my name is Mike.')
If IsObj($Mary) Then _Speak($Mary, 'Hello my name is Mary.')

_SetRate($Default, 5)
_Speak($Default, 'This is Sam talking really really fast.')
_SetRate($Default, -5)
_Speak($Default, 'This is Sam talking slow.')
Link to comment
Share on other sites

This is awesome, I've seen the functions a lot of time on the forums but not the speed and how to get a list of available voices, now I have them all in one place.

Great work >_<

[font="Impact"]Use the helpfile, It´s one of the best exlusive features of Autoit.[/font]http://support.microsoft.com/kb/q555375ALIBI Run - a replacement for the windows run promptPC Controller - an application for controlling other PCs[size="1"]Science flies us to the moon. Religion flies us into buildings.[/size][size="1"]http://bit.ly/cAMPZV[/size]
Link to comment
Share on other sites

Link to comment
Share on other sites

C:\Program Files (x86)\AutoIt3\Include\TTS.au3 (99) : ==> The requested action with this object has failed.:

$Object.Speak($sText)

$Object.Speak($sText)^ ERROR

Vista x64

You got this error from running the example?

Link to comment
Share on other sites

Yes, both before and after I installed those voices you linked to in your post.

Im not sure. It might have something to do with vista. Will the object return the list of voices?

Ex:

$Default = _StartTTS()
If Not IsObj($Default) Then
    MsgBox(0, 'Error', 'Failed create object')
    Exit
EndIf
MsgBox(0, 'Voices installed', StringReplace(_GetVoices($Default, False), '|', @CRLF))
Link to comment
Share on other sites

Link to comment
Share on other sites

Thanks Yashied. Glad to here its working for you.

Edited by bchris01
Link to comment
Share on other sites

  • 6 months later...
  • 10 months later...

Thanks for this.

I was looking for a way to have it speak in an asynchronous fashion.

I would like to continue the code execution while it speaks - couldn't find out how (looked at the SAPI documentation and experimented a little, no luck).

Also - assuming the above can be done, does anyone know how to stop the playback?

Thanks in advance,

Link to comment
Share on other sites

Found it.

The SAPI ASYNC flag can be added to the Speak method.

Updated _Speak() for the UDF

; #FUNCTION# ====================================================================================================================
; Name...........: _Speak
; Description ...: Speaks the contents of the text string.
; Syntax.........: _Speak(ByRef $Object, $sText)
; Parameters ....: $Object        - Object returned from _StartTTS().
;                  $sText         - String of text you want spoken.
;                  $bWait (false) - If true, waits for the playback to end
; Return values .:  Success - Speaks the text.
; Author ........: bchris01
; Example .......: Yes
; ===============================================================================================================================
Func _Speak(ByRef $Object, $sText, $bWait=false)
    Local $iFlags = 1
    If $bWait Then $iFlags = 0
    $Object.Speak($sText,$iFlags)
EndFunc   ;==>_Speak
Link to comment
Share on other sites

  • 2 weeks later...

Just played around with the UDF and found your change suggestion. Good option :x to a nice :shifty: UDF , but wouldn't it be better to use

Func _Speak(ByRef $Object, $sText, $bWait=true)

as the default value?

Because the original UDF acts in the same way? (at least in my WIN7 it does behave like set to true). So if someone will use the "updated" version the output will remain the same for the "old" code? Just a suggestion...not to offence you.

//edit:

Ah, maybe interesting for others:

I DO NOT installed the two voices in my Win7, I just gave it a try. And ANNA Voice was found. So SAM is retired? Poor Sam :P

I found this:

http://en.wikipedia.org/wiki/Microsoft_text-to-speech_voices

Edited by Tankbuster
Link to comment
Share on other sites

Nice one. This is really fit in my automation tool.

[font="Palatino Linotype"][size="2"]*** The information contained in this post should be considered and certified WORKS ON MY MACHINE ***[/size][/font][font="Palatino Linotype"][size="2"] [/size][/font]
Link to comment
Share on other sites

  • 2 months later...

Hi Guys,

Wrote this little gui (with the use of a function written by another forum member), cents been modified almost unrecognizable, but I give creadit to the originator of the Google translator voice function. The rest of the code is purely mine and now it is yours.

The gui lets you enter the name of a mp3 file minus the extention. Then you enter some text and press the play / recreate button. You hear the text, and if you don't like it you just edit it in the box and replay / recreate as many times as you like. As long as you do not change the file name it simply replays and recreates the same file ; which is an mp3 in the root of c:

have fun with your new toy

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=C:\Program Files\AutoIt3\Examples\form3.kxf
Global $filename
$Form1_1 = GUICreate("Form1", 288, 229, 192, 114)
$Input1 = GUICtrlCreateInput("", 88, 48, 121, 21)
$Input2 = GUICtrlCreateInput("", 8, 104, 273, 21)
$Button1 = GUICtrlCreateButton("Make mp3", 112, 160, 75, 25, $WS_GROUP)
$Label1 = GUICtrlCreateLabel("File Name", 104, 16, 77, 27)
GUICtrlSetFont(-1, 12, 400, 0, "Comic Sans MS")
$Label2 = GUICtrlCreateLabel("Enter Text here", 72, 72, 146, 30)
GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
;$Label3 = GUICtrlCreateLabel("", 64, 16, 160, 20)
;GUICtrlSetFont(-1, 10, 800, 0, "Arial")
;GUICtrlSetBkColor(-1, 0xFFFFFF)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $File, $audio_text

Func speak_and_record()
    Local $temp, $file1
    $temp = StringReplace($audio_text, " ", "+")
    InetGet("http://translate.google.com/translate_tts?q=" & $temp, "\" & $File & ".mp3")
    $file1 = "\" & $File & ".mp3"
    $filename = $filename & ".mp3"
    $Label3 = GUICtrlCreateLabel($filename, 64, 16, 160, 20)
    $filename = GUICtrlRead($Input1)              ;for display of name of file
    GUICtrlSetFont(-1, 10, 800, 0, "Arial")
    GUICtrlSetBkColor(-1, 0xFFFFFF)
    GUISetState(@SW_SHOW)
    SoundPlay($file1, 1)                           ;play the mp3
    Return
EndFunc   ;==> speak_and_record

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Input1
            $File = GUICtrlRead($Input1)              ;get name of mp3 file
            $filename = GUICtrlRead($Input1)          ;eliminates multiple displays of ".mp3"
        Case $Input2
            $audio_text = GUICtrlRead($Input2)        ;get test for google to translate
        Case $Button1
            speak_and_record()                        ;ok, do it: speek, record, display

    EndSwitch
WEnd

post-38053-0-25628100-1302365298_thumb.j

Edited by DaveB
Link to comment
Share on other sites

  • 2 weeks later...

Yet, another version of google tells the time.

all the functions are adaptations from other members, I just put them together.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 233, 238, 192, 114)
$Button1 = GUICtrlCreateButton("Present Time", 80, 104, 75, 25, $WS_GROUP)
$Label1 = GUICtrlCreateLabel("Google Speaks the time", 0, 16, 237, 27)
GUICtrlSetFont(-1, 14, 800, 0, "Century Schoolbook")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###



Global $File, $gspeech
$File = "present time1"

;$gspeech = "Hello,  the time is now, " & @HOUR & "hours and" & @MIN & "minutes"
$gspeech = "  the time is now, " & time()

Func speech()
    Local $temp, $file1
    $temp = StringReplace($gspeech, " ", "+")
    InetGet("http://translate.google.com/translate_tts?q=" & $temp, "\" & $File & ".mp3")
    $file1 = "\" & $File & ".mp3"
    ;SoundPlay($file1, 1)
    SoundPlay($file1, 1)
    Return
EndFunc   ;==>speech

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            speech()
        Case $Label1
    EndSwitch
WEnd

Func Time()
    If @HOUR > 12 Then
        $hour = @HOUR - 12
        $AMPM = ",Pee M"
    ElseIf @HOUR = 0 Then
        $hour = 12
        $AMPM = ",Ae M"
    Else
        $hour = @HOUR
        $AMPM = ",Ae M"
    EndIf
    Return $hour & ":" & @MIN & $AMPM
EndFunc

post-38053-0-80863900-1303143526_thumb.g

Link to comment
Share on other sites

  • 1 month later...

Im not sure. It might have something to do with vista. Will the object return the list of voices?

Hi Beege

Great UDF, but I tried with no success to introduce a language parameter (Portuguese in my case). Do you know how in Win 7?

Thanks

Jose

Yet, another version of google tells the time.

all the functions are adaptations from other members, I just put them together.

[Hi DaveB

On yourUDF I was able to put portuguese, as follows (I'm not sure if is the best method....)

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=C:\Program Files\AutoIt3\Examples\form3.kxf
Global $filename
$Form1_1 = GUICreate("Form1", 288, 229, 192, 114)
$Input1 = GUICtrlCreateInput("", 88, 48, 121, 21)
$Input2 = GUICtrlCreateInput("", 8, 104, 273, 21)
$Button1 = GUICtrlCreateButton("Make mp3", 112, 160, 75, 25, $WS_GROUP)
$Label1 = GUICtrlCreateLabel("File Name", 104, 16, 77, 27)
GUICtrlSetFont(-1, 12, 400, 0, "Comic Sans MS")
$Label2 = GUICtrlCreateLabel("Enter Text here", 72, 72, 146, 30)
GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
;$Label3 = GUICtrlCreateLabel("", 64, 16, 160, 20)
;GUICtrlSetFont(-1, 10, 800, 0, "Arial")
;GUICtrlSetBkColor(-1, 0xFFFFFF)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $File, $audio_text

Func speak_and_record()
    Local $temp, $file1
    $temp = StringReplace($audio_text, " ", "+")
$temp = StringReplace($temp, "í", "%C3%AD")
$temp = StringReplace($temp, "ç", "%C3%87")
$temp = StringReplace($temp, "ã", "%C3%83")
$temp = StringReplace($temp, "ê", "%C3%AA")
$temp = StringReplace($temp, "á", "%C3%A1")
$temp = StringReplace($temp, "é", "%C3%A9")
$temp = StringReplace($temp, "õ", "C3%B5")
$temp = StringReplace($temp, "ó", "%C3%B3")
$temp = StringReplace($temp, "à", "%C3%A0")
; $temp = StringReplace($temp, "", "")
; $temp = StringReplace($temp, "í", "")
    InetGet("[url="http://translate.google.com/translate_tts?hl=pt-BR&ie=UTF-8&q"]http://translate.google.com/translate_tts?hl=pt-BR&ie=UTF-8&q[/url]=" & $temp, "\" & $File & ".mp3")
    $file1 = "\" & $File & ".mp3"
    $filename = $filename & ".mp3"
    $Label3 = GUICtrlCreateLabel($filename, 64, 16, 160, 20)
    $filename = GUICtrlRead($Input1)              ;for display of name of file
    GUICtrlSetFont(-1, 10, 800, 0, "Arial")
    GUICtrlSetBkColor(-1, 0xFFFFFF)
    GUISetState(@SW_SHOW)
    SoundPlay($file1, 1)                           ;play the mp3
    Return
EndFunc   ;==> speak_and_record

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Input1
            $File = GUICtrlRead($Input1)              ;get name of mp3 file
            $filename = GUICtrlRead($Input1)          ;eliminates multiple displays of ".mp3"
        Case $Input2
            $audio_text = GUICtrlRead($Input2)        ;get test for google to translate
        Case $Button1
            speak_and_record()                        ;ok, do it: speek, record, display

    EndSwitch
WEnd
==> The problem is that Google TTS works only with about 100 letters.... I tried BING's site, and in portuguese it is better than Google, AND there is no limit of letters. I was not able to substitute it at your UDF, but I believe this will be a snap for you.

See site at: http://www.microsofttranslator.com/Default.aspx , and you can try this sentence in portuguese (selec portugues in both boxes -> from and to):

quero ver você falando... palavras bem complexas, com til e cecidilha, tipo açafrão, até, então, sei não, querendo, atacherches, paralelepipedo, e outras cossitas más. Enfim, vamos ver se esta frase, bem longa, é falada pelo BING de forma correta.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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