https://github.com/zhaopuyang/golang-tts 这个库确实可用,不过功能还是简单了点。
Windows下有效
package main
import golang_tts "github.com/zhaopuyang/golang-tts"
func main() {
golang_tts.SpeakText("你好,世界!")
}
https://github.com/guquan-lengyue/ms_edge_tts 看起来是API调用,暂时不选择
另有网友通过OLE调用Win实现的,其实最好要能够选音色,这里暂时没有。
import "github.com/go-ole/go-ole"
import "github.com/go-ole/go-ole/oleutil"
func ttsOutput(content, file string) {
ole.CoInitialize(0)
unknown, _ := oleutil.CreateObject("SAPI.SpVoice")
voice, _ := unknown.QueryInterface(ole.IID_IDispatch)
saveFile, _ := oleutil.CreateObject("SAPI.SpFileStream")
ff, _ := saveFile.QueryInterface(ole.IID_IDispatch)
oleutil.CallMethod(ff, "Open", file, 3, true) // 打开wav文件
oleutil.PutPropertyRef(voice, "AudioOutputStream", ff) // 设置voice的AudioOutputStream属性,必须是PutPropertyRef,如果是PutProperty就无法生效
oleutil.PutProperty(voice, "Rate", 2) // 设置语速
oleutil.PutProperty(voice, "Volume", 200) // 设置音量
oleutil.CallMethod(voice, "Speak", content) // 说话
// oleutil.CallMethod(voice, "Speak", "bb", 1)
//oleutil.CallMethod(voice, "Pause")// 停止说话
//oleutil.CallMethod(voice, "Resume")// 恢复说话
oleutil.CallMethod(voice, "WaitUntilDone", 1000000) // 等待结束
oleutil.CallMethod(ff, "Close") // 关闭文件
ff.Release()
voice.Release()
ole.CoUninitialize()
}