C ++的MS SAPI 5.1问题 [英] MS SAPI 5.1 issue with C++
问题描述
我正在将MS Speech API 5.1与mingw编译器一起使用.因此,我没有VS,但可以使用c :: b编译代码. 该代码段如下:
I am using MS Speech API 5.1 with mingw compiler. I don't have VS hence but I was able to compile the code with c::b. The code snippet is below:
#include <sapi.h>
int main(int argc, char* argv[])
{
ISpVoice * pVoice = NULL;
if (FAILED(::CoInitialize(NULL)))
return FALSE;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
hr = pVoice->Speak(L"Hello world", 0, NULL);
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return TRUE;
}
执行时我发现
hr = pVoice->Speak(L"Hello world", 0, NULL);
返回NULL.环顾了一会后,我从MS SAPI文件夹中运行了示例TTSApp.exe.默认情况下,语音被选择为"Microsoft Marry".当我键入文本并单击开始"时,没有语音提示,并且出现语音错误".当我将声音更改为"Microsoft Anna-English(美国)"时,它起作用了.
returning NULL. After looking around for a while, I ran the example TTSApp.exe from the MS SAPI folder. By default the voice was selected as "Microsoft Marry". When I type a text and click Start, no speech sound is produce and I got "Speak error". When I changed the voice to "Microsoft Anna - English (United States)", it worked.
我不知道我的想法是否正确,但是我的代码是否还会因为错误/默认的语音选择("Microsoft Marry")而失败?有什么方法可以将声音更改为"Microsoft Anna-English(美国)"进行测试吗?
I do not know if I am thinking correct, but is the code of mine also failing because of a wrong/default voice selection ("Microsoft Marry")? Is there any way I can change the voice to "Microsoft Anna - English (United States)" for testing?
我需要您的帮助.您能帮我使以上代码的SAPI正常工作吗?
I need you help here. Can you help me get the SAPI working fro the above code?
谢谢!
推荐答案
由于这是Microsoft的特定问题. 我在某些system()调用中混合使用了一些vb,这导致代码简单得多. 我还没有清理,但是效果很好
Since this is a microsoft specific question. I use some vb mixed within some system() calls it results in much simpler code. I haven't cleaned it up, but it works well
int say(std::string s)
{
string part1content;
string part2content;
string word,complete;
const char * temp2="WScript.exe Scrpt.vbs";
const char * temp="Scrpt.vbs";
string pat1=(
"\'By Timbo\n\n\
Const SVSFlagsAsync = 1\n\
const SVSFPurgeBeforeSpeak =2\n\n\
Dim Speech\n\
Dim FSO\n\n\
CreateObjects\n\
Main\n\
DestroyObjects\n\
Quit\n\n\
Sub Main\n\
Dim sText\n\n\
sText = (\""
);
string pat2=
(
"\")\n\
Speech.rate = 0.05\n\
If sText <> \"\" Then\n\
SpeakText sText\n\n\
End If\n\
End Sub\n\n\
Sub SpeakText(sText)\n\
On Error Resume Next\n\
Speech.Speak sText, SVSFlagsAsync + SVSFPurgeBeforeSpeak\n\
Do\n\
Sleep 200\n\
Loop Until Speech.WaitUntilDone(10)\n\
End Sub\n\n\
Sub StopSpeaking()\n\
On Error Resume Next\n\
Speech.Speak vbNullString, SVSFPurgeBeforeSpeak\n\
Set Speech = Nothing\n\
End Sub\n\n\
Sub CreateObjects\n\
Set Speech = CreateObject(\"SAPI.SpVoice\")\n\
Set FSO = CreateObject(\"Scripting.FileSystemObject\")\n\
End Sub\n\n\
Sub DestroyObjects\n\
Set Speech = Nothing\n\
Set FSO = Nothing\n\
End Sub\n\n\
Sub Sleep(nTimeout)\n\
WScript.Sleep nTimeout\n\
End Sub\n\n\
Sub Quit\n\
WScript.Quit\n\
End Sub\n\n\
"
);
/*Will say string s*/
complete=pat1+s+pat2;
ofstream myfile;
myfile.open (temp);
myfile << complete;
myfile.close();
system(temp2);
complete.clear();
s.clear();
}
对于其他语音,请使用
string pat2=
(
"\")\n\
Speech.rate = 0.05\n\
If sText <> \"\" Then\n\
SpeakText sText\n\n\
End If\n\
End Sub\n\n\
Sub SpeakText(sText)\n\
On Error Resume Next\n\
Speech.Speak sText, SVSFlagsAsync + SVSFPurgeBeforeSpeak\n\
Do\n\
Sleep 200\n\
Loop Until Speech.WaitUntilDone(10)\n\
End Sub\n\n\
Sub StopSpeaking()\n\
On Error Resume Next\n\
Speech.Speak vbNullString, SVSFPurgeBeforeSpeak\n\
Set Speech = Nothing\n\
End Sub\n\n\
Sub CreateObjects\n\
Set Speech = CreateObject(\"SAPI.SpVoice\")\n\
with Speech \n\
Set .voice = .getvoices.item(1)\n\
.Volume = 100\n\
.Rate = 4\n\
end with\n\
Set FSO = CreateObject(\"Scripting.FileSystemObject\")\n\
End Sub\n\n\
Sub DestroyObjects\n\
Set Speech = Nothing\n\
Set FSO = Nothing\n\
End Sub\n\n\
Sub Sleep(nTimeout)\n\
WScript.Sleep nTimeout\n\
End Sub\n\n\
Sub Quit\n\
WScript.Quit\n\
End Sub\n\n\
"
);
然后您只需调用say("This is a test");
要获得更多控制,可以使用管道代替sytem(),如果要停止它,则可以杀死vbscript进程.
For more control you can use pipes instead of sytem() , this will enable you to kill the vbscript process if you want to stop it.
这篇关于C ++的MS SAPI 5.1问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!