├── go.mod ├── .gitattributes ├── base ├── tts.dll ├── tts.h └── tts.cc ├── tts.go ├── README.md └── .vscode └── c_cpp_properties.json /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/zhaopuyang/golang-tts 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.cc linguist-language=go 2 | *.h linguist-language=go 3 | -------------------------------------------------------------------------------- /base/tts.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaopuyang/golang-tts/HEAD/base/tts.dll -------------------------------------------------------------------------------- /tts.go: -------------------------------------------------------------------------------- 1 | package golang_tts 2 | 3 | import( 4 | "syscall" 5 | "unsafe" 6 | ) 7 | 8 | func SpeakText(text string){ 9 | ttsdll:=syscall.NewLazyDLL("tts.dll") 10 | speak:=ttsdll.NewProc("rapidSpeakText") 11 | speak.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text)))) 12 | } -------------------------------------------------------------------------------- /base/tts.h: -------------------------------------------------------------------------------- 1 | #ifndef TTS_H 2 | #define TTS_H 3 | #define EXPORTS_API extern "C" __declspec(dllexport) 4 | 5 | EXPORTS_API void speakText(const wchar_t *text); 6 | EXPORTS_API void initEnv(); 7 | EXPORTS_API void releaseEnv(); 8 | EXPORTS_API void rapidSpeakText(const wchar_t *text); 9 | 10 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # golang-tts 2 | 文本到语音转换,基于微软SAPI 3 | 4 |

安装

5 | 1、go get -u github.com/zhaopuyang/golang-tts
6 | 2、将base文件夹中的tts.dll复制到当前文件目录下
7 | 3、完毕!
8 |

使用

9 | golang_tts.SpeakText("你好,世界!") 10 |

注意

11 | base文件夹为tts引擎的c++源文件
12 | 若要重新编译请先安装mingw64,编译命令:gcc --share tts.cc -o tts.dll -lole32 -lsapi 13 | -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Win32", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [ 9 | "_DEBUG", 10 | "UNICODE", 11 | "_UNICODE" 12 | ], 13 | "windowsSdkVersion": "8.1", 14 | "compilerPath": "D:/mingw64/bin/gcc.exe", 15 | "cStandard": "c11", 16 | "cppStandard": "c++17", 17 | "intelliSenseMode": "gcc-x64" 18 | } 19 | ], 20 | "version": 4 21 | } -------------------------------------------------------------------------------- /base/tts.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "tts.h" 4 | 5 | 6 | ISpVoice *pVoice = NULL; 7 | 8 | void speakText(const wchar_t * text) 9 | { 10 | pVoice->Speak(text,SVSFDefault | SVSFPurgeBeforeSpeak, NULL); 11 | } 12 | 13 | void initEnv() 14 | { 15 | CoInitialize(NULL); 16 | CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice); 17 | } 18 | void releaseEnv() 19 | { 20 | pVoice->Release(); 21 | pVoice = NULL; 22 | CoUninitialize(); 23 | } 24 | void rapidSpeakText(const wchar_t * text) 25 | { 26 | initEnv(); 27 | speakText(text); 28 | releaseEnv(); 29 | } --------------------------------------------------------------------------------