Jump to content

TTS Text-to-Speech to .wav file using SAPI


natedog102
 Share

Recommended Posts

This small UDF helps you save any text to speech to a .wav file. 

UDF:

#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#include-once

; #INDEX# =======================================================================================================================
; Title .........: _TTStoWav()
; AutoIt Version : 3.3.14.2
; Language ......: English
; Author(s) .....: natedog102
; Modifiers .....:
; Forum link ....: https://www.autoitscript.com/forum/topic/191573-tts-text-to-speech-to-wav-file-using-sapi/
; Description ...: TTS to .wav file using SAPI
; ===============================================================================================================================


; #FUNCTION# ====================================================================================================================
; Name...........:  _TTStoWav
; Description ...: TTS to .wav file using SAPI
; Syntax.........: _TTStoWav($sText, $sLocation[, $iRate = 1[, $iVolume = 100]])
; Parameters ....: $sText           - String you want converted to a .wav
;                  $sLocation       - Save location + file name, example: "C:\folder\sample.wav"
;                  $iRate           - TTS speak rate between -10 and 10
;                  $iVolume         - Volume of the TTS between 0 and 100
; Return values .: Success          - Returns one
;                  Failure          - Returns zero
; Author ........: natedog102
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================

Func _TTStoWav($sText, $sLocation, $iRate = 1, $iVolume = 100)
    $oFileStream = ObjCreate("SAPI.SpFileStream")
    If @error Then
        Return 0
    EndIf
    $oFileStream.Format.Type = 39
    $oFileStream.Open($sLocation, 3)
    $oSpeech = ObjCreate('SAPI.SpVoice')
    If @error Then
        Return 0
    EndIf
    $oSpeech.AudioOutputStream = $oFileStream
    $oSpeech.Rate = $iRate
    $oSpeech.Volume = $iVolume
    $oSpeech.Speak($sText, 3)
    $oSpeech.WaitUntilDone(10000)
    $oFileStream.Close()
    Return 1
EndFunc   ;==>_TTStoWav

And here's example usage:

_TTStoWav("This is a test", "C:\folder-must-exist\sample2.wav")

I have never made a UDF before so I don't know if I coded it correctly or if I followed all the recommended guidelines. Apologies if I haven't.

DOWNLOAD:

 _TTStoWav.au3

Edited by natedog102
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

×
×
  • Create New...