AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TTS() Text To Speech using COM
Goto page 1, 2, 3, 4  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
foom



Joined: 19 Apr 2006
Posts: 386

PostPosted: Sun Feb 11, 2007 6:17 pm    Post subject: TTS() Text To Speech using COM Reply with quote

Well its yet another tts script but this one doesn't create a tempfile. It uses the Speech API directly.

Here you can download the TTS.ahk includefile and an example file.

Or you can copy the example in the codebox below.
Code:
TTS(sSpeechText, dwFlags=0)
{
    ;For info on the dwFlags bitmask see the SAPI helpfile:
    ;http://download.microsoft.com/download/speechSDK/SDK/5.1/WXP/EN-US/sapi.chm

    static TTSInitialized, ppSpVoice, pSpeak

    wSpeechTextBufLen:=VarSetCapacity(wSpeechText, StrLen(sSpeechText)*2+2,0)
    DllCall("MultiByteToWideChar", "UInt", 0, "UInt", 0, "Str", sSpeechText, "Int", -1, "UInt", &wSpeechText, "Int", wSpeechTextBufLen/2)

    if !TTSInitialized
    {
        ComInit := DllCall("ole32\CoInitialize", "Uint", 0)
        if ComInit not in 0,1
            return "CoInitialize() failed: " ComInit

        sCLSID_SpVoice:="{96749377-3391-11D2-9EE3-00C04F797396}"
        sIID_ISpeechVoice:="{269316D8-57BD-11D2-9EEE-00C04F797396}"
        ;Make space for unicode representations.
       wCLSID_SpVoiceBufLen:=VarSetCapacity(wCLSID_SpVoice, StrLen(sCLSID_SpVoice)*2+2)
       wIID_ISpeechVoiceBufLen:=VarSetCapacity(wIID_ISpeechVoice, StrLen(sIID_ISpeechVoice)*2+2)
       ;Convert to unicode
       DllCall("MultiByteToWideChar", "UInt",0, "UInt",0, "Str",sCLSID_SpVoice, "Int",-1, "UInt",&wCLSID_SpVoice, "Int",wCLSID_SpVoiceBufLen/2)
       DllCall("MultiByteToWideChar", "UInt",0, "UInt",0, "Str",sIID_ISpeechVoice, "Int",-1, "UInt",&wIID_ISpeechVoice, "Int",wIID_ISpeechVoiceBufLen/2)
       
        ;Convert string representations to originals.
        VarSetCapacity(CLSID_SpVoice, 16)
        VarSetCapacity(IID_ISpeechVoice, 16)
        if ret:=DllCall("ole32\CLSIDFromString", "str", wCLSID_SpVoice, "str", CLSID_SpVoice)
        {
            DllCall("ole32\CoUninitialize")
            return "CLSIDFromString() failed: " ret
        }
        if ret:=DllCall("ole32\IIDFromString", "str", wIID_ISpeechVoice, "str", IID_ISpeechVoice)
        {
            DllCall("ole32\CoUninitialize")
            return "IIDFromString() failed: " ret
        }
   
        ;Obtain ISpeechVoice Interface.
        if ret:=DllCall("ole32\CoCreateInstance", "Uint", &CLSID_SpVoice, "Uint", 0, "Uint", 1, "Uint", &IID_ISpeechVoice, "UintP", ppSpVoice)
        {
            DllCall("ole32\CoUninitialize")
            return "CoCreateInstance() failed: " ret
        }
        ;Get pointer to interface.
        DllCall("ntdll\RtlMoveMemory", "UintP", pSpVoice, "Uint", ppSpVoice, "Uint", 4)
        ;Get pointer to Speak().
        DllCall("ntdll\RtlMoveMemory", "UintP", pSpeak, "Uint", pSpVoice + 4*28, "Uint", 4)

       
        if ret:=DllCall(pSpeak, "Uint", ppSpVoice, "str" , wSpeechText, "Uint", dwFlags, "Uint", 0)
        {
            DllCall("ole32\CoUninitialize")
            return "ISpeechVoice::Speak() failed: " ret
        }

        DllCall("ole32\CoUninitialize")

        TTSInitialized = 1
        return
    }

    if ret:=DllCall(pSpeak, "Uint", ppSpVoice, "str" , wSpeechText, "Uint", dwFlags, "Uint", 0)
        return "ISpeechVoice::Speak() failed: " ret
}


;Some usage examples. #####################################
TTS("You can read simple text.")

xmltext=
(
<xml version="1.0">
    <SAPI>
        If the first character of the text is a less then character and the dwFlags parameter is 0 the text will be parsed as xml.
        With the use of xml markup
        <VOICE REQUIRED="NAME=Microsoft Sam">you can</VOICE>
        <VOICE REQUIRED="NAME=Microsoft mike">choose</VOICE>
        <VOICE REQUIRED="NAME=Microsoft Mary">which voice</VOICE>
        <VOICE REQUIRED="NAME=Microsoft Sam">you wish to hear. And much more.</VOICE>
    </SAPI>
</xml>
)
TTS(xmltext)


FileAppend ,
(
You can also specify a fully qualified path to a file and set the dwFlags param to 4. This way the files text will be read.
), tmpspeech.txt
TTS(A_ScriptDir "\tmpspeech.txt", 0 | 4)
FileDelete, tmpspeech.txt


TTS("There are even more features. For details read the speech API documentation.")


Last edited by foom on Tue Feb 13, 2007 12:27 pm; edited 4 times in total
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Mon Feb 12, 2007 10:41 am    Post subject: Reply with quote

It reports

---------------------------
CLSIDFromString() failed: -2147221005
---------------------------


here...
_________________
Back to top
View user's profile Send private message
foom



