Jump to content



Photo

Text-to-Speech UDF


  • Please log in to reply
16 replies to this topic

#1 Beege

Beege

    Universalist

  • MVPs
  • 843 posts

Posted 17 August 2009 - 08:43 PM

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:
AutoIt         
; #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
Attached File  TTS.au3   5K   1217 downloads

Example:
AutoIt         
#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.')






#2 colafrysen

colafrysen

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 264 posts

Posted 17 August 2009 - 08:48 PM

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 >_<
Use the helpfile, It´s one of the best exlusive features of Autoit.http://support.microsoft.com/kb/q555375ALIBI Run - a replacement for the windows run promptPC Controller - an application for controlling other PCsScience flies us to the moon. Religion flies us into buildings.http://bit.ly/cAMPZV

#3 Beege

Beege

    Universalist

  • MVPs
  • 843 posts

Posted 17 August 2009 - 08:58 PM

Thankyou very much! I'm glad you like it.

#4 kor

kor

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 484 posts

Posted 17 August 2009 - 09:16 PM

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

#5 Beege

Beege

    Universalist

  • MVPs
  • 843 posts

Posted 17 August 2009 - 09:26 PM

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?

#6 kor

kor

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 484 posts

Posted 17 August 2009 - 10:46 PM

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

#7 Beege

Beege

    Universalist

  • MVPs
  • 843 posts

Posted 17 August 2009 - 10:56 PM

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))


#8 Yashied

Yashied

    Happy in Moscow

  • MVPs
  • 2,512 posts

Posted 17 August 2009 - 11:13 PM

bchris01, I never had to deal with this. I played with it some time. It`s very fun toy.

>_<

Well done, 5* from me.

Edited by Yashied, 17 August 2009 - 11:18 PM.


#9 Beege

Beege

    Universalist

  • MVPs
  • 843 posts

Posted 17 August 2009 - 11:22 PM

Thanks Yashied. Glad to here its working for you.

Edited by bchris01, 17 August 2009 - 11:24 PM.


#10 madmorgan

madmorgan

    Adventurer

  • Active Members
  • PipPip
  • 111 posts

Posted 25 February 2010 - 07:41 PM

hello Guys,

i got the UDF to work grate but i want to know is there a way to stop the _Speak once it has started reading some text??? if so can you tell how please.

#11 DannyB

DannyB

    Seeker

  • New Members
  • 3 posts

Posted 01 January 2011 - 08:20 PM

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,

#12 DannyB

DannyB

    Seeker

  • New Members
  • 3 posts

Posted 01 January 2011 - 09:28 PM

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


#13 Tankbuster

Tankbuster

    Adventurer

  • Active Members
  • PipPip
  • 133 posts

Posted 12 January 2011 - 02:40 PM

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, 12 January 2011 - 02:46 PM.


#14 nfaustin

nfaustin

    Adventurer

  • Active Members
  • PipPip
  • 124 posts

Posted 13 January 2011 - 07:22 AM

Nice one. This is really fit in my automation tool.
*** The information contained in this post should be considered and certified WORKS ON MY MACHINE ***

#15 DaveB

DaveB

    Seeker

  • Active Members
  • 13 posts

Posted 09 April 2011 - 04:34 AM

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

AutoIt         
#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("<a href='http://translate.google.com/translate_tts?q=' class='bbc_url' title='External link' rel='nofollow external'>http://translate.google.com/translate_tts?q="</a> & $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

Attached Thumbnails

  • screenshot.jpg

Edited by DaveB, 09 April 2011 - 04:08 PM.


#16 DaveB

DaveB

    Seeker

  • Active Members
  • 13 posts

Posted 18 April 2011 - 04:19 PM

Yet, another version of google tells the time.
all the functions are adaptations from other members, I just put them together.

AutoIt         
#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("<a href='http://translate.google.com/translate_tts?q=' class='bbc_url' title='External link' rel='nofollow external'>http://translate.google.com/translate_tts?q="</a> & $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

Attached Thumbnails

  • Untitled 1.gif


#17 joseLB

joseLB

    Polymath

  • Active Members
  • PipPipPipPip
  • 210 posts

Posted 12 June 2011 - 09:17 PM

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....)
AutoIt         
 #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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users