How to save SAPI text to speech to an audio file in VBScript?

旧巷老猫 提交于 2019-12-29 09:16:06

问题


I have the following VBScript code for text to speech conversion:

Set objVoice = CreateObject("SAPI.SpVoice")
objVoice.Speak Inputbox("Enter Text")

I want to save the speech to an audio file. How can I do this?


回答1:


You can save the SAPI output to a .WAV file as follows:

  1. Create and open a .WAV file as a stream using the SpFileStream.Open method.

  2. Assign this file stream to the SpVoice.AudioStream property.

Here's an example:

Const SAFT48kHz16BitStereo = 39
Const SSFMCreateForWrite = 3 ' Creates file even if file exists and so destroys or overwrites the existing file

Dim oFileStream, oVoice

Set oFileStream = CreateObject("SAPI.SpFileStream")
oFileStream.Format.Type = SAFT48kHz16BitStereo
oFileStream.Open "C:\Work\Sample.wav", SSFMCreateForWrite

Set oVoice = CreateObject("SAPI.SpVoice")
Set oVoice.AudioOutputStream = oFileStream
oVoice.Speak "Hello world"

oFileStream.Close


来源:https://stackoverflow.com/questions/20498004/how-to-save-sapi-text-to-speech-to-an-audio-file-in-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!