Joined: 19 Apr 2006
Posts: 386

PostPosted: Mon Feb 12, 2007 6:40 pm    Post subject: Reply with quote

Ooops. Thanks for the report. Should be fixed now.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Tue Feb 13, 2007 8:25 am    Post subject: Reply with quote

Still doesn't work here.
Strangely enoguh, return is empty string and manualy traversing to code showed me that all dllcalls executed with correct return codes. Still, script exits the same moment as it enters...

Speak is of coursed installed. I use it with Babylon Pro.
_________________
Back to top
View user's profile Send private message
foom



Joined: 19 Apr 2006
Posts: 386

PostPosted: Tue Feb 13, 2007 9:42 am    Post subject: Reply with quote

Strange. Which version do you have installed? I have 5.1 SDK on my machine and 5.1 Redist on the machine of my brother and it works on both.

Last edited by foom on Tue Feb 13, 2007 12:13 pm; edited 1 time in total
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Tue Feb 13, 2007 10:10 am    Post subject: Reply with quote

foom wrote:
I please people that read this to try it out and give feedback if it works for them even if they are not interested in tts.

It worked fine here.
BTW, the error message -2147221005 is "invalid class string", so looks like something got wrong during copy and paste.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Tue Feb 13, 2007 10:13 am    Post subject: Reply with quote

it works, I was using wrong version. Sorry.

I created COM page here for colaborative work:

http://www.autohotkey.com/wiki/index.php?title=COM_Wrappers
_________________
Back to top
View user's profile Send private message
jps



Joined: 02 Sep 2006
Posts: 279
Location: Scotland

PostPosted: Tue Feb 13, 2007 11:11 am    Post subject: Reply with quote

It worked for me using your example
Back to top
View user's profile Send private message
Keffi
Guest





PostPosted: Tue Feb 13, 2007 11:33 am    Post subject: Re: TTS() Text To Speech using COM Reply with quote

Hello,

just tried it this way:
Code:
#s::
TTS("You can read simple text.")
return

TTS(sSpeechText, dwFlags=0)
{
;For info on the dwFlags bitmask see the SAPI helpfile:
......here follows your script...

it simply does not speak at all, although it is executed. I habve SAPI 4 and SAPI5 installed with a bunch of voices.

Did I call the function wrong?

Cheers,
Keffi
Back to top
foom



Joined: 19 Apr 2006
Posts: 386

PostPosted: Tue Feb 13, 2007 12:10 pm    Post subject: Reply with quote

Thanks for the feedback.
@ Sean
Yeah i know what the code means. I deleted a comma while bringing the dllcalls down to one line and that cause the error.

@ keffi
Which ahk version do u use? I requirres 1.0.46.08.
I edited my first post so u need to copy and paste only once and run it.

@ majkinetor
Thanks for mentioning the wikipage. The artikle on codeprojekt is very educational. A very good read for COM newbies like me.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Tue Feb 13, 2007 12:41 pm    Post subject: Reply with quote

I added you script to the wiki.

I did this as an example of wiki usage and authors should add their script to relevant places so we can keep things organised.

Thx goes to jonny who polished ahk wiki.


http://www.autohotkey.com/wiki/index.php?title=COM_Wrappers#Scripts


I also created global autohotkey.net account to keep there files for download.

Account name and pass: anonymous
Put the COM realted scripts in COM\Scripts directory.
_________________


Last edited by majkinetor on Tue Feb 13, 2007 12:51 pm; edited 1 time in total
Back to top
View user's profile Send private message
Keffi



Joined: 13 Feb 2007
Posts: 2
Location: Germany

PostPosted: Tue Feb 13, 2007 12:46 pm    Post subject: Re: TTS() Text To Speech using COM Reply with quote

Well, I used sub-version .06 and guess what, after upgrading to .08 it works perfectly. Thanks a lot for this fine done script and your excellent support. As a blind user I'm always intersted in putting speech output to my scripts in the most efficient way. This will bring me a step forward.

Cheers, Keffi
_________________
Blindness is the opportunity to see more
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
foom



Joined: 19 Apr 2006
Posts: 386

PostPosted: Tue Feb 13, 2007 1:06 pm    Post subject: Reply with quote

Thanks for adding the script. Althought i am doubtfull about creating libraries because of ahks lack of elementary features to support librarys. However, its nice to see that someone takes the initiative. Hopefully Chris will recognise and honor this and start working on features that aid libraries.
majkinetor wrote:
I also created global autohotkey.net account to keep there files for download.

I have the feeling that this is a bad idea. This might sooner or later get abused eighter to change scripts to do harmfull things or to upload illegal content. You might want to inforn Titan as he is in charge for the site.


Last edited by foom on Tue Feb 13, 2007 1:26 pm; edited 1 time in total
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Tue Feb 13, 2007 1:21 pm    Post subject: Reply with quote

I don't think so.

ppl can change entire ahk wiki to contain malitious scripts now, yet, they don't do it. Or they do, we will never know. That is the wiki my friend. You will have to beleive or not use it at all.

Well, I beleive, thats why I use it.


Quote:
You might want to inforn Titan as he is in charge for the site.
This is regular ahk.net usage, nothing is invalid in it. The fact that it is open changes nothing. Feel free foom to update the download links in the wiki to your user space anytime so, you can be sure noone will ever change it.
_________________
Back to top
View user's profile Send private message
foom



Joined: 19 Apr 2006
Posts: 386

PostPosted: Tue Feb 13, 2007 1:29 pm    Post subject: Reply with quote

Ok the malicious code issue is nonsense on second thought. But still. If someone uses it for illegal activities, Titan is in charge and he should be informed.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group