问题描述
我的任务
可以在Office应用程序中使用语音.我的目标是将MS SAPI语音保存为给定的文件类型.AFAIK我的代码示例保存到WAV文件.
It's possible to use speech in Office applications. My goal to save MS SAPI speech to a given file type. AFAIK my code example saves to a WAV file.
问题
我不知道,是否可以仅定义所需的文件类型扩展名,或者是否有必要进行进一步的设置.我没有找到使用VBA的合适解决方案.
I don't know, if it's possible to define the wanted file type extension only or if it's necessary to do some further setting.I didn't find an appropriate solution using VBA.
问题是否有代码示例如何精确定义所需的文件类型,例如MP3,是否使用必要的设置(AudioStream)将给定的文本保存到此文件类型?
QuestionIs there a code example how to precisely define a wanted file type, e.g. MP3, save a given text to this file type using the necessary settings (AudioStream)?
代码
在此代码示例中,我将输出文件直接命名为WAV,并且不确定是否为WAV文件.
In this code example I' m naming the output file directly as WAV with full uncertainty if this will be a WAV file.
我使用了后期绑定,并且还对早期绑定进行了评论.
I used late binding and included a comment to early binding, too.
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
注意
向@ashleedawg致谢我可以推荐以下指向MS Speech API的链接:
Thx to @ashleedawg 's comment I can recommend the following links to the MS Speech API:
推荐答案
sapi仅生成wav文件.
sapi only generates wav files.
使用ffmpeg转换为其他格式... http://ffmpeg.org
use ffmpeg to convert to other formats ... http://ffmpeg.org
在vba中使用的示例... 在以下位置使用ffmpegvba更改视频格式
example of usage in vba ... use ffmpeg in vba to change video format
这篇关于VBA-将SAPI语音保存为GIVEN文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!