Windows.Media.SpeechSynthesis.SpeechSynthesizer Class

Get help with using AutoHotkey and its commands and hotkeys
anotherautohotkeyusr
Posts: 21
Joined: 27 Oct 2015, 18:45

Windows.Media.SpeechSynthesis.SpeechSynthesizer Class

28 Oct 2018, 01:29

Good day everyone,

I would like to learn how to use the Windows.Media.SpeechSynthesis.SpeechSynthesizer Class in AutoHotkey, but there do not appear to be any examples of anyone having done this yet. I am really struggling to find AutoHotkey examples of working with Windows 10 API objects. Is there anyone that could give me a shove in the right direction as to how to create and use a SpeechSynthesizer object?

The Microsoft reference at https://docs.microsoft.com/en-us/uwp/ap ... ynthesizer gives the following C# example, but I am at a loss as to how to translate this process in to AutoHotkey:

Code: Select all

// The media object for controlling and playing audio.
MediaElement mediaElement = this.media;

// The object for controlling the speech synthesis engine (voice).
var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();

// Generate the audio stream from plain text.
SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("Hello World");

// Send the stream to the media object.
mediaElement.SetSource(stream, stream.ContentType);
mediaElement.Play();
Thank you in advance.

Rob
anotherautohotkeyusr
Posts: 21
Joined: 27 Oct 2015, 18:45

Re: Windows.Media.SpeechSynthesis.SpeechSynthesizer Class

29 Oct 2018, 18:33

Can anyone shed light on to how to instantiate a UWP object? If I could get that starting line of code, then I hope I could work out the rest.
User avatar
SL5
Posts: 877
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Windows.Media.SpeechSynthesis.SpeechSynthesizer Class

31 Oct 2018, 14:29

thats a great idear. i also want use it.
BTW you may also interested in: https://quest-app.appspot.com/
User avatar
Coderooney
Posts: 46
Joined: 23 Mar 2017, 22:41

Re: Windows.Media.SpeechSynthesis.SpeechSynthesizer Class

31 Oct 2018, 15:29

I'm not sure if it used the SpeechSynthesizer object, but if you are after text to speech, there is this function by Learning One that EvilC adapted which was very easy to use: https://autohotkey.com/boards/viewtopic.php?f=6&t=12304

Something I did for a prank :P :

Code: Select all

#NoTrayIcon
#SingleInstance force
s := new TTS()

Loop, 1 {
    s.SetRate(-2)
    s.SetPitch(10)
    s.Speak("Yeaaaaah boy e!")
    Sleep, 2000
}

Class TTS {
    VoiceList := []
    VoiceAssoc := {}
    VoiceCount := 0
    VoiceNumber := 0
    VoiceName := ""
    
    __New(){
        this.oVoice := ComObjCreate("SAPI.SpVoice")
        this._GetVoices()
        this.SetVoice(this.VoiceList.1)
    }

    ToggleSpeak(text){
        Status := this.oVoice.Status.RunningState
        if Status = 1
        this.oVoice.Speak(text,0x1)
        Else if Status = 0
        {
            this.oVoice.Resume
            this.oVoice.Speak("",0x1|0x2)
            this.oVoice.Speak(text,0x1)
        }
        Else if Status = 2
        this.oVoice.Speak("",0x1|0x2)
    }

    Speak(text){
        Status := this.oVoice.Status.RunningState
        if Status = 0
        this.oVoice.Resume
        this.oVoice.Speak("",0x1|0x2)
        this.oVoice.Speak(text,0x1)
    }
    
    SpeakWait(text){
        Status := this.oVoice.Status.RunningState
        if Status = 0
        this.oVoice.Resume
        this.oVoice.Speak("",0x1|0x2)
        this.oVoice.Speak(text,0x0)
    }

    Pause(){
        Status := this.oVoice.Status.RunningState
        if Status = 0
        this.oVoice.Resume
        else if Status = 2
        this.oVoice.Pause
    }
    
    Stop(){
        Status := this.oVoice.Status.RunningState
        if Status = 0
        this.oVoice.Resume
        this.oVoice.Speak("",0x1|0x2)
    }
    
    SetRate(rate){
        this.oVoice.Rate := rate
    }
    
    SetVolume(vol){
        this.oVoice.Volume := vol
    }
    
    SetPitch(pitch){
        this.oVoice.Speak("<pitch absmiddle = '" pitch "'/>",0x20)
    }

    SetVoice(VoiceName){
        if (!ObjHasKey(this.VoiceAssoc, VoiceName))
            return 0
        While !(this.oVoice.Status.RunningState = 1)
        Sleep, 20
        this.oVoice.Voice := this.oVoice.GetVoices("Name=" VoiceName).Item(0)
        this.VoiceName := VoiceName
        this.VoiceNumber := this.VoiceAssoc[VoiceName]
        return 1
    }

    SetVoiceByIndex(VoiceIndex){
        return this.SetVoice(this.VoiceList[VoiceIndex])
    }

    NextVoice(){
        v := this.VoiceNumber + 1
        if (v > this.VoiceCount)
            v := 1
        return this.SetVoiceByIndex(v)
    }
    
    GetVoices(){
        return this.VoiceList
    }

    GetStatus(){
        Status := this.oVoice.Status.RunningState
        if Status = 0
        Return "paused"
        Else if Status = 1
        Return "finished"
        Else if Status = 2
        Return "reading"
    }
    
    GetCount(){
        return this.VoiceCount
    }
    
    SpeakToFile(param1, param2){
        oldAOS := this.oVoice.AudioOutputStream
        oldAAOFCONS := this.oVoice.AllowAudioOutputFormatChangesOnNextSet
        this.oVoice.AllowAudioOutputFormatChangesOnNextSet := 1	
        
        SpStream := ComObjCreate("SAPI.SpFileStream")
        FileDelete, % param2
        SpStream.Open(param2, 3)
        this.oVoice.AudioOutputStream := SpStream
        this.TTS("SpeakWait", param1)
        SpStream.Close()
        this.oVoice.AudioOutputStream := oldAOS
        this.oVoice.AllowAudioOutputFormatChangesOnNextSet := oldAAOFCONS
    }

    _GetVoices(){
        this.VoiceList := []
        this.VoiceAssoc := {}
        this.VoiceCount := this.oVoice.GetVoices.Count
        Loop, % this.VoiceCount
        {
            Name := this.oVoice.GetVoices.Item(A_Index-1).GetAttribute("Name")
            this.VoiceList.push(Name)
            this.VoiceAssoc[Name] := A_Index
        }
    }
}
User avatar
SL5
Posts: 877
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Windows.Media.SpeechSynthesis.SpeechSynthesizer Class

