Speaking text to audio file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WantToKnow
Posts: 61
Joined: 28 Mar 2020, 08:46

Speaking text to audio file

Post by WantToKnow » 04 Oct 2022, 07:29

Hello,

the code below works, but unfortunately only once. As soon as I call the function FKTSpeakToFile()
a second time, there is an error message. If I want to call the function a second time
I have to restart the whole program. Where is the error?

Code: Select all

FKTSpeakToFile()
{
strText := "This is a simple sentence, spoken in English"
strWav := "Audio.wav"
SpVoice := ComObjCreate("SAPI.SpVoice")
spVoice.Volume := 100
spVoice.Rate := -2
oVoice.AllowAudioOutputFormatChangesOnNextSet := 1
SpStream := ComObjCreate("SAPI.SpFileStream")
SpStream.Open(strWav,3) ; ErrorMessage here, as soon as I call the function a second time
SpVoice.AudioOutputStream := SpStream
SpVoice.voice := SpVoice.GetVoices().Item(5)
SpVoice.Speak(strText)
SpStream.Close()
}
Thanks a lot for your help.

User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: Speaking text to audio file

Post by boiler » 04 Oct 2022, 08:42

I'm not able to reproduce the error. It would help if you showed what error message you are getting. And show a simple script that calls it twice that produces the message, such as this (which doesn't result in an error for me):

Code: Select all

#Include, FKTSpeakToFile.ahk
FKTSpeakToFile()
Sleep, 5000
FKTSpeakToFile()

WantToKnow
Posts: 61
Joined: 28 Mar 2020, 08:46

Re: Speaking text to audio file

Post by WantToKnow » 04 Oct 2022, 12:26

Whatever I try I just can call my function only one time. Then I have to restart the whole program or
there is an error message.

File one to call the function:

Code: Select all

; StartTest01.ahk
; Speaking text to audio file
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=109071

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance Force
#include ScriptFKTSpeakToFile01.ahk
F1::
Loop 4
{
ToolTip % "Loop number:"  A_Index, 600,90
FKTSpeakToFile()
Sleep 2000
}
return
F12::
ExitApp
return

file two, to create an audio file

Code: Select all

; ScriptFKTSpeakToFile01.ahk
; Speaking text to audio file
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=109071

FKTSpeakToFile()
{
strText := "This is a simple sentence, spoken in English"
strWav := "Audio.Wav"
SpStream := ComObjCreate("SAPI.SpFileStream")
SpVoice := ComObjCreate("SAPI.SpVoice")
SpVoice.voice := SpVoice.GetVoices().Item(1)
spVoice.Volume := 100
spVoice.Rate := 0
oVoice.AllowAudioOutputFormatChangesOnNextSet := 1
SpStream.Open(strWav,3)  ; !!!  error message as soon the function is calling a second time    !!!!
SpVoice.AudioOutputStream := SpStream
SpVoice.Speak(strText)
SpStream.Close()
}


User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: Speaking text to audio file

Post by boiler » 04 Oct 2022, 12:35

boiler wrote:
04 Oct 2022, 08:42
It would help if you showed what error message you are getting.

WantToKnow
Posts: 61
Joined: 28 Mar 2020, 08:46

Re: Speaking text to audio file

Post by WantToKnow » 05 Oct 2022, 02:53

This error appears as soon as the function is called a second time:

Image
Attachments
Error.jpg
Error.jpg (43.76 KiB) Viewed 388 times
Last edited by WantToKnow on 05 Oct 2022, 04:29, edited 2 times in total.

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Speaking text to audio file

Post by gregster » 05 Oct 2022, 03:29

@WantToKnow: Please use the 'Attachments' tab in the editor (scroll down a bit) to upload pictures. Your attempt above didn't work.

Btw, an error message's text can usually be copied by pressing Ctrl and c when the messagebox is active. That would be an alternative to posting an image of it.

User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: Speaking text to audio file  Topic is solved

Post by boiler » 05 Oct 2022, 05:16

I don’t know what make of that error message other than it’s interesting that most of the error message is in English except for the word “Falscher”? Are you in a location where German is the primary language?

One thing to possibly try, which I have little hope of working but is worth a shot, is to add the third parameter to Open() which defaults at 0 (false), so perhaps try it with true and see what happens:

Code: Select all

SpStream.Open(strWav,3,1)

WantToKnow
Posts: 61
Joined: 28 Mar 2020, 08:46

Re: Speaking text to audio file

Post by WantToKnow » 05 Oct 2022, 05:56

Chapeu! Boiler, you're the greatest. It works.
Thank you so much!

Post Reply

Return to “Ask for Help (v1)”