在办公室应用程序中使用语音是可能的。我的目标是将MS SAPI语音保存到给定的文件类型。AFAIK我的代码示例保存到WAV文件。
问题
是否有一个代码示例,如何精确定义想要的文件类型,例如MP3,使用必要的设置(音频流)将给定的文本保存到此文件类型?
密码
在这个代码示例中,我将输出文件直接命名为WAV,如果这是一个WAV文件,则完全不确定。
我使用了后期绑定,并对早期绑定进行了评论。
Private Sub Speech2WAV()
' Purpose: save text Voice object to file
' Idea: cf. .Net Article with some adaptions http://www.codeguru.com/vb/gen/vb_misc/samples/article.php/c13893/Text-to-Speech-Using-Windows-SAPI.htm
' Declare variables
Dim s As String
s = "Could you give me a code example to save this text to a defined file type?"
'' ----------------------------------------------
'' Early Binding - reference do MS Speech Object Lib (SAPI.dll) needed
'' ----------------------------------------------
' Dim oVoice As New SpeechLib.SpVoice
' Dim cpFileStream As New SpeechLib.SpFileStream
'' ----------------------------------------------
' ----------------------------------------------
' Late Binding
' ----------------------------------------------
Dim oVoice As Object
Dim cpFileStream As Object
Set oVoice = CreateObject("SAPI.SpVoice")
Set cpFileStream = CreateObject("SAPI.SpFileStream")
' ----------------------------------------------
10 cpFileStream.Open ThisWorkbook.Path & "\test.wav", _
SpeechLib.SpeechStreamFileMode.SSFMCreateForWrite, False
20 Set oVoice.AudioOutputStream = cpFileStream
30 Set oVoice.Voice = oVoice.GetVoices.Item(0)
40 oVoice.Volume = 100
50 oVoice.Speak s, _
SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault
55 oVoice.Rate = 1 ' speed
56 oVoice.Volume = 100 ' volume
60 Set oVoice = Nothing
70 cpFileStream.Close
80 Set cpFileStream = Nothing
Exit Sub
OOPS: ' Error Handler
MsgBox "ERL=" & Erl & "|ErrNo=" & Err.Number & "|" & Err.Description, vbExclamation, "Error in Speec2WAV"
End Sub
Thx至@ashleedawg
's comment
我可以推荐MS语音API的以下链接:
White papers SAPI 5.3
-
Microsoft Speech API 5.4