31 Oct 2018, 22:55

Coderooney wrote:
31 Oct 2018, 15:29
I'm not sure if it used the SpeechSynthesizer object, but if you are after text to speech, there is this function by Learning One that EvilC adapted which was very easy to use: https://autohotkey.com/boards/viewtopic.php?f=6&t=12304

> Something I did for a prank :P :
Wow this works really great! :)

and here a wraper for QTranslate

Code: Select all

speak(text){
	ControlSetText,RICHEDIT50W1,% text,QTranslate ahk_class #32770 ahk_exe QTranslate.exe
	ControlClick,Button4,QTranslate ahk_class #32770 ahk_exe QTranslate.exe
}

User avatar
Coderooney
Posts: 46
Joined: 23 Mar 2017, 22:41

Re: Windows.Media.SpeechSynthesis.SpeechSynthesizer Class

02 Nov 2018, 05:55

Glad it helped! Very grateful to the guy who made it :D
User avatar
evilC
Posts: 4772
Joined: 27 Feb 2014, 12:30

Re: Windows.Media.SpeechSynthesis.SpeechSynthesizer Class

02 Nov 2018, 11:49

I tend to do stuff like this in C# code, then call the C# code from AHK using the CLR library: https://autohotkey.com/boards/viewtopic.php?t=4633

See here, I implemented the opposite (Speech recognition): https://autohotkey.com/boards/viewtopic.php?t=34288
The heavy lifting (Interacting with the windows API) is done in C#, then I provide an AHK wrapper which can call my C# code
User avatar
Coderooney
Posts: 46
Joined: 23 Mar 2017, 22:41

Re: Windows.Media.SpeechSynthesis.SpeechSynthesizer Class

29 Nov 2018, 18:49

Ah, so as someone who knows nothing about C#, this is basically black magic :P I would never even think of getting speech recognition tied into AHK so that is fascinating. Thanks for all your work evilC.
User avatar
evilC
Posts: 4772
Joined: 27 Feb 2014, 12:30

Re: Windows.Media.SpeechSynthesis.SpeechSynthesizer Class

01 Dec 2018, 09:06

Honestly, if you are any good with AHK (Especially if you use expression syntax and functions/classes), C# is not hard. In fact, in many ways it is easier - Visual Studio tells you when you made a mistake, and often can tell you how to fix it, or even fix it for you. There are also tons of examples on the internet you can use code from.
That voice library is basically the result of playing around with the example code from the MS documentation for the API, and the chucking an AHK wrapper around it. It's not rocket science....
User avatar
Coderooney
Posts: 46
Joined: 23 Mar 2017, 22:41

Re: Windows.Media.SpeechSynthesis.SpeechSynthesizer Class

02 Dec 2018, 02:43

I can program in a few languages and it's not that C# is intimidating, but I have not even 1 bit of experience with it. We all only have a certain amount of time and these days I advance the languages I know rather than picking up new ones. Perhaps I can see C# as a way to advance my knowledge of AHK one day :)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Windows.Media.SpeechSynthesis.SpeechSynthesizer Class

02 Dec 2018, 02:51

I mentioned the Derek Banas 'Learn in One Video' videos here:
resources for learning a programming language - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 17&t=56140

They are great for getting a feel for a programming language, e.g. strings/maths/objects. Cheers.
C# Tutorial - YouTube
https://www.youtube.com/watch?v=lisiwUZJXqQ
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, barbaque, boiler, Chiefkes, Google [Bot], Ian, Jrdixon_, lucasstella, vsub, yuyu and 300 guests